-Fixed error handling serializing invalid JSON in certain cases
This commit is contained in:
@@ -838,5 +838,19 @@ _____'propertyName': NaN
|
||||
|
||||
Assert.AreEqual(@"""2000-01-01T01:01:01Z""", sw.ToString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteEndOnProperty()
|
||||
{
|
||||
StringWriter sw = new StringWriter();
|
||||
JsonTextWriter writer = new JsonTextWriter(sw);
|
||||
writer.QuoteChar = '\'';
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("Blah");
|
||||
writer.WriteEnd();
|
||||
|
||||
Assert.AreEqual("{'Blah':null}", sw.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -72,5 +72,5 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("4.5.0.0")]
|
||||
#if !PocketPC
|
||||
[assembly: AssemblyFileVersion("4.5.8.15204")]
|
||||
[assembly: AssemblyFileVersion("4.5.8.15205")]
|
||||
#endif
|
||||
|
||||
@@ -27,6 +27,11 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Tests.TestObjects;
|
||||
#if NET20
|
||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||
#else
|
||||
using System.Linq;
|
||||
#endif
|
||||
#if !NETFX_CORE
|
||||
using NUnit.Framework;
|
||||
#else
|
||||
@@ -641,6 +646,61 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
Assert.AreEqual(@"Unexpected end when deserializing object. Path 'ChildObject.Integer', line 6, position 19.", errors[1]);
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void WriteEndOnPropertyState()
|
||||
{
|
||||
JsonSerializerSettings settings = new JsonSerializerSettings();
|
||||
settings.Error += (obj, args) =>
|
||||
{
|
||||
args.ErrorContext.Handled = true;
|
||||
};
|
||||
|
||||
var data = new List<ErrorPerson2>()
|
||||
{
|
||||
new ErrorPerson2{FirstName = "Scott", LastName = "Hanselman"},
|
||||
new ErrorPerson2{FirstName = "Scott", LastName = "Hunter"},
|
||||
new ErrorPerson2{FirstName = "Scott", LastName = "Guthrie"},
|
||||
};
|
||||
|
||||
var dictionary = data.GroupBy(person => person.FirstName).ToDictionary(group => @group.Key, group => @group.Cast<IErrorPerson2>());
|
||||
var output = JsonConvert.SerializeObject(dictionary, Formatting.None, settings);
|
||||
Assert.AreEqual(@"{""Scott"":null}", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteEndOnPropertyState2()
|
||||
{
|
||||
JsonSerializerSettings settings = new JsonSerializerSettings();
|
||||
settings.Error += (obj, args) =>
|
||||
{
|
||||
args.ErrorContext.Handled = true;
|
||||
};
|
||||
|
||||
var data = new List<ErrorPerson2>
|
||||
{
|
||||
new ErrorPerson2 {FirstName = "Scott", LastName = "Hanselman"},
|
||||
new ErrorPerson2 {FirstName = "Scott", LastName = "Hunter"},
|
||||
new ErrorPerson2 {FirstName = "Scott", LastName = "Guthrie"},
|
||||
new ErrorPerson2 {FirstName = "James", LastName = "Newton-King"},
|
||||
};
|
||||
|
||||
var dictionary = data.GroupBy(person => person.FirstName).ToDictionary(group => @group.Key, group => @group.Cast<IErrorPerson2>());
|
||||
var output = JsonConvert.SerializeObject(dictionary, Formatting.None, settings);
|
||||
|
||||
Assert.AreEqual(@"{""Scott"":null,""James"":null}", output);
|
||||
}
|
||||
}
|
||||
|
||||
interface IErrorPerson2
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
class ErrorPerson2//:IPerson - oops! Forgot to implement the person interface
|
||||
{
|
||||
public string LastName { get; set; }
|
||||
public string FirstName { get; set; }
|
||||
}
|
||||
|
||||
public class ThrowingReader : TextReader
|
||||
|
||||
@@ -582,7 +582,6 @@ namespace Newtonsoft.Json
|
||||
private void AutoCompleteClose(JsonToken tokenBeingClosed)
|
||||
{
|
||||
// write closing symbol and calculate new state
|
||||
|
||||
int levelsToComplete = 0;
|
||||
JsonContainerType type = GetTypeForCloseToken(tokenBeingClosed);
|
||||
|
||||
@@ -612,6 +611,9 @@ namespace Newtonsoft.Json
|
||||
{
|
||||
JsonToken token = GetCloseTokenForType(Pop());
|
||||
|
||||
if (_currentState == State.Property)
|
||||
WriteNull();
|
||||
|
||||
if (_formatting == Formatting.Indented)
|
||||
{
|
||||
if (_currentState != State.ObjectStart && _currentState != State.ArrayStart)
|
||||
|
||||
@@ -91,7 +91,7 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("4.5.0.0")]
|
||||
#if !PocketPC
|
||||
[assembly: AssemblyFileVersion("4.5.8.15204")]
|
||||
[assembly: AssemblyFileVersion("4.5.8.15205")]
|
||||
#endif
|
||||
|
||||
[assembly: CLSCompliant(true)]
|
||||
|
||||
@@ -804,6 +804,9 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
{
|
||||
ClearErrorContext();
|
||||
|
||||
if (writer.WriteState == WriteState.Property)
|
||||
writer.WriteNull();
|
||||
|
||||
while (writer.Top > initialDepth)
|
||||
{
|
||||
writer.WriteEnd();
|
||||
|
||||
Reference in New Issue
Block a user