-Added ToObject to JToken for deserializing LINQ to JSON objects to a .NET object
-Added support for Guid, TimeSpan and Uri to LINQ to JSON
This commit is contained in:
@@ -31,6 +31,7 @@ using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Newtonsoft.Json.Bson;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json.Tests.Serialization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
@@ -1164,5 +1165,54 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
|
||||
}
|
||||
|
||||
public void UriGuidTimeSpanTestClassEmptyTest()
|
||||
{
|
||||
UriGuidTimeSpanTestClass c1 = new UriGuidTimeSpanTestClass();
|
||||
|
||||
var memoryStream = new MemoryStream();
|
||||
var bsonWriter = new BsonWriter(memoryStream);
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
serializer.Serialize(bsonWriter, c1);
|
||||
bsonWriter.Flush();
|
||||
memoryStream.Position = 0;
|
||||
|
||||
var bsonReader = new BsonReader(memoryStream);
|
||||
|
||||
UriGuidTimeSpanTestClass c2 = serializer.Deserialize<UriGuidTimeSpanTestClass>(bsonReader);
|
||||
Assert.AreEqual(c1.Guid, c2.Guid);
|
||||
Assert.AreEqual(c1.NullableGuid, c2.NullableGuid);
|
||||
Assert.AreEqual(c1.TimeSpan, c2.TimeSpan);
|
||||
Assert.AreEqual(c1.NullableTimeSpan, c2.NullableTimeSpan);
|
||||
Assert.AreEqual(c1.Uri, c2.Uri);
|
||||
}
|
||||
|
||||
public void UriGuidTimeSpanTestClassValuesTest()
|
||||
{
|
||||
UriGuidTimeSpanTestClass c1 = new UriGuidTimeSpanTestClass
|
||||
{
|
||||
Guid = new Guid("1924129C-F7E0-40F3-9607-9939C531395A"),
|
||||
NullableGuid = new Guid("9E9F3ADF-E017-4F72-91E0-617EBE85967D"),
|
||||
TimeSpan = TimeSpan.FromDays(1),
|
||||
NullableTimeSpan = TimeSpan.FromHours(1),
|
||||
Uri = new Uri("http://testuri.com")
|
||||
};
|
||||
|
||||
var memoryStream = new MemoryStream();
|
||||
var bsonWriter = new BsonWriter(memoryStream);
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
serializer.Serialize(bsonWriter, c1);
|
||||
bsonWriter.Flush();
|
||||
memoryStream.Position = 0;
|
||||
|
||||
var bsonReader = new BsonReader(memoryStream);
|
||||
|
||||
UriGuidTimeSpanTestClass c2 = serializer.Deserialize<UriGuidTimeSpanTestClass>(bsonReader);
|
||||
Assert.AreEqual(c1.Guid, c2.Guid);
|
||||
Assert.AreEqual(c1.NullableGuid, c2.NullableGuid);
|
||||
Assert.AreEqual(c1.TimeSpan, c2.TimeSpan);
|
||||
Assert.AreEqual(c1.NullableTimeSpan, c2.NullableTimeSpan);
|
||||
Assert.AreEqual(c1.Uri, c2.Uri);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -118,7 +118,10 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
new JProperty("DateTime", new JValue(new DateTime(2000, 12, 29, 23, 51, 10, DateTimeKind.Utc))),
|
||||
new JProperty("Boolean", new JValue(true)),
|
||||
new JProperty("String", new JValue("A string lol!")),
|
||||
new JProperty("Bytes", new JValue(Encoding.UTF8.GetBytes("A string lol!")))
|
||||
new JProperty("Bytes", new JValue(Encoding.UTF8.GetBytes("A string lol!"))),
|
||||
new JProperty("Uri", new Uri("http://json.codeplex.com/")),
|
||||
new JProperty("Guid", new Guid("EA27FE1D-0D80-44F2-BF34-4654156FA7AF")),
|
||||
new JProperty("TimeSpan", TimeSpan.FromDays(1))
|
||||
);
|
||||
|
||||
dynamic d = o;
|
||||
@@ -166,6 +169,27 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.IsTrue(d.Bytes == d.Bytes);
|
||||
Assert.IsTrue(d.Bytes == Encoding.UTF8.GetBytes("A string lol!"));
|
||||
Assert.IsTrue(d.Bytes == new JValue(Encoding.UTF8.GetBytes("A string lol!")));
|
||||
|
||||
Assert.IsTrue(d.Uri == d.Uri);
|
||||
Assert.IsTrue(d.Uri == new Uri("http://json.codeplex.com/"));
|
||||
Assert.IsTrue(d.Uri > new Uri("http://abc.org/"));
|
||||
Assert.IsTrue(d.Uri >= new Uri("http://abc.com/"));
|
||||
Assert.IsTrue(d.Uri > null);
|
||||
Assert.IsTrue(d.Uri >= null);
|
||||
|
||||
Assert.IsTrue(d.Guid == d.Guid);
|
||||
Assert.IsTrue(d.Guid == new Guid("EA27FE1D-0D80-44F2-BF34-4654156FA7AF"));
|
||||
Assert.IsTrue(d.Guid > new Guid("AAAAAAAA-0D80-44F2-BF34-4654156FA7AF"));
|
||||
Assert.IsTrue(d.Guid >= new Guid("AAAAAAAA-0D80-44F2-BF34-4654156FA7AF"));
|
||||
Assert.IsTrue(d.Guid > null);
|
||||
Assert.IsTrue(d.Guid >= null);
|
||||
|
||||
Assert.IsTrue(d.TimeSpan == d.TimeSpan);
|
||||
Assert.IsTrue(d.TimeSpan == TimeSpan.FromDays(1));
|
||||
Assert.IsTrue(d.TimeSpan > TimeSpan.FromHours(1));
|
||||
Assert.IsTrue(d.TimeSpan >= TimeSpan.FromHours(1));
|
||||
Assert.IsTrue(d.TimeSpan > null);
|
||||
Assert.IsTrue(d.TimeSpan >= null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -179,7 +203,10 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
new JProperty("DateTime", new JValue(new DateTime(2000, 12, 29, 23, 51, 10, DateTimeKind.Utc))),
|
||||
new JProperty("Boolean", new JValue(true)),
|
||||
new JProperty("String", new JValue("A string lol!")),
|
||||
new JProperty("Bytes", new JValue(Encoding.UTF8.GetBytes("A string lol!")))
|
||||
new JProperty("Bytes", new JValue(Encoding.UTF8.GetBytes("A string lol!"))),
|
||||
new JProperty("Uri", new Uri("http://json.codeplex.com/")),
|
||||
new JProperty("Guid", new Guid("EA27FE1D-0D80-44F2-BF34-4654156FA7AF")),
|
||||
new JProperty("TimeSpan", TimeSpan.FromDays(1))
|
||||
);
|
||||
|
||||
dynamic d = o;
|
||||
@@ -454,7 +481,10 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
new JProperty("DateTime", new JValue(new DateTime(2000, 12, 29, 23, 51, 10, DateTimeKind.Utc))),
|
||||
new JProperty("Boolean", new JValue(true)),
|
||||
new JProperty("String", new JValue("A string lol!")),
|
||||
new JProperty("Bytes", new JValue(Encoding.UTF8.GetBytes("A string lol!")))
|
||||
new JProperty("Bytes", new JValue(Encoding.UTF8.GetBytes("A string lol!"))),
|
||||
new JProperty("Uri", new Uri("http://json.codeplex.com/")),
|
||||
new JProperty("Guid", new Guid("EA27FE1D-0D80-44F2-BF34-4654156FA7AF")),
|
||||
new JProperty("TimeSpan", TimeSpan.FromDays(1))
|
||||
);
|
||||
|
||||
dynamic d = o;
|
||||
@@ -466,6 +496,9 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual("True", d.Boolean.ToString());
|
||||
Assert.AreEqual("A string lol!", d.String.ToString());
|
||||
Assert.AreEqual("System.Byte[]", d.Bytes.ToString());
|
||||
Assert.AreEqual("http://json.codeplex.com/", d.Uri.ToString());
|
||||
Assert.AreEqual("ea27fe1d-0d80-44f2-bf34-4654156fa7af", d.Guid.ToString());
|
||||
Assert.AreEqual("1.00:00:00", d.TimeSpan.ToString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -539,6 +572,14 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
AssertValueConverted<ushort>(ushort.MinValue);
|
||||
AssertValueConverted<ushort?>(ushort.MinValue);
|
||||
AssertValueConverted<ushort?>(null);
|
||||
AssertValueConverted<TimeSpan>(TimeSpan.FromDays(1));
|
||||
AssertValueConverted<TimeSpan?>(TimeSpan.FromDays(1));
|
||||
AssertValueConverted<TimeSpan?>(null);
|
||||
AssertValueConverted<Guid>(new Guid("60304274-CD13-4060-B38C-057C8557AB54"));
|
||||
AssertValueConverted<Guid?>(new Guid("60304274-CD13-4060-B38C-057C8557AB54"));
|
||||
AssertValueConverted<Guid?>(null);
|
||||
AssertValueConverted<Uri>(new Uri("http://json.codeplex.com/"));
|
||||
AssertValueConverted<Uri>(null);
|
||||
}
|
||||
|
||||
private static void AssertValueConverted<T>(object value)
|
||||
|
||||
@@ -1591,5 +1591,32 @@ Parameter name: arrayIndex")]
|
||||
Assert.AreEqual(false, prop4.ShouldSerializeValue(o));
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void FromObjectTimeSpan()
|
||||
{
|
||||
JValue v = (JValue)JToken.FromObject(TimeSpan.FromDays(1));
|
||||
Assert.AreEqual(v.Value, TimeSpan.FromDays(1));
|
||||
|
||||
Assert.AreEqual("1.00:00:00", v.ToString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FromObjectUri()
|
||||
{
|
||||
JValue v = (JValue)JToken.FromObject(new Uri("http://www.stuff.co.nz"));
|
||||
Assert.AreEqual(v.Value, new Uri("http://www.stuff.co.nz"));
|
||||
|
||||
Assert.AreEqual("http://www.stuff.co.nz/", v.ToString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FromObjectGuid()
|
||||
{
|
||||
JValue v = (JValue)JToken.FromObject(new Guid("9065ACF3-C820-467D-BE50-8D4664BEAF35"));
|
||||
Assert.AreEqual(v.Value, new Guid("9065ACF3-C820-467D-BE50-8D4664BEAF35"));
|
||||
|
||||
Assert.AreEqual("9065acf3-c820-467d-be50-8d4664beaf35", v.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,6 +71,19 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
v.Value = StringComparison.OrdinalIgnoreCase;
|
||||
Assert.AreEqual(StringComparison.OrdinalIgnoreCase, v.Value);
|
||||
Assert.AreEqual(JTokenType.Integer, v.Type);
|
||||
|
||||
v.Value = new Uri("http://json.codeplex.com/");
|
||||
Assert.AreEqual(new Uri("http://json.codeplex.com/"), v.Value);
|
||||
Assert.AreEqual(JTokenType.Uri, v.Type);
|
||||
|
||||
v.Value = TimeSpan.FromDays(1);
|
||||
Assert.AreEqual(TimeSpan.FromDays(1), v.Value);
|
||||
Assert.AreEqual(JTokenType.TimeSpan, v.Type);
|
||||
|
||||
Guid g = Guid.NewGuid();
|
||||
v.Value = g;
|
||||
Assert.AreEqual(g, v.Value);
|
||||
Assert.AreEqual(JTokenType.Guid, v.Type);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -115,6 +128,15 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
|
||||
v = new JValue(new DateTime(2000, 12, 12, 20, 59, 59, DateTimeKind.Utc), JTokenType.Date);
|
||||
Assert.AreEqual("12/12/2000 20:59:59", v.ToString(null, CultureInfo.InvariantCulture));
|
||||
|
||||
v = new JValue(new Uri("http://json.codeplex.com/"));
|
||||
Assert.AreEqual("http://json.codeplex.com/", v.ToString(null, CultureInfo.InvariantCulture));
|
||||
|
||||
v = new JValue(TimeSpan.FromDays(1));
|
||||
Assert.AreEqual("1.00:00:00", v.ToString(null, CultureInfo.InvariantCulture));
|
||||
|
||||
v = new JValue(new Guid("B282ADE7-C520-496C-A448-4084F6803DE5"));
|
||||
Assert.AreEqual("b282ade7-c520-496c-a448-4084f6803de5", v.ToString(null, CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -33,6 +33,7 @@ using Newtonsoft.Json.Linq;
|
||||
using System.Xml;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Tests.Serialization;
|
||||
using Newtonsoft.Json.Tests.TestObjects;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Linq
|
||||
@@ -782,5 +783,54 @@ keyword such as type of business.""
|
||||
},
|
||||
o.Children()["item"].Children()["title"].Values<string>().ToArray());
|
||||
}
|
||||
|
||||
public void UriGuidTimeSpanTestClassEmptyTest()
|
||||
{
|
||||
UriGuidTimeSpanTestClass c1 = new UriGuidTimeSpanTestClass();
|
||||
JObject o = JObject.FromObject(c1);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""Guid"": ""00000000-0000-0000-0000-000000000000"",
|
||||
""NullableGuid"": null,
|
||||
""TimeSpan"": ""00:00:00"",
|
||||
""NullableTimeSpan"": null,
|
||||
""Uri"": null
|
||||
}", o.ToString());
|
||||
|
||||
UriGuidTimeSpanTestClass c2 = o.ToObject<UriGuidTimeSpanTestClass>();
|
||||
Assert.AreEqual(c1.Guid, c2.Guid);
|
||||
Assert.AreEqual(c1.NullableGuid, c2.NullableGuid);
|
||||
Assert.AreEqual(c1.TimeSpan, c2.TimeSpan);
|
||||
Assert.AreEqual(c1.NullableTimeSpan, c2.NullableTimeSpan);
|
||||
Assert.AreEqual(c1.Uri, c2.Uri);
|
||||
}
|
||||
|
||||
public void UriGuidTimeSpanTestClassValuesTest()
|
||||
{
|
||||
UriGuidTimeSpanTestClass c1 = new UriGuidTimeSpanTestClass
|
||||
{
|
||||
Guid = new Guid("1924129C-F7E0-40F3-9607-9939C531395A"),
|
||||
NullableGuid = new Guid("9E9F3ADF-E017-4F72-91E0-617EBE85967D"),
|
||||
TimeSpan = TimeSpan.FromDays(1),
|
||||
NullableTimeSpan = TimeSpan.FromHours(1),
|
||||
Uri = new Uri("http://testuri.com")
|
||||
};
|
||||
JObject o = JObject.FromObject(c1);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""Guid"": ""1924129c-f7e0-40f3-9607-9939c531395a"",
|
||||
""NullableGuid"": ""9e9f3adf-e017-4f72-91e0-617ebe85967d"",
|
||||
""TimeSpan"": ""1.00:00:00"",
|
||||
""NullableTimeSpan"": ""01:00:00"",
|
||||
""Uri"": ""http://testuri.com/""
|
||||
}", o.ToString());
|
||||
|
||||
UriGuidTimeSpanTestClass c2 = o.ToObject<UriGuidTimeSpanTestClass>();
|
||||
Assert.AreEqual(c1.Guid, c2.Guid);
|
||||
Assert.AreEqual(c1.NullableGuid, c2.NullableGuid);
|
||||
Assert.AreEqual(c1.TimeSpan, c2.TimeSpan);
|
||||
Assert.AreEqual(c1.NullableTimeSpan, c2.NullableTimeSpan);
|
||||
Assert.AreEqual(c1.Uri, c2.Uri);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4795,5 +4795,63 @@ keyword such as type of business.""
|
||||
IList list = JsonConvert.DeserializeObject<IList>("['1', 'two', 'III']");
|
||||
Assert.AreEqual(3, list.Count);
|
||||
}
|
||||
|
||||
public void UriGuidTimeSpanTestClassEmptyTest()
|
||||
{
|
||||
UriGuidTimeSpanTestClass c1 = new UriGuidTimeSpanTestClass();
|
||||
string json = JsonConvert.SerializeObject(c1, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""Guid"": ""00000000-0000-0000-0000-000000000000"",
|
||||
""NullableGuid"": null,
|
||||
""TimeSpan"": ""00:00:00"",
|
||||
""NullableTimeSpan"": null,
|
||||
""Uri"": null
|
||||
}", json);
|
||||
|
||||
UriGuidTimeSpanTestClass c2 = JsonConvert.DeserializeObject<UriGuidTimeSpanTestClass>(json);
|
||||
Assert.AreEqual(c1.Guid, c2.Guid);
|
||||
Assert.AreEqual(c1.NullableGuid, c2.NullableGuid);
|
||||
Assert.AreEqual(c1.TimeSpan, c2.TimeSpan);
|
||||
Assert.AreEqual(c1.NullableTimeSpan, c2.NullableTimeSpan);
|
||||
Assert.AreEqual(c1.Uri, c2.Uri);
|
||||
}
|
||||
|
||||
public void UriGuidTimeSpanTestClassValuesTest()
|
||||
{
|
||||
UriGuidTimeSpanTestClass c1 = new UriGuidTimeSpanTestClass
|
||||
{
|
||||
Guid = new Guid("1924129C-F7E0-40F3-9607-9939C531395A"),
|
||||
NullableGuid = new Guid("9E9F3ADF-E017-4F72-91E0-617EBE85967D"),
|
||||
TimeSpan = TimeSpan.FromDays(1),
|
||||
NullableTimeSpan = TimeSpan.FromHours(1),
|
||||
Uri = new Uri("http://testuri.com")
|
||||
};
|
||||
string json = JsonConvert.SerializeObject(c1, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""Guid"": ""1924129c-f7e0-40f3-9607-9939c531395a"",
|
||||
""NullableGuid"": ""9e9f3adf-e017-4f72-91e0-617ebe85967d"",
|
||||
""TimeSpan"": ""1.00:00:00"",
|
||||
""NullableTimeSpan"": ""01:00:00"",
|
||||
""Uri"": ""http://testuri.com/""
|
||||
}", json);
|
||||
|
||||
UriGuidTimeSpanTestClass c2 = JsonConvert.DeserializeObject<UriGuidTimeSpanTestClass>(json);
|
||||
Assert.AreEqual(c1.Guid, c2.Guid);
|
||||
Assert.AreEqual(c1.NullableGuid, c2.NullableGuid);
|
||||
Assert.AreEqual(c1.TimeSpan, c2.TimeSpan);
|
||||
Assert.AreEqual(c1.NullableTimeSpan, c2.NullableTimeSpan);
|
||||
Assert.AreEqual(c1.Uri, c2.Uri);
|
||||
}
|
||||
}
|
||||
|
||||
public class UriGuidTimeSpanTestClass
|
||||
{
|
||||
public Guid Guid { get; set; }
|
||||
public Guid? NullableGuid { get; set; }
|
||||
public TimeSpan TimeSpan { get; set; }
|
||||
public TimeSpan? NullableTimeSpan { get; set; }
|
||||
public Uri Uri { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -410,6 +410,36 @@ namespace Newtonsoft.Json.Bson
|
||||
base.WriteValue(value);
|
||||
AddValue(value, BsonType.Binary);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="Guid"/> value.
|
||||
/// </summary>
|
||||
/// <param name="value">The <see cref="Guid"/> value to write.</param>
|
||||
public override void WriteValue(Guid value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
AddToken(new BsonString(value.ToString(), true));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="TimeSpan"/> value.
|
||||
/// </summary>
|
||||
/// <param name="value">The <see cref="TimeSpan"/> value to write.</param>
|
||||
public override void WriteValue(TimeSpan value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
AddToken(new BsonString(value.ToString(), true));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="Uri"/> value.
|
||||
/// </summary>
|
||||
/// <param name="value">The <see cref="Uri"/> value to write.</param>
|
||||
public override void WriteValue(Uri value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
AddToken(new BsonString(value.ToString(), true));
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -375,6 +375,26 @@ namespace Newtonsoft.Json
|
||||
return '"' + value.ToString("D", CultureInfo.InvariantCulture) + '"';
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="TimeSpan"/> to its JSON string representation.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to convert.</param>
|
||||
/// <returns>A JSON string representation of the <see cref="TimeSpan"/>.</returns>
|
||||
public static string ToString(TimeSpan value)
|
||||
{
|
||||
return '"' + value.ToString() + '"';
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="Uri"/> to its JSON string representation.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to convert.</param>
|
||||
/// <returns>A JSON string representation of the <see cref="Uri"/>.</returns>
|
||||
public static string ToString(Uri value)
|
||||
{
|
||||
return '"' + value.ToString() + '"';
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="String"/> to its JSON string representation.
|
||||
/// </summary>
|
||||
@@ -452,6 +472,18 @@ namespace Newtonsoft.Json
|
||||
return ToString((DateTimeOffset)value);
|
||||
}
|
||||
#endif
|
||||
else if (value is Guid)
|
||||
{
|
||||
return ToString((Guid) value);
|
||||
}
|
||||
else if (value is Uri)
|
||||
{
|
||||
return ToString((Uri) value);
|
||||
}
|
||||
else if (value is TimeSpan)
|
||||
{
|
||||
return ToString((TimeSpan) value);
|
||||
}
|
||||
|
||||
throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
|
||||
}
|
||||
@@ -493,6 +525,12 @@ namespace Newtonsoft.Json
|
||||
#endif
|
||||
if (type == typeof(byte[]))
|
||||
return true;
|
||||
if (type == typeof(Uri))
|
||||
return true;
|
||||
if (type == typeof(TimeSpan))
|
||||
return true;
|
||||
if (type == typeof(Guid))
|
||||
return true;
|
||||
|
||||
return IsJsonPrimitiveTypeCode(Type.GetTypeCode(type));
|
||||
}
|
||||
@@ -511,9 +549,14 @@ namespace Newtonsoft.Json
|
||||
if (value is DateTimeOffset)
|
||||
return true;
|
||||
#endif
|
||||
|
||||
if (value is byte[])
|
||||
return true;
|
||||
if (value is Uri)
|
||||
return true;
|
||||
if (value is TimeSpan)
|
||||
return true;
|
||||
if (value is Guid)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -461,6 +461,36 @@ namespace Newtonsoft.Json
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Date);
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="Guid"/> value.
|
||||
/// </summary>
|
||||
/// <param name="value">The <see cref="Guid"/> value to write.</param>
|
||||
public override void WriteValue(Guid value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.String);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="TimeSpan"/> value.
|
||||
/// </summary>
|
||||
/// <param name="value">The <see cref="TimeSpan"/> value to write.</param>
|
||||
public override void WriteValue(TimeSpan value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Date);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="Uri"/> value.
|
||||
/// </summary>
|
||||
/// <param name="value">The <see cref="Uri"/> value to write.</param>
|
||||
public override void WriteValue(Uri value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Date);
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -814,6 +814,24 @@ namespace Newtonsoft.Json
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="Guid"/> value.
|
||||
/// </summary>
|
||||
/// <param name="value">The <see cref="Guid"/> value to write.</param>
|
||||
public virtual void WriteValue(Guid value)
|
||||
{
|
||||
AutoComplete(JsonToken.String);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="TimeSpan"/> value.
|
||||
/// </summary>
|
||||
/// <param name="value">The <see cref="TimeSpan"/> value to write.</param>
|
||||
public virtual void WriteValue(TimeSpan value)
|
||||
{
|
||||
AutoComplete(JsonToken.String);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="Nullable{Int32}"/> value.
|
||||
/// </summary>
|
||||
@@ -1000,6 +1018,30 @@ namespace Newtonsoft.Json
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="Nullable{Guid}"/> value.
|
||||
/// </summary>
|
||||
/// <param name="value">The <see cref="Nullable{Guid}"/> value to write.</param>
|
||||
public virtual void WriteValue(Guid? value)
|
||||
{
|
||||
if (value == null)
|
||||
WriteNull();
|
||||
else
|
||||
WriteValue(value.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="Nullable{TimeSpan}"/> value.
|
||||
/// </summary>
|
||||
/// <param name="value">The <see cref="Nullable{TimeSpan}"/> value to write.</param>
|
||||
public virtual void WriteValue(TimeSpan? value)
|
||||
{
|
||||
if (value == null)
|
||||
WriteNull();
|
||||
else
|
||||
WriteValue(value.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="T:Byte[]"/> value.
|
||||
/// </summary>
|
||||
@@ -1012,6 +1054,18 @@ namespace Newtonsoft.Json
|
||||
AutoComplete(JsonToken.Bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="Uri"/> value.
|
||||
/// </summary>
|
||||
/// <param name="value">The <see cref="Uri"/> value to write.</param>
|
||||
public virtual void WriteValue(Uri value)
|
||||
{
|
||||
if (value == null)
|
||||
WriteNull();
|
||||
else
|
||||
AutoComplete(JsonToken.String);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="Object"/> value.
|
||||
/// An error will raised if the value cannot be written as a single JSON token.
|
||||
@@ -1092,6 +1146,21 @@ namespace Newtonsoft.Json
|
||||
WriteValue((byte[])value);
|
||||
return;
|
||||
}
|
||||
else if (value is Guid)
|
||||
{
|
||||
WriteValue((Guid)value);
|
||||
return;
|
||||
}
|
||||
else if (value is Uri)
|
||||
{
|
||||
WriteValue((Uri)value);
|
||||
return;
|
||||
}
|
||||
else if (value is TimeSpan)
|
||||
{
|
||||
WriteValue((TimeSpan)value);
|
||||
return;
|
||||
}
|
||||
|
||||
throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
|
||||
}
|
||||
|
||||
@@ -1157,6 +1157,30 @@ namespace Newtonsoft.Json.Linq
|
||||
return FromObjectInternal(o, jsonSerializer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the specified .NET type from the <see cref="JToken"/>.
|
||||
/// </summary>
|
||||
/// <returns>The new object created from the JSON value.</returns>
|
||||
public T ToObject<T>()
|
||||
{
|
||||
return ToObject<T>(new JsonSerializer());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the specified .NET type from the <see cref="JToken"/> using the specified <see cref="JsonSerializer"/>.
|
||||
/// </summary>
|
||||
/// <param name="jsonSerializer">The <see cref="JsonSerializer"/> that will be used when creating the object.</param>
|
||||
/// <returns>The new object created from the JSON value.</returns>
|
||||
public T ToObject<T>(JsonSerializer jsonSerializer)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(jsonSerializer, "jsonSerializer");
|
||||
|
||||
using (JTokenReader jsonReader = new JTokenReader(this))
|
||||
{
|
||||
return jsonSerializer.Deserialize<T>(jsonReader);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="JToken"/> from a <see cref="JsonReader"/>.
|
||||
/// </summary>
|
||||
|
||||
@@ -242,11 +242,25 @@ namespace Newtonsoft.Json.Linq
|
||||
case JTokenType.Bytes:
|
||||
SetToken(JsonToken.Bytes, ((JValue)token).Value);
|
||||
break;
|
||||
case JTokenType.Guid:
|
||||
SetToken(JsonToken.String, SafeToString(((JValue)token).Value));
|
||||
break;
|
||||
case JTokenType.Uri:
|
||||
SetToken(JsonToken.String, SafeToString(((JValue)token).Value));
|
||||
break;
|
||||
case JTokenType.TimeSpan:
|
||||
SetToken(JsonToken.String, SafeToString(((JValue)token).Value));
|
||||
break;
|
||||
default:
|
||||
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("Type", token.Type, "Unexpected JTokenType.");
|
||||
}
|
||||
}
|
||||
|
||||
private string SafeToString(object value)
|
||||
{
|
||||
return (value != null) ? value.ToString() : null;
|
||||
}
|
||||
|
||||
bool IJsonLineInfo.HasLineInfo()
|
||||
{
|
||||
if (CurrentState == State.Start)
|
||||
|
||||
@@ -89,6 +89,18 @@ namespace Newtonsoft.Json.Linq
|
||||
/// <summary>
|
||||
/// A collection of bytes value.
|
||||
/// </summary>
|
||||
Bytes
|
||||
Bytes,
|
||||
/// <summary>
|
||||
/// A Guid value.
|
||||
/// </summary>
|
||||
Guid,
|
||||
/// <summary>
|
||||
/// A Uri value.
|
||||
/// </summary>
|
||||
Uri,
|
||||
/// <summary>
|
||||
/// A TimeSpan value.
|
||||
/// </summary>
|
||||
TimeSpan
|
||||
}
|
||||
}
|
||||
@@ -368,6 +368,36 @@ namespace Newtonsoft.Json.Linq
|
||||
base.WriteValue(value);
|
||||
AddValue(value, JsonToken.Bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="TimeSpan"/> value.
|
||||
/// </summary>
|
||||
/// <param name="value">The <see cref="TimeSpan"/> value to write.</param>
|
||||
public override void WriteValue(TimeSpan value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
AddValue(value, JsonToken.String);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="Guid"/> value.
|
||||
/// </summary>
|
||||
/// <param name="value">The <see cref="Guid"/> value to write.</param>
|
||||
public override void WriteValue(Guid value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
AddValue(value, JsonToken.String);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="Uri"/> value.
|
||||
/// </summary>
|
||||
/// <param name="value">The <see cref="Uri"/> value to write.</param>
|
||||
public override void WriteValue(Uri value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
AddValue(value, JsonToken.String);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,6 +115,33 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JValue"/> class with the given value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value.</param>
|
||||
public JValue(Guid value)
|
||||
: this(value, JTokenType.String)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JValue"/> class with the given value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value.</param>
|
||||
public JValue(Uri value)
|
||||
: this(value, JTokenType.String)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JValue"/> class with the given value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value.</param>
|
||||
public JValue(TimeSpan value)
|
||||
: this(value, JTokenType.String)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JValue"/> class with the given value.
|
||||
/// </summary>
|
||||
@@ -192,15 +219,15 @@ namespace Newtonsoft.Json.Linq
|
||||
if (!(objB is DateTimeOffset))
|
||||
throw new ArgumentException("Object must be of type DateTimeOffset.");
|
||||
|
||||
DateTimeOffset date1 = (DateTimeOffset)objA;
|
||||
DateTimeOffset date2 = (DateTimeOffset)objB;
|
||||
DateTimeOffset date1 = (DateTimeOffset) objA;
|
||||
DateTimeOffset date2 = (DateTimeOffset) objB;
|
||||
|
||||
return date1.CompareTo(date2);
|
||||
}
|
||||
#endif
|
||||
case JTokenType.Bytes:
|
||||
if (!(objB is byte[]))
|
||||
throw new ArgumentException("Object must be of type byte[].");
|
||||
throw new ArgumentException("Object must be of type byte[].");
|
||||
|
||||
byte[] bytes1 = objA as byte[];
|
||||
byte[] bytes2 = objB as byte[];
|
||||
@@ -210,6 +237,30 @@ namespace Newtonsoft.Json.Linq
|
||||
return 1;
|
||||
|
||||
return MiscellaneousUtils.ByteArrayCompare(bytes1, bytes2);
|
||||
case JTokenType.Guid:
|
||||
if (!(objB is Guid))
|
||||
throw new ArgumentException("Object must be of type Guid.");
|
||||
|
||||
Guid guid1 = (Guid) objA;
|
||||
Guid guid2 = (Guid) objB;
|
||||
|
||||
return guid1.CompareTo(guid2);
|
||||
case JTokenType.Uri:
|
||||
if (!(objB is Uri))
|
||||
throw new ArgumentException("Object must be of type Uri.");
|
||||
|
||||
Uri uri1 = (Uri)objA;
|
||||
Uri uri2 = (Uri)objB;
|
||||
|
||||
return Comparer<string>.Default.Compare(uri1.ToString(), uri2.ToString());
|
||||
case JTokenType.TimeSpan:
|
||||
if (!(objB is TimeSpan))
|
||||
throw new ArgumentException("Object must be of type TimeSpan.");
|
||||
|
||||
TimeSpan ts1 = (TimeSpan)objA;
|
||||
TimeSpan ts2 = (TimeSpan)objB;
|
||||
|
||||
return ts1.CompareTo(ts2);
|
||||
default:
|
||||
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("valueType", valueType, "Unexpected value type: {0}".FormatWith(CultureInfo.InvariantCulture, valueType));
|
||||
}
|
||||
@@ -389,6 +440,12 @@ namespace Newtonsoft.Json.Linq
|
||||
return JTokenType.Bytes;
|
||||
else if (value is bool)
|
||||
return JTokenType.Boolean;
|
||||
else if (value is Guid)
|
||||
return JTokenType.Guid;
|
||||
else if (value is Uri)
|
||||
return JTokenType.Uri;
|
||||
else if (value is TimeSpan)
|
||||
return JTokenType.TimeSpan;
|
||||
|
||||
throw new ArgumentException("Could not determine JSON object type for type {0}.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
|
||||
}
|
||||
@@ -492,6 +549,11 @@ namespace Newtonsoft.Json.Linq
|
||||
case JTokenType.Bytes:
|
||||
writer.WriteValue((byte[])_value);
|
||||
return;
|
||||
case JTokenType.Guid:
|
||||
case JTokenType.Uri:
|
||||
case JTokenType.TimeSpan:
|
||||
writer.WriteValue((_value != null) ? _value.ToString() : null);
|
||||
return;
|
||||
}
|
||||
|
||||
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", _valueType, "Unexpected token type.");
|
||||
|
||||
Reference in New Issue
Block a user