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


C# JsonSerializer.Serialize方法代码示例

本文整理汇总了C#中JsonSerializer.Serialize方法的典型用法代码示例。如果您正苦于以下问题:C# JsonSerializer.Serialize方法的具体用法?C# JsonSerializer.Serialize怎么用?C# JsonSerializer.Serialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在JsonSerializer的用法示例。


在下文中一共展示了JsonSerializer.Serialize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: NullSerialize

 public void NullSerialize()
 {
     var Serializer = new JsonSerializer();
     Assert.Equal("", Serializer.Serialize(typeof(object), null));
     Assert.Equal("", Serializer.Serialize(null, new { A = "" }));
     Assert.Equal("", Serializer.Serialize(null, null));
 }
开发者ID:modulexcite,项目名称:Craig-s-Utility-Library,代码行数:7,代码来源:JsonSerializer.cs

示例2: 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:modulexcite,项目名称:Transformalize,代码行数:32,代码来源:AnalysisSettingsConverter.cs

示例3: TryWriteFeature

		public bool TryWriteFeature(JsonWriter writer, object value, JsonSerializer serializer)
		{
			if (!_shapeConverter.CanConvert(ObjectType.Feature))
				return false;

			ObjectType geomType = _shapeConverter.GetObjectType(value);
			if (geomType != ObjectType.Feature)
				return false;

			writer.WriteStartObject();
			writer.WritePropertyName("type");
			writer.WriteValue(Enum.GetName(typeof(ObjectType), ObjectType.Feature));

			object id;
			Dictionary<string, object> props;
			var geometry = _shapeConverter.FromFeature(value, out id, out props);

			if (id != null)
			{
				writer.WritePropertyName("id");
				serializer.Serialize(writer, id);
			}

			if (props != null && props.Count > 0)
			{
				writer.WritePropertyName("properties");
				serializer.Serialize(writer, props);
			}

			writer.WritePropertyName("geometry");
			serializer.Serialize(writer, geometry);

			writer.WriteEndObject();
			return true;
		}
开发者ID:sibartlett,项目名称:RavenDB.Client.Spatial,代码行数:35,代码来源:GeoJsonWriter.cs

示例4: Serialize

 public void Serialize()
 {
     var Serializer = new JsonSerializer();
     dynamic Object = new { A = 5, B = "ASDF" };
     Assert.Equal("{\"A\":5,\"B\":\"ASDF\"}", Serializer.Serialize(Object.GetType(), Object));
     Object = new TestObject { A = 5, B = "ASDF" };
     Assert.Equal("{\"A\":5,\"B\":\"ASDF\"}", Serializer.Serialize(Object.GetType(), Object));
 }
开发者ID:modulexcite,项目名称:Craig-s-Utility-Library,代码行数:8,代码来源:JsonSerializer.cs

示例5: TestGetPlaylist

        public void TestGetPlaylist()
        {
            var playlists = soundCloudClient.User(settings.TestUserId).GetPlaylists();
            if (playlists.Length == 0)
                return;

            var playlist = soundCloudClient.Playlist(playlists[0].Id).GetPlaylist();
            var jsonSerializer = new JsonSerializer();
            Assert.AreEqual(jsonSerializer.Serialize(playlists[0]), jsonSerializer.Serialize(playlist));
        }
开发者ID:Onkelborg,项目名称:SoundCloud.API.Client,代码行数:10,代码来源:PlaylistApiTest.cs

示例6: WriteJson

        /// <summary>
        /// Writes the JSON representation of the object.
        /// </summary>
        /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
        /// <param name="value">The value.</param>
        /// <param name="serializer">The calling serializer.</param>
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            ReflectionObject reflectionObject = ReflectionObjectPerType.Get(value.GetType());

            DefaultContractResolver resolver = serializer.ContractResolver as DefaultContractResolver;

            writer.WriteStartObject();
            writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(KeyName) : KeyName);
            serializer.Serialize(writer, reflectionObject.GetValue(value, KeyName), reflectionObject.GetType(KeyName));
            writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(ValueName) : ValueName);
            serializer.Serialize(writer, reflectionObject.GetValue(value, ValueName), reflectionObject.GetType(ValueName));
            writer.WriteEndObject();
        }
开发者ID:arpitgold,项目名称:Exceptionless,代码行数:19,代码来源:KeyValuePairConverter.cs

