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


C# JsonSerializer.Deserialize方法代码示例

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


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

示例1: Example

        public void Example()
        {
            #region Usage
            string json = @"{ 'name': 'Admin' }{ 'name': 'Publisher' }";

            IList<Role> roles = new List<Role>();

            JsonTextReader reader = new JsonTextReader(new StringReader(json));
            reader.SupportMultipleContent = true;

            while (true)
            {
                if (!reader.Read())
                    break;

                JsonSerializer serializer = new JsonSerializer();
                Role role = serializer.Deserialize<Role>(reader);

                roles.Add(role);
            }

            foreach (Role role in roles)
            {
                Console.WriteLine(role.Name);
            }

            // Admin
            // Publisher
            #endregion

            Assert.AreEqual(2, roles.Count);
            Assert.AreEqual("Admin", roles[0].Name);
            Assert.AreEqual("Publisher", roles[1].Name);
        }
开发者ID:b-bot-110,项目名称:Newtonsoft.Json,代码行数:34,代码来源:ReadMultipleContentWithJsonReader.cs

示例2: DeserializesUInt32

 public void DeserializesUInt32()
 {
     var serializer = new JsonSerializer();
     Assert.AreEqual<UInt32>(0, serializer.Deserialize<UInt32>("0"));
     Assert.AreEqual<UInt32>(1, serializer.Deserialize<UInt32>("1"));
     Assert.AreEqual(UInt32.MaxValue, serializer.Deserialize<UInt32>("4294967295"));
 }
开发者ID:ALyman,项目名称:Liquid.Json,代码行数:7,代码来源:Integers.cs

示例3: DeserializesUInt16

 public void DeserializesUInt16()
 {
     var serializer = new JsonSerializer();
     Assert.AreEqual<UInt16>(0, serializer.Deserialize<UInt16>("0"));
     Assert.AreEqual<UInt16>(1, serializer.Deserialize<UInt16>("1"));
     Assert.AreEqual(UInt16.MaxValue, serializer.Deserialize<UInt16>("65535"));
 }
开发者ID:ALyman,项目名称:Liquid.Json,代码行数:7,代码来源:Integers.cs

示例4: DeserializesByte

 public void DeserializesByte()
 {
     var serializer = new JsonSerializer();
     Assert.AreEqual<Byte>(0, serializer.Deserialize<Byte>("0"));
     Assert.AreEqual<Byte>(1, serializer.Deserialize<Byte>("1"));
     Assert.AreEqual(Byte.MaxValue, serializer.Deserialize<Byte>("255"));
 }
开发者ID:ALyman,项目名称:Liquid.Json,代码行数:7,代码来源:Integers.cs

示例5: DeserializesUInt64

 public void DeserializesUInt64()
 {
     var serializer = new JsonSerializer();
     Assert.AreEqual<UInt64>(0, serializer.Deserialize<UInt64>("0"));
     Assert.AreEqual<UInt64>(1, serializer.Deserialize<UInt64>("1"));
     Assert.AreEqual(UInt64.MaxValue, serializer.Deserialize<UInt64>("18446744073709551615"));
 }
开发者ID:ALyman,项目名称:Liquid.Json,代码行数:7,代码来源:Integers.cs

示例6: DeserializesSByte

 public void DeserializesSByte()
 {
     var serializer = new JsonSerializer();
     Assert.AreEqual<SByte>(0, serializer.Deserialize<SByte>("0"));
     Assert.AreEqual<SByte>(1, serializer.Deserialize<SByte>("1"));
     Assert.AreEqual<SByte>(-1, serializer.Deserialize<SByte>("-1"));
 }
开发者ID:ALyman,项目名称:Liquid.Json,代码行数:7,代码来源:Integers.cs

示例7: ReadJson

		public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
		{
			JObject o = JObject.Load(reader);
			var result = existingValue as AnalysisSettings ?? new AnalysisSettings();

			foreach (var rootProperty in o.Children<JProperty>())
			{
				if (rootProperty.Name.Equals("analyzer", StringComparison.InvariantCultureIgnoreCase))
				{
					result.Analyzers = serializer.Deserialize<IDictionary<string,AnalyzerBase>>(rootProperty.Value.CreateReader());
				}
				else if (rootProperty.Name.Equals("filter", StringComparison.InvariantCultureIgnoreCase))
				{
					result.TokenFilters = serializer.Deserialize<IDictionary<string, TokenFilterBase>>(rootProperty.Value.CreateReader());
				}
				else if (rootProperty.Name.Equals("tokenizer", StringComparison.InvariantCultureIgnoreCase))
				{
					result.Tokenizers = serializer.Deserialize<IDictionary<string, TokenizerBase>>(rootProperty.Value.CreateReader());
				}
				else if (rootProperty.Name.Equals("char_filter", StringComparison.InvariantCultureIgnoreCase))
				{
					result.CharFilters = serializer.Deserialize<IDictionary<string, CharFilterBase>>(rootProperty.Value.CreateReader());
				}
			}

			return result;
		}
