Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/Newtonsoft.Json.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamesNK <james@newtonking.com>2011-08-05 12:38:47 +0400
committerJamesNK <james@newtonking.com>2011-08-05 12:38:47 +0400
commit7d3ce867da7620f8c6c3961e1833c5c37df4730b (patch)
tree4dd322e1101c0c0969864c0dcf278987030bdb15
parentd8b32dfdc4ea875f3efa1457c55ffb05837078c1 (diff)
-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
-rw-r--r--Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs50
-rw-r--r--Src/Newtonsoft.Json.Tests/Linq/DynamicTests.cs47
-rw-r--r--Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs27
-rw-r--r--Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs22
-rw-r--r--Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs50
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs58
-rw-r--r--Src/Newtonsoft.Json/Bson/BsonWriter.cs30
-rw-r--r--Src/Newtonsoft.Json/JsonConvert.cs45
-rw-r--r--Src/Newtonsoft.Json/JsonTextWriter.cs30
-rw-r--r--Src/Newtonsoft.Json/JsonWriter.cs69
-rw-r--r--Src/Newtonsoft.Json/Linq/JToken.cs24
-rw-r--r--Src/Newtonsoft.Json/Linq/JTokenReader.cs14
-rw-r--r--Src/Newtonsoft.Json/Linq/JTokenType.cs14
-rw-r--r--Src/Newtonsoft.Json/Linq/JTokenWriter.cs30
-rw-r--r--Src/Newtonsoft.Json/Linq/JValue.cs68
15 files changed, 570 insertions, 8 deletions
diff --git a/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs b/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs
index 7548052..18e701c 100644
--- a/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs
+++ b/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs
@@ -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);
+ }
}
} \ No newline at end of file
diff --git a/Src/Newtonsoft.Json.Tests/Linq/DynamicTests.cs b/Src/Newtonsoft.Json.Tests/Linq/DynamicTests.cs
index 12c6e23..83b3f8f 100644
--- a/Src/Newtonsoft.Json.Tests/Linq/DynamicTests.cs
+++ b/Src/Newtonsoft.Json.Tests/Linq/DynamicTests.cs
@@ -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)
diff --git a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs
index 0a14d86..731e344 100644
--- a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs
+++ b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs
@@ -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());
+ }
}
} \ No newline at end of file
diff --git a/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs
index 16ebf46..94673a2 100644
--- a/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs
+++ b/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs
@@ -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]
diff --git a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs
index 73fae2b..56ea70f 100644
--- a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs
+++ b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs
@@ -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);
+ }
}
} \ No newline at end of file
diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
index 3bafc02..c446773 100644
--- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
+++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
@@ -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; }
}
} \ No newline at end of file
diff --git a/Src/Newtonsoft.Json/Bson/BsonWriter.cs b/Src/Newtonsoft.Json/Bson/BsonWriter.cs
index 4f7c4cb..3d2d6eb 100644
--- a/Src/Newtonsoft.Json/Bson/BsonWriter.cs
+++ b/Src/Newtonsoft.Json/Bson/BsonWriter.cs
@@ -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>
diff --git a/Src/Newtonsoft.Json/JsonConvert.cs b/Src/Newtonsoft.Json/JsonConvert.cs
index f3a0978..86d20d5 100644
--- a/Src/Newtonsoft.Json/JsonConvert.cs
+++ b/Src/Newtonsoft.Json/JsonConvert.cs
@@ -376,6 +376,26 @@ namespace Newtonsoft.Json
}
/// <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>
/// <param name="value">The value to convert.</param>
@@ -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;
}
diff --git a/Src/Newtonsoft.Json/JsonTextWriter.cs b/Src/Newtonsoft.Json/JsonTextWriter.cs
index 02aa35a..71cce01 100644
--- a/Src/Newtonsoft.Json/JsonTextWriter.cs
+++ b/Src/Newtonsoft.Json/JsonTextWriter.cs
@@ -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>
diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs
index 9a5b8c1..00b5c7d 100644
--- a/Src/Newtonsoft.Json/JsonWriter.cs
+++ b/Src/Newtonsoft.Json/JsonWriter.cs
@@ -815,6 +815,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>
/// <param name="value">The <see cref="Nullable{Int32}"/> value to write.</param>
@@ -1001,6 +1019,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>
/// <param name="value">The <see cref="T:Byte[]"/> value to write.</param>
@@ -1013,6 +1055,18 @@ namespace Newtonsoft.Json
}
/// <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.
/// </summary>
@@ -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()));
}
diff --git a/Src/Newtonsoft.Json/Linq/JToken.cs b/Src/Newtonsoft.Json/Linq/JToken.cs
index a024f20..600dbe3 100644
--- a/Src/Newtonsoft.Json/Linq/JToken.cs
+++ b/Src/Newtonsoft.Json/Linq/JToken.cs
@@ -1158,6 +1158,30 @@ namespace Newtonsoft.Json.Linq
}
/// <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>
/// <param name="reader">An <see cref="JsonReader"/> positioned at the token to read into this <see cref="JToken"/>.</param>
diff --git a/Src/Newtonsoft.Json/Linq/JTokenReader.cs b/Src/Newtonsoft.Json/Linq/JTokenReader.cs
index f70f6f1..b88495b 100644
--- a/Src/Newtonsoft.Json/Linq/JTokenReader.cs
+++ b/Src/Newtonsoft.Json/Linq/JTokenReader.cs
@@ -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)
diff --git a/Src/Newtonsoft.Json/Linq/JTokenType.cs b/Src/Newtonsoft.Json/Linq/JTokenType.cs
index dafb7e1..18e0500 100644
--- a/Src/Newtonsoft.Json/Linq/JTokenType.cs
+++ b/Src/Newtonsoft.Json/Linq/JTokenType.cs
@@ -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
}
} \ No newline at end of file
diff --git a/Src/Newtonsoft.Json/Linq/JTokenWriter.cs b/Src/Newtonsoft.Json/Linq/JTokenWriter.cs
index ecb208d..2aa233d 100644
--- a/Src/Newtonsoft.Json/Linq/JTokenWriter.cs
+++ b/Src/Newtonsoft.Json/Linq/JTokenWriter.cs
@@ -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
}
}
diff --git a/Src/Newtonsoft.Json/Linq/JValue.cs b/Src/Newtonsoft.Json/Linq/JValue.cs
index 927e36b..2054e84 100644
--- a/Src/Newtonsoft.Json/Linq/JValue.cs
+++ b/Src/Newtonsoft.Json/Linq/JValue.cs
@@ -119,6 +119,33 @@ namespace Newtonsoft.Json.Linq
/// 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>
+ /// <param name="value">The value.</param>
public JValue(object value)
: this(value, GetValueType(null, value))
{
@@ -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.");