-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:
@@ -0,0 +1,180 @@
|
||||
#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.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Newtonsoft.Json.Bson;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Bson
|
||||
{
|
||||
public class BsonReaderTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void ReadSingleObject()
|
||||
{
|
||||
byte[] data = MiscellaneousUtils.HexToBytes("0F-00-00-00-10-42-6C-61-68-00-01-00-00-00-00");
|
||||
MemoryStream ms = new MemoryStream(data);
|
||||
BsonReader reader = new BsonReader(ms);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
|
||||
Assert.AreEqual("Blah", reader.Value);
|
||||
Assert.AreEqual(typeof(string), reader.ValueType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
|
||||
Assert.AreEqual(1, reader.Value);
|
||||
Assert.AreEqual(typeof(long), reader.ValueType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
|
||||
|
||||
Assert.IsFalse(reader.Read());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadObjectBsonFromSite()
|
||||
{
|
||||
byte[] data = MiscellaneousUtils.HexToBytes("20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00");
|
||||
|
||||
MemoryStream ms = new MemoryStream(data);
|
||||
BsonReader reader = new BsonReader(ms);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
|
||||
Assert.AreEqual("0", reader.Value);
|
||||
Assert.AreEqual(typeof(string), reader.ValueType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.String, reader.TokenType);
|
||||
Assert.AreEqual("a", reader.Value);
|
||||
Assert.AreEqual(typeof(string), reader.ValueType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
|
||||
Assert.AreEqual("1", reader.Value);
|
||||
Assert.AreEqual(typeof(string), reader.ValueType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.String, reader.TokenType);
|
||||
Assert.AreEqual("b", reader.Value);
|
||||
Assert.AreEqual(typeof(string), reader.ValueType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
|
||||
Assert.AreEqual("2", reader.Value);
|
||||
Assert.AreEqual(typeof(string), reader.ValueType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.String, reader.TokenType);
|
||||
Assert.AreEqual("c", reader.Value);
|
||||
Assert.AreEqual(typeof(string), reader.ValueType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
|
||||
|
||||
Assert.IsFalse(reader.Read());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadArrayBsonFromSite()
|
||||
{
|
||||
byte[] data = MiscellaneousUtils.HexToBytes("20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00");
|
||||
|
||||
MemoryStream ms = new MemoryStream(data);
|
||||
BsonReader reader = new BsonReader(ms, true);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.String, reader.TokenType);
|
||||
Assert.AreEqual("a", reader.Value);
|
||||
Assert.AreEqual(typeof(string), reader.ValueType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.String, reader.TokenType);
|
||||
Assert.AreEqual("b", reader.Value);
|
||||
Assert.AreEqual(typeof(string), reader.ValueType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.String, reader.TokenType);
|
||||
Assert.AreEqual("c", reader.Value);
|
||||
Assert.AreEqual(typeof(string), reader.ValueType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
|
||||
|
||||
Assert.IsFalse(reader.Read());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadBytes()
|
||||
{
|
||||
byte[] data = MiscellaneousUtils.HexToBytes("2B-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-05-32-00-0C-00-00-00-02-48-65-6C-6C-6F-20-77-6F-72-6C-64-21-00");
|
||||
|
||||
MemoryStream ms = new MemoryStream(data);
|
||||
BsonReader reader = new BsonReader(ms, true);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.String, reader.TokenType);
|
||||
Assert.AreEqual("a", reader.Value);
|
||||
Assert.AreEqual(typeof(string), reader.ValueType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.String, reader.TokenType);
|
||||
Assert.AreEqual("b", reader.Value);
|
||||
Assert.AreEqual(typeof(string), reader.ValueType);
|
||||
|
||||
byte[] encodedStringData = reader.ReadAsBytes();
|
||||
Assert.IsNotNull(encodedStringData);
|
||||
Assert.AreEqual(JsonToken.Bytes, reader.TokenType);
|
||||
Assert.AreEqual(encodedStringData, reader.Value);
|
||||
Assert.AreEqual(typeof(byte[]), reader.ValueType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
|
||||
|
||||
Assert.IsFalse(reader.Read());
|
||||
|
||||
string decodedString = Encoding.UTF8.GetString(encodedStringData);
|
||||
Assert.AreEqual("Hello world!", decodedString);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
#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.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Newtonsoft.Json.Bson;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Bson
|
||||
{
|
||||
public class BsonWriterTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void WriteSingleObject()
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
BsonWriter writer = new BsonWriter(ms);
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("Blah");
|
||||
writer.WriteValue(1);
|
||||
writer.WriteEndObject();
|
||||
writer.Flush();
|
||||
|
||||
string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
|
||||
Assert.AreEqual("0F-00-00-00-10-42-6C-61-68-00-01-00-00-00-00", bson);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonWriterException), ExpectedMessage = "Cannot flush BsonWriter until JSON is complete.")]
|
||||
public void FlushInsideObject()
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
BsonWriter writer = new BsonWriter(ms);
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("Blah");
|
||||
writer.WriteValue(1);
|
||||
writer.Flush();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteArrayBsonFromSite()
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
BsonWriter writer = new BsonWriter(ms);
|
||||
writer.WriteStartArray();
|
||||
writer.WriteValue("a");
|
||||
writer.WriteValue("b");
|
||||
writer.WriteValue("c");
|
||||
writer.WriteEndArray();
|
||||
|
||||
writer.Flush();
|
||||
|
||||
ms.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
string expected = "20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00";
|
||||
string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
|
||||
|
||||
Assert.AreEqual(expected, bson);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteBytes()
|
||||
{
|
||||
byte[] data = Encoding.UTF8.GetBytes("Hello world!");
|
||||
|
||||
MemoryStream ms = new MemoryStream();
|
||||
BsonWriter writer = new BsonWriter(ms);
|
||||
writer.WriteStartArray();
|
||||
writer.WriteValue("a");
|
||||
writer.WriteValue("b");
|
||||
writer.WriteValue(data);
|
||||
writer.WriteEndArray();
|
||||
|
||||
writer.Flush();
|
||||
|
||||
ms.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
string expected = "2B-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-05-32-00-0C-00-00-00-02-48-65-6C-6C-6F-20-77-6F-72-6C-64-21-00";
|
||||
string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
|
||||
|
||||
Assert.AreEqual(expected, bson);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -142,6 +142,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
[Test]
|
||||
public void SerializeDateTimeOffset()
|
||||
{
|
||||
@@ -266,6 +267,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
string json2 = JsonConvert.SerializeObject(c, new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal });
|
||||
Assert.AreNotEqual(json, json2);
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void BlogCodeSample()
|
||||
|
||||
@@ -47,6 +47,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
Assert.AreEqual("new Date(976918263055)", result);
|
||||
}
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
[Test]
|
||||
public void SerializeDateTimeOffset()
|
||||
{
|
||||
@@ -85,15 +86,6 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
Assert.AreEqual(@"{""PreField"":null,""DateTimeField"":new Date(976918263055),""DateTimeOffsetField"":new Date(976918263055),""PostField"":null}", result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeDateTime()
|
||||
{
|
||||
JavaScriptDateTimeConverter converter = new JavaScriptDateTimeConverter();
|
||||
|
||||
DateTime result = JsonConvert.DeserializeObject<DateTime>("new Date(976918263055)", converter);
|
||||
Assert.AreEqual(new DateTime(2000, 12, 15, 22, 11, 3, 55, DateTimeKind.Utc), result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(Exception), ExpectedMessage = "Cannot convert null value to System.DateTime.")]
|
||||
public void DeserializeNullToNonNullable()
|
||||
@@ -113,5 +105,15 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
DateTimeOffset result = JsonConvert.DeserializeObject<DateTimeOffset>(json, converter);
|
||||
Assert.AreEqual(new DateTimeOffset(2000, 12, 15, 22, 11, 3, 55, TimeSpan.Zero), result);
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void DeserializeDateTime()
|
||||
{
|
||||
JavaScriptDateTimeConverter converter = new JavaScriptDateTimeConverter();
|
||||
|
||||
DateTime result = JsonConvert.DeserializeObject<DateTime>("new Date(976918263055)", converter);
|
||||
Assert.AreEqual(new DateTime(2000, 12, 15, 22, 11, 3, 55, DateTimeKind.Utc), result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -523,6 +523,35 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
string json = JsonConvert.SerializeXmlNode(doc);
|
||||
Assert.AreEqual(@"{""name"":""O\""Connor""}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeComment()
|
||||
{
|
||||
string xml = @"<span class=""vevent"">
|
||||
<a class=""url"" href=""http://www.web2con.com/"">Text</a><!-- Hi! -->
|
||||
</span>";
|
||||
XmlDocument doc = new XmlDocument();
|
||||
doc.LoadXml(xml);
|
||||
|
||||
string jsonText = JsonConvert.SerializeObject(doc, Formatting.Indented, new XmlNodeConverter());
|
||||
|
||||
string expected = @"{
|
||||
""span"": {
|
||||
""@class"": ""vevent"",
|
||||
""a"": {
|
||||
""@class"": ""url"",
|
||||
""@href"": ""http://www.web2con.com/"",
|
||||
""#text"": ""Text""
|
||||
}/* Hi! */
|
||||
}
|
||||
}";
|
||||
|
||||
Assert.AreEqual(expected, jsonText);
|
||||
|
||||
XmlDocument newDoc = JsonConvert.DeserializeObject<XmlDocument>(jsonText, new XmlNodeConverter());
|
||||
Assert.AreEqual(@"<span class=""vevent""><a class=""url"" href=""http://www.web2con.com/"">Text</a><!-- Hi! --></span>", newDoc.InnerXml);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -232,8 +232,10 @@ now brown cow?", '"', true);
|
||||
value = new DateTime(JsonConvert.InitialJavaScriptDateTicks, DateTimeKind.Utc);
|
||||
Assert.AreEqual(@"""\/Date(0)\/""", JsonConvert.ToString(value));
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
value = new DateTimeOffset(JsonConvert.InitialJavaScriptDateTicks, TimeSpan.Zero);
|
||||
Assert.AreEqual(@"""\/Date(0+0000)\/""", JsonConvert.ToString(value));
|
||||
#endif
|
||||
|
||||
value = null;
|
||||
Assert.AreEqual("null", JsonConvert.ToString(value));
|
||||
@@ -271,7 +273,7 @@ now brown cow?", '"', true);
|
||||
int i = JsonConvert.DeserializeObject<int>("1");
|
||||
Assert.AreEqual(1, i);
|
||||
|
||||
#if !PocketPC
|
||||
#if !PocketPC && !NET20
|
||||
DateTimeOffset d = JsonConvert.DeserializeObject<DateTimeOffset>(@"""\/Date(-59011455539000+0000)\/""");
|
||||
Assert.AreEqual(new DateTimeOffset(new DateTime(100, 1, 1, 1, 1, 1, DateTimeKind.Utc)), d);
|
||||
#endif
|
||||
|
||||
@@ -469,5 +469,18 @@ namespace Newtonsoft.Json.Tests
|
||||
|
||||
reader.Read();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadSingleBytes()
|
||||
{
|
||||
StringReader s = new StringReader(@"""SGVsbG8gd29ybGQu""");
|
||||
JsonTextReader reader = new JsonTextReader(s);
|
||||
|
||||
byte[] data = reader.ReadAsBytes();
|
||||
Assert.IsNotNull(data);
|
||||
|
||||
string text = Encoding.UTF8.GetString(data, 0, data.Length);
|
||||
Assert.AreEqual("Hello world.", text);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -103,13 +103,21 @@ namespace Newtonsoft.Json.Tests
|
||||
jsonWriter.WriteValue((decimal?)1.1m);
|
||||
jsonWriter.WriteValue((DateTime?)null);
|
||||
jsonWriter.WriteValue((DateTime?)new DateTime(JsonConvert.InitialJavaScriptDateTicks, DateTimeKind.Utc));
|
||||
#if !PocketPC && !NET20
|
||||
jsonWriter.WriteValue((DateTimeOffset?)null);
|
||||
jsonWriter.WriteValue((DateTimeOffset?)new DateTimeOffset(JsonConvert.InitialJavaScriptDateTicks, TimeSpan.Zero));
|
||||
#endif
|
||||
jsonWriter.WriteEndArray();
|
||||
}
|
||||
|
||||
string json = sw.ToString();
|
||||
string expected = @"[null,""c"",null,true,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1.1,null,1.1,null,1.1,null,""\/Date(0)\/"",null,""\/Date(0+0000)\/""]";
|
||||
string expected;
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
expected = @"[null,""c"",null,true,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1.1,null,1.1,null,1.1,null,""\/Date(0)\/"",null,""\/Date(0+0000)\/""]";
|
||||
#else
|
||||
expected = @"[null,""c"",null,true,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1.1,null,1.1,null,1.1,null,""\/Date(0)\/""]";
|
||||
#endif
|
||||
|
||||
Assert.AreEqual(expected, json);
|
||||
}
|
||||
@@ -532,5 +540,65 @@ _____'propertyName': NaN
|
||||
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteSingleBytes()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringWriter sw = new StringWriter(sb);
|
||||
|
||||
string text = "Hello world.";
|
||||
byte[] data = Encoding.UTF8.GetBytes(text);
|
||||
|
||||
using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
Assert.AreEqual(Formatting.Indented, jsonWriter.Formatting);
|
||||
|
||||
jsonWriter.WriteValue(data);
|
||||
}
|
||||
|
||||
string expected = @"""SGVsbG8gd29ybGQu""";
|
||||
string result = sb.ToString();
|
||||
|
||||
Assert.AreEqual(expected, result);
|
||||
|
||||
byte[] d2 = Convert.FromBase64String(result.Trim('"'));
|
||||
|
||||
Assert.AreEqual(text, Encoding.UTF8.GetString(d2, 0, d2.Length));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteBytesInArray()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringWriter sw = new StringWriter(sb);
|
||||
|
||||
string text = "Hello world.";
|
||||
byte[] data = Encoding.UTF8.GetBytes(text);
|
||||
|
||||
using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
Assert.AreEqual(Formatting.Indented, jsonWriter.Formatting);
|
||||
|
||||
jsonWriter.WriteStartArray();
|
||||
jsonWriter.WriteValue(data);
|
||||
jsonWriter.WriteValue(data);
|
||||
jsonWriter.WriteValue((object)data);
|
||||
jsonWriter.WriteValue((byte[])null);
|
||||
jsonWriter.WriteEndArray();
|
||||
}
|
||||
|
||||
string expected = @"[
|
||||
""SGVsbG8gd29ybGQu"",
|
||||
""SGVsbG8gd29ybGQu"",
|
||||
""SGVsbG8gd29ybGQu"",
|
||||
null
|
||||
]";
|
||||
string result = sb.ToString();
|
||||
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,6 @@ using System.IO;
|
||||
using System.Collections;
|
||||
#if !PocketPC && !SILVERLIGHT
|
||||
using System.Web.UI;
|
||||
using System.Collections;
|
||||
using System.ComponentModel;
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Linq
|
||||
@@ -596,7 +594,7 @@ Parameter name: arrayIndex")]
|
||||
Assert.AreEqual(p4, l[1]);
|
||||
}
|
||||
|
||||
#if !PocketPC && !SILVERLIGHT
|
||||
#if !PocketPC && !SILVERLIGHT && !NET20
|
||||
[Test]
|
||||
public void PropertyChanging()
|
||||
{
|
||||
|
||||
@@ -35,6 +35,7 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
{
|
||||
public class JTokenReaderTest : TestFixtureBase
|
||||
{
|
||||
#if !PocketPC && !NET20
|
||||
[Test]
|
||||
public void YahooFinance()
|
||||
{
|
||||
@@ -109,6 +110,7 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.IsFalse(jsonReader.Read());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void ReadLineInfo()
|
||||
@@ -194,5 +196,56 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(false, lineInfo.HasLineInfo());
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadBytes()
|
||||
{
|
||||
byte[] data = Encoding.UTF8.GetBytes("Hello world!");
|
||||
|
||||
JObject o =
|
||||
new JObject(
|
||||
new JProperty("Test1", data)
|
||||
);
|
||||
|
||||
using (JTokenReader jsonReader = new JTokenReader(o))
|
||||
{
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.StartObject, jsonReader.TokenType);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
|
||||
Assert.AreEqual("Test1", jsonReader.Value);
|
||||
|
||||
byte[] readBytes = jsonReader.ReadAsBytes();
|
||||
Assert.AreEqual(data, readBytes);
|
||||
|
||||
Assert.IsTrue(jsonReader.Read());
|
||||
Assert.AreEqual(JsonToken.EndObject, jsonReader.TokenType);
|
||||
|
||||
Assert.IsFalse(jsonReader.Read());
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = "Error reading bytes. Expected bytes but got Integer.")]
|
||||
public void ReadBytesFailure()
|
||||
{
|
||||
JObject o =
|
||||
new JObject(
|
||||
new JProperty("Test1", 1)
|
||||
);
|
||||
|
||||
using (JTokenReader jsonReader = new JTokenReader(o))
|
||||
{
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.StartObject, jsonReader.TokenType);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
|
||||
Assert.AreEqual("Test1", jsonReader.Value);
|
||||
|
||||
jsonReader.ReadAsBytes();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -196,7 +196,7 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
public void Casting()
|
||||
{
|
||||
Assert.AreEqual(new DateTime(2000, 12, 20), (DateTime)new JValue(new DateTime(2000, 12, 20)));
|
||||
#if !PocketPC
|
||||
#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)));
|
||||
Assert.AreEqual(null, (DateTimeOffset?)new JValue((DateTimeOffset?)null));
|
||||
Assert.AreEqual(null, (DateTimeOffset?)(JValue)null);
|
||||
@@ -240,13 +240,16 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(5f, (float)(new JValue(5L)));
|
||||
Assert.AreEqual(5f, (float)(new JValue(5m)));
|
||||
Assert.AreEqual(5f, (float?)(new JValue(5m)));
|
||||
|
||||
byte[] data = new byte[0];
|
||||
Assert.AreEqual(data, (byte[])(new JValue(data)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImplicitCastingTo()
|
||||
{
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(new DateTime(2000, 12, 20)), (JValue)new DateTime(2000, 12, 20)));
|
||||
#if !PocketPC
|
||||
#if !PocketPC && !NET20
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(new DateTimeOffset(2000, 12, 20, 23, 50, 10, TimeSpan.Zero)), (JValue)new DateTimeOffset(2000, 12, 20, 23, 50, 10, TimeSpan.Zero)));
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue((DateTimeOffset?)null), (JValue)(DateTimeOffset?)null));
|
||||
#endif
|
||||
@@ -269,6 +272,7 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(ushort.MaxValue), (JValue)ushort.MaxValue));
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(11.1f), (JValue)11.1f));
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(float.MinValue), (JValue)float.MinValue));
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(double.MinValue), (JValue)double.MinValue));
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(uint.MaxValue), (JValue)uint.MaxValue));
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(ulong.MaxValue), (JValue)ulong.MaxValue));
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(ulong.MinValue), (JValue)ulong.MinValue));
|
||||
@@ -281,7 +285,13 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(double.MaxValue), (JValue)(double?)double.MaxValue));
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue((object)null), (JValue)(double?)null));
|
||||
|
||||
Assert.IsFalse(JToken.DeepEquals(new JValue(true), (JValue)(bool?)null));
|
||||
Assert.IsFalse(JToken.DeepEquals(new JValue((object)null), (JValue)(object)null));
|
||||
|
||||
byte[] emptyData = new byte[0];
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(emptyData), (JValue)emptyData));
|
||||
Assert.IsFalse(JToken.DeepEquals(new JValue(emptyData), (JValue)new byte[1]));
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(Encoding.UTF8.GetBytes("Hi")), (JValue)Encoding.UTF8.GetBytes("Hi")));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -39,6 +39,8 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
[Test]
|
||||
public void ValueFormatting()
|
||||
{
|
||||
byte[] data = Encoding.UTF8.GetBytes("Hello world.");
|
||||
|
||||
JToken root;
|
||||
using (JTokenWriter jsonWriter = new JTokenWriter())
|
||||
{
|
||||
@@ -55,13 +57,14 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
jsonWriter.WriteValue("This is a string.");
|
||||
jsonWriter.WriteNull();
|
||||
jsonWriter.WriteUndefined();
|
||||
jsonWriter.WriteValue(data);
|
||||
jsonWriter.WriteEndArray();
|
||||
|
||||
root = jsonWriter.Token;
|
||||
}
|
||||
|
||||
Assert.IsInstanceOfType(typeof(JArray), root);
|
||||
Assert.AreEqual(12, root.Children().Count());
|
||||
Assert.AreEqual(13, root.Children().Count());
|
||||
Assert.AreEqual("@", (string)root[0]);
|
||||
Assert.AreEqual("\r\n\t\f\b?{\\r\\n\"\'", (string)root[1]);
|
||||
Assert.AreEqual(true, (bool)root[2]);
|
||||
@@ -73,6 +76,8 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(string.Empty, (string)root[8]);
|
||||
Assert.AreEqual("This is a string.", (string)root[9]);
|
||||
Assert.AreEqual(null, ((JValue)root[10]).Value);
|
||||
Assert.AreEqual(null, ((JValue)root[11]).Value);
|
||||
Assert.AreEqual(data, (byte[])root[12]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -100,6 +105,9 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
jsonWriter.WriteValue("DVD read/writer");
|
||||
Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
|
||||
|
||||
jsonWriter.WriteValue(new byte[0]);
|
||||
Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
|
||||
|
||||
jsonWriter.WriteEnd();
|
||||
Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
|
||||
|
||||
|
||||
@@ -60,6 +60,12 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
v.Value = DBNull.Value;
|
||||
Assert.AreEqual(DBNull.Value, v.Value);
|
||||
Assert.AreEqual(JTokenType.Null, v.Type);
|
||||
|
||||
byte[] data = new byte[0];
|
||||
v.Value = data;
|
||||
|
||||
Assert.AreEqual(data, v.Value);
|
||||
Assert.AreEqual(JTokenType.Bytes, v.Type);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -545,6 +545,7 @@ keyword such as type of business.""
|
||||
Assert.AreEqual(null, c["purple"]);
|
||||
}
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
[Test]
|
||||
public void ToStringJsonConverter()
|
||||
{
|
||||
@@ -597,6 +598,7 @@ keyword such as type of business.""
|
||||
|
||||
Assert.AreEqual(4, jsonWriter.Token.Children().Count());
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void FromObject()
|
||||
@@ -683,9 +685,8 @@ keyword such as type of business.""
|
||||
o =
|
||||
new JObject(
|
||||
new JProperty("Test1", new DateTime(2000, 10, 15, 5, 5, 5, DateTimeKind.Utc)),
|
||||
new JProperty("Test2", new DateTimeOffset(2000, 10, 15, 5, 5, 5, new TimeSpan(11, 11, 0))),
|
||||
new JProperty("Test3", "Test3Value"),
|
||||
new JProperty("Test4", null)
|
||||
new JProperty("Test2", "Test2Value"),
|
||||
new JProperty("Test3", null)
|
||||
);
|
||||
|
||||
enumerable = o.AsJEnumerable();
|
||||
|
||||
@@ -83,6 +83,8 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Bson\BsonWriterTests.cs" />
|
||||
<Compile Include="Bson\BsonReaderTests.cs" />
|
||||
<Compile Include="Converters\BinaryConverterTests.cs" />
|
||||
<Compile Include="Converters\DataTableConverterTests.cs" />
|
||||
<Compile Include="Converters\DataSetConverterTests.cs" />
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -37,4 +38,5 @@ namespace Newtonsoft.Json.Tests.TestObjects
|
||||
public DateTimeOffset DateTimeOffsetField { get; set; }
|
||||
public string PostField { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -23,6 +23,7 @@
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -37,4 +38,5 @@ namespace Newtonsoft.Json.Tests.TestObjects
|
||||
public DateTimeOffset? DateTimeOffsetField { get; set; }
|
||||
public string PostField { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -31,7 +31,7 @@ namespace Newtonsoft.Json.Tests.TestObjects
|
||||
public class Store
|
||||
{
|
||||
public StoreColor Color = StoreColor.Yellow;
|
||||
public DateTimeOffset Establised = new DateTimeOffset(2010, 1, 22, 1, 1, 1, TimeSpan.Zero);
|
||||
public DateTime Establised = new DateTime(2010, 1, 22, 1, 1, 1, DateTimeKind.Utc);
|
||||
public double Width = 1.1;
|
||||
public int Employees = 999;
|
||||
public int[] RoomsPerFloor = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
@@ -61,4 +61,4 @@ namespace Newtonsoft.Json.Tests.TestObjects
|
||||
product.Add(alien);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#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.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Newtonsoft.Json.Bson
|
||||
{
|
||||
internal enum BsonBinaryType : byte
|
||||
{
|
||||
Function = 0x01,
|
||||
Data = 0x02,
|
||||
Uuid = 0x03,
|
||||
Md5 = 0x05,
|
||||
UserDefined = 0x80
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,312 @@
|
||||
#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.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Bson
|
||||
{
|
||||
public class BsonReader : JsonReader
|
||||
{
|
||||
private readonly BinaryReader _reader;
|
||||
private readonly bool _rootTypeIsArray;
|
||||
private readonly List<ContainerContext> _stack;
|
||||
|
||||
private BsonType _currentElementType;
|
||||
|
||||
|
||||
private class ContainerContext
|
||||
{
|
||||
public JTokenType Type { get; private set; }
|
||||
public int Length { get; set; }
|
||||
public int Position { get; set; }
|
||||
|
||||
public ContainerContext(JTokenType type)
|
||||
{
|
||||
Type = type;
|
||||
}
|
||||
}
|
||||
|
||||
public BsonReader(Stream stream) : this(stream, false)
|
||||
{
|
||||
}
|
||||
|
||||
public BsonReader(Stream stream, bool rootTypeIsArray)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(stream, "stream");
|
||||
_reader = new BinaryReader(stream);
|
||||
_stack = new List<ContainerContext>();
|
||||
_rootTypeIsArray = rootTypeIsArray;
|
||||
}
|
||||
|
||||
private string ReadElement()
|
||||
{
|
||||
_currentElementType = ReadType();
|
||||
string elementName = ReadString();
|
||||
return elementName;
|
||||
}
|
||||
|
||||
public override byte[] ReadAsBytes()
|
||||
{
|
||||
Read();
|
||||
if (TokenType != JsonToken.Bytes)
|
||||
throw new JsonReaderException("Error reading bytes. Expected bytes but got {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
|
||||
|
||||
return (byte[])Value;
|
||||
}
|
||||
|
||||
public override bool Read()
|
||||
{
|
||||
try
|
||||
{
|
||||
switch (CurrentState)
|
||||
{
|
||||
case State.Start:
|
||||
{
|
||||
JsonToken token = (!_rootTypeIsArray) ? JsonToken.StartObject : JsonToken.StartArray;
|
||||
JTokenType type = (!_rootTypeIsArray) ? JTokenType.Object : JTokenType.Array;
|
||||
|
||||
SetToken(token);
|
||||
ContainerContext newContext = new ContainerContext(type);
|
||||
_stack.Add(newContext);
|
||||
newContext.Length = ReadInt32();
|
||||
return true;
|
||||
}
|
||||
case State.ObjectStart:
|
||||
{
|
||||
SetToken(JsonToken.PropertyName, ReadElement());
|
||||
return true;
|
||||
}
|
||||
case State.Complete:
|
||||
break;
|
||||
case State.Property:
|
||||
{
|
||||
ReadType(_currentElementType);
|
||||
return true;
|
||||
}
|
||||
case State.Object:
|
||||
break;
|
||||
case State.ArrayStart:
|
||||
ReadElement();
|
||||
ReadType(_currentElementType);
|
||||
return true;
|
||||
case State.Array:
|
||||
break;
|
||||
case State.Closed:
|
||||
break;
|
||||
case State.PostValue:
|
||||
ContainerContext context = GetCurrentContext();
|
||||
if (context == null)
|
||||
return false;
|
||||
|
||||
int lengthMinusEnd = context.Length - 1;
|
||||
|
||||
if (context.Position < lengthMinusEnd)
|
||||
{
|
||||
if (context.Type == JTokenType.Array)
|
||||
{
|
||||
ReadElement();
|
||||
ReadType(_currentElementType);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
SetToken(JsonToken.PropertyName, ReadElement());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (context.Position == lengthMinusEnd)
|
||||
{
|
||||
if (ReadByte() != 0)
|
||||
throw new JsonReaderException("Unexpected end of object byte value.");
|
||||
|
||||
_stack.RemoveAt(_stack.Count - 1);
|
||||
if (_stack.Count != 0)
|
||||
MovePosition(context.Length);
|
||||
|
||||
JsonToken endToken = (context.Type == JTokenType.Object) ? JsonToken.EndObject : JsonToken.EndArray;
|
||||
SetToken(endToken);
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case State.ConstructorStart:
|
||||
break;
|
||||
case State.Constructor:
|
||||
break;
|
||||
case State.Error:
|
||||
break;
|
||||
case State.Finished:
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
catch (EndOfStreamException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private byte ReadByte()
|
||||
{
|
||||
MovePosition(1);
|
||||
return _reader.ReadByte();
|
||||
}
|
||||
|
||||
private void ReadType(BsonType type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case BsonType.Number:
|
||||
SetToken(JsonToken.Float, ReadDouble());
|
||||
break;
|
||||
case BsonType.String:
|
||||
SetToken(JsonToken.String, ReadLengthString());
|
||||
break;
|
||||
case BsonType.Object:
|
||||
break;
|
||||
case BsonType.Array:
|
||||
break;
|
||||
case BsonType.Binary:
|
||||
SetToken(JsonToken.Bytes, ReadBinary());
|
||||
break;
|
||||
case BsonType.Undefined:
|
||||
break;
|
||||
case BsonType.Oid:
|
||||
break;
|
||||
case BsonType.Boolean:
|
||||
break;
|
||||
case BsonType.Date:
|
||||
break;
|
||||
case BsonType.Null:
|
||||
break;
|
||||
case BsonType.Regex:
|
||||
break;
|
||||
case BsonType.Reference:
|
||||
break;
|
||||
case BsonType.Code:
|
||||
break;
|
||||
case BsonType.Symbol:
|
||||
break;
|
||||
case BsonType.CodeWScope:
|
||||
break;
|
||||
case BsonType.Integer:
|
||||
SetToken(JsonToken.Integer, (long)ReadInt32());
|
||||
break;
|
||||
case BsonType.TimeStamp:
|
||||
break;
|
||||
case BsonType.MinKey:
|
||||
break;
|
||||
case BsonType.MaxKey:
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException("type");
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] ReadBinary()
|
||||
{
|
||||
int dataLength = ReadInt32();
|
||||
|
||||
// BsonBinaryType not used
|
||||
ReadByte();
|
||||
|
||||
return ReadBytes(dataLength);
|
||||
}
|
||||
|
||||
private string ReadString()
|
||||
{
|
||||
List<byte> buff = new List<byte>();
|
||||
byte b = _reader.ReadByte();
|
||||
while (b != 0)
|
||||
{
|
||||
buff.Add(b);
|
||||
b = _reader.ReadByte();
|
||||
}
|
||||
|
||||
MovePosition(buff.Count + 1);
|
||||
string ret = Encoding.UTF8.GetString(buff.ToArray());
|
||||
return ret;
|
||||
}
|
||||
|
||||
private string ReadLengthString()
|
||||
{
|
||||
int length = ReadInt32();
|
||||
|
||||
MovePosition(length);
|
||||
byte[] buffer = _reader.ReadBytes(length - 1);
|
||||
_reader.ReadByte();
|
||||
|
||||
return Encoding.UTF8.GetString(buffer);
|
||||
}
|
||||
|
||||
private double ReadDouble()
|
||||
{
|
||||
MovePosition(8);
|
||||
return _reader.ReadDouble();
|
||||
}
|
||||
|
||||
private int ReadInt32()
|
||||
{
|
||||
MovePosition(4);
|
||||
return _reader.ReadInt32();
|
||||
}
|
||||
|
||||
private BsonType ReadType()
|
||||
{
|
||||
MovePosition(1);
|
||||
return (BsonType)_reader.ReadSByte();
|
||||
}
|
||||
|
||||
private ContainerContext GetCurrentContext()
|
||||
{
|
||||
int count = _stack.Count;
|
||||
if (count == 0)
|
||||
return null;
|
||||
|
||||
return _stack[count - 1];
|
||||
}
|
||||
|
||||
private void MovePosition(int count)
|
||||
{
|
||||
ContainerContext context = GetCurrentContext();
|
||||
context.Position += count;
|
||||
}
|
||||
|
||||
private byte[] ReadBytes(int count)
|
||||
{
|
||||
MovePosition(count);
|
||||
return _reader.ReadBytes(count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
#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.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Newtonsoft.Json.Bson
|
||||
{
|
||||
internal enum BsonType : sbyte
|
||||
{
|
||||
Number = 1,
|
||||
String = 2,
|
||||
Object = 3,
|
||||
Array = 4,
|
||||
Binary = 5,
|
||||
Undefined = 6,
|
||||
Oid = 7,
|
||||
Boolean = 8,
|
||||
Date = 9,
|
||||
Null = 10,
|
||||
Regex = 11,
|
||||
Reference = 12,
|
||||
Code = 13,
|
||||
Symbol = 14,
|
||||
CodeWScope = 15,
|
||||
Integer = 16,
|
||||
TimeStamp = 17,
|
||||
MinKey = -1,
|
||||
MaxKey = 127
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
#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;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Bson
|
||||
{
|
||||
public class BsonWriter : JTokenWriter
|
||||
{
|
||||
private readonly Stream _stream;
|
||||
private readonly BinaryWriter _writer;
|
||||
|
||||
|
||||
public BsonWriter(Stream stream)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(stream, "stream");
|
||||
_stream = stream;
|
||||
_writer = new BinaryWriter(stream);
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
if (Top != 0)
|
||||
throw new JsonWriterException("Cannot flush BsonWriter until JSON is complete.");
|
||||
|
||||
WriteToken(Token);
|
||||
_writer.Flush();
|
||||
}
|
||||
|
||||
private void WriteToken(JToken t)
|
||||
{
|
||||
switch (t.Type)
|
||||
{
|
||||
case JTokenType.Object:
|
||||
{
|
||||
int size = CalculateSize(t);
|
||||
_writer.Write(size);
|
||||
foreach (JProperty property in t)
|
||||
{
|
||||
_writer.Write((sbyte)GetTypeNumber(property.Value));
|
||||
WriteString(property.Name);
|
||||
WriteToken(property.Value);
|
||||
}
|
||||
_writer.Write((byte)0);
|
||||
}
|
||||
break;
|
||||
case JTokenType.Array:
|
||||
{
|
||||
int size = CalculateSize(t);
|
||||
_writer.Write(size);
|
||||
int index = 0;
|
||||
foreach (JToken c in t)
|
||||
{
|
||||
_writer.Write((sbyte)GetTypeNumber(c));
|
||||
WriteString(index.ToString());
|
||||
WriteToken(c);
|
||||
index++;
|
||||
}
|
||||
_writer.Write((byte)0);
|
||||
}
|
||||
break;
|
||||
case JTokenType.Integer:
|
||||
_writer.Write((int)t);
|
||||
break;
|
||||
case JTokenType.Float:
|
||||
_writer.Write((double)t);
|
||||
break;
|
||||
case JTokenType.String:
|
||||
WriteStringWithLength((string)t);
|
||||
break;
|
||||
case JTokenType.Boolean:
|
||||
_writer.Write((bool)t);
|
||||
break;
|
||||
case JTokenType.Null:
|
||||
case JTokenType.Undefined:
|
||||
break;
|
||||
case JTokenType.Date:
|
||||
DateTime dateTime = (DateTime) t;
|
||||
dateTime = dateTime.ToUniversalTime();
|
||||
long ticks = JsonConvert.ConvertDateTimeToJavaScriptTicks(dateTime);
|
||||
_writer.Write(ticks);
|
||||
break;
|
||||
case JTokenType.Bytes:
|
||||
byte[] data = (byte[])t;
|
||||
_writer.Write(data.Length);
|
||||
_writer.Write((byte)BsonBinaryType.Data);
|
||||
_writer.Write(data);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteString(string s)
|
||||
{
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(s);
|
||||
_writer.Write(bytes);
|
||||
_writer.Write((byte)0);
|
||||
}
|
||||
|
||||
private void WriteStringWithLength(string s)
|
||||
{
|
||||
_writer.Write(CalculateSizeWithLength(s, false));
|
||||
WriteString(s);
|
||||
}
|
||||
|
||||
private BsonType GetTypeNumber(JToken t)
|
||||
{
|
||||
switch (t.Type)
|
||||
{
|
||||
case JTokenType.Object:
|
||||
return BsonType.Object;
|
||||
case JTokenType.Array:
|
||||
return BsonType.Array;
|
||||
case JTokenType.Integer:
|
||||
return BsonType.Integer;
|
||||
case JTokenType.Float:
|
||||
return BsonType.Number;
|
||||
case JTokenType.String:
|
||||
return BsonType.String;
|
||||
case JTokenType.Boolean:
|
||||
return BsonType.Boolean;
|
||||
case JTokenType.Null:
|
||||
case JTokenType.Undefined:
|
||||
return BsonType.Null;
|
||||
case JTokenType.Date:
|
||||
return BsonType.Date;
|
||||
case JTokenType.Bytes:
|
||||
return BsonType.Binary;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
private int CalculateSize(string s)
|
||||
{
|
||||
int ret;
|
||||
if (s != null)
|
||||
ret = Encoding.UTF8.GetByteCount(s);
|
||||
else
|
||||
ret = 0;
|
||||
return ret + 1;
|
||||
}
|
||||
|
||||
private int CalculateSizeWithLength(string s, bool includeSize)
|
||||
{
|
||||
int baseSize = (includeSize)
|
||||
? 5 // size bytes + terminator
|
||||
: 1; // terminator
|
||||
|
||||
int ret;
|
||||
if (s != null)
|
||||
ret = Encoding.UTF8.GetByteCount(s);
|
||||
else
|
||||
ret = 0;
|
||||
return baseSize + ret;
|
||||
}
|
||||
|
||||
private int CalculateSize(JToken t)
|
||||
{
|
||||
switch (t.Type)
|
||||
{
|
||||
case JTokenType.Object:
|
||||
{
|
||||
int bases = 4;
|
||||
foreach (JProperty p in t)
|
||||
{
|
||||
bases += CalculateSize(p);
|
||||
}
|
||||
bases += 1;
|
||||
return bases;
|
||||
}
|
||||
case JTokenType.Array:
|
||||
{
|
||||
int bases = 4;
|
||||
int index = 0;
|
||||
foreach (JToken c in t)
|
||||
{
|
||||
bases += 1;
|
||||
bases += CalculateSize(index.ToString());
|
||||
bases += CalculateSize(c);
|
||||
index++;
|
||||
}
|
||||
bases += 1;
|
||||
return bases;
|
||||
}
|
||||
case JTokenType.Constructor:
|
||||
throw new JsonWriterException("Cannot write JSON constructor as BSON.");
|
||||
case JTokenType.Property:
|
||||
JProperty property = (JProperty) t;
|
||||
int ss = 1;
|
||||
ss += CalculateSize(property.Name);
|
||||
ss += CalculateSize(property.Value);
|
||||
return ss;
|
||||
case JTokenType.Comment:
|
||||
throw new JsonWriterException("Cannot write JSON comment as BSON.");
|
||||
case JTokenType.Integer:
|
||||
return 4;
|
||||
case JTokenType.Float:
|
||||
return 8;
|
||||
case JTokenType.String:
|
||||
string s = (string)t;
|
||||
|
||||
return CalculateSizeWithLength(s, true);
|
||||
case JTokenType.Boolean:
|
||||
return 1;
|
||||
case JTokenType.Null:
|
||||
case JTokenType.Undefined:
|
||||
return 0;
|
||||
case JTokenType.Date:
|
||||
return 8;
|
||||
case JTokenType.Raw:
|
||||
throw new JsonWriterException("Cannot write raw JSON as BSON.");
|
||||
case JTokenType.Bytes:
|
||||
byte[] data = (byte[]) t;
|
||||
return 4 + 1 + data.Length;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
public void Write(string str)
|
||||
{
|
||||
Byte[] buf = new byte[Encoding.UTF8.GetByteCount(str) + 1];
|
||||
buf[buf.Length - 1] = 0;
|
||||
Encoding.UTF8.GetBytes(str, 0, str.Length, buf, 0);
|
||||
_writer.Write(buf);
|
||||
}
|
||||
|
||||
public void Write(bool val)
|
||||
{
|
||||
if (val)
|
||||
_writer.Write((byte)1);
|
||||
else
|
||||
_writer.Write((byte)0);
|
||||
}
|
||||
|
||||
public void Write(byte val)
|
||||
{
|
||||
_writer.Write(val);
|
||||
}
|
||||
|
||||
public void Write(byte[] val)
|
||||
{
|
||||
_writer.Write(val);
|
||||
}
|
||||
|
||||
public void Write(int val)
|
||||
{
|
||||
_writer.Write(val);
|
||||
}
|
||||
|
||||
public void Write(long val)
|
||||
{
|
||||
_writer.Write(val);
|
||||
}
|
||||
|
||||
public void Write(double val)
|
||||
{
|
||||
_writer.Write(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,12 +65,9 @@ namespace Newtonsoft.Json.Converters
|
||||
return;
|
||||
}
|
||||
|
||||
byte[] data = value as byte[];
|
||||
byte[] data = GetByteArray(value);
|
||||
|
||||
if (data == null)
|
||||
data = GetByteArray(value);
|
||||
|
||||
writer.WriteValue(Convert.ToBase64String(data));
|
||||
writer.WriteValue(data);
|
||||
}
|
||||
|
||||
private byte[] GetByteArray(object value)
|
||||
@@ -113,19 +110,17 @@ namespace Newtonsoft.Json.Converters
|
||||
if (reader.TokenType != JsonToken.String)
|
||||
throw new Exception("Unexpected token parsing binary. Expected String, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
|
||||
// current token is already at base64 string
|
||||
// unable to call ReadAsBytes so do it the old fashion way
|
||||
string encodedData = reader.Value.ToString();
|
||||
|
||||
byte[] data = Convert.FromBase64String(encodedData);
|
||||
|
||||
if (t == typeof(byte[]))
|
||||
return data;
|
||||
|
||||
#if !SILVERLIGHT && !PocketPC && !NET20
|
||||
if (t.AssignableToTypeName(BinaryTypeName))
|
||||
return Activator.CreateInstance(t, data);
|
||||
#endif
|
||||
#if !SILVERLIGHT
|
||||
if (typeof(SqlBinary).IsAssignableFrom(t))
|
||||
if (t == typeof(SqlBinary))
|
||||
return new SqlBinary(data);
|
||||
#endif
|
||||
throw new Exception("Unexpected object type when writing binary: {0}".FormatWith(CultureInfo.InvariantCulture, objectType));
|
||||
@@ -144,15 +139,12 @@ namespace Newtonsoft.Json.Converters
|
||||
? Nullable.GetUnderlyingType(objectType)
|
||||
: objectType;
|
||||
|
||||
if (t == typeof(byte[]))
|
||||
return true;
|
||||
|
||||
#if !SILVERLIGHT && !PocketPC && !NET20
|
||||
if (t.AssignableToTypeName(BinaryTypeName))
|
||||
return true;
|
||||
#endif
|
||||
#if !SILVERLIGHT
|
||||
if (typeof(SqlBinary).IsAssignableFrom(t))
|
||||
if (t == typeof(SqlBinary))
|
||||
return true;
|
||||
#endif
|
||||
return false;
|
||||
|
||||
@@ -1,4 +1,29 @@
|
||||
#if !SILVERLIGHT
|
||||
#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
|
||||
|
||||
#if !SILVERLIGHT
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
@@ -48,7 +73,7 @@ namespace Newtonsoft.Json.Converters
|
||||
|
||||
public override bool CanConvert(Type valueType)
|
||||
{
|
||||
return typeof(DataSet).IsAssignableFrom(valueType);
|
||||
return (valueType == typeof(DataSet));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,29 @@
|
||||
#if !SILVERLIGHT
|
||||
#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
|
||||
|
||||
#if !SILVERLIGHT
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -100,7 +125,7 @@ namespace Newtonsoft.Json.Converters
|
||||
|
||||
public override bool CanConvert(Type valueType)
|
||||
{
|
||||
return typeof(DataTable).IsAssignableFrom(valueType);
|
||||
return (valueType == typeof(DataTable));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ namespace Newtonsoft.Json.Converters
|
||||
/// </returns>
|
||||
public override bool CanConvert(Type valueType)
|
||||
{
|
||||
return typeof(Color).IsAssignableFrom(valueType);
|
||||
return (valueType == typeof(Color));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -68,6 +68,7 @@ namespace Newtonsoft.Json.Converters
|
||||
|
||||
text = dateTime.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture);
|
||||
}
|
||||
#if !PocketPC && !NET20
|
||||
else if (value is DateTimeOffset)
|
||||
{
|
||||
DateTimeOffset dateTimeOffset = (DateTimeOffset)value;
|
||||
@@ -77,6 +78,7 @@ namespace Newtonsoft.Json.Converters
|
||||
|
||||
text = dateTimeOffset.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture);
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
throw new Exception("Unexpected value when converting date. Expected DateTime or DateTimeOffset, got {0}.".FormatWith(CultureInfo.InvariantCulture, ReflectionUtils.GetObjectType(value)));
|
||||
@@ -115,6 +117,7 @@ namespace Newtonsoft.Json.Converters
|
||||
if (string.IsNullOrEmpty(dateText) && nullable)
|
||||
return null;
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
if (t == typeof(DateTimeOffset))
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_dateTimeFormat))
|
||||
@@ -122,6 +125,7 @@ namespace Newtonsoft.Json.Converters
|
||||
else
|
||||
return DateTimeOffset.Parse(dateText, Culture, _dateTimeStyles);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!string.IsNullOrEmpty(_dateTimeFormat))
|
||||
return DateTime.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles);
|
||||
@@ -142,10 +146,12 @@ namespace Newtonsoft.Json.Converters
|
||||
? Nullable.GetUnderlyingType(objectType)
|
||||
: objectType;
|
||||
|
||||
if (typeof(DateTime).IsAssignableFrom(t))
|
||||
if (t == typeof(DateTime))
|
||||
return true;
|
||||
if (typeof(DateTimeOffset).IsAssignableFrom(t))
|
||||
#if !PocketPC && !NET20
|
||||
if (t == typeof(DateTimeOffset))
|
||||
return true;
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -25,12 +25,18 @@ namespace Newtonsoft.Json.Converters
|
||||
DateTime utcDateTime = dateTime.ToUniversalTime();
|
||||
ticks = JsonConvert.ConvertDateTimeToJavaScriptTicks(utcDateTime);
|
||||
}
|
||||
else
|
||||
#if !PocketPC && !NET20
|
||||
else if (value is DateTimeOffset)
|
||||
{
|
||||
DateTimeOffset dateTimeOffset = (DateTimeOffset)value;
|
||||
DateTimeOffset utcDateTimeOffset = dateTimeOffset.ToUniversalTime();
|
||||
ticks = JsonConvert.ConvertDateTimeToJavaScriptTicks(utcDateTimeOffset.UtcDateTime);
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
throw new Exception("Expected date object value.");
|
||||
}
|
||||
|
||||
writer.WriteStartConstructor("Date");
|
||||
writer.WriteValue(ticks);
|
||||
@@ -75,8 +81,10 @@ namespace Newtonsoft.Json.Converters
|
||||
if (reader.TokenType != JsonToken.EndConstructor)
|
||||
throw new Exception("Unexpected token parsing date. Expected EndConstructor, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
if (t == typeof(DateTimeOffset))
|
||||
return new DateTimeOffset(d);
|
||||
#endif
|
||||
|
||||
return d;
|
||||
}
|
||||
@@ -94,10 +102,12 @@ namespace Newtonsoft.Json.Converters
|
||||
? Nullable.GetUnderlyingType(objectType)
|
||||
: objectType;
|
||||
|
||||
if (typeof(DateTime).IsAssignableFrom(t))
|
||||
if (t == typeof(DateTime))
|
||||
return true;
|
||||
if (typeof(DateTimeOffset).IsAssignableFrom(t))
|
||||
#if !PocketPC && !NET20
|
||||
if (t == typeof(DateTimeOffset))
|
||||
return true;
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -522,6 +522,9 @@ namespace Newtonsoft.Json.Converters
|
||||
DeserializeValue(reader, document, manager, "-" + constructorName, currentNode);
|
||||
}
|
||||
break;
|
||||
case JsonToken.Comment:
|
||||
currentNode.AppendChild(document.CreateComment((string)reader.Value));
|
||||
break;
|
||||
case JsonToken.EndObject:
|
||||
case JsonToken.EndArray:
|
||||
return;
|
||||
|
||||
@@ -1,544 +0,0 @@
|
||||
#if PocketPC
|
||||
#pragma warning disable 1591
|
||||
|
||||
// This class is... borrowed from .NET and Microsoft for a short time.
|
||||
// Hopefully Microsoft will add DateTimeOffset to the compact framework
|
||||
// or I will rewrite a striped down version of this file myself
|
||||
|
||||
using System.Globalization;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Security.Permissions;
|
||||
using System;
|
||||
|
||||
namespace System
|
||||
{
|
||||
public struct DateTimeOffset : IComparable, IFormattable, IComparable<DateTimeOffset>, IEquatable<DateTimeOffset>
|
||||
{
|
||||
private DateTime _dateTime;
|
||||
private short _offsetMinutes;
|
||||
internal const long MaxOffset = 0x7558bdb000L;
|
||||
public static readonly DateTimeOffset MaxValue;
|
||||
internal const long MinOffset = -504000000000L;
|
||||
public static readonly DateTimeOffset MinValue;
|
||||
|
||||
static DateTimeOffset()
|
||||
{
|
||||
MinValue = new DateTimeOffset(0L, TimeSpan.Zero);
|
||||
MaxValue = new DateTimeOffset(0x2bca2875f4373fffL, TimeSpan.Zero);
|
||||
}
|
||||
|
||||
public DateTimeOffset(System.DateTime dateTime)
|
||||
{
|
||||
TimeSpan utcOffset;
|
||||
if (dateTime.Kind != DateTimeKind.Utc)
|
||||
{
|
||||
utcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime);
|
||||
}
|
||||
else
|
||||
{
|
||||
utcOffset = new TimeSpan(0L);
|
||||
}
|
||||
this._offsetMinutes = ValidateOffset(utcOffset);
|
||||
this._dateTime = ValidateDate(dateTime, utcOffset);
|
||||
}
|
||||
|
||||
public DateTimeOffset(System.DateTime dateTime, TimeSpan offset)
|
||||
{
|
||||
if (dateTime.Kind == DateTimeKind.Local)
|
||||
{
|
||||
if (offset != TimeZone.CurrentTimeZone.GetUtcOffset(dateTime))
|
||||
{
|
||||
throw new ArgumentException("The UTC Offset of the local dateTime parameter does not match the offset argument.", "offset");
|
||||
}
|
||||
}
|
||||
else if ((dateTime.Kind == DateTimeKind.Utc) && (offset != TimeSpan.Zero))
|
||||
{
|
||||
throw new ArgumentException("The UTC Offset for Utc DateTime instances must be 0.", "offset");
|
||||
}
|
||||
this._offsetMinutes = ValidateOffset(offset);
|
||||
this._dateTime = ValidateDate(dateTime, offset);
|
||||
}
|
||||
|
||||
public DateTimeOffset(long ticks, TimeSpan offset)
|
||||
{
|
||||
this._offsetMinutes = ValidateOffset(offset);
|
||||
System.DateTime dateTime = new System.DateTime(ticks);
|
||||
this._dateTime = ValidateDate(dateTime, offset);
|
||||
}
|
||||
|
||||
public DateTimeOffset(int year, int month, int day, int hour, int minute, int second, TimeSpan offset)
|
||||
{
|
||||
this._offsetMinutes = ValidateOffset(offset);
|
||||
this._dateTime = ValidateDate(new System.DateTime(year, month, day, hour, minute, second), offset);
|
||||
}
|
||||
|
||||
public DateTimeOffset(int year, int month, int day, int hour, int minute, int second, int millisecond, TimeSpan offset)
|
||||
{
|
||||
this._offsetMinutes = ValidateOffset(offset);
|
||||
this._dateTime = ValidateDate(new System.DateTime(year, month, day, hour, minute, second, millisecond), offset);
|
||||
}
|
||||
|
||||
public DateTimeOffset(int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar, TimeSpan offset)
|
||||
{
|
||||
this._offsetMinutes = ValidateOffset(offset);
|
||||
this._dateTime = ValidateDate(new System.DateTime(year, month, day, hour, minute, second, millisecond, calendar), offset);
|
||||
}
|
||||
|
||||
public DateTimeOffset Add(TimeSpan timeSpan)
|
||||
{
|
||||
return new DateTimeOffset(this.ClockDateTime.Add(timeSpan), this.Offset);
|
||||
}
|
||||
|
||||
public DateTimeOffset AddDays(double days)
|
||||
{
|
||||
return new DateTimeOffset(this.ClockDateTime.AddDays(days), this.Offset);
|
||||
}
|
||||
|
||||
public DateTimeOffset AddHours(double hours)
|
||||
{
|
||||
return new DateTimeOffset(this.ClockDateTime.AddHours(hours), this.Offset);
|
||||
}
|
||||
|
||||
public DateTimeOffset AddMilliseconds(double milliseconds)
|
||||
{
|
||||
return new DateTimeOffset(this.ClockDateTime.AddMilliseconds(milliseconds), this.Offset);
|
||||
}
|
||||
|
||||
public DateTimeOffset AddMinutes(double minutes)
|
||||
{
|
||||
return new DateTimeOffset(this.ClockDateTime.AddMinutes(minutes), this.Offset);
|
||||
}
|
||||
|
||||
public DateTimeOffset AddMonths(int months)
|
||||
{
|
||||
return new DateTimeOffset(this.ClockDateTime.AddMonths(months), this.Offset);
|
||||
}
|
||||
|
||||
public DateTimeOffset AddSeconds(double seconds)
|
||||
{
|
||||
return new DateTimeOffset(this.ClockDateTime.AddSeconds(seconds), this.Offset);
|
||||
}
|
||||
|
||||
public DateTimeOffset AddTicks(long ticks)
|
||||
{
|
||||
return new DateTimeOffset(this.ClockDateTime.AddTicks(ticks), this.Offset);
|
||||
}
|
||||
|
||||
public DateTimeOffset AddYears(int years)
|
||||
{
|
||||
return new DateTimeOffset(this.ClockDateTime.AddYears(years), this.Offset);
|
||||
}
|
||||
|
||||
public static int Compare(DateTimeOffset first, DateTimeOffset second)
|
||||
{
|
||||
return System.DateTime.Compare(first.UtcDateTime, second.UtcDateTime);
|
||||
}
|
||||
|
||||
public int CompareTo(DateTimeOffset other)
|
||||
{
|
||||
System.DateTime utcDateTime = other.UtcDateTime;
|
||||
System.DateTime time2 = this.UtcDateTime;
|
||||
if (time2 > utcDateTime)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (time2 < utcDateTime)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public bool Equals(DateTimeOffset other)
|
||||
{
|
||||
return this.UtcDateTime.Equals(other.UtcDateTime);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is DateTimeOffset)
|
||||
{
|
||||
DateTimeOffset offset = (DateTimeOffset)obj;
|
||||
return this.UtcDateTime.Equals(offset.UtcDateTime);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool Equals(DateTimeOffset first, DateTimeOffset second)
|
||||
{
|
||||
return System.DateTime.Equals(first.UtcDateTime, second.UtcDateTime);
|
||||
}
|
||||
|
||||
public bool EqualsExact(DateTimeOffset other)
|
||||
{
|
||||
return (((this.ClockDateTime == other.ClockDateTime) && (this.Offset == other.Offset)) && (this.ClockDateTime.Kind == other.ClockDateTime.Kind));
|
||||
}
|
||||
|
||||
public static DateTimeOffset FromFileTime(long fileTime)
|
||||
{
|
||||
return new DateTimeOffset(System.DateTime.FromFileTime(fileTime));
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return this.UtcDateTime.GetHashCode();
|
||||
}
|
||||
|
||||
public static DateTimeOffset operator +(DateTimeOffset dateTimeTz, TimeSpan timeSpan)
|
||||
{
|
||||
return new DateTimeOffset(dateTimeTz.ClockDateTime + timeSpan, dateTimeTz.Offset);
|
||||
}
|
||||
|
||||
public static bool operator ==(DateTimeOffset left, DateTimeOffset right)
|
||||
{
|
||||
return (left.UtcDateTime == right.UtcDateTime);
|
||||
}
|
||||
|
||||
public static bool operator >(DateTimeOffset left, DateTimeOffset right)
|
||||
{
|
||||
return (left.UtcDateTime > right.UtcDateTime);
|
||||
}
|
||||
|
||||
public static bool operator >=(DateTimeOffset left, DateTimeOffset right)
|
||||
{
|
||||
return (left.UtcDateTime >= right.UtcDateTime);
|
||||
}
|
||||
|
||||
public static implicit operator DateTimeOffset(System.DateTime dateTime)
|
||||
{
|
||||
return new DateTimeOffset(dateTime);
|
||||
}
|
||||
|
||||
public static bool operator !=(DateTimeOffset left, DateTimeOffset right)
|
||||
{
|
||||
return (left.UtcDateTime != right.UtcDateTime);
|
||||
}
|
||||
|
||||
public static bool operator <(DateTimeOffset left, DateTimeOffset right)
|
||||
{
|
||||
return (left.UtcDateTime < right.UtcDateTime);
|
||||
}
|
||||
|
||||
public static bool operator <=(DateTimeOffset left, DateTimeOffset right)
|
||||
{
|
||||
return (left.UtcDateTime <= right.UtcDateTime);
|
||||
}
|
||||
|
||||
public static TimeSpan operator -(DateTimeOffset left, DateTimeOffset right)
|
||||
{
|
||||
return (TimeSpan)(left.UtcDateTime - right.UtcDateTime);
|
||||
}
|
||||
|
||||
public static DateTimeOffset operator -(DateTimeOffset dateTimeTz, TimeSpan timeSpan)
|
||||
{
|
||||
return new DateTimeOffset(dateTimeTz.ClockDateTime - timeSpan, dateTimeTz.Offset);
|
||||
}
|
||||
|
||||
public static DateTimeOffset Parse(string input)
|
||||
{
|
||||
return new DateTimeOffset(DateTime.Parse(input, DateTimeFormatInfo.CurrentInfo, DateTimeStyles.None));
|
||||
}
|
||||
|
||||
public static DateTimeOffset Parse(string input, IFormatProvider formatProvider)
|
||||
{
|
||||
return Parse(input, formatProvider, DateTimeStyles.None);
|
||||
}
|
||||
|
||||
public static DateTimeOffset Parse(string input, IFormatProvider formatProvider, DateTimeStyles styles)
|
||||
{
|
||||
styles = ValidateStyles(styles, "styles");
|
||||
return new DateTimeOffset(DateTime.Parse(input, DateTimeFormatInfo.GetInstance(formatProvider), styles));
|
||||
}
|
||||
|
||||
public static DateTimeOffset ParseExact(string input, string format, IFormatProvider formatProvider)
|
||||
{
|
||||
return ParseExact(input, format, formatProvider, DateTimeStyles.None);
|
||||
}
|
||||
|
||||
public static DateTimeOffset ParseExact(string input, string format, IFormatProvider formatProvider, DateTimeStyles styles)
|
||||
{
|
||||
styles = ValidateStyles(styles, "styles");
|
||||
return new DateTimeOffset(DateTime.ParseExact(input, format, DateTimeFormatInfo.GetInstance(formatProvider), styles));
|
||||
}
|
||||
|
||||
public static DateTimeOffset ParseExact(string input, string[] formats, IFormatProvider formatProvider, DateTimeStyles styles)
|
||||
{
|
||||
styles = ValidateStyles(styles, "styles");
|
||||
return new DateTimeOffset(DateTime.ParseExact(input, formats, DateTimeFormatInfo.GetInstance(formatProvider), styles));
|
||||
}
|
||||
|
||||
public TimeSpan Subtract(DateTimeOffset value)
|
||||
{
|
||||
return this.UtcDateTime.Subtract(value.UtcDateTime);
|
||||
}
|
||||
|
||||
public DateTimeOffset Subtract(TimeSpan value)
|
||||
{
|
||||
return new DateTimeOffset(this.ClockDateTime.Subtract(value), this.Offset);
|
||||
}
|
||||
|
||||
int IComparable.CompareTo(object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (!(obj is DateTimeOffset))
|
||||
{
|
||||
throw new ArgumentException("Object must be of type DateTimeOffset.");
|
||||
}
|
||||
DateTimeOffset offset = (DateTimeOffset)obj;
|
||||
System.DateTime utcDateTime = offset.UtcDateTime;
|
||||
System.DateTime time2 = this.UtcDateTime;
|
||||
if (time2 > utcDateTime)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (time2 < utcDateTime)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public long ToFileTime()
|
||||
{
|
||||
return this.UtcDateTime.ToFileTime();
|
||||
}
|
||||
|
||||
public DateTimeOffset ToLocalTime()
|
||||
{
|
||||
return new DateTimeOffset(this.UtcDateTime.ToLocalTime());
|
||||
}
|
||||
|
||||
public DateTimeOffset ToOffset(TimeSpan offset)
|
||||
{
|
||||
System.DateTime time = this._dateTime + offset;
|
||||
return new DateTimeOffset(time.Ticks, offset);
|
||||
}
|
||||
|
||||
public string ToString(string format, IFormatProvider formatProvider)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(format))
|
||||
{
|
||||
format = format.Replace("K", "zzz");
|
||||
format = format.Replace("zzz", Offset.Hours.ToString("+00;-00", CultureInfo.InvariantCulture) + ":" + Offset.Minutes.ToString("00;00", CultureInfo.InvariantCulture));
|
||||
format = format.Replace("zz", Offset.Hours.ToString("+00;-00", CultureInfo.InvariantCulture));
|
||||
format = format.Replace("z", Offset.Hours.ToString("+0;-0", CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
return ClockDateTime.ToString(format, formatProvider);
|
||||
}
|
||||
|
||||
public DateTimeOffset ToUniversalTime()
|
||||
{
|
||||
return new DateTimeOffset(this.UtcDateTime);
|
||||
}
|
||||
|
||||
private static System.DateTime ValidateDate(System.DateTime dateTime, TimeSpan offset)
|
||||
{
|
||||
long ticks = dateTime.Ticks - offset.Ticks;
|
||||
if ((ticks < 0L) || (ticks > 0x2bca2875f4373fffL))
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("offset", "The UTC time represented when the offset is applied must be between year 0 and 10,000.");
|
||||
}
|
||||
return new System.DateTime(ticks, DateTimeKind.Unspecified);
|
||||
}
|
||||
|
||||
private static short ValidateOffset(TimeSpan offset)
|
||||
{
|
||||
long ticks = offset.Ticks;
|
||||
if ((ticks % 0x23c34600L) != 0L)
|
||||
{
|
||||
throw new ArgumentException("Offset must be specified in whole minutes.", "offset");
|
||||
}
|
||||
if ((ticks < -504000000000L) || (ticks > 0x7558bdb000L))
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("offset", "Offset must be within plus or minus 14 hours.");
|
||||
}
|
||||
return (short)(offset.Ticks / 0x23c34600L);
|
||||
}
|
||||
|
||||
private static DateTimeStyles ValidateStyles(DateTimeStyles style, string parameterName)
|
||||
{
|
||||
if ((style & ~(DateTimeStyles.RoundtripKind | DateTimeStyles.AssumeUniversal | DateTimeStyles.AssumeLocal | DateTimeStyles.AdjustToUniversal | DateTimeStyles.NoCurrentDateDefault | DateTimeStyles.AllowWhiteSpaces)) != DateTimeStyles.None)
|
||||
{
|
||||
throw new ArgumentException("An undefined DateTimeStyles value is being used.", parameterName);
|
||||
}
|
||||
if (((style & DateTimeStyles.AssumeLocal) != DateTimeStyles.None) && ((style & DateTimeStyles.AssumeUniversal) != DateTimeStyles.None))
|
||||
{
|
||||
throw new ArgumentException("The DateTimeStyles values AssumeLocal and AssumeUniversal cannot be used together.", parameterName);
|
||||
}
|
||||
if ((style & DateTimeStyles.NoCurrentDateDefault) != DateTimeStyles.None)
|
||||
{
|
||||
throw new ArgumentException("The DateTimeStyles value 'NoCurrentDateDefault' is not allowed when parsing DateTimeOffset.", parameterName);
|
||||
}
|
||||
style &= ~DateTimeStyles.RoundtripKind;
|
||||
style &= ~DateTimeStyles.AssumeLocal;
|
||||
return style;
|
||||
}
|
||||
|
||||
private System.DateTime ClockDateTime
|
||||
{
|
||||
get
|
||||
{
|
||||
System.DateTime time = this._dateTime + this.Offset;
|
||||
return new System.DateTime(time.Ticks, DateTimeKind.Unspecified);
|
||||
}
|
||||
}
|
||||
|
||||
public System.DateTime Date
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.ClockDateTime.Date;
|
||||
}
|
||||
}
|
||||
|
||||
public System.DateTime DateTime
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.ClockDateTime;
|
||||
}
|
||||
}
|
||||
|
||||
public int Day
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.ClockDateTime.Day;
|
||||
}
|
||||
}
|
||||
|
||||
public System.DayOfWeek DayOfWeek
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.ClockDateTime.DayOfWeek;
|
||||
}
|
||||
}
|
||||
|
||||
public int DayOfYear
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.ClockDateTime.DayOfYear;
|
||||
}
|
||||
}
|
||||
|
||||
public int Hour
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.ClockDateTime.Hour;
|
||||
}
|
||||
}
|
||||
|
||||
public System.DateTime LocalDateTime
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.UtcDateTime.ToLocalTime();
|
||||
}
|
||||
}
|
||||
|
||||
public int Millisecond
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.ClockDateTime.Millisecond;
|
||||
}
|
||||
}
|
||||
|
||||
public int Minute
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.ClockDateTime.Minute;
|
||||
}
|
||||
}
|
||||
|
||||
public int Month
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.ClockDateTime.Month;
|
||||
}
|
||||
}
|
||||
|
||||
public static DateTimeOffset Now
|
||||
{
|
||||
get
|
||||
{
|
||||
return new DateTimeOffset(System.DateTime.Now);
|
||||
}
|
||||
}
|
||||
|
||||
public TimeSpan Offset
|
||||
{
|
||||
get
|
||||
{
|
||||
return new TimeSpan(0, this._offsetMinutes, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public int Second
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.ClockDateTime.Second;
|
||||
}
|
||||
}
|
||||
|
||||
public long Ticks
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.ClockDateTime.Ticks;
|
||||
}
|
||||
}
|
||||
|
||||
public TimeSpan TimeOfDay
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.ClockDateTime.TimeOfDay;
|
||||
}
|
||||
}
|
||||
|
||||
public System.DateTime UtcDateTime
|
||||
{
|
||||
get
|
||||
{
|
||||
return System.DateTime.SpecifyKind(this._dateTime, DateTimeKind.Utc);
|
||||
}
|
||||
}
|
||||
|
||||
public static DateTimeOffset UtcNow
|
||||
{
|
||||
get
|
||||
{
|
||||
return new DateTimeOffset(System.DateTime.UtcNow);
|
||||
}
|
||||
}
|
||||
|
||||
public long UtcTicks
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.UtcDateTime.Ticks;
|
||||
}
|
||||
}
|
||||
|
||||
public int Year
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.ClockDateTime.Year;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -106,6 +106,7 @@ namespace Newtonsoft.Json
|
||||
return ToStringInternal(value, utcOffset, value.Kind);
|
||||
}
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
/// <summary>
|
||||
/// Converts the <see cref="DateTimeOffset"/> to its JSON string representation.
|
||||
/// </summary>
|
||||
@@ -115,6 +116,7 @@ namespace Newtonsoft.Json
|
||||
{
|
||||
return ToStringInternal(value.UtcDateTime, value.Offset, DateTimeKind.Local);
|
||||
}
|
||||
#endif
|
||||
|
||||
internal static string ToStringInternal(DateTime value, TimeSpan offset, DateTimeKind kind)
|
||||
{
|
||||
@@ -387,10 +389,12 @@ namespace Newtonsoft.Json
|
||||
return Null;
|
||||
}
|
||||
}
|
||||
#if !PocketPC && !NET20
|
||||
else if (value is DateTimeOffset)
|
||||
{
|
||||
return ToString((DateTimeOffset)value);
|
||||
}
|
||||
#endif
|
||||
|
||||
throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
|
||||
}
|
||||
@@ -427,9 +431,14 @@ namespace Newtonsoft.Json
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
if (value is DateTimeOffset)
|
||||
return true;
|
||||
#endif
|
||||
|
||||
if (value is byte[])
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -169,7 +169,7 @@ namespace Newtonsoft.Json
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JsonReader"/> class with the specified <see cref="TextReader"/>.
|
||||
/// </summary>
|
||||
public JsonReader()
|
||||
protected JsonReader()
|
||||
{
|
||||
//_testBuffer = new StringBuilder();
|
||||
_currentState = State.Start;
|
||||
@@ -204,6 +204,8 @@ namespace Newtonsoft.Json
|
||||
/// <returns>true if the next token was read successfully; false if there are no more tokens to read.</returns>
|
||||
public abstract bool Read();
|
||||
|
||||
public abstract byte[] ReadAsBytes();
|
||||
|
||||
/// <summary>
|
||||
/// Skips the children of the current token.
|
||||
/// </summary>
|
||||
@@ -275,6 +277,7 @@ namespace Newtonsoft.Json
|
||||
case JsonToken.Date:
|
||||
case JsonToken.String:
|
||||
case JsonToken.Raw:
|
||||
case JsonToken.Bytes:
|
||||
_currentState = State.PostValue;
|
||||
break;
|
||||
}
|
||||
@@ -351,6 +354,7 @@ namespace Newtonsoft.Json
|
||||
case JsonToken.EndConstructor:
|
||||
case JsonToken.Date:
|
||||
case JsonToken.Raw:
|
||||
case JsonToken.Bytes:
|
||||
return false;
|
||||
default:
|
||||
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("token", token, "Unexpected JsonToken value.");
|
||||
|
||||
@@ -423,7 +423,7 @@ namespace Newtonsoft.Json
|
||||
if (objectType == null)
|
||||
throw new ArgumentNullException("objectType");
|
||||
|
||||
converter = JsonTypeReflector.GetConverter(objectType, objectType);
|
||||
converter = JsonTypeReflector.GetJsonConverter(objectType, objectType);
|
||||
return (converter != null);
|
||||
}
|
||||
|
||||
|
||||
@@ -60,12 +60,30 @@ namespace Newtonsoft.Json
|
||||
}
|
||||
|
||||
private void ParseString(char quote)
|
||||
{
|
||||
ReadStringIntoBuffer(quote, null);
|
||||
|
||||
string text = _buffer.ToString();
|
||||
_buffer.Position = 0;
|
||||
|
||||
if (text.StartsWith("/Date(", StringComparison.Ordinal) && text.EndsWith(")/", StringComparison.Ordinal))
|
||||
{
|
||||
ParseDate(text);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetToken(JsonToken.String, text);
|
||||
QuoteChar = quote;
|
||||
}
|
||||
}
|
||||
|
||||
private void ReadStringIntoBuffer(char quote, int? untilPosition)
|
||||
{
|
||||
bool stringTerminated = false;
|
||||
bool hexNumber = false;
|
||||
int hexCount = 0;
|
||||
|
||||
while (!stringTerminated && MoveNext())
|
||||
while (!stringTerminated && (untilPosition == null || _buffer.Position < untilPosition) && MoveNext())
|
||||
{
|
||||
if (hexNumber)
|
||||
hexCount++;
|
||||
@@ -136,18 +154,6 @@ namespace Newtonsoft.Json
|
||||
throw CreateJsonReaderException("Unterminated string. Expected delimiter: {0}. Line {1}, position {2}.", quote, _currentLineNumber, _currentLinePosition);
|
||||
|
||||
ClearCurrentChar();
|
||||
string text = _buffer.ToString();
|
||||
_buffer.Position = 0;
|
||||
|
||||
if (text.StartsWith("/Date(", StringComparison.Ordinal) && text.EndsWith(")/", StringComparison.Ordinal))
|
||||
{
|
||||
ParseDate(text);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetToken(JsonToken.String, text);
|
||||
QuoteChar = quote;
|
||||
}
|
||||
}
|
||||
|
||||
private JsonReaderException CreateJsonReaderException(string format, params object[] args)
|
||||
@@ -321,6 +327,61 @@ namespace Newtonsoft.Json
|
||||
}
|
||||
}
|
||||
|
||||
public override byte[] ReadAsBytes()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (_currentChar == '\0')
|
||||
{
|
||||
if (!MoveNext())
|
||||
throw CreateJsonReaderException("Unexpected end: Line {0}, position {1}.", _currentLineNumber, _currentLinePosition);
|
||||
}
|
||||
|
||||
switch (CurrentState)
|
||||
{
|
||||
case State.Start:
|
||||
case State.Property:
|
||||
case State.Array:
|
||||
case State.ArrayStart:
|
||||
case State.Constructor:
|
||||
case State.ConstructorStart:
|
||||
do
|
||||
{
|
||||
switch (_currentChar)
|
||||
{
|
||||
case '"':
|
||||
case '\'':
|
||||
ReadStringIntoBuffer(_currentChar, null);
|
||||
|
||||
byte[] data = Convert.FromBase64CharArray(_buffer.GetInternalBuffer(), 0, _buffer.Position);
|
||||
_buffer.Position = 0;
|
||||
|
||||
SetToken(JsonToken.Bytes, data);
|
||||
|
||||
return data;
|
||||
case 'n':
|
||||
ParseNull();
|
||||
return null;
|
||||
default:
|
||||
if (char.IsWhiteSpace(_currentChar))
|
||||
{
|
||||
// eat
|
||||
}
|
||||
else
|
||||
{
|
||||
throw CreateJsonReaderException("Unexpected character encountered while parsing value: {0}. Line {1}, position {2}.", _currentChar, _currentLineNumber, _currentLinePosition);
|
||||
}
|
||||
break;
|
||||
}
|
||||
} while (MoveNext());
|
||||
ParseValue();
|
||||
break;
|
||||
default:
|
||||
throw CreateJsonReaderException("Unexpected state: {0}. Line {1}, position {2}.", CurrentState, _currentLineNumber, _currentLinePosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool ParsePostValue()
|
||||
{
|
||||
do
|
||||
|
||||
@@ -37,12 +37,24 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
public class JsonTextWriter : JsonWriter
|
||||
{
|
||||
private TextWriter _writer;
|
||||
private readonly TextWriter _writer;
|
||||
private Base64Encoder _base64Encoder;
|
||||
private char _indentChar;
|
||||
private int _indentation;
|
||||
private char _quoteChar;
|
||||
private bool _quoteName;
|
||||
|
||||
private Base64Encoder Base64Encoder
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_base64Encoder == null)
|
||||
_base64Encoder = new Base64Encoder(_writer);
|
||||
|
||||
return _base64Encoder;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how many IndentChars to write for each level in the hierarchy when <paramref name="Formatting"/> is set to <c>Formatting.Indented</c>.
|
||||
/// </summary>
|
||||
@@ -419,6 +431,20 @@ namespace Newtonsoft.Json
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Date);
|
||||
}
|
||||
|
||||
public override void WriteValue(byte[] value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
|
||||
if (value != null)
|
||||
{
|
||||
_writer.Write(_quoteChar);
|
||||
Base64Encoder.Encode(value, 0, value.Length);
|
||||
Base64Encoder.Flush();
|
||||
_writer.Write(_quoteChar);
|
||||
}
|
||||
}
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
/// <summary>
|
||||
/// Writes a <see cref="DateTimeOffset"/> value.
|
||||
/// </summary>
|
||||
@@ -428,6 +454,7 @@ namespace Newtonsoft.Json
|
||||
base.WriteValue(value);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Date);
|
||||
}
|
||||
#endif
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -47,11 +47,11 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
StartArray,
|
||||
/// <summary>
|
||||
/// An object property name.
|
||||
/// A constructor start token.
|
||||
/// </summary>
|
||||
StartConstructor,
|
||||
/// <summary>
|
||||
/// A constructor end token.
|
||||
/// An object property name.
|
||||
/// </summary>
|
||||
PropertyName,
|
||||
/// <summary>
|
||||
@@ -95,12 +95,16 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
EndArray,
|
||||
/// <summary>
|
||||
/// A constructor start token.
|
||||
/// A constructor end token.
|
||||
/// </summary>
|
||||
EndConstructor,
|
||||
/// <summary>
|
||||
/// A Date.
|
||||
/// </summary>
|
||||
Date
|
||||
Date,
|
||||
/// <summary>
|
||||
/// Byte data.
|
||||
/// </summary>
|
||||
Bytes
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -304,6 +304,14 @@ namespace Newtonsoft.Json
|
||||
}
|
||||
}
|
||||
|
||||
public override byte[] ReadAsBytes()
|
||||
{
|
||||
byte[] data = _reader.ReadAsBytes();
|
||||
|
||||
ValidateCurrentToken();
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the next JSON token from the stream.
|
||||
/// </summary>
|
||||
@@ -318,7 +326,13 @@ namespace Newtonsoft.Json
|
||||
if (_reader.TokenType == JsonToken.Comment)
|
||||
return true;
|
||||
|
||||
// first time Read has been called. build model
|
||||
ValidateCurrentToken();
|
||||
return true;
|
||||
}
|
||||
|
||||
private void ValidateCurrentToken()
|
||||
{
|
||||
// first time validate has been called. build model
|
||||
if (_model == null)
|
||||
{
|
||||
JsonSchemaModelBuilder builder = new JsonSchemaModelBuilder();
|
||||
@@ -330,15 +344,15 @@ namespace Newtonsoft.Json
|
||||
case JsonToken.StartObject:
|
||||
ProcessValue();
|
||||
JsonSchemaModel objectSchema = (ValidateObject(CurrentMemberSchema))
|
||||
? CurrentMemberSchema
|
||||
: null;
|
||||
? CurrentMemberSchema
|
||||
: null;
|
||||
Push(new SchemaScope(JTokenType.Object, objectSchema));
|
||||
break;
|
||||
case JsonToken.StartArray:
|
||||
ProcessValue();
|
||||
JsonSchemaModel arraySchema = (ValidateArray(CurrentMemberSchema))
|
||||
? CurrentMemberSchema
|
||||
: null;
|
||||
? CurrentMemberSchema
|
||||
: null;
|
||||
Push(new SchemaScope(JTokenType.Array, arraySchema));
|
||||
break;
|
||||
case JsonToken.StartConstructor:
|
||||
@@ -387,8 +401,6 @@ namespace Newtonsoft.Json
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void ValidateEndObject(JsonSchemaModel schema)
|
||||
|
||||
@@ -101,6 +101,7 @@ namespace Newtonsoft.Json
|
||||
Array,
|
||||
ConstructorStart,
|
||||
Constructor,
|
||||
Bytes,
|
||||
Closed,
|
||||
Error
|
||||
}
|
||||
@@ -578,6 +579,7 @@ namespace Newtonsoft.Json
|
||||
case JsonToken.Null:
|
||||
case JsonToken.Undefined:
|
||||
case JsonToken.Date:
|
||||
case JsonToken.Bytes:
|
||||
// a value is being written
|
||||
token = 7;
|
||||
break;
|
||||
@@ -780,6 +782,7 @@ namespace Newtonsoft.Json
|
||||
AutoComplete(JsonToken.Date);
|
||||
}
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
/// <summary>
|
||||
/// Writes a <see cref="DateTimeOffset"/> value.
|
||||
/// </summary>
|
||||
@@ -788,6 +791,7 @@ namespace Newtonsoft.Json
|
||||
{
|
||||
AutoComplete(JsonToken.Date);
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="Nullable{Int32}"/> value.
|
||||
@@ -957,6 +961,7 @@ namespace Newtonsoft.Json
|
||||
WriteValue(value.Value);
|
||||
}
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
/// <summary>
|
||||
/// Writes a <see cref="Nullable{DateTimeOffset}"/> value.
|
||||
/// </summary>
|
||||
@@ -968,6 +973,15 @@ namespace Newtonsoft.Json
|
||||
else
|
||||
WriteValue(value.Value);
|
||||
}
|
||||
#endif
|
||||
|
||||
public virtual void WriteValue(byte[] value)
|
||||
{
|
||||
if (value == null)
|
||||
WriteNull();
|
||||
else
|
||||
AutoComplete(JsonToken.Bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="Object"/> value.
|
||||
@@ -1037,11 +1051,18 @@ namespace Newtonsoft.Json
|
||||
return;
|
||||
}
|
||||
}
|
||||
#if !PocketPC && !NET20
|
||||
else if (value is DateTimeOffset)
|
||||
{
|
||||
WriteValue((DateTimeOffset)value);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
else if (value is byte[])
|
||||
{
|
||||
WriteValue((byte[])value);
|
||||
return;
|
||||
}
|
||||
|
||||
throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
|
||||
}
|
||||
|
||||
@@ -257,7 +257,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
internal bool IsMultiContent(object content)
|
||||
{
|
||||
return (content is IEnumerable && !(content is string) && !(content is JToken));
|
||||
return (content is IEnumerable && !(content is string) && !(content is JToken) && !(content is byte[]));
|
||||
}
|
||||
|
||||
internal virtual void AddItem(bool isLast, JToken previous, JToken item)
|
||||
|
||||
@@ -43,7 +43,7 @@ namespace Newtonsoft.Json.Linq
|
||||
[TypeDescriptionProvider(typeof(JTypeDescriptionProvider))]
|
||||
#endif
|
||||
public class JObject : JContainer, IDictionary<string, JToken>, INotifyPropertyChanged
|
||||
#if !PocketPC && !SILVERLIGHT
|
||||
#if !PocketPC && !SILVERLIGHT && !NET20
|
||||
, INotifyPropertyChanging
|
||||
#endif
|
||||
{
|
||||
@@ -51,7 +51,7 @@ namespace Newtonsoft.Json.Linq
|
||||
/// Occurs when a property value changes.
|
||||
/// </summary>
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
#if !PocketPC && !SILVERLIGHT
|
||||
#if !PocketPC && !SILVERLIGHT && !NET20
|
||||
/// <summary>
|
||||
/// Occurs when a property value is changing.
|
||||
/// </summary>
|
||||
@@ -121,7 +121,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
internal void InternalPropertyChanging(JProperty childProperty)
|
||||
{
|
||||
#if !PocketPC && !SILVERLIGHT
|
||||
#if !PocketPC && !SILVERLIGHT && !NET20
|
||||
OnPropertyChanging(childProperty.Name);
|
||||
#endif
|
||||
}
|
||||
@@ -221,7 +221,7 @@ namespace Newtonsoft.Json.Linq
|
||||
}
|
||||
else
|
||||
{
|
||||
#if !PocketPC && !SILVERLIGHT
|
||||
#if !PocketPC && !SILVERLIGHT && !NET20
|
||||
OnPropertyChanging(propertyName);
|
||||
#endif
|
||||
Add(new JProperty(propertyName, value));
|
||||
@@ -472,7 +472,7 @@ namespace Newtonsoft.Json.Linq
|
||||
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
|
||||
#if !PocketPC && !SILVERLIGHT
|
||||
#if !PocketPC && !SILVERLIGHT && !NET20
|
||||
/// <summary>
|
||||
/// Raises the <see cref="PropertyChanging"/> event with the provided arguments.
|
||||
/// </summary>
|
||||
|
||||
@@ -400,6 +400,11 @@ namespace Newtonsoft.Json.Linq
|
||||
return (o.Type == JTokenType.String || o.Type == JTokenType.Comment || o.Type == JTokenType.Raw || IsNullable(o));
|
||||
}
|
||||
|
||||
private static bool ValidateBytes(JToken o)
|
||||
{
|
||||
return (o.Type == JTokenType.Bytes || IsNullable(o));
|
||||
}
|
||||
|
||||
private static string GetType(JToken t)
|
||||
{
|
||||
return (t != null) ? t.Type.ToString() : "{null}";
|
||||
@@ -420,6 +425,7 @@ namespace Newtonsoft.Json.Linq
|
||||
return (bool)v.Value;
|
||||
}
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
/// <summary>
|
||||
/// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.DateTimeOffset"/>.
|
||||
/// </summary>
|
||||
@@ -433,6 +439,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
return (DateTimeOffset)v.Value;
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Boolean}"/>.
|
||||
@@ -482,6 +489,7 @@ namespace Newtonsoft.Json.Linq
|
||||
return (DateTime?)v.Value;
|
||||
}
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
/// <summary>
|
||||
/// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{DateTimeOffset}"/>.
|
||||
/// </summary>
|
||||
@@ -498,6 +506,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
return (DateTimeOffset?)v.Value;
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Decimal}"/>.
|
||||
@@ -732,6 +741,15 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
return Convert.ToUInt64(v.Value, CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
public static explicit operator byte[](JToken value)
|
||||
{
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateBytes(v))
|
||||
throw new ArgumentException("Can not convert {0} to byte array.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (byte[])v.Value;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Cast to operators
|
||||
@@ -745,6 +763,7 @@ namespace Newtonsoft.Json.Linq
|
||||
return new JValue(value);
|
||||
}
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
/// <summary>
|
||||
/// Performs an implicit conversion from <see cref="DateTimeOffset"/> to <see cref="JToken"/>.
|
||||
/// </summary>
|
||||
@@ -754,6 +773,7 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
return new JValue(value);
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Performs an implicit conversion from <see cref="Nullable{Boolean}"/> to <see cref="JToken"/>.
|
||||
@@ -785,6 +805,7 @@ namespace Newtonsoft.Json.Linq
|
||||
return new JValue(value);
|
||||
}
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
/// <summary>
|
||||
/// Performs an implicit conversion from <see cref="Nullable{DateTimeOffset}"/> to <see cref="JToken"/>.
|
||||
/// </summary>
|
||||
@@ -794,6 +815,7 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
return new JValue(value);
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Performs an implicit conversion from <see cref="Nullable{Decimal}"/> to <see cref="JToken"/>.
|
||||
@@ -964,6 +986,11 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
return new JValue(value);
|
||||
}
|
||||
|
||||
public static implicit operator JToken(byte[] value)
|
||||
{
|
||||
return new JValue(value);
|
||||
}
|
||||
#endregion
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
@@ -27,6 +28,15 @@ namespace Newtonsoft.Json.Linq
|
||||
_current = token;
|
||||
}
|
||||
|
||||
public override byte[] ReadAsBytes()
|
||||
{
|
||||
Read();
|
||||
if (TokenType != JsonToken.Bytes)
|
||||
throw new JsonReaderException("Error reading bytes. Expected bytes but got {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
|
||||
|
||||
return (byte[])Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the next JSON token from the stream.
|
||||
/// </summary>
|
||||
@@ -172,6 +182,9 @@ namespace Newtonsoft.Json.Linq
|
||||
case JTokenType.Raw:
|
||||
SetToken(JsonToken.Raw, ((JValue)token).Value);
|
||||
break;
|
||||
case JTokenType.Bytes:
|
||||
SetToken(JsonToken.Bytes, ((JValue)token).Value);
|
||||
break;
|
||||
default:
|
||||
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("Type", token.Type, "Unexpected JTokenType.");
|
||||
}
|
||||
@@ -216,4 +229,4 @@ namespace Newtonsoft.Json.Linq
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -90,6 +90,7 @@ namespace Newtonsoft.Json.Linq
|
||||
/// <summary>
|
||||
/// A raw JSON value.
|
||||
/// </summary>
|
||||
Raw
|
||||
Raw,
|
||||
Bytes
|
||||
}
|
||||
}
|
||||
@@ -334,6 +334,7 @@ namespace Newtonsoft.Json.Linq
|
||||
AddValue(value, JsonToken.Date);
|
||||
}
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
/// <summary>
|
||||
/// Writes a <see cref="DateTimeOffset"/> value.
|
||||
/// </summary>
|
||||
@@ -343,6 +344,13 @@ namespace Newtonsoft.Json.Linq
|
||||
base.WriteValue(value);
|
||||
AddValue(value, JsonToken.Date);
|
||||
}
|
||||
#endif
|
||||
|
||||
public override void WriteValue(byte[] value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
AddValue(value, JsonToken.Bytes);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,6 +142,8 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
if (objA == null && objB == null)
|
||||
return true;
|
||||
if (objA == null || objB == null)
|
||||
return false;
|
||||
|
||||
switch (_valueType)
|
||||
{
|
||||
@@ -158,6 +160,13 @@ namespace Newtonsoft.Json.Linq
|
||||
return objA.Equals(objB);
|
||||
case JTokenType.Date:
|
||||
return objA.Equals(objB);
|
||||
case JTokenType.Bytes:
|
||||
byte[] b1 = objA as byte[];
|
||||
byte[] b2 = objB as byte[];
|
||||
if (b1 == null || b2 == null)
|
||||
return false;
|
||||
|
||||
return MiscellaneousUtils.ByteArrayCompare(b1, b2);
|
||||
default:
|
||||
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("valueType", _valueType, "Unexpected value type: {0}".FormatWith(CultureInfo.InvariantCulture, _valueType));
|
||||
}
|
||||
@@ -214,8 +223,12 @@ namespace Newtonsoft.Json.Linq
|
||||
return JTokenType.Float;
|
||||
else if (value is DateTime)
|
||||
return JTokenType.Date;
|
||||
#if !PocketPC && !NET20
|
||||
else if (value is DateTimeOffset)
|
||||
return JTokenType.Date;
|
||||
#endif
|
||||
else if (value is byte[])
|
||||
return JTokenType.Bytes;
|
||||
else if (value is bool)
|
||||
return JTokenType.Boolean;
|
||||
|
||||
@@ -303,12 +316,17 @@ namespace Newtonsoft.Json.Linq
|
||||
case JTokenType.Date:
|
||||
WriteConvertableValue(writer, converters, v =>
|
||||
{
|
||||
#if !PocketPC && !NET20
|
||||
if (v is DateTimeOffset)
|
||||
writer.WriteValue((DateTimeOffset)v);
|
||||
else
|
||||
writer.WriteValue(Convert.ToDateTime(v, CultureInfo.InvariantCulture));
|
||||
#endif
|
||||
writer.WriteValue(Convert.ToDateTime(v, CultureInfo.InvariantCulture));
|
||||
}, _value);
|
||||
break;
|
||||
case JTokenType.Bytes:
|
||||
WriteConvertableValue(writer, converters, v => writer.WriteValue((byte[])v), _value);
|
||||
break;
|
||||
case JTokenType.Raw:
|
||||
writer.WriteRawValue(_value.ToString());
|
||||
break;
|
||||
|
||||
@@ -64,7 +64,6 @@
|
||||
<Compile Include="Converters\DataTableConverter.cs" />
|
||||
<Compile Include="Converters\EntityKeyMemberConverter.cs" />
|
||||
<Compile Include="Converters\StringEnumConverter.cs" />
|
||||
<Compile Include="DateTimeOffset.cs" />
|
||||
<Compile Include="IJsonLineInfo.cs" />
|
||||
<Compile Include="JsonArrayAttribute.cs" />
|
||||
<Compile Include="JsonContainerAttribute.cs" />
|
||||
@@ -159,6 +158,7 @@
|
||||
<Compile Include="Serialization\ReflectionValueProvider.cs" />
|
||||
<Compile Include="StreamingContext.cs" />
|
||||
<Compile Include="TypeNameHandling.cs" />
|
||||
<Compile Include="Utilities\Base64Encoder.cs" />
|
||||
<Compile Include="Utilities\BidirectionalDictionary.cs" />
|
||||
<Compile Include="Utilities\ConvertUtils.cs" />
|
||||
<Compile Include="Utilities\CollectionWrapper.cs" />
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -66,7 +66,6 @@
|
||||
<Compile Include="Converters\StringEnumConverter.cs" />
|
||||
<Compile Include="Converters\XmlNodeConverter.cs" />
|
||||
<Compile Include="Converters\HtmlColorConverter.cs" />
|
||||
<Compile Include="DateTimeOffset.cs" />
|
||||
<Compile Include="DefaultValueHandling.cs" />
|
||||
<Compile Include="IJsonLineInfo.cs" />
|
||||
<Compile Include="JsonArrayAttribute.cs" />
|
||||
@@ -142,6 +141,7 @@
|
||||
<Compile Include="Serialization\ReflectionValueProvider.cs" />
|
||||
<Compile Include="StreamingContext.cs" />
|
||||
<Compile Include="TypeNameHandling.cs" />
|
||||
<Compile Include="Utilities\Base64Encoder.cs" />
|
||||
<Compile Include="Utilities\BidirectionalDictionary.cs" />
|
||||
<Compile Include="Utilities\CollectionUtils.cs" />
|
||||
<Compile Include="Utilities\CollectionWrapper.cs" />
|
||||
|
||||
@@ -65,9 +65,6 @@
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.ComponentModel.DataAnnotations">
|
||||
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||
</Reference>
|
||||
<Reference Include="System.Core">
|
||||
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||
</Reference>
|
||||
@@ -80,6 +77,10 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Bson\BsonBinaryType.cs" />
|
||||
<Compile Include="Bson\BsonReader.cs" />
|
||||
<Compile Include="Bson\BsonType.cs" />
|
||||
<Compile Include="Bson\BsonWriter.cs" />
|
||||
<Compile Include="Converters\BinaryConverter.cs" />
|
||||
<Compile Include="Converters\DataSetConverter.cs" />
|
||||
<Compile Include="Converters\DataTableConverter.cs" />
|
||||
@@ -178,6 +179,7 @@
|
||||
<Compile Include="Serialization\CachedAttributeGetter.cs" />
|
||||
<Compile Include="Serialization\ReflectionValueProvider.cs" />
|
||||
<Compile Include="Serialization\OnErrorAttribute.cs" />
|
||||
<Compile Include="Utilities\Base64Encoder.cs" />
|
||||
<Compile Include="Utilities\DynamicWrapper.cs" />
|
||||
<Compile Include="Utilities\LateBoundDelegateFactory.cs" />
|
||||
<Compile Include="Utilities\ThreadSafeStore.cs" />
|
||||
|
||||
@@ -193,6 +193,8 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
foreach (MethodInfo method in contract.UnderlyingType.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
|
||||
{
|
||||
// compact framework errors when getting parameters for a generic method
|
||||
// lame, but generic methods should not be callbacks anyway
|
||||
if (method.ContainsGenericParameters)
|
||||
continue;
|
||||
|
||||
@@ -348,7 +350,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
#else
|
||||
property.ValueProvider = new ReflectionValueProvider(member);
|
||||
#endif
|
||||
property.Converter = JsonTypeReflector.GetConverter(member, property.PropertyType);
|
||||
property.Converter = JsonTypeReflector.GetJsonConverter(member, property.PropertyType);
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(member.DeclaringType);
|
||||
@@ -395,7 +397,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
property.Readable = ReflectionUtils.CanReadMemberValue(member);
|
||||
property.Writable = ReflectionUtils.CanSetMemberValue(member);
|
||||
|
||||
property.MemberConverter = JsonTypeReflector.GetConverter(member, ReflectionUtils.GetMemberUnderlyingType(member));
|
||||
property.MemberConverter = JsonTypeReflector.GetJsonConverter(member, ReflectionUtils.GetMemberUnderlyingType(member));
|
||||
|
||||
DefaultValueAttribute defaultValueAttribute = JsonTypeReflector.GetAttribute<DefaultValueAttribute>(member);
|
||||
property.DefaultValue = (defaultValueAttribute != null) ? defaultValueAttribute.Value : null;
|
||||
|
||||
@@ -168,6 +168,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
case JsonToken.Float:
|
||||
case JsonToken.Boolean:
|
||||
case JsonToken.Date:
|
||||
case JsonToken.Bytes:
|
||||
return EnsureType(reader.Value, objectType);
|
||||
case JsonToken.String:
|
||||
// convert empty string to null automatically for nullable types
|
||||
@@ -382,24 +383,6 @@ namespace Newtonsoft.Json.Serialization
|
||||
return value.ToString();
|
||||
}
|
||||
|
||||
private void SetObjectMember(JsonReader reader, object target, JsonObjectContract contract, string memberName)
|
||||
{
|
||||
JsonProperty property;
|
||||
// attempt exact case match first
|
||||
// then try match ignoring case
|
||||
if (contract.Properties.TryGetClosestMatchProperty(memberName, out property))
|
||||
{
|
||||
SetPropertyValue(property, reader, target);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Serializer.MissingMemberHandling == MissingMemberHandling.Error)
|
||||
throw new JsonSerializationException("Could not find member '{0}' on object of type '{1}'".FormatWith(CultureInfo.InvariantCulture, memberName, contract.UnderlyingType.Name));
|
||||
|
||||
reader.Skip();
|
||||
}
|
||||
}
|
||||
|
||||
private void SetPropertyValue(JsonProperty property, JsonReader reader, object target)
|
||||
{
|
||||
if (property.Ignored)
|
||||
@@ -431,7 +414,11 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
object value = CreateValue(reader, property.PropertyType, (useExistingValue) ? currentValue : null, property.Converter);
|
||||
|
||||
if (!useExistingValue && ShouldSetPropertyValue(property, value))
|
||||
// always set the value if useExistingValue is false,
|
||||
// otherwise also set it if CreateValue returns a new value compared to the currentValue
|
||||
// this could happen because of a JsonConverter against the type
|
||||
if ((!useExistingValue || value != currentValue)
|
||||
&& ShouldSetPropertyValue(property, value))
|
||||
property.ValueProvider.SetValue(target, value);
|
||||
}
|
||||
|
||||
@@ -679,14 +666,34 @@ namespace Newtonsoft.Json.Serialization
|
||||
case JsonToken.PropertyName:
|
||||
string memberName = reader.Value.ToString();
|
||||
|
||||
if (!reader.Read())
|
||||
throw new JsonSerializationException("Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName));
|
||||
JsonProperty property;
|
||||
// attempt exact case match first
|
||||
// then try match ignoring case
|
||||
if (!contract.Properties.TryGetClosestMatchProperty(memberName, out property))
|
||||
{
|
||||
if (Serializer.MissingMemberHandling == MissingMemberHandling.Error)
|
||||
throw new JsonSerializationException("Could not find member '{0}' on object of type '{1}'".FormatWith(CultureInfo.InvariantCulture, memberName, contract.UnderlyingType.Name));
|
||||
|
||||
SetRequiredProperty(reader, memberName, contract, requiredProperties);
|
||||
reader.Skip();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (property.PropertyType == typeof(byte[]))
|
||||
{
|
||||
reader.ReadAsBytes();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!reader.Read())
|
||||
throw new JsonSerializationException(
|
||||
"Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName));
|
||||
}
|
||||
|
||||
SetRequiredProperty(reader, property, requiredProperties);
|
||||
|
||||
try
|
||||
{
|
||||
SetObjectMember(reader, newObject, contract, memberName);
|
||||
SetPropertyValue(property, reader, newObject);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -718,9 +725,8 @@ namespace Newtonsoft.Json.Serialization
|
||||
throw new JsonSerializationException("Unexpected end when deserializing object.");
|
||||
}
|
||||
|
||||
private void SetRequiredProperty(JsonReader reader, string memberName, JsonObjectContract contract, Dictionary<JsonProperty, RequiredValue> requiredProperties)
|
||||
private void SetRequiredProperty(JsonReader reader, JsonProperty property, Dictionary<JsonProperty, RequiredValue> requiredProperties)
|
||||
{
|
||||
JsonProperty property = requiredProperties.Keys.ForgivingCaseSensitiveFind(p => p.PropertyName, memberName);
|
||||
if (property != null)
|
||||
{
|
||||
requiredProperties[property] = (reader.TokenType == JsonToken.Null || reader.TokenType == JsonToken.Undefined)
|
||||
|
||||
@@ -226,19 +226,29 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
internal static bool TryConvertToString(object value, Type type, out string s)
|
||||
{
|
||||
#if !SILVERLIGHT && !PocketPC
|
||||
TypeConverter converter = TypeDescriptor.GetConverter(type);
|
||||
#if !PocketPC
|
||||
TypeConverter converter = ConvertUtils.GetConverter(type);
|
||||
|
||||
// use the objectType's TypeConverter if it has one and can convert to a string
|
||||
if (converter != null && !(converter is ComponentConverter) && (converter.GetType() != typeof(TypeConverter) || value is Type))
|
||||
if (converter != null
|
||||
#if !SILVERLIGHT
|
||||
&& !(converter is ComponentConverter)
|
||||
#endif
|
||||
&& (converter.GetType() != typeof(TypeConverter) || value is Type))
|
||||
{
|
||||
if (converter.CanConvertTo(typeof(string)))
|
||||
{
|
||||
#if !SILVERLIGHT
|
||||
s = converter.ConvertToInvariantString(value);
|
||||
#else
|
||||
s = converter.ConvertToString(value);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
}
|
||||
#else
|
||||
#endif
|
||||
|
||||
#if SILVERLIGHT || PocketPC
|
||||
if (value is Guid || value is Type || value is Uri || value is TimeSpan)
|
||||
{
|
||||
s = value.ToString();
|
||||
|
||||
@@ -24,9 +24,7 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
#if !SILVERLIGHT && !PocketPC && !NET20
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
#endif
|
||||
using System.ComponentModel;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
@@ -35,6 +33,13 @@ using Newtonsoft.Json.Utilities;
|
||||
|
||||
namespace Newtonsoft.Json.Serialization
|
||||
{
|
||||
#if !SILVERLIGHT && !PocketPC && !NET20
|
||||
internal interface IMetadataTypeAttribute
|
||||
{
|
||||
Type MetadataClassType { get; }
|
||||
}
|
||||
#endif
|
||||
|
||||
internal static class JsonTypeReflector
|
||||
{
|
||||
public const string IdPropertyName = "$id";
|
||||
@@ -42,9 +47,30 @@ namespace Newtonsoft.Json.Serialization
|
||||
public const string TypePropertyName = "$type";
|
||||
public const string ArrayValuesPropertyName = "$values";
|
||||
|
||||
private static readonly ThreadSafeStore<ICustomAttributeProvider, Type> ConverterTypeCache = new ThreadSafeStore<ICustomAttributeProvider, Type>(GetConverterTypeFromAttribute);
|
||||
private static readonly ThreadSafeStore<ICustomAttributeProvider, Type> JsonConverterTypeCache = new ThreadSafeStore<ICustomAttributeProvider, Type>(GetJsonConverterTypeFromAttribute);
|
||||
#if !SILVERLIGHT && !PocketPC && !NET20
|
||||
private static readonly ThreadSafeStore<Type, Type> AssociatedMetadataTypesCache = new ThreadSafeStore<Type, Type>(GetAssociateMetadataTypeFromAttribute);
|
||||
|
||||
private const string MetadataTypeAttributeTypeName =
|
||||
"System.ComponentModel.DataAnnotations.MetadataTypeAttribute, System.ComponentModel.DataAnnotations, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
|
||||
private static Type _cachedMetadataTypeAttributeType;
|
||||
#endif
|
||||
#if SILVERLIGHT
|
||||
private static readonly ThreadSafeStore<ICustomAttributeProvider, Type> TypeConverterTypeCache = new ThreadSafeStore<ICustomAttributeProvider, Type>(GetTypeConverterTypeFromAttribute);
|
||||
|
||||
private static Type GetTypeConverterTypeFromAttribute(ICustomAttributeProvider attributeProvider)
|
||||
{
|
||||
TypeConverterAttribute converterAttribute = GetAttribute<TypeConverterAttribute>(attributeProvider);
|
||||
if (converterAttribute == null)
|
||||
return null;
|
||||
|
||||
return Type.GetType(converterAttribute.ConverterTypeName);
|
||||
}
|
||||
|
||||
private static Type GetTypeConverterType(ICustomAttributeProvider attributeProvider)
|
||||
{
|
||||
return TypeConverterTypeCache.Get(attributeProvider);
|
||||
}
|
||||
#endif
|
||||
|
||||
public static JsonContainerAttribute GetJsonContainerAttribute(Type type)
|
||||
@@ -83,12 +109,12 @@ namespace Newtonsoft.Json.Serialization
|
||||
return objectAttribute.MemberSerialization;
|
||||
}
|
||||
|
||||
private static Type GetConverterType(ICustomAttributeProvider attributeProvider)
|
||||
private static Type GetJsonConverterType(ICustomAttributeProvider attributeProvider)
|
||||
{
|
||||
return ConverterTypeCache.Get(attributeProvider);
|
||||
return JsonConverterTypeCache.Get(attributeProvider);
|
||||
}
|
||||
|
||||
private static Type GetConverterTypeFromAttribute(ICustomAttributeProvider attributeProvider)
|
||||
private static Type GetJsonConverterTypeFromAttribute(ICustomAttributeProvider attributeProvider)
|
||||
{
|
||||
JsonConverterAttribute converterAttribute = GetAttribute<JsonConverterAttribute>(attributeProvider);
|
||||
return (converterAttribute != null)
|
||||
@@ -96,9 +122,9 @@ namespace Newtonsoft.Json.Serialization
|
||||
: null;
|
||||
}
|
||||
|
||||
public static JsonConverter GetConverter(ICustomAttributeProvider attributeProvider, Type targetConvertedType)
|
||||
public static JsonConverter GetJsonConverter(ICustomAttributeProvider attributeProvider, Type targetConvertedType)
|
||||
{
|
||||
Type converterType = GetConverterType(attributeProvider);
|
||||
Type converterType = GetJsonConverterType(attributeProvider);
|
||||
|
||||
if (converterType != null)
|
||||
{
|
||||
@@ -113,6 +139,22 @@ namespace Newtonsoft.Json.Serialization
|
||||
return null;
|
||||
}
|
||||
|
||||
#if !PocketPC
|
||||
public static TypeConverter GetTypeConverter(Type type)
|
||||
{
|
||||
#if !SILVERLIGHT
|
||||
return TypeDescriptor.GetConverter(type);
|
||||
#else
|
||||
Type converterType = GetTypeConverterType(type);
|
||||
|
||||
if (converterType != null)
|
||||
return (TypeConverter)ReflectionUtils.CreateInstance(converterType);
|
||||
|
||||
return null;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !SILVERLIGHT && !PocketPC && !NET20
|
||||
private static Type GetAssociatedMetadataType(Type type)
|
||||
{
|
||||
@@ -121,9 +163,34 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
private static Type GetAssociateMetadataTypeFromAttribute(Type type)
|
||||
{
|
||||
MetadataTypeAttribute metadataTypeAttribute = ReflectionUtils.GetAttribute<MetadataTypeAttribute>(type, true);
|
||||
Type metadataTypeAttributeType = GetMetadataTypeAttributeType();
|
||||
if (metadataTypeAttributeType == null)
|
||||
return null;
|
||||
|
||||
return (metadataTypeAttribute != null) ? metadataTypeAttribute.MetadataClassType : null;
|
||||
object attribute = type.GetCustomAttributes(metadataTypeAttributeType, true).SingleOrDefault();
|
||||
if (attribute == null)
|
||||
return null;
|
||||
|
||||
IMetadataTypeAttribute metadataTypeAttribute = DynamicWrapper.CreateWrapper<IMetadataTypeAttribute>(attribute);
|
||||
|
||||
return metadataTypeAttribute.MetadataClassType;
|
||||
}
|
||||
|
||||
private static Type GetMetadataTypeAttributeType()
|
||||
{
|
||||
// always attempt to get the metadata type attribute type
|
||||
// the assembly may have been loaded since last time
|
||||
if (_cachedMetadataTypeAttributeType == null)
|
||||
{
|
||||
Type metadataTypeAttributeType = Type.GetType(MetadataTypeAttributeTypeName);
|
||||
|
||||
if (metadataTypeAttributeType != null)
|
||||
_cachedMetadataTypeAttributeType = metadataTypeAttributeType;
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
return _cachedMetadataTypeAttributeType;
|
||||
}
|
||||
|
||||
private static T GetAttribute<T>(Type type) where T : Attribute
|
||||
@@ -169,7 +236,5 @@ namespace Newtonsoft.Json.Serialization
|
||||
return ReflectionUtils.GetAttribute<T>(attributeProvider, true);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
internal class Base64Encoder
|
||||
{
|
||||
private const int Base64LineSize = 76;
|
||||
private const int LineSizeInBytes = 57;
|
||||
|
||||
private readonly char[] _charsLine = new char[Base64LineSize];
|
||||
private readonly TextWriter _writer;
|
||||
|
||||
private byte[] _leftOverBytes;
|
||||
private int _leftOverBytesCount;
|
||||
|
||||
public Base64Encoder(TextWriter writer)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(writer, "writer");
|
||||
_writer = writer;
|
||||
}
|
||||
|
||||
public void Encode(byte[] buffer, int index, int count)
|
||||
{
|
||||
if (buffer == null)
|
||||
throw new ArgumentNullException("buffer");
|
||||
|
||||
if (index < 0)
|
||||
throw new ArgumentOutOfRangeException("index");
|
||||
|
||||
if (count < 0)
|
||||
throw new ArgumentOutOfRangeException("count");
|
||||
|
||||
if (count > (buffer.Length - index))
|
||||
throw new ArgumentOutOfRangeException("count");
|
||||
|
||||
if (_leftOverBytesCount > 0)
|
||||
{
|
||||
int leftOverBytesCount = _leftOverBytesCount;
|
||||
while (leftOverBytesCount < 3 && count > 0)
|
||||
{
|
||||
_leftOverBytes[leftOverBytesCount++] = buffer[index++];
|
||||
count--;
|
||||
}
|
||||
if (count == 0 && leftOverBytesCount < 3)
|
||||
{
|
||||
_leftOverBytesCount = leftOverBytesCount;
|
||||
return;
|
||||
}
|
||||
int num2 = Convert.ToBase64CharArray(_leftOverBytes, 0, 3, _charsLine, 0);
|
||||
WriteChars(_charsLine, 0, num2);
|
||||
}
|
||||
_leftOverBytesCount = count % 3;
|
||||
if (_leftOverBytesCount > 0)
|
||||
{
|
||||
count -= _leftOverBytesCount;
|
||||
if (_leftOverBytes == null)
|
||||
{
|
||||
_leftOverBytes = new byte[3];
|
||||
}
|
||||
for (int i = 0; i < _leftOverBytesCount; i++)
|
||||
{
|
||||
_leftOverBytes[i] = buffer[(index + count) + i];
|
||||
}
|
||||
}
|
||||
int num4 = index + count;
|
||||
int length = LineSizeInBytes;
|
||||
while (index < num4)
|
||||
{
|
||||
if ((index + length) > num4)
|
||||
{
|
||||
length = num4 - index;
|
||||
}
|
||||
int num6 = Convert.ToBase64CharArray(buffer, index, length, _charsLine, 0);
|
||||
WriteChars(_charsLine, 0, num6);
|
||||
index += length;
|
||||
}
|
||||
}
|
||||
|
||||
public void Flush()
|
||||
{
|
||||
if (_leftOverBytesCount > 0)
|
||||
{
|
||||
int count = Convert.ToBase64CharArray(_leftOverBytes, 0, _leftOverBytesCount, _charsLine, 0);
|
||||
WriteChars(_charsLine, 0, count);
|
||||
_leftOverBytesCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteChars(char[] chars, int index, int count)
|
||||
{
|
||||
_writer.Write(chars, index, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,8 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Globalization;
|
||||
using System.ComponentModel;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
#if !SILVERLIGHT
|
||||
using System.Data.SqlTypes;
|
||||
#endif
|
||||
@@ -53,8 +55,10 @@ namespace Newtonsoft.Json.Utilities
|
||||
return true;
|
||||
}
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
if (initialType == typeof(DateTime) && targetType == typeof(DateTimeOffset))
|
||||
return true;
|
||||
#endif
|
||||
|
||||
if (initialType == typeof(Guid) && (targetType == typeof(Guid) || targetType == typeof(string)))
|
||||
return true;
|
||||
@@ -161,8 +165,10 @@ namespace Newtonsoft.Json.Utilities
|
||||
return System.Convert.ChangeType(initialValue, targetType, culture);
|
||||
}
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
if (initialValue is DateTime && targetType == typeof(DateTimeOffset))
|
||||
return new DateTimeOffset((DateTime)initialValue);
|
||||
#endif
|
||||
|
||||
if (initialValue is string)
|
||||
{
|
||||
@@ -403,20 +409,9 @@ namespace Newtonsoft.Json.Utilities
|
||||
#endif
|
||||
|
||||
#if !PocketPC
|
||||
private static TypeConverter GetConverter(Type t)
|
||||
internal static TypeConverter GetConverter(Type t)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(t, "t");
|
||||
|
||||
#if !SILVERLIGHT
|
||||
return TypeDescriptor.GetConverter(t);
|
||||
#else
|
||||
object[] customAttributes = t.GetCustomAttributes(typeof(TypeConverterAttribute), true);
|
||||
if (customAttributes.Length != 1)
|
||||
return null;
|
||||
|
||||
TypeConverterAttribute typeConverterAttribute = (TypeConverterAttribute)customAttributes[0];
|
||||
return (Activator.CreateInstance(Type.GetType(typeConverterAttribute.ConverterTypeName)) as TypeConverter);
|
||||
#endif
|
||||
return JsonTypeReflector.GetTypeConverter(t);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -42,5 +42,61 @@ namespace Newtonsoft.Json.Utilities
|
||||
|
||||
return (value is string) ? @"""" + value.ToString() + @"""" : value.ToString();
|
||||
}
|
||||
|
||||
public static byte[] HexToBytes(string hex)
|
||||
{
|
||||
// array to put the result in
|
||||
byte[] bytes = new byte[hex.Length / 2];
|
||||
// variable to determine shift of high/low nibble
|
||||
int shift = 4;
|
||||
// offset of the current byte in the array
|
||||
int offset = 0;
|
||||
// loop the characters in the string
|
||||
foreach (char c in hex)
|
||||
{
|
||||
if (c == '-' && shift == 4)
|
||||
continue;
|
||||
|
||||
// get character code in range 0-9, 17-22
|
||||
// the % 32 handles lower case characters
|
||||
int b = (c - '0') % 32;
|
||||
// correction for a-f
|
||||
if (b > 9) b -= 7;
|
||||
// store nibble (4 bits) in byte array
|
||||
bytes[offset] |= (byte)(b << shift);
|
||||
// toggle the shift variable between 0 and 4
|
||||
shift ^= 4;
|
||||
// move to next byte
|
||||
if (shift != 0) offset++;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public static string BytesToHex(byte[] bytes)
|
||||
{
|
||||
return BytesToHex(bytes, false);
|
||||
}
|
||||
|
||||
public static string BytesToHex(byte[] bytes, bool removeDashes)
|
||||
{
|
||||
string hex = BitConverter.ToString(bytes);
|
||||
if (removeDashes)
|
||||
hex = hex.Replace("-", "");
|
||||
|
||||
return hex;
|
||||
}
|
||||
|
||||
public static bool ByteArrayCompare(byte[] a1, byte[] a2)
|
||||
{
|
||||
if (a1.Length != a2.Length)
|
||||
return false;
|
||||
|
||||
for (int i = 0; i < a1.Length; i++)
|
||||
if (a1[i] != a2[i])
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -90,5 +90,10 @@ namespace Newtonsoft.Json.Utilities
|
||||
// TODO: validation
|
||||
return new string(_buffer, start, length);
|
||||
}
|
||||
|
||||
public char[] GetInternalBuffer()
|
||||
{
|
||||
return _buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user