示例7: WriteJson

		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			var mapping = (WarmerMapping) value;
			writer.WriteStartObject();

			writer.WritePropertyName("types");
			serializer.Serialize(writer, mapping.Types);

			writer.WritePropertyName("source");
			serializer.Serialize(writer, mapping.Source);

			writer.WriteEndObject();
		}
开发者ID:modulexcite,项目名称:Transformalize,代码行数:13,代码来源:WarmerMappingConverter.cs

示例8: WriteJson

    /// <summary>
    /// Writes the JSON representation of the object.
    /// </summary>
    /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
    /// <param name="value">The value.</param>
    /// <param name="serializer">The calling serializer.</param>
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
      Type t = value.GetType();
      PropertyInfo keyProperty = t.GetProperty("Key");
      PropertyInfo valueProperty = t.GetProperty("Value");

      writer.WriteStartObject();
      writer.WritePropertyName("Key");
      serializer.Serialize(writer, ReflectionUtils.GetMemberValue(keyProperty, value));
      writer.WritePropertyName("Value");
      serializer.Serialize(writer, ReflectionUtils.GetMemberValue(valueProperty, value));
      writer.WriteEndObject();
    }
开发者ID:RecursosOnline,项目名称:c-sharp,代码行数:19,代码来源:KeyValuePairConverter.cs

示例9: WriteJson

		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			var f = value as ITermsBaseFilter;
			if (f == null || (f.IsConditionless && !f.IsVerbatim)) return;
			
			var contract = serializer.ContractResolver as SettingsContractResolver;
			if (contract == null)
				return;
			
			var field = contract.Infer.PropertyPath(f.Field);
			if (field.IsNullOrEmpty())
				return;

			writer.WriteStartObject();
			{
				var lf = f as ITermsLookupFilter;
				if (lf != null)
				{
					writer.WritePropertyName(field);
					writer.WriteStartObject();
					{
						WriteProperty(writer, f, "id", lf.Id);
						SerializeProperty(writer, serializer, f, "type", lf.Type);
						SerializeProperty(writer, serializer, f, "index", lf.Index);
						SerializeProperty(writer, serializer, f, "path", lf.Path);
						WriteProperty(writer, f, "routing", lf.Routing);
						WriteProperty(writer, f, "cache", lf.CacheLookup);
					}
					writer.WriteEndObject();
				}
				var tf = f as ITermsFilter;
				if (tf != null)
				{
					writer.WritePropertyName(field);
					serializer.Serialize(writer, tf.Terms);
				}
				if (f.Execution.HasValue)
				{
					writer.WritePropertyName("execution");
					serializer.Serialize(writer, f.Execution.Value);
				}

				WriteProperty(writer, f, "_cache", f.Cache);
				WriteProperty(writer, f, "_cache_key", f.CacheKey);
				WriteProperty(writer, f, "_name", f.FilterName);
				
			}
			writer.WriteEndObject();		
		}
开发者ID:modulexcite,项目名称:Transformalize,代码行数:49,代码来源:TermsFilterConverter.cs

示例10: WriteJson

		/// <summary>
		/// Writes the JSON representation of the object.
		/// </summary>
		/// <param name="writer">The <see cref="T:Raven.Imports.Newtonsoft.Json.JsonWriter"/> to write to.</param>
		/// <param name="value">The value.</param>
		/// <param name="serializer">The calling serializer.</param>
		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			if (value == null)
			{
					writer.WriteNull();
				return;
			}
			if (value.GetType() == typeof (object))
			{
				writer.WriteStartObject();
				writer.WriteEndObject();
				return;
			}

			var dynamicValue = ((IDynamicMetaObjectProvider) value).GetMetaObject(Expression.Constant(value));

			writer.WriteStartObject();
			foreach (var dynamicMemberName in dynamicValue.GetDynamicMemberNames())
			{
				writer.WritePropertyName(dynamicMemberName);
				var memberValue = DynamicUtil.GetValueDynamically(value, dynamicMemberName);
				if(memberValue == null || memberValue is ValueType || memberValue is string)
					writer.WriteValue(memberValue);
				else
					serializer.Serialize(writer, memberValue);
			}
			writer.WriteEndObject();

		}
开发者ID:925coder,项目名称:ravendb,代码行数:35,代码来源:JsonDynamicConverter.cs

