-Documentation
-Added Path to JsonWriterException
This commit is contained in:
@@ -378,7 +378,7 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
[Test]
|
||||
public void WriteComment()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonWriterException>("Cannot write JSON comment as BSON.",
|
||||
ExceptionAssert.Throws<JsonWriterException>("Cannot write JSON comment as BSON. Path ''.",
|
||||
() =>
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
@@ -392,7 +392,7 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
[Test]
|
||||
public void WriteConstructor()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonWriterException>("Cannot write JSON constructor as BSON.",
|
||||
ExceptionAssert.Throws<JsonWriterException>("Cannot write JSON constructor as BSON. Path ''.",
|
||||
() =>
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
@@ -406,7 +406,7 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
[Test]
|
||||
public void WriteRaw()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonWriterException>("Cannot write raw JSON as BSON.",
|
||||
ExceptionAssert.Throws<JsonWriterException>("Cannot write raw JSON as BSON. Path ''.",
|
||||
() =>
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
@@ -420,7 +420,7 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
[Test]
|
||||
public void WriteRawValue()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonWriterException>("Cannot write raw JSON as BSON.",
|
||||
ExceptionAssert.Throws<JsonWriterException>("Cannot write raw JSON as BSON. Path ''.",
|
||||
() =>
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
@@ -652,7 +652,7 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
[Test]
|
||||
public void WriteValueOutsideOfObjectOrArray()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonWriterException>("Error writing String value. BSON must start with an Object or Array.",
|
||||
ExceptionAssert.Throws<JsonWriterException>("Error writing String value. BSON must start with an Object or Array. Path ''.",
|
||||
() =>
|
||||
{
|
||||
MemoryStream stream = new MemoryStream();
|
||||
|
||||
@@ -174,8 +174,8 @@ namespace Newtonsoft.Json.Tests
|
||||
[Test]
|
||||
public void WriteValueObjectWithUnsupportedValue()
|
||||
{
|
||||
ExceptionAssert.Throws<ArgumentException>(
|
||||
@"Unsupported type: System.Version. Use the JsonSerializer class to get the object's JSON representation.",
|
||||
ExceptionAssert.Throws<JsonWriterException>(
|
||||
@"Unsupported type: System.Version. Use the JsonSerializer class to get the object's JSON representation. Path ''.",
|
||||
() =>
|
||||
{
|
||||
StringWriter sw = new StringWriter();
|
||||
@@ -597,7 +597,7 @@ namespace Newtonsoft.Json.Tests
|
||||
public void BadWriteEndArray()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonWriterException>(
|
||||
"No token to close.",
|
||||
"No token to close. Path ''.",
|
||||
() =>
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
@@ -93,6 +93,7 @@ namespace Newtonsoft.Json.Tests
|
||||
JsonNet,
|
||||
JsonNetWithIsoConverter,
|
||||
JsonNetBinary,
|
||||
JsonNetLinq,
|
||||
BinaryFormatter,
|
||||
JavaScriptSerializer,
|
||||
DataContractSerializer,
|
||||
@@ -130,6 +131,7 @@ namespace Newtonsoft.Json.Tests
|
||||
BenchmarkSerializeMethod(SerializeMethod.JavaScriptSerializer, value);
|
||||
BenchmarkSerializeMethod(SerializeMethod.DataContractJsonSerializer, value);
|
||||
BenchmarkSerializeMethod(SerializeMethod.JsonNet, value);
|
||||
BenchmarkSerializeMethod(SerializeMethod.JsonNetLinq, value);
|
||||
BenchmarkSerializeMethod(SerializeMethod.JsonNetWithIsoConverter, value);
|
||||
BenchmarkSerializeMethod(SerializeMethod.JsonNetBinary, value);
|
||||
}
|
||||
@@ -428,33 +430,6 @@ namespace Newtonsoft.Json.Tests
|
||||
};
|
||||
}
|
||||
|
||||
public string SerializeJsonNet(object value)
|
||||
{
|
||||
Type type = value.GetType();
|
||||
|
||||
Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();
|
||||
|
||||
json.NullValueHandling = NullValueHandling.Ignore;
|
||||
|
||||
json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
|
||||
json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
|
||||
json.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
||||
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
Newtonsoft.Json.JsonTextWriter writer = new JsonTextWriter(sw);
|
||||
|
||||
writer.Formatting = Formatting.None;
|
||||
|
||||
writer.QuoteChar = '"';
|
||||
json.Serialize(writer, value);
|
||||
|
||||
string output = sw.ToString();
|
||||
writer.Close();
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
public string SerializeWebExtensions(object value)
|
||||
{
|
||||
JavaScriptSerializer ser = new JavaScriptSerializer();
|
||||
@@ -506,6 +481,38 @@ namespace Newtonsoft.Json.Tests
|
||||
case SerializeMethod.JsonNetWithIsoConverter:
|
||||
json = JsonConvert.SerializeObject(value, new IsoDateTimeConverter());
|
||||
break;
|
||||
case SerializeMethod.JsonNetLinq:
|
||||
{
|
||||
TestClass c = value as TestClass;
|
||||
if (c != null)
|
||||
{
|
||||
JObject o = new JObject(
|
||||
new JProperty("strings", new JArray(
|
||||
c.strings
|
||||
)),
|
||||
new JProperty("dictionary", new JObject(c.dictionary.Select(d => new JProperty(d.Key, d.Value)))),
|
||||
new JProperty("Name", c.Name),
|
||||
new JProperty("Now", c.Now),
|
||||
new JProperty("BigNumber", c.BigNumber),
|
||||
new JProperty("Address1", new JObject(
|
||||
new JProperty("Street", c.Address1.Street),
|
||||
new JProperty("Phone", c.Address1.Phone),
|
||||
new JProperty("Entered", c.Address1.Entered))),
|
||||
new JProperty("Addresses", new JArray(c.Addresses.Select(a =>
|
||||
new JObject(
|
||||
new JProperty("Street", a.Street),
|
||||
new JProperty("Phone", a.Phone),
|
||||
new JProperty("Entered", a.Entered)))))
|
||||
);
|
||||
|
||||
json = o.ToString(Formatting.None);
|
||||
}
|
||||
else
|
||||
{
|
||||
json = string.Empty;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SerializeMethod.JsonNetBinary:
|
||||
{
|
||||
MemoryStream ms = new MemoryStream(Buffer);
|
||||
|
||||
@@ -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.4.14906")]
|
||||
[assembly: AssemblyFileVersion("4.5.4.14907")]
|
||||
#endif
|
||||
|
||||
@@ -1197,7 +1197,7 @@ keyword such as type of business.""
|
||||
public void SerializeRequiredMembersClassNullRequiredValueProperty()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Cannot write a null value for property 'FirstName'. Property requires a value.",
|
||||
"Cannot write a null value for property 'FirstName'. Property requires a value. Path ''.",
|
||||
() =>
|
||||
{
|
||||
RequiredMembersClass requiredMembersClass = new RequiredMembersClass
|
||||
@@ -1480,7 +1480,7 @@ keyword such as type of business.""
|
||||
string classRef = typeof(JsonPropertyWithHandlingValues).FullName;
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Self referencing loop detected for type '" + classRef + "'.",
|
||||
"Self referencing loop detected for property 'ReferenceLoopHandlingErrorProperty' with type '" + classRef + "'. Path ''.",
|
||||
() =>
|
||||
{
|
||||
JsonPropertyWithHandlingValues o = new JsonPropertyWithHandlingValues();
|
||||
@@ -2948,7 +2948,7 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Type 'Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+ISerializableTestObject' implements ISerializable but cannot be serialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data.
|
||||
To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add to JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true.",
|
||||
To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add to JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true. Path ''.",
|
||||
() =>
|
||||
{
|
||||
JsonTypeReflector.SetFullyTrusted(false);
|
||||
@@ -6083,9 +6083,9 @@ Parameter name: value",
|
||||
}", json);
|
||||
|
||||
Assert.AreEqual(3, errors.Count);
|
||||
Assert.AreEqual("Cannot write a null value for property 'NonAttributeProperty'. Property requires a value.", errors[0]);
|
||||
Assert.AreEqual("Cannot write a null value for property 'UnsetProperty'. Property requires a value.", errors[1]);
|
||||
Assert.AreEqual("Cannot write a null value for property 'AlwaysProperty'. Property requires a value.", errors[2]);
|
||||
Assert.AreEqual("Cannot write a null value for property 'NonAttributeProperty'. Property requires a value. Path ''.", errors[0]);
|
||||
Assert.AreEqual("Cannot write a null value for property 'UnsetProperty'. Property requires a value. Path ''.", errors[1]);
|
||||
Assert.AreEqual("Cannot write a null value for property 'AlwaysProperty'. Property requires a value. Path ''.", errors[2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -6133,8 +6133,78 @@ Parameter name: value",
|
||||
string s = (string) new JsonSerializer().Deserialize(new JsonTextReader(new StringReader("''")));
|
||||
Assert.AreEqual("", s);
|
||||
}
|
||||
|
||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||
[Test]
|
||||
public void SerializeAndDeserializeWithAttributes()
|
||||
{
|
||||
var testObj = new PersonSerializable() { Name = "John Doe", Age = 28 };
|
||||
var objDeserialized = this.SerializeAndDeserialize<PersonSerializable>(testObj);
|
||||
|
||||
Assert.AreEqual(testObj.Name, objDeserialized.Name);
|
||||
Assert.AreEqual(0, objDeserialized.Age);
|
||||
}
|
||||
|
||||
private T SerializeAndDeserialize<T>(T obj)
|
||||
where T : class
|
||||
{
|
||||
var json = Serialize(obj);
|
||||
return Deserialize<T>(json);
|
||||
}
|
||||
|
||||
private string Serialize<T>(T obj)
|
||||
where T : class
|
||||
{
|
||||
var stringWriter = new StringWriter();
|
||||
var serializer = new Newtonsoft.Json.JsonSerializer();
|
||||
serializer.ContractResolver = new DefaultContractResolver(false)
|
||||
{
|
||||
IgnoreSerializableAttribute = false
|
||||
};
|
||||
serializer.Serialize(stringWriter, obj);
|
||||
|
||||
return stringWriter.ToString();
|
||||
}
|
||||
|
||||
private T Deserialize<T>(string json)
|
||||
where T : class
|
||||
{
|
||||
var jsonReader = new Newtonsoft.Json.JsonTextReader(new StringReader(json));
|
||||
var serializer = new Newtonsoft.Json.JsonSerializer();
|
||||
serializer.ContractResolver = new DefaultContractResolver(false)
|
||||
{
|
||||
IgnoreSerializableAttribute = false
|
||||
};
|
||||
|
||||
return serializer.Deserialize(jsonReader, typeof(T)) as T;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||
[Serializable]
|
||||
public class PersonSerializable
|
||||
{
|
||||
public PersonSerializable()
|
||||
{ }
|
||||
|
||||
private string _name = "";
|
||||
public string Name
|
||||
{
|
||||
get { return _name; }
|
||||
set { _name = value; }
|
||||
}
|
||||
|
||||
[NonSerialized]
|
||||
private int _age = 0;
|
||||
public int Age
|
||||
{
|
||||
get { return _age; }
|
||||
set { _age = value; }
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
public class PropertyItemConverter
|
||||
{
|
||||
[JsonProperty(ItemConverterType = typeof(MetroStringConverter))]
|
||||
|
||||
@@ -104,7 +104,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
circularList.Add(new CircularList { new CircularList { circularList } });
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Self referencing loop detected for type '" + classRef + "'.",
|
||||
"Self referencing loop detected with type '" + classRef + "'. Path '[2][0]'.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.SerializeObject(circularList, Formatting.Indented);
|
||||
@@ -264,7 +264,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
circularDictionary.Add("self", circularDictionary);
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
@"Self referencing loop detected for type '" + classRef + "'.",
|
||||
@"Self referencing loop detected with type '" + classRef + "'. Path ''.",
|
||||
() =>
|
||||
{
|
||||
JsonConvert.SerializeObject(circularDictionary, Formatting.Indented);
|
||||
|
||||
Reference in New Issue
Block a user