-Fixed serializer handling of comments in JSON
This commit is contained in:
@@ -2549,5 +2549,29 @@ keyword such as type of business.""
|
||||
TypeNameHandling = TypeNameHandling.Objects
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TypedObjectDeserializationWithComments()
|
||||
{
|
||||
string json = @"/*comment*/ { /*comment*/
|
||||
""Name"": /*comment*/ ""Apple"" /*comment*/, /*comment*/
|
||||
""ExpiryDate"": ""\/Date(1230375600000+1300)\/"",
|
||||
""Price"": 3.99,
|
||||
""Sizes"": /*comment*/ [ /*comment*/
|
||||
""Small"", /*comment*/
|
||||
""Medium"" /*comment*/,
|
||||
/*comment*/ ""Large""
|
||||
/*comment*/ ] /*comment*/
|
||||
} /*comment*/";
|
||||
|
||||
Product deserializedProduct = (Product)JsonConvert.DeserializeObject(json, typeof(Product));
|
||||
|
||||
Assert.AreEqual("Apple", deserializedProduct.Name);
|
||||
Assert.AreEqual(new DateTime(2008, 12, 28), deserializedProduct.ExpiryDate);
|
||||
Assert.AreEqual(3.99, deserializedProduct.Price);
|
||||
Assert.AreEqual("Small", deserializedProduct.Sizes[0]);
|
||||
Assert.AreEqual("Medium", deserializedProduct.Sizes[1]);
|
||||
Assert.AreEqual("Large", deserializedProduct.Sizes[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -426,5 +426,33 @@ Parameter name: arrayIndex")]
|
||||
""title"": null
|
||||
}", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
string json = @"{
|
||||
""Name"": ""Apple"",
|
||||
""Expiry"": new Date(1230422400000),
|
||||
""Price"": 3.99,
|
||||
""Sizes"": [
|
||||
""Small"",
|
||||
""Medium"",
|
||||
""Large""
|
||||
]
|
||||
}";
|
||||
|
||||
JObject o = JObject.Parse(json);
|
||||
|
||||
string name = (string)o["Name"];
|
||||
// Apple
|
||||
|
||||
JArray sizes = (JArray)o["Sizes"];
|
||||
|
||||
string smallest = (string)sizes[0];
|
||||
// Small
|
||||
|
||||
Console.WriteLine(name);
|
||||
Console.WriteLine(smallest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user