开发者ID:modulexcite,项目名称:Transformalize,代码行数:27,代码来源:AnalysisSettingsConverter.cs

示例8: GetTypeFromJObject

		private IElasticCoreType GetTypeFromJObject(JObject po, JsonSerializer serializer)
		{
			JToken typeToken;
			serializer.TypeNameHandling = TypeNameHandling.None;
			if (po.TryGetValue("type", out typeToken))
			{
				var type = typeToken.Value<string>().ToLowerInvariant();
				switch (type)
				{
					case "string":
						return serializer.Deserialize(po.CreateReader(), typeof(StringMapping)) as StringMapping;
					case "float":
					case "double":
					case "byte":
					case "short":
					case "integer":
					case "long":
						return serializer.Deserialize(po.CreateReader(), typeof(NumberMapping)) as NumberMapping;
					case "date":
						return serializer.Deserialize(po.CreateReader(), typeof(DateMapping)) as DateMapping;
					case "boolean":
						return serializer.Deserialize(po.CreateReader(), typeof(BooleanMapping)) as BooleanMapping;
					case "binary":
						return serializer.Deserialize(po.CreateReader(), typeof(BinaryMapping)) as BinaryMapping;
				}
			}
			return null;
		}
开发者ID:modulexcite,项目名称:Transformalize,代码行数:28,代码来源:ElasticCoreTypeConverter.cs

示例9: DeserializesInt64

 public void DeserializesInt64()
 {
     var serializer = new JsonSerializer();
     Assert.AreEqual(0, serializer.Deserialize<Int64>("0"));
     Assert.AreEqual(1, serializer.Deserialize<Int64>("1"));
     Assert.AreEqual(-1, serializer.Deserialize<Int64>("-1"));
 }
开发者ID:ALyman,项目名称:Liquid.Json,代码行数:7,代码来源:Integers.cs

示例10: NullDeserialize

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

示例11: DeserializesSingle

 public void DeserializesSingle()
 {
     var serializer = new JsonSerializer();
     Assert.AreEqual(0f, serializer.Deserialize<Single>("0"));
     Assert.AreEqual(1f, serializer.Deserialize<Single>("1"));
     Assert.AreEqual(-1f, serializer.Deserialize<Single>("-1"));
     Assert.AreEqual(1.5f, serializer.Deserialize<Single>("1.5"));
     Assert.AreEqual(-1.5f, serializer.Deserialize<Single>("-1.5"));
 }
开发者ID:ALyman,项目名称:Liquid.Json,代码行数:9,代码来源:Reals.cs

示例12: DeserializesDecimal

 public void DeserializesDecimal()
 {
     var serializer = new JsonSerializer();
     Assert.AreEqual(0m, serializer.Deserialize<Decimal>("0"));
     Assert.AreEqual(1m, serializer.Deserialize<Decimal>("1"));
     Assert.AreEqual(-1m, serializer.Deserialize<Decimal>("-1"));
     Assert.AreEqual(1.5m, serializer.Deserialize<Decimal>("1.5"));
     Assert.AreEqual(-1.5m, serializer.Deserialize<Decimal>("-1.5"));
 }
开发者ID:ALyman,项目名称:Liquid.Json,代码行数:9,代码来源:Reals.cs

示例13: DeserializesDouble

 public void DeserializesDouble()
 {
     var serializer = new JsonSerializer();
     Assert.AreEqual(0, serializer.Deserialize<Double>("0"));
     Assert.AreEqual(1, serializer.Deserialize<Double>("1"));
     Assert.AreEqual(-1, serializer.Deserialize<Double>("-1"));
     Assert.AreEqual(1.5, serializer.Deserialize<Double>("1.5"));
     Assert.AreEqual(-1.5, serializer.Deserialize<Double>("-1.5"));
 }
开发者ID:ALyman,项目名称:Liquid.Json,代码行数:9,代码来源:Reals.cs

示例14: JsonDeserializer_ShouldReturnCorrectResultWhenCorrectInput

        public void JsonDeserializer_ShouldReturnCorrectResultWhenCorrectInput()
        {
            var serializer = new JsonSerializer();
            string input = "{\"Name\":\"Test\",\"Score\":65,\"Id\":\"1\"}";
            Player expResult = new Player { Name = "Test", Score = 65, Id = "1" };

            Assert.AreEqual(expResult.Name, serializer.Deserialize<Player>(input).Name);
            Assert.AreEqual(expResult.Score, serializer.Deserialize<Player>(input).Score);
            Assert.AreEqual(expResult.Id, serializer.Deserialize<Player>(input).Id);
        }
开发者ID:Baloons-Pop-3,项目名称:Baloons-Pop-Three,代码行数:10,代码来源:JsonSerializerTests.cs

示例15: Deserialize

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


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