本文整理汇总了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);
}
示例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"));
}
示例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"));
}
示例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"));
}
示例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"));
}
示例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"));
}
示例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;
}
示例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;
}
示例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"));
}
示例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, ""));
}
示例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"));
}
示例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"));
}
示例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"));
}
示例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);
}
示例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);
}