示例11: WhenSerializeWithMoreDatatypes_GetSerialiedJsonString

        public void WhenSerializeWithMoreDatatypes_GetSerialiedJsonString()
        {
            // Arrange
            var jsonSerializer = new JsonSerializer();
            var jsonMoreDataTypes = new JsonMoreDataTypes
            {
                BoolProperty = true,
                ByteProperty = 1,
                SByteProperty = 2,
                CharProperty = 'K',
                DoubleProperty = 12.52,
                FloatProperty = 12.52F,
                IntProperty = 15789,
                UIntProperty = 1234,
                LongProperty = 152,
                ULongProperty = 12345,
                ShortProperty = 123,
                UShortProperty = 555,
                StringProperty = "This is a string"
            };

            // Act
            var result = jsonSerializer.Serialize(jsonMoreDataTypes);

            // Assert
            Assert.AreEqual("{\"boolProperty\": true,\"byteProperty\": 1,\"sByteProperty\": 2,\"charProperty\": \"K\",\"doubleProperty\": 12.52,\"floatProperty\": 12.5200005,\"intProperty\": 15789,\"uIntProperty\": 1234,\"longProperty\": 152,\"uLongProperty\": 12345,\"shortProperty\": 123,\"uShortProperty\": 555,\"stringProperty\": \"This is a string\"}", result);
        }
开发者ID:JakeLardinois,项目名称:WebServer,代码行数:27,代码来源:JsonSerializerTests.cs

示例12: WriteLargeJson

        public static void WriteLargeJson()
        {
            var json = System.IO.File.ReadAllText("large.json");

            IList<PerformanceTests.RootObject> o = JsonConvert.DeserializeObject<IList<PerformanceTests.RootObject>>(json);

            Console.WriteLine("Press any key to start serialize");
            Console.ReadKey();
            Console.WriteLine("Serializing...");

            Stopwatch sw = new Stopwatch();
            sw.Start();

            for (int i = 0; i < 100; i++)
            {
                using (StreamWriter file = System.IO.File.CreateText("largewrite.json"))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    serializer.Formatting = Formatting.Indented;
                    serializer.Serialize(file, o);
                }
            }

            sw.Stop();

            Console.WriteLine("Finished. Total seconds: " + sw.Elapsed.TotalSeconds);
        }
开发者ID:ZhouAnPing,项目名称:Mail,代码行数:27,代码来源:Program.cs

示例13: CyclicalObject

 public void CyclicalObject()
 {
     var serializer = new JsonSerializer();
     var a = new CyclicalObject_Class();
     a.A = a;
     string result = serializer.Serialize(a);
 }
开发者ID:ALyman,项目名称:Liquid.Json,代码行数:7,代码来源:ComplexObjects.cs

示例14: AClassDecoratedWithTheEncryptAttributeIsEncrypted

        public void AClassDecoratedWithTheEncryptAttributeIsEncrypted()
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            var configuration = new JsonSerializerConfiguration
            {
                EncryptionMechanism = encryptionMechanism,
            };

            var serializer = new JsonSerializer<Grault>(configuration);

            var instance = new Grault
            {
                Qux = "abc",
                Garply = true
            };

            var json = serializer.Serialize(instance);

            var expected =
                @""""
                + encryptionMechanism.Encrypt(@"{""Qux"":""abc"",""Garply"":true}")
                + @"""";

            Assert.That(json, Is.EqualTo(expected));
        }
开发者ID:JonOsment,项目名称:XSerializer,代码行数:26,代码来源:CustomJsonSerializerTests.cs

示例15: CanSerializeWithEncryptRootObjectEnabled

        public void CanSerializeWithEncryptRootObjectEnabled()
        {
            var encryptionMechanism = new Base64EncryptionMechanism();

            var configuration = new JsonSerializerConfiguration
            {
                EncryptionMechanism = encryptionMechanism,
                EncryptRootObject = true
            };

            var serializer = new JsonSerializer<Bar>(configuration);

            var instance = new Bar
            {
                Baz = new Baz
                {
                    Qux = "abc",
                    Garply = true
                },
                Corge = 123.45
            };

            var json = serializer.Serialize(instance);

            var expected =
                @""""
                + encryptionMechanism.Encrypt(@"{""Baz"":{""Qux"":""abc"",""Garply"":true},""Corge"":123.45}")
                + @"""";

            Assert.That(json, Is.EqualTo(expected));
        }
开发者ID:JonOsment,项目名称:XSerializer,代码行数:31,代码来源:CustomJsonSerializerTests.cs


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