Merge branch 'master' of https://github.com/JamesNK/Newtonsoft.Json
This commit is contained in:
@@ -882,6 +882,63 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
|
||||
Assert.IsNull(reader.ReadAsBytes());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteEndOnProperty()
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
BsonWriter writer = new BsonWriter(ms);
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("Blah");
|
||||
writer.WriteEnd();
|
||||
|
||||
Assert.AreEqual("0B-00-00-00-0A-42-6C-61-68-00-00", (BitConverter.ToString(ms.ToArray())));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteEndOnProperty_Close()
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
BsonWriter writer = new BsonWriter(ms);
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("Blah");
|
||||
writer.Close();
|
||||
|
||||
Assert.AreEqual("0B-00-00-00-0A-42-6C-61-68-00-00", (BitConverter.ToString(ms.ToArray())));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteEndOnProperty_Dispose()
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
using (BsonWriter writer = new BsonWriter(ms))
|
||||
{
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("Blah");
|
||||
}
|
||||
|
||||
Assert.AreEqual("0B-00-00-00-0A-42-6C-61-68-00-00", (BitConverter.ToString(ms.ToArray())));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AutoCompleteOnClose_False()
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
using (BsonWriter writer = new BsonWriter(ms))
|
||||
{
|
||||
writer.AutoCompleteOnClose = false;
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("Blah");
|
||||
|
||||
writer.Flush();
|
||||
}
|
||||
|
||||
// nothing is written because a BSON document needs to be completed before it can be written
|
||||
Assert.AreEqual(string.Empty, (BitConverter.ToString(ms.ToArray())));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -2913,6 +2913,47 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
var deserialized = JsonConvert.DeserializeObject<XDocument>(json);
|
||||
Assert.AreEqual(@"<MyElement xmlns=""http://example.com"" />", deserialized.ToString());
|
||||
}
|
||||
|
||||
public class Model
|
||||
{
|
||||
public XElement Document { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeDateInElementText()
|
||||
{
|
||||
Model model = new Model();
|
||||
model.Document = new XElement("Value", new XAttribute("foo", "bar"))
|
||||
{
|
||||
Value = "2001-01-01T11:11:11"
|
||||
};
|
||||
|
||||
var serializer = JsonSerializer.Create(new JsonSerializerSettings
|
||||
{
|
||||
Converters = new List<JsonConverter>(new[] { new XmlNodeConverter() })
|
||||
});
|
||||
|
||||
var json = new StringBuilder(1024);
|
||||
|
||||
using (var stringWriter = new StringWriter(json, CultureInfo.InvariantCulture))
|
||||
using (var jsonWriter = new JsonTextWriter(stringWriter))
|
||||
{
|
||||
jsonWriter.Formatting = Formatting.None;
|
||||
serializer.Serialize(jsonWriter, model);
|
||||
|
||||
Assert.AreEqual(@"{""Document"":{""Value"":{""@foo"":""bar"",""#text"":""2001-01-01T11:11:11""}}}", json.ToString());
|
||||
}
|
||||
|
||||
using (var stringReader = new StringReader(json.ToString()))
|
||||
using (var jsonReader = new JsonTextReader(stringReader))
|
||||
{
|
||||
var document = (XDocument)serializer.Deserialize(jsonReader, typeof(XDocument));
|
||||
|
||||
StringAssert.AreEqual(@"<Document>
|
||||
<Value foo=""bar"">2001-01-01T11:11:11</Value>
|
||||
</Document>", document.ToString());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1310,6 +1310,51 @@ _____'propertyName': NaN,
|
||||
Assert.AreEqual("{'Blah':null}", sw.ToString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteEndOnProperty_Close()
|
||||
{
|
||||
StringWriter sw = new StringWriter();
|
||||
JsonTextWriter writer = new JsonTextWriter(sw);
|
||||
writer.QuoteChar = '\'';
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("Blah");
|
||||
writer.Close();
|
||||
|
||||
Assert.AreEqual("{'Blah':null}", sw.ToString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteEndOnProperty_Dispose()
|
||||
{
|
||||
StringWriter sw = new StringWriter();
|
||||
using (JsonTextWriter writer = new JsonTextWriter(sw))
|
||||
{
|
||||
writer.QuoteChar = '\'';
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("Blah");
|
||||
}
|
||||
|
||||
Assert.AreEqual("{'Blah':null}", sw.ToString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AutoCompleteOnClose_False()
|
||||
{
|
||||
StringWriter sw = new StringWriter();
|
||||
using (JsonTextWriter writer = new JsonTextWriter(sw))
|
||||
{
|
||||
writer.AutoCompleteOnClose = false;
|
||||
writer.QuoteChar = '\'';
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("Blah");
|
||||
}
|
||||
|
||||
Assert.AreEqual("{'Blah':", sw.ToString());
|
||||
}
|
||||
|
||||
#if !NET20
|
||||
[Test]
|
||||
public void QuoteChar()
|
||||
|
||||
@@ -556,6 +556,31 @@ Parameter name: index");
|
||||
Assert.AreEqual(3, (int)a[2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_ExcessiveContentJustComments()
|
||||
{
|
||||
string json = @"[1,2,3]/*comment*/
|
||||
//Another comment.";
|
||||
|
||||
JArray a = JArray.Parse(json);
|
||||
|
||||
Assert.AreEqual(3, a.Count);
|
||||
Assert.AreEqual(1, (int)a[0]);
|
||||
Assert.AreEqual(2, (int)a[1]);
|
||||
Assert.AreEqual(3, (int)a[2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_ExcessiveContent()
|
||||
{
|
||||
string json = @"[1,2,3]/*comment*/
|
||||
//Another comment.
|
||||
[]";
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(() => JArray.Parse(json),
|
||||
"Additional text encountered after finished reading JSON content: [. Path '', line 3, position 0.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_LineInfo()
|
||||
{
|
||||
|
||||
@@ -1994,5 +1994,30 @@ Parameter name: arrayIndex");
|
||||
Assert.AreEqual(2, (int)o["prop"][1]);
|
||||
Assert.AreEqual(3, (int)o["prop"][2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_ExcessiveContentJustComments()
|
||||
{
|
||||
string json = @"{'prop':[1,2,3]}/*comment*/
|
||||
//Another comment.";
|
||||
|
||||
JObject o = JObject.Parse(json);
|
||||
|
||||
Assert.AreEqual(3, o["prop"].Count());
|
||||
Assert.AreEqual(1, (int)o["prop"][0]);
|
||||
Assert.AreEqual(2, (int)o["prop"][1]);
|
||||
Assert.AreEqual(3, (int)o["prop"][2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_ExcessiveContent()
|
||||
{
|
||||
string json = @"{'prop':[1,2,3]}/*comment*/
|
||||
//Another comment.
|
||||
[]";
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(() => JObject.Parse(json),
|
||||
"Additional text encountered after finished reading JSON content: [. Path '', line 3, position 0.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1264,5 +1264,30 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(2, (int)o["prop"][1]);
|
||||
Assert.AreEqual(3, (int)o["prop"][2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_ExcessiveContentJustComments()
|
||||
{
|
||||
string json = @"{'prop':[1,2,3]}/*comment*/
|
||||
//Another comment.";
|
||||
|
||||
JToken o = JToken.Parse(json);
|
||||
|
||||
Assert.AreEqual(3, o["prop"].Count());
|
||||
Assert.AreEqual(1, (int)o["prop"][0]);
|
||||
Assert.AreEqual(2, (int)o["prop"][1]);
|
||||
Assert.AreEqual(3, (int)o["prop"][2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_ExcessiveContent()
|
||||
{
|
||||
string json = @"{'prop':[1,2,3]}/*comment*/
|
||||
//Another comment.
|
||||
{}";
|
||||
|
||||
ExceptionAssert.Throws<JsonReaderException>(() => JToken.Parse(json),
|
||||
"Additional text encountered after finished reading JSON content: {. Path '', line 3, position 0.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -274,7 +274,7 @@ namespace Newtonsoft.Json.Tests
|
||||
{
|
||||
var json = System.IO.File.ReadAllText("large.json");
|
||||
|
||||
BenchmarkDeserializeMethod<IList<RootObject>>(SerializeMethod.JsonNet, json, Iterations / 10, false);
|
||||
BenchmarkDeserializeMethod<IList<RootObject>>(SerializeMethod.JsonNet, json, Math.Min(Iterations, 10), false);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -8253,7 +8253,52 @@ lines.*/
|
||||
reader.Read();
|
||||
|
||||
serializer.Deserialize(reader, typeof(MyType));
|
||||
}, "Additional text found in JSON string after finishing deserializing object.");
|
||||
}, "Additional text found in JSON string after finishing deserializing object. Path '[1]', line 1, position 5.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AdditionalContentAfterFinishCheckNotRequested()
|
||||
{
|
||||
string json = @"{ ""MyProperty"":{""Key"":""Value""}} A bunch of junk at the end of the json";
|
||||
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
|
||||
var reader = new JsonTextReader(new StringReader(json));
|
||||
|
||||
MyType mt = (MyType)serializer.Deserialize(reader, typeof(MyType));
|
||||
Assert.AreEqual(1, mt.MyProperty.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AdditionalContentAfterCommentsCheckNotRequested()
|
||||
{
|
||||
string json = @"{ ""MyProperty"":{""Key"":""Value""}} /*this is a comment */
|
||||
// this is also a comment
|
||||
This is just junk, though.";
|
||||
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
|
||||
var reader = new JsonTextReader(new StringReader(json));
|
||||
|
||||
MyType mt = (MyType)serializer.Deserialize(reader, typeof(MyType));
|
||||
Assert.AreEqual(1, mt.MyProperty.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AdditionalContentAfterComments()
|
||||
{
|
||||
string json = @"[{ ""MyProperty"":{""Key"":""Value""}} /*this is a comment */
|
||||
// this is also a comment
|
||||
,{}";
|
||||
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
serializer.CheckAdditionalContent = true;
|
||||
var reader = new JsonTextReader(new StringReader(json));
|
||||
reader.Read();
|
||||
reader.Read();
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(() => serializer.Deserialize(reader, typeof(MyType)),
|
||||
"Additional text found in JSON string after finishing deserializing object. Path '[1]', line 3, position 2.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -9044,6 +9089,119 @@ lines.*/
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !(PORTABLE || PORTABLE40 || DNXCORE50) || NETSTANDARD1_0 || NETSTANDARD1_1
|
||||
[Test]
|
||||
public void SerializeDictionaryWithStructKey_Custom()
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(
|
||||
new Dictionary<TypeConverterSize, TypeConverterSize> { { new TypeConverterSize(1, 2), new TypeConverterSize(3, 4) } }
|
||||
);
|
||||
|
||||
Assert.AreEqual(@"{""1, 2"":""3, 4""}", json);
|
||||
|
||||
Dictionary<TypeConverterSize, TypeConverterSize> d = JsonConvert.DeserializeObject<Dictionary<TypeConverterSize, TypeConverterSize>>(json);
|
||||
|
||||
Assert.AreEqual(new TypeConverterSize(1, 2), d.Keys.First());
|
||||
Assert.AreEqual(new TypeConverterSize(3, 4), d.Values.First());
|
||||
}
|
||||
|
||||
[TypeConverter(typeof(TypeConverterSizeConverter))]
|
||||
public struct TypeConverterSize
|
||||
{
|
||||
public static readonly TypeConverterSize Empty;
|
||||
private int _width;
|
||||
private int _height;
|
||||
|
||||
public TypeConverterSize(int width, int height)
|
||||
{
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
|
||||
public int Width
|
||||
{
|
||||
get { return _width; }
|
||||
set { _width = value; }
|
||||
}
|
||||
|
||||
public int Height
|
||||
{
|
||||
get { return _height; }
|
||||
set { _height = value; }
|
||||
}
|
||||
}
|
||||
|
||||
public class TypeConverterSizeConverter : TypeConverter
|
||||
{
|
||||
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
|
||||
{
|
||||
return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
|
||||
}
|
||||
|
||||
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
|
||||
{
|
||||
return base.CanConvertTo(context, destinationType);
|
||||
}
|
||||
|
||||
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
|
||||
{
|
||||
string str = value as string;
|
||||
if (str == null)
|
||||
{
|
||||
return base.ConvertFrom(context, culture, value);
|
||||
}
|
||||
string str2 = str.Trim();
|
||||
if (str2.Length == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (culture == null)
|
||||
{
|
||||
culture = CultureInfo.CurrentCulture;
|
||||
}
|
||||
string[] strArray = str2.Split(',');
|
||||
int[] numArray = new int[strArray.Length];
|
||||
TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
|
||||
for (int i = 0; i < numArray.Length; i++)
|
||||
{
|
||||
numArray[i] = (int)converter.ConvertFromString(context, culture, strArray[i]);
|
||||
}
|
||||
if (numArray.Length == 2)
|
||||
{
|
||||
return new TypeConverterSize(numArray[0], numArray[1]);
|
||||
}
|
||||
|
||||
throw new ArgumentException("Bad format.");
|
||||
}
|
||||
|
||||
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
|
||||
{
|
||||
if (destinationType == null)
|
||||
{
|
||||
throw new ArgumentNullException("destinationType");
|
||||
}
|
||||
if (value is TypeConverterSize)
|
||||
{
|
||||
if (destinationType == typeof(string))
|
||||
{
|
||||
TypeConverterSize size = (TypeConverterSize)value;
|
||||
if (culture == null)
|
||||
{
|
||||
culture = CultureInfo.CurrentCulture;
|
||||
}
|
||||
TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
|
||||
string[] strArray = new string[2];
|
||||
int num = 0;
|
||||
strArray[num++] = converter.ConvertToString(context, culture, size.Width);
|
||||
strArray[num++] = converter.ConvertToString(context, culture, size.Height);
|
||||
return string.Join(", ", strArray);
|
||||
}
|
||||
}
|
||||
return base.ConvertTo(context, culture, value, destinationType);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void DeserializeCustomReferenceResolver()
|
||||
{
|
||||
|
||||
@@ -95,6 +95,7 @@
|
||||
"System.Xml.XmlSerializer": "4.0.11",
|
||||
"System.Runtime.Serialization.Json": "4.0.2",
|
||||
"System.Runtime.Serialization.Xml": "4.1.1",
|
||||
"System.ComponentModel.TypeConverter": "4.1.0",
|
||||
"xunit": "2.1.0",
|
||||
"dotnet-test-xunit": "2.2.0-preview2-build1029"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user