Merge branch 'master' of github.com:JamesNK/Newtonsoft.Json
Conflicts: Src/Newtonsoft.Json/JsonWriter.cs
This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
#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 !(NET35 || NET20 || PORTABLE)
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Dynamic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Linq;
|
||||
#if !NETFX_CORE
|
||||
using NUnit.Framework;
|
||||
#else
|
||||
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
|
||||
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
|
||||
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
|
||||
#endif
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Newtonsoft.Json.Tests.TestObjects;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation
|
||||
{
|
||||
public class Employee
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public Employee Manager { get; set; }
|
||||
}
|
||||
|
||||
#region ShouldSerializeContractResolver
|
||||
public class ShouldSerializeContractResolver : DefaultContractResolver
|
||||
{
|
||||
public new static readonly ShouldSerializeContractResolver Instance = new ShouldSerializeContractResolver();
|
||||
|
||||
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
|
||||
{
|
||||
JsonProperty property = base.CreateProperty(member, memberSerialization);
|
||||
|
||||
if (property.DeclaringType == typeof(Employee) && property.PropertyName == "Manager")
|
||||
{
|
||||
property.ShouldSerialize =
|
||||
instance =>
|
||||
{
|
||||
Employee e = (Employee) instance;
|
||||
return e.Manager != e;
|
||||
};
|
||||
}
|
||||
|
||||
return property;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
[TestFixture]
|
||||
public class ConditionalPropertiesTests : TestFixtureBase
|
||||
{
|
||||
#region EmployeeShouldSerializeExample
|
||||
public class Employee
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public Employee Manager { get; set; }
|
||||
|
||||
public bool ShouldSerializeManager()
|
||||
{
|
||||
// don't serialize the Manager property if an employee is their own manager
|
||||
return (Manager != this);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
[Test]
|
||||
public void ShouldSerializeClassTest()
|
||||
{
|
||||
#region ShouldSerializeClassTest
|
||||
Employee joe = new Employee();
|
||||
joe.Name = "Joe Employee";
|
||||
Employee mike = new Employee();
|
||||
mike.Name = "Mike Manager";
|
||||
|
||||
joe.Manager = mike;
|
||||
|
||||
// mike is his own manager
|
||||
// ShouldSerialize will skip this property
|
||||
mike.Manager = mike;
|
||||
|
||||
string json = JsonConvert.SerializeObject(new[] { joe, mike }, Formatting.Indented);
|
||||
// [
|
||||
// {
|
||||
// "Name": "Joe Employee",
|
||||
// "Manager": {
|
||||
// "Name": "Mike Manager"
|
||||
// }
|
||||
// },
|
||||
// {
|
||||
// "Name": "Mike Manager"
|
||||
// }
|
||||
// ]
|
||||
#endregion
|
||||
|
||||
Assert.AreEqual(@"[
|
||||
{
|
||||
""Name"": ""Joe Employee"",
|
||||
""Manager"": {
|
||||
""Name"": ""Mike Manager""
|
||||
}
|
||||
},
|
||||
{
|
||||
""Name"": ""Mike Manager""
|
||||
}
|
||||
]", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ShouldSerializeContractResolverTest()
|
||||
{
|
||||
Newtonsoft.Json.Tests.Documentation.Employee joe = new Newtonsoft.Json.Tests.Documentation.Employee();
|
||||
joe.Name = "Joe Employee";
|
||||
Newtonsoft.Json.Tests.Documentation.Employee mike = new Newtonsoft.Json.Tests.Documentation.Employee();
|
||||
mike.Name = "Mike Manager";
|
||||
|
||||
joe.Manager = mike;
|
||||
mike.Manager = mike;
|
||||
|
||||
string json = JsonConvert.SerializeObject(
|
||||
new[] {joe, mike},
|
||||
Formatting.Indented,
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = ShouldSerializeContractResolver.Instance
|
||||
});
|
||||
|
||||
Assert.AreEqual(@"[
|
||||
{
|
||||
""Name"": ""Joe Employee"",
|
||||
""Manager"": {
|
||||
""Name"": ""Mike Manager""
|
||||
}
|
||||
},
|
||||
{
|
||||
""Name"": ""Mike Manager""
|
||||
}
|
||||
]", json);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation
|
||||
{
|
||||
public class JsonNetVsWindowsDataJsonTests
|
||||
{
|
||||
public void CreatingJson()
|
||||
{
|
||||
#if NETFX_CORE
|
||||
#region CreatingJSON
|
||||
// Windows.Data.Json
|
||||
// -----------------
|
||||
JsonObject jsonObject = new JsonObject
|
||||
{
|
||||
{"CPU", JsonValue.CreateStringValue("Intel")},
|
||||
{
|
||||
"Drives", new JsonArray
|
||||
{
|
||||
JsonValue.CreateStringValue("DVD read/writer"),
|
||||
JsonValue.CreateStringValue("500 gigabyte hard drive")
|
||||
}
|
||||
}
|
||||
};
|
||||
string json1 = jsonObject.Stringify();
|
||||
|
||||
// LINQ to JSON
|
||||
// ------------
|
||||
JObject jObject = new JObject
|
||||
{
|
||||
{"CPU", "Intel"},
|
||||
{
|
||||
"Drives", new JArray
|
||||
{
|
||||
"DVD read/writer",
|
||||
"500 gigabyte hard drive"
|
||||
}
|
||||
}
|
||||
};
|
||||
string json2 = jObject.ToString();
|
||||
#endregion
|
||||
#endif
|
||||
}
|
||||
|
||||
public void QueryingJson()
|
||||
{
|
||||
#if NETFX_CORE
|
||||
#region QueryingJSON
|
||||
string json = @"{
|
||||
'channel': {
|
||||
'title': 'James Newton-King',
|
||||
'link': 'http://james.newtonking.com',
|
||||
'description': 'James Newton-King's blog.',
|
||||
'item': [
|
||||
{
|
||||
'title': 'Json.NET 1.3 + New license + Now on CodePlex',
|
||||
'description': 'Annoucing the release of Json.NET 1.3, the MIT license and the source being available on CodePlex',
|
||||
'link': 'http://james.newtonking.com/projects/json-net.aspx',
|
||||
'category': [
|
||||
'Json.NET',
|
||||
'CodePlex'
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}";
|
||||
|
||||
// Windows.Data.Json
|
||||
// -----------------
|
||||
JsonObject jsonObject = JsonObject.Parse(json);
|
||||
string itemTitle1 = jsonObject["channel"].GetObject()["item"].GetArray()[0].GetObject()["title"].GetString();
|
||||
|
||||
// LINQ to JSON
|
||||
// ------------
|
||||
JObject jObject = JObject.Parse(json);
|
||||
string itemTitle2 = (string)jObject["channel"]["item"][0]["title"];
|
||||
#endregion
|
||||
#endif
|
||||
}
|
||||
|
||||
public void Converting()
|
||||
{
|
||||
#if NETFX_CORE
|
||||
#region Converting
|
||||
JsonObject jsonObject = new JsonObject
|
||||
{
|
||||
{"CPU", JsonValue.CreateStringValue("Intel")},
|
||||
{"Drives", new JsonArray {
|
||||
JsonValue.CreateStringValue("DVD read/writer"),
|
||||
JsonValue.CreateStringValue("500 gigabyte hard drive")
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// convert Windows.Data.Json to LINQ to JSON
|
||||
JObject o = JObject.FromObject(jsonObject);
|
||||
|
||||
// convert LINQ to JSON to Windows.Data.Json
|
||||
JArray a = (JArray)o["Drives"];
|
||||
JsonArray jsonArray = a.ToObject<JsonArray>();
|
||||
#endregion
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -80,10 +80,13 @@ namespace Newtonsoft.Json.Tests.Documentation
|
||||
#region JsonConverterContractResolver
|
||||
public class ConverterContractResolver : DefaultContractResolver
|
||||
{
|
||||
public new static readonly ConverterContractResolver Instance = new ConverterContractResolver();
|
||||
|
||||
protected override JsonContract CreateContract(Type objectType)
|
||||
{
|
||||
JsonContract contract = base.CreateContract(objectType);
|
||||
|
||||
// this will only be called once and then cached
|
||||
if (objectType == typeof(DateTime) || objectType == typeof(DateTimeOffset))
|
||||
contract.Converter = new JavaScriptDateTimeConverter();
|
||||
|
||||
@@ -117,7 +120,7 @@ namespace Newtonsoft.Json.Tests.Documentation
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(new DateTime(2000, 10, 10, 10, 10, 10, DateTimeKind.Utc), new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new ConverterContractResolver()
|
||||
ContractResolver = ConverterContractResolver.Instance
|
||||
});
|
||||
|
||||
Console.WriteLine(json);
|
||||
|
||||
@@ -713,6 +713,7 @@ _____'propertyName': NaN
|
||||
jsonWriter.WriteValue(data);
|
||||
jsonWriter.WriteValue((object)data);
|
||||
jsonWriter.WriteValue((byte[])null);
|
||||
jsonWriter.WriteValue((Uri)null);
|
||||
jsonWriter.WriteEndArray();
|
||||
}
|
||||
|
||||
@@ -720,6 +721,7 @@ _____'propertyName': NaN
|
||||
""SGVsbG8gd29ybGQu"",
|
||||
""SGVsbG8gd29ybGQu"",
|
||||
""SGVsbG8gd29ybGQu"",
|
||||
null,
|
||||
null
|
||||
]";
|
||||
string result = sb.ToString();
|
||||
|
||||
@@ -117,6 +117,7 @@
|
||||
<Compile Include="Converters\StringEnumConverterTests.cs" />
|
||||
<Compile Include="Converters\VersionConverterTests.cs" />
|
||||
<Compile Include="Documentation\ConvertingJsonAndXmlTests.cs" />
|
||||
<Compile Include="Documentation\ConditionalPropertiesTests.cs" />
|
||||
<Compile Include="Documentation\PerformanceTests.cs" />
|
||||
<Compile Include="Documentation\JsonSchemaTests.cs" />
|
||||
<Compile Include="Documentation\ReadingAndWritingJsonTests.cs" />
|
||||
|
||||
@@ -64,8 +64,8 @@ namespace Newtonsoft.Json.Tests
|
||||
[TestFixture]
|
||||
public class PerformanceTests : TestFixtureBase
|
||||
{
|
||||
private const int Iterations = 100;
|
||||
//private const int Iterations = 5000;
|
||||
//private const int Iterations = 100;
|
||||
private const int Iterations = 5000;
|
||||
|
||||
#region Data
|
||||
|
||||
@@ -122,8 +122,6 @@ namespace Newtonsoft.Json.Tests
|
||||
TestClass test = CreateSerializationObject();
|
||||
|
||||
SerializeTests(test);
|
||||
|
||||
Deserialize();
|
||||
}
|
||||
|
||||
private void SerializeTests(object value)
|
||||
|
||||
@@ -76,5 +76,5 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("4.5.0.0")]
|
||||
#if !PocketPC
|
||||
[assembly: AssemblyFileVersion("4.5.10.15427")]
|
||||
[assembly: AssemblyFileVersion("4.5.10.15501")]
|
||||
#endif
|
||||
|
||||
@@ -148,7 +148,7 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
public override void WriteStartObject()
|
||||
{
|
||||
base.WriteStartObject();
|
||||
InternalWriteStart(JsonToken.StartObject, JsonContainerType.Object);
|
||||
|
||||
_writer.Write("{");
|
||||
}
|
||||
@@ -158,7 +158,7 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
public override void WriteStartArray()
|
||||
{
|
||||
base.WriteStartArray();
|
||||
InternalWriteStart(JsonToken.StartArray, JsonContainerType.Array);
|
||||
|
||||
_writer.Write("[");
|
||||
}
|
||||
@@ -169,7 +169,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="name">The name of the constructor.</param>
|
||||
public override void WriteStartConstructor(string name)
|
||||
{
|
||||
base.WriteStartConstructor(name);
|
||||
InternalWriteStart(JsonToken.StartConstructor, JsonContainerType.Constructor);
|
||||
|
||||
_writer.Write("new ");
|
||||
_writer.Write(name);
|
||||
@@ -204,7 +204,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="name">The name of the property.</param>
|
||||
public override void WritePropertyName(string name)
|
||||
{
|
||||
base.WritePropertyName(name);
|
||||
InternalWritePropertyName(name);
|
||||
|
||||
JavaScriptUtils.WriteEscapedJavaScriptString(_writer, name, _quoteChar, _quoteName);
|
||||
|
||||
@@ -259,7 +259,7 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
public override void WriteNull()
|
||||
{
|
||||
base.WriteNull();
|
||||
InternalWriteNull();
|
||||
WriteValueInternal(JsonConvert.Null, JsonToken.Null);
|
||||
}
|
||||
|
||||
@@ -268,7 +268,7 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
public override void WriteUndefined()
|
||||
{
|
||||
base.WriteUndefined();
|
||||
InternalWriteUndefined();
|
||||
WriteValueInternal(JsonConvert.Undefined, JsonToken.Undefined);
|
||||
}
|
||||
|
||||
@@ -278,7 +278,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="json">The raw JSON to write.</param>
|
||||
public override void WriteRaw(string json)
|
||||
{
|
||||
base.WriteRaw(json);
|
||||
InternalWriteRaw();
|
||||
|
||||
_writer.Write(json);
|
||||
}
|
||||
@@ -289,7 +289,8 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="String"/> value to write.</param>
|
||||
public override void WriteValue(string value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
InternalWriteValue(JsonToken.String);
|
||||
|
||||
if (value == null)
|
||||
WriteValueInternal(JsonConvert.Null, JsonToken.Null);
|
||||
else
|
||||
@@ -302,7 +303,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Int32"/> value to write.</param>
|
||||
public override void WriteValue(int value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
InternalWriteValue(JsonToken.Integer);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
|
||||
}
|
||||
|
||||
@@ -313,7 +314,7 @@ namespace Newtonsoft.Json
|
||||
[CLSCompliant(false)]
|
||||
public override void WriteValue(uint value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
InternalWriteValue(JsonToken.Integer);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
|
||||
}
|
||||
|
||||
@@ -323,7 +324,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Int64"/> value to write.</param>
|
||||
public override void WriteValue(long value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
InternalWriteValue(JsonToken.Integer);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
|
||||
}
|
||||
|
||||
@@ -334,7 +335,7 @@ namespace Newtonsoft.Json
|
||||
[CLSCompliant(false)]
|
||||
public override void WriteValue(ulong value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
InternalWriteValue(JsonToken.Integer);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
|
||||
}
|
||||
|
||||
@@ -344,7 +345,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Single"/> value to write.</param>
|
||||
public override void WriteValue(float value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
InternalWriteValue(JsonToken.Float);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Float);
|
||||
}
|
||||
|
||||
@@ -354,7 +355,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Double"/> value to write.</param>
|
||||
public override void WriteValue(double value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
InternalWriteValue(JsonToken.Float);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Float);
|
||||
}
|
||||
|
||||
@@ -364,7 +365,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Boolean"/> value to write.</param>
|
||||
public override void WriteValue(bool value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
InternalWriteValue(JsonToken.Boolean);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Boolean);
|
||||
}
|
||||
|
||||
@@ -374,7 +375,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Int16"/> value to write.</param>
|
||||
public override void WriteValue(short value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
InternalWriteValue(JsonToken.Integer);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
|
||||
}
|
||||
|
||||
@@ -385,7 +386,7 @@ namespace Newtonsoft.Json
|
||||
[CLSCompliant(false)]
|
||||
public override void WriteValue(ushort value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
InternalWriteValue(JsonToken.Integer);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
|
||||
}
|
||||
|
||||
@@ -395,8 +396,8 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Char"/> value to write.</param>
|
||||
public override void WriteValue(char value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
|
||||
InternalWriteValue(JsonToken.String);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.String);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -405,7 +406,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Byte"/> value to write.</param>
|
||||
public override void WriteValue(byte value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
InternalWriteValue(JsonToken.Integer);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
|
||||
}
|
||||
|
||||
@@ -416,7 +417,7 @@ namespace Newtonsoft.Json
|
||||
[CLSCompliant(false)]
|
||||
public override void WriteValue(sbyte value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
InternalWriteValue(JsonToken.Integer);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
|
||||
}
|
||||
|
||||
@@ -426,7 +427,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Decimal"/> value to write.</param>
|
||||
public override void WriteValue(decimal value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
InternalWriteValue(JsonToken.Float);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Float);
|
||||
}
|
||||
|
||||
@@ -436,7 +437,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="DateTime"/> value to write.</param>
|
||||
public override void WriteValue(DateTime value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
InternalWriteValue(JsonToken.Date);
|
||||
value = JsonConvert.EnsureDateTime(value, DateTimeZoneHandling);
|
||||
JsonConvert.WriteDateTimeString(_writer, value, DateFormatHandling);
|
||||
}
|
||||
@@ -447,10 +448,13 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="T:Byte[]"/> value to write.</param>
|
||||
public override void WriteValue(byte[] value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
|
||||
if (value != null)
|
||||
if (value == null)
|
||||
{
|
||||
WriteNull();
|
||||
}
|
||||
else
|
||||
{
|
||||
InternalWriteValue(JsonToken.Bytes);
|
||||
_writer.Write(_quoteChar);
|
||||
Base64Encoder.Encode(value, 0, value.Length);
|
||||
Base64Encoder.Flush();
|
||||
@@ -465,7 +469,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="DateTimeOffset"/> value to write.</param>
|
||||
public override void WriteValue(DateTimeOffset value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
InternalWriteValue(JsonToken.Date);
|
||||
WriteValueInternal(JsonConvert.ToString(value, DateFormatHandling), JsonToken.Date);
|
||||
}
|
||||
#endif
|
||||
@@ -476,7 +480,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Guid"/> value to write.</param>
|
||||
public override void WriteValue(Guid value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
InternalWriteValue(JsonToken.String);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.String);
|
||||
}
|
||||
|
||||
@@ -486,7 +490,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="TimeSpan"/> value to write.</param>
|
||||
public override void WriteValue(TimeSpan value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
InternalWriteValue(JsonToken.String);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.String);
|
||||
}
|
||||
|
||||
@@ -496,8 +500,15 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Uri"/> value to write.</param>
|
||||
public override void WriteValue(Uri value)
|
||||
{
|
||||
base.WriteValue(value);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.String);
|
||||
if (value == null)
|
||||
{
|
||||
WriteNull();
|
||||
}
|
||||
else
|
||||
{
|
||||
InternalWriteValue(JsonToken.String);
|
||||
WriteValueInternal(JsonConvert.ToString(value), JsonToken.String);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -507,7 +518,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="text">Text to place inside the comment.</param>
|
||||
public override void WriteComment(string text)
|
||||
{
|
||||
base.WriteComment(text);
|
||||
InternalWriteComment();
|
||||
|
||||
_writer.Write("/*");
|
||||
_writer.Write(text);
|
||||
@@ -520,7 +531,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="ws">The string of white space characters.</param>
|
||||
public override void WriteWhitespace(string ws)
|
||||
{
|
||||
base.WriteWhitespace(ws);
|
||||
InternalWriteWhitespace(ws);
|
||||
|
||||
_writer.Write(ws);
|
||||
}
|
||||
|
||||
@@ -54,7 +54,6 @@ namespace Newtonsoft.Json
|
||||
Array,
|
||||
ConstructorStart,
|
||||
Constructor,
|
||||
Bytes,
|
||||
Closed,
|
||||
Error
|
||||
}
|
||||
@@ -69,7 +68,7 @@ namespace Newtonsoft.Json
|
||||
/* StartObject */new[]{ State.ObjectStart, State.ObjectStart, State.Error, State.Error, State.ObjectStart, State.ObjectStart, State.ObjectStart, State.ObjectStart, State.Error, State.Error },
|
||||
/* StartArray */new[]{ State.ArrayStart, State.ArrayStart, State.Error, State.Error, State.ArrayStart, State.ArrayStart, State.ArrayStart, State.ArrayStart, State.Error, State.Error },
|
||||
/* StartConstructor */new[]{ State.ConstructorStart, State.ConstructorStart, State.Error, State.Error, State.ConstructorStart, State.ConstructorStart, State.ConstructorStart, State.ConstructorStart, State.Error, State.Error },
|
||||
/* StartProperty */new[]{ State.Property, State.Error, State.Property, State.Property, State.Error, State.Error, State.Error, State.Error, State.Error, State.Error },
|
||||
/* Property */new[]{ State.Property, State.Error, State.Property, State.Property, State.Error, State.Error, State.Error, State.Error, State.Error, State.Error },
|
||||
/* Comment */new[]{ State.Start, State.Property, State.ObjectStart, State.Object, State.ArrayStart, State.Array, State.Constructor, State.Constructor, State.Error, State.Error },
|
||||
/* Raw */new[]{ State.Start, State.Property, State.ObjectStart, State.Object, State.ArrayStart, State.Array, State.Constructor, State.Constructor, State.Error, State.Error },
|
||||
/* Value (this will be copied) */new[]{ State.Start, State.Object, State.Error, State.Error, State.Array, State.Array, State.Constructor, State.Constructor, State.Error, State.Error }
|
||||
@@ -142,7 +141,7 @@ namespace Newtonsoft.Json
|
||||
return depth;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets the state of the writer.
|
||||
/// </summary>
|
||||
@@ -304,10 +303,15 @@ namespace Newtonsoft.Json
|
||||
/// Writes the beginning of a Json object.
|
||||
/// </summary>
|
||||
public virtual void WriteStartObject()
|
||||
{
|
||||
InternalWriteStart(JsonToken.StartObject, JsonContainerType.Object);
|
||||
}
|
||||
|
||||
internal void InternalWriteStart(JsonToken token, JsonContainerType container)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.StartObject);
|
||||
Push(JsonContainerType.Object);
|
||||
AutoComplete(token);
|
||||
Push(container);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -315,7 +319,7 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
public virtual void WriteEndObject()
|
||||
{
|
||||
AutoCompleteClose(JsonContainerType.Object);
|
||||
InternalWriteEnd(JsonContainerType.Object);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -323,9 +327,7 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
public virtual void WriteStartArray()
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.StartArray);
|
||||
Push(JsonContainerType.Array);
|
||||
InternalWriteStart(JsonToken.StartArray, JsonContainerType.Array);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -333,7 +335,7 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
public virtual void WriteEndArray()
|
||||
{
|
||||
AutoCompleteClose(JsonContainerType.Array);
|
||||
InternalWriteEnd(JsonContainerType.Array);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -342,9 +344,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="name">The name of the constructor.</param>
|
||||
public virtual void WriteStartConstructor(string name)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.StartConstructor);
|
||||
Push(JsonContainerType.Constructor);
|
||||
InternalWriteStart(JsonToken.StartConstructor, JsonContainerType.Constructor);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -352,7 +352,12 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
public virtual void WriteEndConstructor()
|
||||
{
|
||||
AutoCompleteClose(JsonContainerType.Constructor);
|
||||
InternalWriteEnd(JsonContainerType.Constructor);
|
||||
}
|
||||
|
||||
internal void InternalWriteEnd(JsonContainerType container)
|
||||
{
|
||||
AutoCompleteClose(container);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -360,6 +365,11 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the property.</param>
|
||||
public virtual void WritePropertyName(string name)
|
||||
{
|
||||
InternalWritePropertyName(name);
|
||||
}
|
||||
|
||||
internal void InternalWritePropertyName(string name)
|
||||
{
|
||||
_currentPosition.PropertyName = name;
|
||||
AutoComplete(JsonToken.PropertyName);
|
||||
@@ -672,22 +682,16 @@ namespace Newtonsoft.Json
|
||||
{
|
||||
WriteValueDelimiter();
|
||||
}
|
||||
else if (_currentState == State.Property)
|
||||
{
|
||||
if (_formatting == Formatting.Indented)
|
||||
WriteIndentSpace();
|
||||
}
|
||||
|
||||
if (_formatting == Formatting.Indented)
|
||||
{
|
||||
WriteState writeState = WriteState;
|
||||
if (_currentState == State.Property)
|
||||
WriteIndentSpace();
|
||||
|
||||
// don't indent a property when it is the first token to be written (i.e. at the start)
|
||||
if ((tokenBeingWritten == JsonToken.PropertyName && writeState != WriteState.Start) ||
|
||||
writeState == WriteState.Array || writeState == WriteState.Constructor)
|
||||
{
|
||||
if ((_currentState == State.Array || _currentState == State.ArrayStart || _currentState == State.Constructor || _currentState == State.ConstructorStart)
|
||||
|| (tokenBeingWritten == JsonToken.PropertyName && _currentState != State.Start))
|
||||
WriteIndent();
|
||||
}
|
||||
}
|
||||
|
||||
_currentState = newState;
|
||||
@@ -698,6 +702,11 @@ namespace Newtonsoft.Json
|
||||
/// Writes a null value.
|
||||
/// </summary>
|
||||
public virtual void WriteNull()
|
||||
{
|
||||
InternalWriteNull();
|
||||
}
|
||||
|
||||
internal void InternalWriteNull()
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.Null);
|
||||
@@ -707,6 +716,11 @@ namespace Newtonsoft.Json
|
||||
/// Writes an undefined value.
|
||||
/// </summary>
|
||||
public virtual void WriteUndefined()
|
||||
{
|
||||
InternalWriteUndefined();
|
||||
}
|
||||
|
||||
internal void InternalWriteUndefined()
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.Undefined);
|
||||
@@ -717,6 +731,11 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
/// <param name="json">The raw JSON to write.</param>
|
||||
public virtual void WriteRaw(string json)
|
||||
{
|
||||
InternalWriteRaw();
|
||||
}
|
||||
|
||||
internal void InternalWriteRaw()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -737,9 +756,14 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
/// <param name="value">The <see cref="String"/> value to write.</param>
|
||||
public virtual void WriteValue(string value)
|
||||
{
|
||||
InternalWriteValue(JsonToken.String);
|
||||
}
|
||||
|
||||
internal void InternalWriteValue(JsonToken token)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.String);
|
||||
AutoComplete(token);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -748,8 +772,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Int32"/> value to write.</param>
|
||||
public virtual void WriteValue(int value)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.Integer);
|
||||
InternalWriteValue(JsonToken.Integer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -759,8 +782,7 @@ namespace Newtonsoft.Json
|
||||
[CLSCompliant(false)]
|
||||
public virtual void WriteValue(uint value)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.Integer);
|
||||
InternalWriteValue(JsonToken.Integer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -769,8 +791,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Int64"/> value to write.</param>
|
||||
public virtual void WriteValue(long value)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.Integer);
|
||||
InternalWriteValue(JsonToken.Integer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -780,8 +801,7 @@ namespace Newtonsoft.Json
|
||||
[CLSCompliant(false)]
|
||||
public virtual void WriteValue(ulong value)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.Integer);
|
||||
InternalWriteValue(JsonToken.Integer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -790,8 +810,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Single"/> value to write.</param>
|
||||
public virtual void WriteValue(float value)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.Float);
|
||||
InternalWriteValue(JsonToken.Float);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -800,8 +819,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Double"/> value to write.</param>
|
||||
public virtual void WriteValue(double value)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.Float);
|
||||
InternalWriteValue(JsonToken.Float);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -810,8 +828,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Boolean"/> value to write.</param>
|
||||
public virtual void WriteValue(bool value)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.Boolean);
|
||||
InternalWriteValue(JsonToken.Boolean);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -820,8 +837,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Int16"/> value to write.</param>
|
||||
public virtual void WriteValue(short value)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.Integer);
|
||||
InternalWriteValue(JsonToken.Integer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -831,8 +847,7 @@ namespace Newtonsoft.Json
|
||||
[CLSCompliant(false)]
|
||||
public virtual void WriteValue(ushort value)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.Integer);
|
||||
InternalWriteValue(JsonToken.Integer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -841,8 +856,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Char"/> value to write.</param>
|
||||
public virtual void WriteValue(char value)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.String);
|
||||
InternalWriteValue(JsonToken.String);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -851,8 +865,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Byte"/> value to write.</param>
|
||||
public virtual void WriteValue(byte value)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.Integer);
|
||||
InternalWriteValue(JsonToken.Integer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -862,8 +875,7 @@ namespace Newtonsoft.Json
|
||||
[CLSCompliant(false)]
|
||||
public virtual void WriteValue(sbyte value)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.Integer);
|
||||
InternalWriteValue(JsonToken.Integer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -872,8 +884,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Decimal"/> value to write.</param>
|
||||
public virtual void WriteValue(decimal value)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.Float);
|
||||
InternalWriteValue(JsonToken.Float);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -882,8 +893,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="DateTime"/> value to write.</param>
|
||||
public virtual void WriteValue(DateTime value)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.Date);
|
||||
InternalWriteValue(JsonToken.Date);
|
||||
}
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
@@ -893,8 +903,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="DateTimeOffset"/> value to write.</param>
|
||||
public virtual void WriteValue(DateTimeOffset value)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.Date);
|
||||
InternalWriteValue(JsonToken.Date);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -904,8 +913,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="Guid"/> value to write.</param>
|
||||
public virtual void WriteValue(Guid value)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.String);
|
||||
InternalWriteValue(JsonToken.String);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -914,8 +922,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The <see cref="TimeSpan"/> value to write.</param>
|
||||
public virtual void WriteValue(TimeSpan value)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.String);
|
||||
InternalWriteValue(JsonToken.String);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1135,14 +1142,9 @@ namespace Newtonsoft.Json
|
||||
public virtual void WriteValue(byte[] value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
WriteNull();
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.Bytes);
|
||||
}
|
||||
InternalWriteValue(JsonToken.Bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1152,14 +1154,9 @@ namespace Newtonsoft.Json
|
||||
public virtual void WriteValue(Uri value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
WriteNull();
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
AutoComplete(JsonToken.String);
|
||||
}
|
||||
InternalWriteValue(JsonToken.String);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1270,7 +1267,11 @@ namespace Newtonsoft.Json
|
||||
/// <param name="text">Text to place inside the comment.</param>
|
||||
public virtual void WriteComment(string text)
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
InternalWriteComment();
|
||||
}
|
||||
|
||||
internal void InternalWriteComment()
|
||||
{
|
||||
AutoComplete(JsonToken.Comment);
|
||||
}
|
||||
|
||||
@@ -1279,6 +1280,11 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
/// <param name="ws">The string of white space characters.</param>
|
||||
public virtual void WriteWhitespace(string ws)
|
||||
{
|
||||
InternalWriteWhitespace(ws);
|
||||
}
|
||||
|
||||
internal void InternalWriteWhitespace(string ws)
|
||||
{
|
||||
if (ws != null)
|
||||
{
|
||||
@@ -1287,7 +1293,6 @@ namespace Newtonsoft.Json
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void IDisposable.Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
|
||||
@@ -90,7 +90,7 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("4.5.0.0")]
|
||||
#if !PocketPC
|
||||
[assembly: AssemblyFileVersion("4.5.10.15427")]
|
||||
[assembly: AssemblyFileVersion("4.5.10.15501")]
|
||||
#endif
|
||||
|
||||
[assembly: CLSCompliant(true)]
|
||||
|
||||
Reference in New Issue
Block a user