-Fixed quoting dictionary keys when QuoteNames is false

-Fixed potential error handling error
This commit is contained in:
James Newton-King
2013-07-20 22:15:34 +12:00
parent 34bca02190
commit 4922c4eedc
8 changed files with 83 additions and 5 deletions
@@ -1339,6 +1339,32 @@ _____'propertyName': NaN
""1ytreporP"": NULL!!!
}}}", sw.ToString());
}
[Test]
public void QuoteDictionaryNames()
{
var d = new Dictionary<string, int>
{
{ "a", 1 },
};
var jsonSerializerSettings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
};
var serializer = JsonSerializer.Create(jsonSerializerSettings);
using (var stringWriter = new StringWriter())
{
using (var writer = new JsonTextWriter(stringWriter) { QuoteName = false })
{
serializer.Serialize(writer, d);
writer.Close();
}
Assert.AreEqual(@"{
a: 1
}", stringWriter.ToString());
}
}
}
public class CustomJsonTextWriter : JsonTextWriter
@@ -35,7 +35,8 @@
<HintPath>..\packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Collections.Immutable">
<Reference Include="System.Collections.Immutable, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.Bcl.Immutable.1.0.12-beta\lib\portable-net45+win8+wp8\System.Collections.Immutable.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.DataAnnotations" />
@@ -75,4 +75,4 @@ using System.Security;
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")]
[assembly: AssemblyFileVersion("5.0.6.16317")]
[assembly: AssemblyFileVersion("5.0.6.16320")]
@@ -25,7 +25,10 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Tests.TestObjects;
#if NET20
using Newtonsoft.Json.Utilities.LinqBridge;
@@ -690,6 +693,40 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual(@"{""Scott"":[],""James"":[]}", output);
}
[Test]
public void NoObjectWithEvent()
{
string json = "{\"}";
byte[] byteArray = Encoding.UTF8.GetBytes(json);
MemoryStream stream = new MemoryStream(byteArray);
JsonTextReader jReader = new JsonTextReader(new StreamReader(stream));
JsonSerializer s = new JsonSerializer();
s.Error += (sender, args) =>
{
args.ErrorContext.Handled = true;
};
ErrorPerson2 obj = s.Deserialize<ErrorPerson2>(jReader);
Assert.IsNull(obj);
}
[Test]
public void NoObjectWithAttribute()
{
string json = "{\"}";
byte[] byteArray = Encoding.UTF8.GetBytes(json);
MemoryStream stream = new MemoryStream(byteArray);
JsonTextReader jReader = new JsonTextReader(new StreamReader(stream));
JsonSerializer s = new JsonSerializer();
ExceptionAssert.Throws<JsonReaderException>(
@"Unterminated string. Expected delimiter: "". Path '', line 1, position 3.",
() =>
{
ErrorTestObject obj = s.Deserialize<ErrorTestObject>(jReader);
});
}
}
interface IErrorPerson2
@@ -777,4 +814,12 @@ namespace Newtonsoft.Json.Tests.Serialization
public string Code { get; set; }
public int Priority { get; set; }
}
public class ErrorTestObject
{
[OnError]
internal void OnError(StreamingContext context, ErrorContext errorContext)
{
}
}
}
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Bcl.Immutable" version="1.0.12-beta" targetFramework="net45" />
<package id="NUnit" version="2.6.2" targetFramework="net45" />
</packages>
+1 -1
View File
@@ -370,7 +370,7 @@ namespace Newtonsoft.Json
private void WriteEscapedString(string value)
{
EnsureWriteBuffer();
JavaScriptUtils.WriteEscapedJavaScriptString(_writer, value, _quoteChar, true, _charEscapeFlags, StringEscapeHandling, ref _writeBuffer);
JavaScriptUtils.WriteEscapedJavaScriptString(_writer, value, _quoteChar, _quoteName, _charEscapeFlags, StringEscapeHandling, ref _writeBuffer);
}
/// <summary>
@@ -92,6 +92,6 @@ using System.Security;
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")]
[assembly: AssemblyFileVersion("5.0.6.16317")]
[assembly: AssemblyFileVersion("5.0.6.16320")]
[assembly: CLSCompliant(true)]
@@ -128,7 +128,8 @@ namespace Newtonsoft.Json.Serialization
TraceWriter.Trace(TraceLevel.Error, message, ex);
}
if (contract != null)
// attribute method is non-static so don't invoke if no object
if (contract != null && currentObject != null)
contract.InvokeOnError(currentObject, Serializer.Context, errorContext);
if (!errorContext.Handled)