-Fix deserializing empty BSON structures
-Fix error message when attempting to deserialize custom dictionary with no default constructor -Fix serializing private base members marked with JsonProperty attribute -Fix performance issue when deserializing to object member
This commit is contained in:
@@ -596,5 +596,55 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
|
||||
Assert.IsFalse(reader.Read());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteAndReadEmptyListsAndDictionaries()
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
BsonWriter writer = new BsonWriter(ms);
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("Arguments");
|
||||
writer.WriteStartObject();
|
||||
writer.WriteEndObject();
|
||||
writer.WritePropertyName("List");
|
||||
writer.WriteStartArray();
|
||||
writer.WriteEndArray();
|
||||
writer.WriteEndObject();
|
||||
|
||||
string bson = BitConverter.ToString(ms.ToArray());
|
||||
|
||||
Assert.AreEqual("20-00-00-00-03-41-72-67-75-6D-65-6E-74-73-00-05-00-00-00-00-04-4C-69-73-74-00-05-00-00-00-00-00", bson);
|
||||
|
||||
BsonReader reader = new BsonReader(new MemoryStream(MiscellaneousUtils.HexToBytes(bson)));
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
|
||||
Assert.AreEqual("Arguments", reader.Value.ToString());
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
|
||||
Assert.AreEqual("List", reader.Value.ToString());
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
|
||||
|
||||
Assert.IsFalse(reader.Read());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -647,5 +647,29 @@ bye", reader.Value);
|
||||
|
||||
Assert.IsFalse(jsonReader.Read());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadUnicode()
|
||||
{
|
||||
string json = @"{""Message"":""Hi,I\u0092ve send you smth""}";
|
||||
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(json));
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
|
||||
Assert.AreEqual("Message", reader.Value);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.String, reader.TokenType);
|
||||
Assert.AreEqual(@"Hi,I" + '\u0092' + "ve send you smth", reader.Value);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
|
||||
|
||||
Assert.IsFalse(reader.Read());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2232,5 +2232,77 @@ keyword such as type of business.""
|
||||
deserialized = JsonConvert.DeserializeObject<JRawValueTestObject>("{value:'3'}");
|
||||
Assert.AreEqual(@"""3""", deserialized.Value.ToString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Unable to find a default constructor to use for type Newtonsoft.Json.Tests.Serialization.JsonSerializerTest+DictionaryWithNoDefaultConstructor.")]
|
||||
public void DeserializeDictionaryWithNoDefaultConstructor()
|
||||
{
|
||||
string json = "{key1:'value',key2:'value',key3:'value'}";
|
||||
JsonConvert.DeserializeObject<DictionaryWithNoDefaultConstructor>(json);
|
||||
}
|
||||
|
||||
public class DictionaryWithNoDefaultConstructor : Dictionary<string, string>
|
||||
{
|
||||
public DictionaryWithNoDefaultConstructor(IEnumerable<KeyValuePair<string, string>> initial)
|
||||
{
|
||||
foreach (KeyValuePair<string, string> pair in initial)
|
||||
{
|
||||
Add(pair.Key, pair.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[JsonObject(MemberSerialization.OptIn)]
|
||||
public class A
|
||||
{
|
||||
[JsonProperty("A1")]
|
||||
private string _A1;
|
||||
public string A1 { get { return _A1; } set { _A1 = value; } }
|
||||
|
||||
[JsonProperty("A2")]
|
||||
private string A2 { get; set; }
|
||||
}
|
||||
|
||||
[JsonObject(MemberSerialization.OptIn)]
|
||||
public class B : A
|
||||
{
|
||||
public string B1 { get; set; }
|
||||
|
||||
[JsonProperty("B2")]
|
||||
string _B2;
|
||||
public string B2 { get { return _B2; } set { _B2 = value; } }
|
||||
|
||||
[JsonProperty("B3")]
|
||||
private string B3 { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeNonPublicBaseJsonProperties()
|
||||
{
|
||||
B value = new B();
|
||||
string json = JsonConvert.SerializeObject(value, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""B2"": null,
|
||||
""A1"": null,
|
||||
""B3"": null,
|
||||
""A2"": null
|
||||
}", json);
|
||||
}
|
||||
|
||||
public class TestClass
|
||||
{
|
||||
public string Key { get; set; }
|
||||
public object Value { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeToObjectProperty()
|
||||
{
|
||||
var json = "{ Key: 'abc', Value: 123 }";
|
||||
var item = JsonConvert.DeserializeObject<TestClass>(json);
|
||||
|
||||
Assert.AreEqual(123, item.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user