-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());
|
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:
|
// by using the '*' as shown below:
|
||||||
[assembly: AssemblyVersion("4.5.0.0")]
|
[assembly: AssemblyVersion("4.5.0.0")]
|
||||||
#if !PocketPC
|
#if !PocketPC
|
||||||
[assembly: AssemblyFileVersion("4.5.8.15204")]
|
[assembly: AssemblyFileVersion("4.5.8.15205")]
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -27,6 +27,11 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Newtonsoft.Json.Converters;
|
using Newtonsoft.Json.Converters;
|
||||||
using Newtonsoft.Json.Tests.TestObjects;
|
using Newtonsoft.Json.Tests.TestObjects;
|
||||||
|
#if NET20
|
||||||
|
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||||
|
#else
|
||||||
|
using System.Linq;
|
||||||
|
#endif
|
||||||
#if !NETFX_CORE
|
#if !NETFX_CORE
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
#else
|
#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]);
|
Assert.AreEqual(@"Unexpected end when deserializing object. Path 'ChildObject.Integer', line 6, position 19.", errors[1]);
|
||||||
}
|
}
|
||||||
#endif
|
#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
|
public class ThrowingReader : TextReader
|
||||||
|
|||||||
@@ -582,7 +582,6 @@ namespace Newtonsoft.Json
|
|||||||
private void AutoCompleteClose(JsonToken tokenBeingClosed)
|
private void AutoCompleteClose(JsonToken tokenBeingClosed)
|
||||||
{
|
{
|
||||||
// write closing symbol and calculate new state
|
// write closing symbol and calculate new state
|
||||||
|
|
||||||
int levelsToComplete = 0;
|
int levelsToComplete = 0;
|
||||||
JsonContainerType type = GetTypeForCloseToken(tokenBeingClosed);
|
JsonContainerType type = GetTypeForCloseToken(tokenBeingClosed);
|
||||||
|
|
||||||
@@ -612,6 +611,9 @@ namespace Newtonsoft.Json
|
|||||||
{
|
{
|
||||||
JsonToken token = GetCloseTokenForType(Pop());
|
JsonToken token = GetCloseTokenForType(Pop());
|
||||||
|
|
||||||
|
if (_currentState == State.Property)
|
||||||
|
WriteNull();
|
||||||
|
|
||||||
if (_formatting == Formatting.Indented)
|
if (_formatting == Formatting.Indented)
|
||||||
{
|
{
|
||||||
if (_currentState != State.ObjectStart && _currentState != State.ArrayStart)
|
if (_currentState != State.ObjectStart && _currentState != State.ArrayStart)
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ using System.Security;
|
|||||||
// by using the '*' as shown below:
|
// by using the '*' as shown below:
|
||||||
[assembly: AssemblyVersion("4.5.0.0")]
|
[assembly: AssemblyVersion("4.5.0.0")]
|
||||||
#if !PocketPC
|
#if !PocketPC
|
||||||
[assembly: AssemblyFileVersion("4.5.8.15204")]
|
[assembly: AssemblyFileVersion("4.5.8.15205")]
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
[assembly: CLSCompliant(true)]
|
[assembly: CLSCompliant(true)]
|
||||||
|
|||||||
@@ -804,6 +804,9 @@ To fix this error either change the environment to be fully trusted, change the
|
|||||||
{
|
{
|
||||||
ClearErrorContext();
|
ClearErrorContext();
|
||||||
|
|
||||||
|
if (writer.WriteState == WriteState.Property)
|
||||||
|
writer.WriteNull();
|
||||||
|
|
||||||
while (writer.Top > initialDepth)
|
while (writer.Top > initialDepth)
|
||||||
{
|
{
|
||||||
writer.WriteEnd();
|
writer.WriteEnd();
|
||||||
|
|||||||
Reference in New Issue
Block a user