-Fixed flag enum items not being correctly camel cased
-Fixed JsonValidatingReader not validating the first array item twice -Fixed JsonSchema not correctly validating integers as a subset of decimal -Changed JsonTextReader.Culture to be CultureInfo.InvariantCulture by default
This commit is contained in:
@@ -23,6 +23,14 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
public T Enum { get; set; }
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum FlagsTestEnum
|
||||
{
|
||||
Default = 0,
|
||||
First = 1,
|
||||
Second = 2
|
||||
}
|
||||
|
||||
public enum NegativeEnum
|
||||
{
|
||||
Negative = -1,
|
||||
@@ -256,5 +264,30 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
Assert.AreEqual((StoreColor)1000, enumClass.NullableStoreColor1);
|
||||
Assert.AreEqual(null, enumClass.NullableStoreColor2);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CamelCaseTextFlagEnumSerialization()
|
||||
{
|
||||
EnumContainer<FlagsTestEnum> c = new EnumContainer<FlagsTestEnum>
|
||||
{
|
||||
Enum = FlagsTestEnum.First | FlagsTestEnum.Second
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(c, Formatting.Indented, new StringEnumConverter { CamelCaseText = true });
|
||||
Assert.AreEqual(@"{
|
||||
""Enum"": ""first, second""
|
||||
}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CamelCaseTextFlagEnumDeserialization()
|
||||
{
|
||||
string json = @"{
|
||||
""Enum"": ""first, second""
|
||||
}";
|
||||
|
||||
EnumContainer<FlagsTestEnum> c = JsonConvert.DeserializeObject<EnumContainer<FlagsTestEnum>>(json, new StringEnumConverter { CamelCaseText = true });
|
||||
Assert.AreEqual(FlagsTestEnum.First | FlagsTestEnum.Second, c.Enum);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,8 @@ using NUnit.Framework;
|
||||
using System.Xml;
|
||||
using System.Xml.Schema;
|
||||
using Newtonsoft.Json.Schema;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using ValidationEventArgs = Newtonsoft.Json.Schema.ValidationEventArgs;
|
||||
|
||||
namespace Newtonsoft.Json.Tests
|
||||
{
|
||||
@@ -502,6 +504,36 @@ namespace Newtonsoft.Json.Tests
|
||||
Assert.IsNotNull(validationEventArgs);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IntValidForNumber()
|
||||
{
|
||||
string schemaJson = @"{
|
||||
""type"":""array"",
|
||||
""items"":{
|
||||
""type"":""number""
|
||||
}
|
||||
}";
|
||||
|
||||
string json = "[1]";
|
||||
|
||||
Json.Schema.ValidationEventArgs validationEventArgs = null;
|
||||
|
||||
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
|
||||
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
|
||||
reader.Schema = JsonSchema.Parse(schemaJson);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
|
||||
|
||||
Assert.IsNull(validationEventArgs);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NullNotInEnum()
|
||||
{
|
||||
@@ -1209,5 +1241,124 @@ namespace Newtonsoft.Json.Tests
|
||||
|
||||
Assert.IsFalse(reader.Read());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DuplicateErrorsTest()
|
||||
{
|
||||
string schema = @"{
|
||||
""id"":""ErrorDemo.Database"",
|
||||
""properties"":{
|
||||
""ErrorDemoDatabase"":{
|
||||
""type"":""object"",
|
||||
""required"":true,
|
||||
""properties"":{
|
||||
""URL"":{
|
||||
""type"":""string"",
|
||||
""required"":true
|
||||
},
|
||||
""Version"":{
|
||||
""type"":""string"",
|
||||
""required"":true
|
||||
},
|
||||
""Date"":{
|
||||
""type"":""string"",
|
||||
""format"":""date-time"",
|
||||
""required"":true
|
||||
},
|
||||
""MACLevels"":{
|
||||
""type"":""object"",
|
||||
""required"":true,
|
||||
""properties"":{
|
||||
""MACLevel"":{
|
||||
""type"":""array"",
|
||||
""required"":true,
|
||||
""items"":[
|
||||
{
|
||||
""required"":true,
|
||||
""properties"":{
|
||||
""IDName"":{
|
||||
""type"":""string"",
|
||||
""required"":true
|
||||
},
|
||||
""Order"":{
|
||||
""type"":""string"",
|
||||
""required"":true
|
||||
},
|
||||
""IDDesc"":{
|
||||
""type"":""string"",
|
||||
""required"":true
|
||||
},
|
||||
""IsActive"":{
|
||||
""type"":""string"",
|
||||
""required"":true
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}";
|
||||
|
||||
string json = @"{
|
||||
""ErrorDemoDatabase"":{
|
||||
""URL"":""localhost:3164"",
|
||||
""Version"":""1.0"",
|
||||
""Date"":""6.23.2010, 9:35:18.121"",
|
||||
""MACLevels"":{
|
||||
""MACLevel"":[
|
||||
{
|
||||
""@IDName"":""Developer"",
|
||||
""Order"":""0"",
|
||||
""IDDesc"":""DeveloperDesc"",
|
||||
""IsActive"":""True""
|
||||
},
|
||||
{
|
||||
""IDName"":""Technician"",
|
||||
""Order"":""1"",
|
||||
""IDDesc"":""TechnicianDesc"",
|
||||
""IsActive"":""True""
|
||||
},
|
||||
{
|
||||
""IDName"":""Administrator"",
|
||||
""Order"":""2"",
|
||||
""IDDesc"":""AdministratorDesc"",
|
||||
""IsActive"":""True""
|
||||
},
|
||||
{
|
||||
""IDName"":""PowerUser"",
|
||||
""Order"":""3"",
|
||||
""IDDesc"":""PowerUserDesc"",
|
||||
""IsActive"":""True""
|
||||
},
|
||||
{
|
||||
""IDName"":""Operator"",
|
||||
""Order"":""4"",
|
||||
""IDDesc"":""OperatorDesc"",
|
||||
""IsActive"":""True""
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}";
|
||||
|
||||
IList<ValidationEventArgs> validationEventArgs = new List<ValidationEventArgs>();
|
||||
|
||||
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
|
||||
reader.ValidationEventHandler += (sender, args) =>
|
||||
{
|
||||
validationEventArgs.Add(args);
|
||||
};
|
||||
reader.Schema = JsonSchema.Parse(schema);
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
}
|
||||
|
||||
Assert.AreEqual(1, validationEventArgs.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5045,6 +5045,38 @@ keyword such as type of business.""
|
||||
Assert.AreEqual(new byte[] { 72, 63, 62, 71, 92, 55 }, newObject.Data);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeDecimalsWithCulture()
|
||||
{
|
||||
CultureInfo initialCulture = Thread.CurrentThread.CurrentCulture;
|
||||
|
||||
try
|
||||
{
|
||||
CultureInfo testCulture = CultureInfo.CreateSpecificCulture("nb-NO");
|
||||
|
||||
Thread.CurrentThread.CurrentCulture = testCulture;
|
||||
Thread.CurrentThread.CurrentUICulture = testCulture;
|
||||
|
||||
string json = @"{ 'Quantity': '1.5', 'OptionalQuantity': '2.2' }";
|
||||
|
||||
DecimalTestClass c = JsonConvert.DeserializeObject<DecimalTestClass>(json);
|
||||
|
||||
Assert.AreEqual(1.5m, c.Quantity);
|
||||
Assert.AreEqual(2.2d, c.OptionalQuantity);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Thread.CurrentThread.CurrentCulture = initialCulture;
|
||||
Thread.CurrentThread.CurrentUICulture = initialCulture;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class DecimalTestClass
|
||||
{
|
||||
public decimal Quantity { get; set; }
|
||||
public double OptionalQuantity { get; set; }
|
||||
}
|
||||
|
||||
public class TestObject
|
||||
|
||||
Reference in New Issue
Block a user