当前位置: 首页>>代码示例>>C#>>正文


C# JsonSerializer.Serialize方法代码示例

本文整理汇总了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();
        }
开发者ID:JoryUK,项目名称:WebApi.Hal,代码行数:34,代码来源:ResourceListConverter.cs

示例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();
			}
		}
开发者ID:starckgates,项目名称:ElasticSearch.Net,代码行数:60,代码来源:ElasticQueryConverter.cs

示例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;
            }
        }
开发者ID:grimcoder,项目名称:ProduceMarketRESTWebAPI,代码行数:32,代码来源:Utils.cs

示例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();
		}
开发者ID:niemyjski,项目名称:elasticsearch-net,代码行数:30,代码来源:MultiGetRequestJsonConverter.cs

示例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();
		}
开发者ID:c1sc0,项目名称:elasticsearch-net,代码行数:33,代码来源:TermsQueryJsonConverter.cs

示例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();
		}
开发者ID:RossLieberman,项目名称:NEST,代码行数:29,代码来源:ScoreFunctionJsonConverter.cs

示例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();
		}
开发者ID:devodo,项目名称:elasticsearch-net,代码行数:32,代码来源:RegisterPercolatorJsonConverter.cs

示例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();
        }
开发者ID:Terradue,项目名称:DotNetOpenSearchGeoJson,代码行数:26,代码来源:FeatureResultJsonConverter.cs

示例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);
        }
开发者ID:JacquiManzi,项目名称:KineticSpectrum,代码行数:25,代码来源:ColorEffectConverter.cs

示例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();
			}
		}
开发者ID:mthomas,项目名称:ElasticSearch.Net,代码行数:29,代码来源:CustomFiltersScoreQueryConverterer.cs

示例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();
        }
开发者ID:iSynaptic,项目名称:iSynaptic.Core,代码行数:28,代码来源:ResultJsonConverter.cs

示例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();

        }
开发者ID:pullpush,项目名称:NAd,代码行数:25,代码来源:NewtonsoftJsonFormatter.cs

示例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();

		}
开发者ID:JebteK,项目名称:elasticsearch-net,代码行数:32,代码来源:AnalysisSettingsConverter.cs

示例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();
        }
开发者ID:Runt-Editor,项目名称:Runt,代码行数:30,代码来源:EntryJsonConverter.cs

示例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();
        }
开发者ID:Paraintom,项目名称:SplitonsPersistence,代码行数:29,代码来源:UpdatableElementConverter.cs


注:本文中的Newtonsoft.Json.JsonSerializer.Serialize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。