-Fixed serializer handling of comments in JSON

This commit is contained in:
JamesNK
2009-05-31 12:10:54 +00:00
parent fec0ec5a1d
commit d160b9f587
4 changed files with 184 additions and 124 deletions
@@ -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);
}
}
}
+1 -1
View File
@@ -624,7 +624,7 @@ namespace Newtonsoft.Json
{
deserializedValue = jsonSerializer.Deserialize(jsonReader, type);
if (jsonReader.Read())
if (jsonReader.Read() && jsonReader.TokenType != JsonToken.Comment)
throw new JsonSerializationException("Additional text found in JSON string after finishing deserializing object.");
}
@@ -136,7 +136,6 @@ namespace Newtonsoft.Json.Serialization
private object CreateValue(JsonReader reader, Type objectType, object existingValue, JsonConverter memberConverter)
{
object value;
JsonConverter converter;
if (memberConverter != null)
@@ -156,6 +155,8 @@ namespace Newtonsoft.Json.Serialization
return JsonRaw.Create(reader);
}
else
{
do
{
switch (reader.TokenType)
{
@@ -250,55 +251,59 @@ namespace Newtonsoft.Json.Serialization
if (objectType == null)
{
value = CreateJObject(reader);
return CreateJObject(reader);
}
else
{
if (CollectionUtils.IsDictionaryType(objectType))
{
if (existingValue == null)
value = CreateAndPopulateDictionary(reader, objectType, id);
return CreateAndPopulateDictionary(reader, objectType, id);
else
value = PopulateDictionary(CollectionUtils.CreateDictionaryWrapper(existingValue), reader, id);
return PopulateDictionary(CollectionUtils.CreateDictionaryWrapper(existingValue), reader, id);
}
else
{
if (existingValue == null)
value = CreateAndPopulateObject(reader, objectType, id);
return CreateAndPopulateObject(reader, objectType, id);
else
value = PopulateObject(existingValue, reader, objectType, id);
return PopulateObject(existingValue, reader, objectType, id);
}
}
break;
case JsonToken.StartArray:
value = CreateList(reader, objectType, existingValue, null);
return CreateList(reader, objectType, existingValue, null);
break;
case JsonToken.Integer:
case JsonToken.Float:
case JsonToken.String:
case JsonToken.Boolean:
case JsonToken.Date:
value = EnsureType(reader.Value, objectType);
return EnsureType(reader.Value, objectType);
break;
case JsonToken.StartConstructor:
case JsonToken.EndConstructor:
string constructorName = reader.Value.ToString();
value = constructorName;
return constructorName;
break;
case JsonToken.Null:
case JsonToken.Undefined:
if (objectType == typeof(DBNull))
value = DBNull.Value;
return DBNull.Value;
else
value = null;
return null;
break;
case JsonToken.Comment:
// ignore
break;
default:
throw new JsonSerializationException("Unexpected token while deserializing object: " + reader.TokenType);
}
}
} while (reader.Read());
return value;
throw new JsonSerializationException("Unexpected end when deserializing object.");
}
}
private void CheckedRead(JsonReader reader)
@@ -646,6 +651,9 @@ namespace Newtonsoft.Json.Serialization
throw new JsonSerializationException("Required property '{0}' not found in JSON.".FormatWith(CultureInfo.InvariantCulture, requiredMapping.Key));
}
return newObject;
case JsonToken.Comment:
// ignore
break;
default:
throw new JsonSerializationException("Unexpected token when deserializing object: " + reader.TokenType);
}