-Fix occasional error when converting non-long integer properties to XML

-Fix boolean values sometimes being capitalized when converting to XML
-Fix error when deserializing ConcurrentDictionary
-Fix serializing some Uris
-Change KeyValurPairConverter to not rely on the order of the key and value properties
This commit is contained in:
JamesNK
2011-10-31 23:44:10 +00:00
parent b24bcda921
commit b305ba100b
12 changed files with 327 additions and 46 deletions
@@ -1,4 +1,4 @@
#region License
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
@@ -1257,6 +1257,50 @@ namespace Newtonsoft.Json.Tests.Converters
}
}", json);
}
[Test]
public void DeserializeNonInt64IntegerValues()
{
var dict = new Dictionary<string, object> { { "Int16", (short)1 }, { "Float", 2f }, { "Int32", 3 } };
var obj = JObject.FromObject(dict);
var serializer = JsonSerializer.Create(new JsonSerializerSettings { Converters = { new XmlNodeConverter() { DeserializeRootElementName = "root" } } });
using (var reader = obj.CreateReader())
{
var value = (XmlDocument)serializer.Deserialize(reader, typeof(XmlDocument));
Assert.AreEqual(@"<root><Int16>1</Int16><Float>2</Float><Int32>3</Int32></root>", value.InnerXml);
}
}
[Test]
public void DeserializingBooleanValues()
{
MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(@"{root:{""@booleanType"":true}}"));
MemoryStream xml = new MemoryStream();
JsonBodyToSoapXml(ms, xml);
string xmlString = System.Text.Encoding.UTF8.GetString(xml.ToArray());
Assert.AreEqual(@"<?xml version=""1.0"" encoding=""utf-8""?><root booleanType=""true"" />", xmlString);
}
static void JsonBodyToSoapXml(Stream json, Stream xml)
{
Newtonsoft.Json.JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings();
settings.Converters.Add(new Newtonsoft.Json.Converters.XmlNodeConverter());
Newtonsoft.Json.JsonSerializer serializer = Newtonsoft.Json.JsonSerializer.Create(settings);
using (Newtonsoft.Json.JsonTextReader reader = new Newtonsoft.Json.JsonTextReader(new System.IO.StreamReader(json)))
{
XmlDocument doc = (XmlDocument)serializer.Deserialize(reader, typeof(XmlDocument));
if (reader.Read() && reader.TokenType != JsonToken.Comment)
throw new JsonSerializationException("Additional text found in JSON string after finishing deserializing object.");
using (XmlWriter writer = XmlWriter.Create(xml))
{
doc.Save(writer);
}
}
}
}
}
#endif
@@ -436,5 +436,17 @@ Parameter name: index")]
{
JArray.Parse("[1");
}
[Test]
public void InsertAddEnd()
{
JArray array = new JArray();
array.Insert(0, 123);
array.Insert(1, 456);
Assert.AreEqual(2, array.Count);
Assert.AreEqual(123, (int)array[0]);
Assert.AreEqual(456, (int)array[1]);
}
}
}
@@ -209,6 +209,9 @@ namespace Newtonsoft.Json.Tests.Linq
[Test]
public void Casting()
{
Assert.AreEqual(1L, (long)(new JValue(1)));
Assert.AreEqual(2L, (long) new JArray(1, 2, 3)[1]);
Assert.AreEqual(new DateTime(2000, 12, 20), (DateTime)new JValue(new DateTime(2000, 12, 20)));
#if !PocketPC && !NET20
Assert.AreEqual(new DateTimeOffset(2000, 12, 20, 23, 50, 10, TimeSpan.Zero), (DateTimeOffset)new JValue(new DateTimeOffset(2000, 12, 20, 23, 50, 10, TimeSpan.Zero)));
@@ -126,14 +126,14 @@ namespace Newtonsoft.Json.Tests
public void BuildJObject()
{
JObject o = new JObject();
for (int i = 0; i < 200; i++)
for (int i = 0; i < 50; i++)
{
o[i.ToString()] = i;
}
string jsonText = o.ToString();
// this is extremely slow with 5000 interations
int interations = 100;
int interations = 1000;
TimeOperation(() =>
{
@@ -66,5 +66,5 @@ using System.Runtime.InteropServices;
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.0.3.0")]
#if !PocketPC
[assembly: AssemblyFileVersion("4.0.3.14202")]
[assembly: AssemblyFileVersion("4.0.3.14301")]
#endif
@@ -24,11 +24,15 @@
#endregion
using System;
#if !(NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE)
using System.Collections.Concurrent;
#endif
using System.Collections.Generic;
#if !SILVERLIGHT && !PocketPC && !NET20
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization.Formatters;
using System.Threading;
using System.Web.Script.Serialization;
#endif
@@ -4910,6 +4914,107 @@ keyword such as type of business.""
Assert.AreEqual(@"{""p"":1}", result);
}
}
#if !(NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE)
[Test]
public void DeserializeConcurrentDictionary()
{
IDictionary<string, Component> components = new Dictionary<string, Component>
{
{"Key!", new Component()}
};
GameObject go = new GameObject
{
Components = new ConcurrentDictionary<string, Component>(components),
Id = "Id!",
Name = "Name!"
};
string originalJson = JsonConvert.SerializeObject(go, Formatting.Indented);
Assert.AreEqual(@"{
""Components"": {
""Key!"": {}
},
""Id"": ""Id!"",
""Name"": ""Name!""
}", originalJson);
GameObject newObject = JsonConvert.DeserializeObject<GameObject>(originalJson);
Assert.AreEqual(1, newObject.Components.Count);
Assert.AreEqual("Id!", newObject.Id);
Assert.AreEqual("Name!", newObject.Name);
}
#endif
[Test]
public void DeserializeKeyValuePairArray()
{
string json = @"[ { ""Value"": [ ""1"", ""2"" ], ""Key"": ""aaa"", ""BadContent"": [ 0 ] }, { ""Value"": [ ""3"", ""4"" ], ""Key"": ""bbb"" } ]";
IList<KeyValuePair<string, IList<string>>> values = JsonConvert.DeserializeObject<IList<KeyValuePair<string, IList<string>>>>(json);
Assert.AreEqual(2, values.Count);
Assert.AreEqual("aaa", values[0].Key);
Assert.AreEqual(2, values[0].Value.Count);
Assert.AreEqual("1", values[0].Value[0]);
Assert.AreEqual("2", values[0].Value[1]);
Assert.AreEqual("bbb", values[1].Key);
Assert.AreEqual(2, values[1].Value.Count);
Assert.AreEqual("3", values[1].Value[0]);
Assert.AreEqual("4", values[1].Value[1]);
}
[Test]
public void DeserializeNullableKeyValuePairArray()
{
string json = @"[ { ""Value"": [ ""1"", ""2"" ], ""Key"": ""aaa"", ""BadContent"": [ 0 ] }, null, { ""Value"": [ ""3"", ""4"" ], ""Key"": ""bbb"" } ]";
IList<KeyValuePair<string, IList<string>>?> values = JsonConvert.DeserializeObject<IList<KeyValuePair<string, IList<string>>?>>(json);
Assert.AreEqual(3, values.Count);
Assert.AreEqual("aaa", values[0].Value.Key);
Assert.AreEqual(2, values[0].Value.Value.Count);
Assert.AreEqual("1", values[0].Value.Value[0]);
Assert.AreEqual("2", values[0].Value.Value[1]);
Assert.AreEqual(null, values[1]);
Assert.AreEqual("bbb", values[2].Value.Key);
Assert.AreEqual(2, values[2].Value.Value.Count);
Assert.AreEqual("3", values[2].Value.Value[0]);
Assert.AreEqual("4", values[2].Value.Value[1]);
}
[Test]
[ExpectedException(typeof (Exception), ExpectedMessage = "Could not deserialize Null to KeyValuePair.")]
public void DeserializeNullToNonNullableKeyValuePairArray()
{
string json = @"[ null ]";
JsonConvert.DeserializeObject<IList<KeyValuePair<string, IList<string>>>>(json);
}
[Test]
public void SerializeUriWithQuotes()
{
string input = "http://test.com/%22foo+bar%22";
Uri uri = new Uri(input);
string json = JsonConvert.SerializeObject(uri);
Uri output = JsonConvert.DeserializeObject<Uri>(json);
Assert.AreEqual(uri, output);
}
[Test]
public void SerializeUriWithSlashes()
{
string input = @"http://tes/?a=b\\c&d=e\";
Uri uri = new Uri(input);
string json = JsonConvert.SerializeObject(uri);
Uri output = JsonConvert.DeserializeObject<Uri>(json);
Assert.AreEqual(uri, output);
}
}
public class UriGuidTimeSpanTestClass
@@ -4920,4 +5025,47 @@ keyword such as type of business.""
public TimeSpan? NullableTimeSpan { get; set; }
public Uri Uri { get; set; }
}
#if !(NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE)
[JsonObject(MemberSerialization.OptIn)]
public class GameObject
{
[JsonProperty]
public string Id { get; set; }
[JsonProperty]
public string Name { get; set; }
[JsonProperty]
public ConcurrentDictionary<string, Component> Components;
public GameObject()
{
Components = new ConcurrentDictionary<string, Component>();
}
}
[JsonObject(MemberSerialization.OptIn)]
public class Component
{
[JsonIgnore] // Ignore circular reference
public GameObject GameObject { get; set; }
public Component()
{
}
}
[JsonObject(MemberSerialization.OptIn)]
public class TestComponent : Component
{
[JsonProperty]
public int MyProperty { get; set; }
public TestComponent()
{
}
}
#endif
}