-Added reading and writing binary JSON (BSON) support - work in progress

-Added BsonReader, BsonWriter
-Added support for reading and writing byte arrays to JsonReader, JsonWriter and LINQ to JSON classes
-Added ReadAsBytes to JsonReader
-Changed Silverlight build serializer to look for a TypeConverter
-Changed .NET build to not rely on System.ComponentModel.DataAnnotations assembly to support .NET 4.0 client profile
-Remove DateTimeOffset stand-in from Compact Framework build
-Fixed .NET 2.0 build to run on environments without .NET 2.0 SP1
-Fixed deserialization to always use a new value created from a JsonConverter
-Fixed XmlNodeConverter to support converting comments in JSON
This commit is contained in:
JamesNK
2009-12-06 23:10:14 +00:00
parent 7d845d1f13
commit f1a9993723
59 changed files with 1952 additions and 697 deletions
@@ -428,7 +428,6 @@ keyword such as type of business.""
result = JsonConvert.SerializeObject(testDates);
Assert.AreEqual(expected, result);
}
#endif
[Test]
public void DateTimeOffset()
@@ -443,6 +442,7 @@ keyword such as type of business.""
string result = JsonConvert.SerializeObject(testDates);
Assert.AreEqual(@"[""\/Date(-59011455539000+0000)\/"",""\/Date(946688461000+0000)\/"",""\/Date(946641661000+1300)\/"",""\/Date(946701061000-0330)\/""]", result);
}
#endif
[Test]
public void NonStringKeyDictionary()
@@ -1715,6 +1715,7 @@ keyword such as type of business.""
Assert.AreEqual("Product 1", products[0].Name);
}
#if !PocketPC && !NET20
[Test]
public void DeserializeEmptyStringToNullableDateTime()
{
@@ -1723,6 +1724,7 @@ keyword such as type of business.""
NullableDateTimeTestClass c = JsonConvert.DeserializeObject<NullableDateTimeTestClass>(json);
Assert.AreEqual(null, c.DateTimeField);
}
#endif
[Test]
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = @"Unable to find a constructor to use for type Newtonsoft.Json.Tests.TestObjects.Event. A class should either have a default constructor or only one constructor with arguments.")]
@@ -1911,5 +1913,85 @@ keyword such as type of business.""
Assert.AreEqual(structTest.IntProperty, deserialized.IntProperty);
Assert.AreEqual(structTest.IntField, deserialized.IntField);
}
public class ListOfIds<T> : JsonConverter where T : Bar, new()
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
IList<T> list = (IList<T>) value;
writer.WriteStartArray();
foreach (T item in list)
{
writer.WriteValue(item.Id);
}
writer.WriteEndArray();
}
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
{
IList<T> list = new List<T>();
reader.Read();
while (reader.TokenType != JsonToken.EndArray)
{
long id = (long)reader.Value;
list.Add(new T
{
Id = Convert.ToInt32(id)
});
reader.Read();
}
return list;
}
public override bool CanConvert(Type objectType)
{
return typeof (IList<T>).IsAssignableFrom(objectType);
}
}
public class Foo
{
public Foo()
{
Bars = new List<Bar>();
}
[JsonConverter(typeof(ListOfIds<Bar>))]
public List<Bar> Bars { get; set; }
}
public class Bar
{
public int Id { get; set; }
}
[Test]
public void SerializeListWithJsonConverter()
{
Foo f = new Foo();
f.Bars.Add(new Bar { Id = 0 });
f.Bars.Add(new Bar { Id = 1 });
f.Bars.Add(new Bar { Id = 2 });
string json = JsonConvert.SerializeObject(f, Formatting.Indented);
Assert.AreEqual(@"{
""Bars"": [
0,
1,
2
]
}", json);
Foo newFoo = JsonConvert.DeserializeObject<Foo>(json);
Assert.AreEqual(3, newFoo.Bars.Count);
Assert.AreEqual(0, newFoo.Bars[0].Id);
Assert.AreEqual(1, newFoo.Bars[1].Id);
Assert.AreEqual(2, newFoo.Bars[2].Id);
}
}
}
@@ -1,4 +1,29 @@
using System;
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -23,7 +48,7 @@ namespace Newtonsoft.Json.Tests.Serialization
//JsonConvert.ConvertDateTimeToJavaScriptTicks(s1.Establised.DateTime)
Assert.AreEqual(@"{""Color"":4,""Establised"":""\/Date(1264122061000+0000)\/"",""Width"":1.1,""Employees"":999,""RoomsPerFloor"":[1,2,3,4,5,6,7,8,9],""Open"":false,""Symbol"":""@"",""Mottos"":[""Hello World"",""öäüÖÄÜ\\'{new Date(12345);}[222]_µ@²³~"",null,"" ""],""Cost"":100980.1,""Escape"":""\r\n\t\f\b?{\\r\\n\""'"",""product"":[{""Name"":""Rocket"",""ExpiryDate"":""\/Date(949532490000)\/"",""Price"":0.0},{""Name"":""Alien"",""ExpiryDate"":""\/Date(946684800000)\/"",""Price"":0.0}]}", sw.GetStringBuilder().ToString());
Assert.AreEqual(@"{""Color"":4,""Establised"":""\/Date(1264122061000)\/"",""Width"":1.1,""Employees"":999,""RoomsPerFloor"":[1,2,3,4,5,6,7,8,9],""Open"":false,""Symbol"":""@"",""Mottos"":[""Hello World"",""öäüÖÄÜ\\'{new Date(12345);}[222]_µ@²³~"",null,"" ""],""Cost"":100980.1,""Escape"":""\r\n\t\f\b?{\\r\\n\""'"",""product"":[{""Name"":""Rocket"",""ExpiryDate"":""\/Date(949532490000)\/"",""Price"":0.0},{""Name"":""Alien"",""ExpiryDate"":""\/Date(946684800000)\/"",""Price"":0.0}]}", sw.GetStringBuilder().ToString());
Store s2 = (Store)jsonSerializer.Deserialize(new JsonTextReader(new StringReader("{}")), typeof(Store));
Assert.AreEqual("\r\n\t\f\b?{\\r\\n\"\'", s2.Escape);