-Fix ToString(JsonConverter[]) overload on JToken

-Ensure JavaScriptDateTimeConverter converts nullable DateTimes
-Make JsonTypeReflector thread safe
This commit is contained in:
JamesNK
2009-02-27 06:12:37 +00:00
parent eedb01645a
commit 6068b06744
5 changed files with 134 additions and 14 deletions
@@ -1259,5 +1259,79 @@ keyword such as type of business.""
String strFromTest = JsonConvert.SerializeObject(testClass); String strFromTest = JsonConvert.SerializeObject(testClass);
Test testFromDe = (Test)JsonConvert.DeserializeObject(strFromTest, typeof(Test)); Test testFromDe = (Test)JsonConvert.DeserializeObject(strFromTest, typeof(Test));
} }
private Person GetPerson()
{
Person person = new Person
{
Name = "Mike Manager",
BirthDate = new DateTime(1983, 8, 3, 0, 0, 0, DateTimeKind.Utc),
Department = "IT",
LastModified = new DateTime(2009, 2, 15, 0, 0, 0, DateTimeKind.Utc)
};
return person;
}
//[Test]
public void WriteJsonToFile()
{
//Person person = GetPerson();
//string json = JsonConvert.SerializeObject(person, Formatting.Indented);
//File.WriteAllText(@"c:\person.json", json);
Person person = GetPerson();
using (FileStream fs = File.Open(@"c:\person.json", FileMode.CreateNew))
using (StreamWriter sw = new StreamWriter(fs))
using (JsonWriter jw = new JsonTextWriter(sw))
{
jw.Formatting = Formatting.Indented;
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(jw, person);
}
}
public class LogEntry
{
public string Details { get; set; }
public DateTime LogDate { get; set; }
}
[Test]
public void WriteJsonDates()
{
LogEntry entry = new LogEntry
{
LogDate = new DateTime(2009, 2, 15, 0, 0, 0, DateTimeKind.Utc),
Details = "Application started."
};
string defaultJson = JsonConvert.SerializeObject(entry);
// {"Details":"Application started.","LogDate":"\/Date(1234656000000)\/"}
string isoJson = JsonConvert.SerializeObject(entry, new IsoDateTimeConverter());
// {"Details":"Application started.","LogDate":"2009-02-15T00:00:00.0000000Z"}
string javascriptJson = JsonConvert.SerializeObject(entry, new JavaScriptDateTimeConverter());
// {"Details":"Application started.","LogDate":new Date(1234656000000)}
Console.WriteLine(defaultJson);
Console.WriteLine(isoJson);
Console.WriteLine(javascriptJson);
}
public class SomeClass
{
public int? SomeProp { get; set; }
}
[Test]
public void Main()
{
var pokes = JsonConvert.DeserializeObject<SomeClass>(@"{""SomeProp"":1}");
}
} }
} }
@@ -27,6 +27,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using Newtonsoft.Json.Converters;
using NUnit.Framework; using NUnit.Framework;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System.IO; using System.IO;
@@ -496,5 +497,24 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.IsTrue(JToken.DeepEquals(new JArray(int.MaxValue, new JArray("Test"), int.MaxValue, new JArray(1, 2, 3)), a)); Assert.IsTrue(JToken.DeepEquals(new JArray(int.MaxValue, new JArray("Test"), int.MaxValue, new JArray(1, 2, 3)), a));
} }
[Test]
public void ToStringWithConverters()
{
JArray a =
new JArray(
new JValue(new DateTime(2009, 2, 15, 0, 0, 0, DateTimeKind.Utc))
);
string json = a.ToString(new IsoDateTimeConverter());
Assert.AreEqual(@"[
""2009-02-15T00:00:00.0000000Z""
]", json);
json = JsonConvert.SerializeObject(a, new IsoDateTimeConverter());
Assert.AreEqual(@"[""2009-02-15T00:00:00.0000000Z""]", json);
}
} }
} }
@@ -95,9 +95,9 @@ namespace Newtonsoft.Json.Converters
? Nullable.GetUnderlyingType(objectType) ? Nullable.GetUnderlyingType(objectType)
: objectType; : objectType;
if (typeof(DateTime).IsAssignableFrom(objectType)) if (typeof(DateTime).IsAssignableFrom(t))
return true; return true;
if (typeof(DateTimeOffset).IsAssignableFrom(objectType)) if (typeof(DateTimeOffset).IsAssignableFrom(t))
return true; return true;
return false; return false;
+2 -2
View File
@@ -327,14 +327,14 @@ namespace Newtonsoft.Json.Linq
/// </summary> /// </summary>
/// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param> /// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param>
/// <returns></returns> /// <returns></returns>
private string ToString(params JsonConverter[] converters) public string ToString(params JsonConverter[] converters)
{ {
using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture)) using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture))
{ {
JsonTextWriter jw = new JsonTextWriter(sw); JsonTextWriter jw = new JsonTextWriter(sw);
jw.Formatting = Formatting.Indented; jw.Formatting = Formatting.Indented;
WriteTo(jw); WriteTo(jw, converters);
return sw.ToString(); return sw.ToString();
} }
@@ -44,6 +44,12 @@ namespace Newtonsoft.Json.Serialization
{ {
JsonContainerAttribute containerAttribute; JsonContainerAttribute containerAttribute;
if (TypeContainerAttributeCache.TryGetValue(type, out containerAttribute))
return containerAttribute;
// double check locking to avoid threading issues
lock (TypeContainerAttributeCache)
{
if (TypeContainerAttributeCache.TryGetValue(type, out containerAttribute)) if (TypeContainerAttributeCache.TryGetValue(type, out containerAttribute))
return containerAttribute; return containerAttribute;
@@ -52,6 +58,7 @@ namespace Newtonsoft.Json.Serialization
return containerAttribute; return containerAttribute;
} }
}
public static MemberSerialization GetObjectMemberSerialization(Type objectType) public static MemberSerialization GetObjectMemberSerialization(Type objectType)
{ {
@@ -67,6 +74,12 @@ namespace Newtonsoft.Json.Serialization
{ {
JsonMemberMappingCollection memberMappings; JsonMemberMappingCollection memberMappings;
if (TypeMemberMappingsCache.TryGetValue(objectType, out memberMappings))
return memberMappings;
// double check locking to avoid threading issues
lock (TypeMemberMappingsCache)
{
if (TypeMemberMappingsCache.TryGetValue(objectType, out memberMappings)) if (TypeMemberMappingsCache.TryGetValue(objectType, out memberMappings))
return memberMappings; return memberMappings;
@@ -75,6 +88,7 @@ namespace Newtonsoft.Json.Serialization
return memberMappings; return memberMappings;
} }
}
public static JsonMemberMappingCollection CreateMemberMappings(Type objectType) public static JsonMemberMappingCollection CreateMemberMappings(Type objectType)
{ {
@@ -116,19 +130,33 @@ namespace Newtonsoft.Json.Serialization
return memberMappings; return memberMappings;
} }
public static JsonConverter GetConverter(ICustomAttributeProvider attributeProvider, Type targetConvertedType) private static Type GetConverterType(ICustomAttributeProvider attributeProvider)
{ {
Type converterType; Type converterType;
if (!ConverterTypeCache.TryGetValue(attributeProvider, out converterType)) if (ConverterTypeCache.TryGetValue(attributeProvider, out converterType))
return converterType;
// double check locking to avoid threading issues
lock (ConverterTypeCache)
{ {
if (ConverterTypeCache.TryGetValue(attributeProvider, out converterType))
return converterType;
JsonConverterAttribute converterAttribute = ReflectionUtils.GetAttribute<JsonConverterAttribute>(attributeProvider, true); JsonConverterAttribute converterAttribute = ReflectionUtils.GetAttribute<JsonConverterAttribute>(attributeProvider, true);
converterType = (converterAttribute != null) converterType = (converterAttribute != null)
? converterAttribute.ConverterType ? converterAttribute.ConverterType
: null; : null;
ConverterTypeCache[attributeProvider] = converterType; ConverterTypeCache[attributeProvider] = converterType;
return converterType;
} }
}
public static JsonConverter GetConverter(ICustomAttributeProvider attributeProvider, Type targetConvertedType)
{
Type converterType = GetConverterType(attributeProvider);
if (converterType != null) if (converterType != null)
{ {
@@ -143,8 +171,6 @@ namespace Newtonsoft.Json.Serialization
return null; return null;
} }
public static List<MemberInfo> GetSerializableMembers(Type objectType) public static List<MemberInfo> GetSerializableMembers(Type objectType)
{ {
return ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.Instance); return ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.Instance);