本文整理汇总了C#中Newtonsoft.Json.JsonSerializer.Serialize方法的典型用法代码示例。如果您正苦于以下问题:C# JsonSerializer.Serialize方法的具体用法?C# JsonSerializer.Serialize怎么用?C# JsonSerializer.Serialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Newtonsoft.Json.JsonSerializer
的用法示例。
在下文中一共展示了JsonSerializer.Serialize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteJson
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var representation = value as Representation;
if (representation != null)
representation.RepopulateHyperMedia();
var list = (IRepresentationList)value;
writer.WriteStartObject();
writer.WritePropertyName("_links");
serializer.Serialize(writer, list.Links);
writer.WritePropertyName("_embedded");
writer.WriteStartObject();
writer.WritePropertyName(list.Rel);
writer.WriteStartArray();
foreach (Representation halResource in list)
{
serializer.Serialize(writer, halResource);
}
writer.WriteEndArray();
writer.WriteEndObject();
var listType = list.GetType();
var propertyInfos = typeof(RepresentationList<>).GetProperties().Select(p => p.Name);
foreach (var property in listType.GetProperties().Where(p => !propertyInfos.Contains(p.Name)))
{
writer.WritePropertyName(property.Name.ToLower());
serializer.Serialize(writer, property.GetValue(value, null));
}
writer.WriteEndObject();
}
示例2: WriteJson
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
ElasticQuery term = (ElasticQuery)value;
if (term != null)
{
writer.WriteStartObject();
writer.WriteRaw("\"from\": " + term.From);
writer.WriteRaw(",\"size\": " + term.Size + ",");
writer.WritePropertyName("query");
serializer.Serialize(writer, term.Query);
if (term.Fields != null && term.Fields.Count > 0)
{
writer.WritePropertyName("fields");
writer.WriteStartArray();
foreach (var field in term.Fields)
{
writer.WriteRawValue("\"" + field + "\"");
}
writer.WriteEndArray();
}
if (term.SortItems != null && term.SortItems.Count > 0)
{
writer.WritePropertyName("sort");
writer.WriteStartObject();
foreach (var sortItem in term.SortItems)
{
if (sortItem != null)
{
writer.WritePropertyName(sortItem.FieldName);
writer.WriteValue(sortItem.SortType.ToString().ToLower());
}
}
writer.WriteEndObject();
}
//facets
if (term.Facets != null)
{
writer.WritePropertyName("facets");
serializer.Serialize(writer, term.Facets);
}
//hightlight
if(term.Hightlight!=null)
{
writer.WritePropertyName("highlight");
serializer.Serialize(writer,term.Hightlight);
}
if (term.Explain)
{
writer.WriteRaw(",\"explain\": " + term.Explain.ToString().ToLower());
}
writer.WriteEndObject();
}
}
示例3: SaveToFile
public static void SaveToFile()
{
try
{
JsonSerializer serializer = new JsonSerializer();
var prices = dataAccess.GetAllPrices();
var sales = dataAccess.GetAllSales();
var priceChanges = dataAccess.GetPriceChanges();
string path = DataDir + "\\Prices.json";
var writer = new JsonTextWriter(File.CreateText(path));
serializer.Serialize(writer, prices);
writer.Close();
path = DataDir + "\\Sales.json";
writer = new JsonTextWriter(File.CreateText(path));
serializer.Serialize(writer, sales);
writer.Close();
path = DataDir + "\\priceChanges.json ";
writer = new JsonTextWriter(File.CreateText(path));
serializer.Serialize(writer, priceChanges);
writer.Close();
}
catch (Exception iException)
{
int x = 10;
throw;
}
}
示例4: WriteJson
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var request = value as IMultiGetRequest;
writer.WriteStartObject();
if (!(request?.Documents.HasAny()).GetValueOrDefault(false))
{
writer.WriteEndObject();
return;
}
var docs = request.Documents.Select(d =>
{
if (request.Index != null) d.Index = null;
if (request.Type != null) d.Type = null;
return d;
}).ToList();
var flatten = docs.All(p => p.CanBeFlattened);
writer.WritePropertyName(flatten ? "ids" : "docs");
writer.WriteStartArray();
foreach (var id in docs)
{
if (flatten)
serializer.Serialize(writer, id.Id);
else
serializer.Serialize(writer, id);
}
writer.WriteEndArray();
writer.WriteEndObject();
}
示例5: WriteJson
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var t = value as ITermsQuery;
if (t == null) return;
var settings = serializer.GetConnectionSettings();
var field = settings.Inferrer.Field(t.Field);
writer.WriteStartObject();
{
if (t.Terms.HasAny())
{
writer.WritePropertyName(field);
serializer.Serialize(writer, t.Terms);
}
else if (t.TermsLookup != null)
{
writer.WritePropertyName(field);
serializer.Serialize(writer, t.TermsLookup);
}
if (t.Boost.HasValue)
{
writer.WritePropertyName("boost");
writer.WriteValue(t.Boost.Value);
}
if (!t.Name.IsNullOrEmpty())
{
writer.WritePropertyName("_name");
writer.WriteValue(t.Name);
}
}
writer.WriteEndObject();
}
示例6: WriteJson
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var function = value as IScoreFunction;
if (function == null) return;
writer.WriteStartObject();
{
if (function.Filter != null)
{
writer.WritePropertyName("filter");
serializer.Serialize(writer, function.Filter);
}
var write = WriteDecay(writer, function as IDecayFunction, serializer)
|| WriteFieldValueFactor(writer, function as IFieldValueFactorFunction, serializer)
|| WriteRandomScore(writer, function as IRandomScoreFunction, serializer)
|| WriteScriptScore(writer, function as IScriptScoreFunction, serializer)
|| WriteWeightFunction(writer, function as IWeightFunction, serializer);
if (!write) throw new Exception($"Can not write function score json for {function.GetType().Name}");
if (function.Weight.HasValue)
{
writer.WritePropertyName("weight");
serializer.Serialize(writer, function.Weight.Value);
}
}
writer.WriteEndObject();
}
示例7: WriteJson
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var request = value as IRegisterPercolatorRequest;
if (request == null) return;
writer.WriteStartObject();
if (request.Query != null)
{
writer.WritePropertyName("query");
serializer.Serialize(writer, request.Query);
}
if (request.Metadata != null)
{
foreach(var kv in request.Metadata)
{
writer.WritePropertyName(kv.Key);
#if DOTNETCORE
if (kv.Value.GetType().GetTypeInfo().IsValueType)
#else
if (kv.Value.GetType().IsValueType)
#endif
writer.WriteValue(kv.Value);
else
serializer.Serialize(writer, kv.Value);
}
}
writer.WriteEndObject();
}
示例8: WriteJson
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
FeatureResult f = (FeatureResult)value;
writer.WriteStartObject();
if (f.Id != null){
writer.WritePropertyName("id");
writer.WriteValue(f.Id);
}
if (f.Properties != null) {
writer.WritePropertyName("properties");
serializer.Serialize(writer, f.Properties);
}
writer.WritePropertyName("type");
serializer.Serialize(writer, (f.Type));
if (f.Geometry != null) {
writer.WritePropertyName("geometry");
serializer.Serialize(writer, f.Geometry);
}
writer.WriteEndObject();
}
示例9: WriteJson
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var val = (IColorEffect) value;
var nameProp = new JProperty("name", val.Name);
var jobj = new JObject(nameProp);
var objWriter = jobj.CreateWriter();
if (value is FixedColor)
{
objWriter.WritePropertyName("color");
serializer.Serialize(objWriter, ((FixedColor) val).Colors[0]);
}
if (value is ImageEffect)
{
objWriter.WritePropertyName("imageName");
objWriter.WriteValue(((ImageEffect)value).ImageName);
objWriter.WritePropertyName("width");
objWriter.WriteValue(((ImageEffect)value).Width);
}
objWriter.WritePropertyName("colors");
//serializer.Serialize(objWriter, ((ColorFade)val).Colors.Select(c => new JValue(c)));
serializer.Serialize(objWriter, val.Colors);
jobj.WriteTo(writer);
}
示例10: WriteJson
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
CustomFiltersScoreQuery term = (CustomFiltersScoreQuery)value;
if (term != null)
{
writer.WriteStartObject();
writer.WritePropertyName("custom_filters_score");
writer.WriteStartObject();
writer.WritePropertyName("query");
serializer.Serialize(writer,term.Query);
writer.WritePropertyName("filters");
writer.WriteStartArray();
foreach (var filter in term.Filters)
{
serializer.Serialize(writer,filter);
}
writer.WriteEndArray();
writer.WritePropertyName("score_mode");
writer.WriteValue(term.ScoreMode);
writer.WritePropertyName("script");
writer.WriteValue(term.Script);
writer.WriteEndObject();
writer.WriteEndObject();
}
}
示例11: WriteJson
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var val = (IResult)value;
writer.WriteStartObject();
if (val.HasValue)
{
writer.WritePropertyName("value");
serializer.Serialize(writer, val.Value);
}
else
{
writer.WritePropertyName("hasValue");
writer.WriteValue(false);
}
writer.WritePropertyName("wasSuccessful");
serializer.Serialize(writer, val.WasSuccessful);
var observations = val.Observations.ToArray();
if (observations.Length > 0)
{
writer.WritePropertyName("observations");
serializer.Serialize(writer, observations);
}
writer.WriteEndObject();
}
示例12: OnWriteToStream
protected override void OnWriteToStream(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, TransportContext context)
{
var serializer = new JsonSerializer { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
// NOTE: we don't dispose or close these as they would
// close the stream, which is used by the rest of the pipeline.
var writer = default(JsonWriter);
if (contentHeaders.ContentType.MediaType == "application/bson")
writer = new BsonWriter(stream);
else
writer = new JsonTextWriter(new StreamWriter(stream));
if (UsesQueryComposition)
{
serializer.Serialize(writer, ((IEnumerable)value).OfType<object>().ToList());
}
else
{
serializer.Serialize(writer, value);
}
writer.Flush();
}
示例13: WriteJson
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var analysisValue = (AnalysisSettings)value;
writer.WriteStartObject();
{
if (analysisValue.Analyzers.Count > 0)
{
writer.WritePropertyName("analyzer");
serializer.Serialize(writer, analysisValue.Analyzers);
}
if (analysisValue.TokenFilters.Count > 0)
{
writer.WritePropertyName("filter");
serializer.Serialize(writer, analysisValue.TokenFilters);
}
if (analysisValue.Tokenizers.Count > 0)
{
writer.WritePropertyName("tokenizer");
serializer.Serialize(writer, analysisValue.Tokenizers);
}
if (analysisValue.CharFilters.Count > 0)
{
writer.WritePropertyName("char_filter");
serializer.Serialize(writer, analysisValue.CharFilters);
}
}
writer.WriteEndObject();
}
示例14: WriteJson
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if(!(value is Entry))
{
serializer.Serialize(writer, value);
return;
}
Entry entry = (Entry)value;
writer.WriteStartObject();
serializer.WriteProperty(writer, Utils.NameOf(() => entry.ContentId), entry.ContentId);
serializer.WriteProperty(writer, Utils.NameOf(() => entry.RelativePath), entry.RelativePath);
serializer.WriteProperty(writer, Utils.NameOf(() => entry.IsOpen), entry.IsOpen);
serializer.WriteProperty(writer, Utils.NameOf(() => entry.Name), entry.Name);
serializer.WriteProperty(writer, Utils.NameOf(() => entry.Type), entry.Type);
writer.WritePropertyName(Utils.NameOf(() => entry.Children));
if (entry.IsOpen)
{
serializer.Serialize(writer, entry.Children);
}
else
{
writer.WriteStartArray();
writer.WriteEndArray();
}
writer.WritePropertyName("has-" + Utils.NameOf(() => entry.Children));
writer.WriteValue(entry.Children.Count > 0);
writer.WriteEndObject();
}
示例15: WriteJson
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value == null)
{
serializer.Serialize(writer, null);
return;
}
var element = (UpdatableElement)value;
var jsonObject = (JObject)JsonConvert.DeserializeObject(element.SerializedValue);
//element.SerializedValue
writer.WriteStartObject();
foreach (var property in jsonObject.Properties())
{
writer.WritePropertyName(property.Name);
if (UpdatableElement.LastUpdatedFieldName.Equals(property.Name))
{
// let the serializer serialize the value itself
serializer.Serialize(writer, element.lastUpdated);
}
else
{
// let the serializer serialize the value itself
serializer.Serialize(writer, property.Value);
}
}
writer.WriteEndObject();
}