Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/Newtonsoft.Json.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamesNK <james@newtonking.com>2011-11-18 13:36:01 +0400
committerJamesNK <james@newtonking.com>2011-11-18 13:36:01 +0400
commit5831b11de11c13761dc82706da5e349ba2739b2e (patch)
treee3dc72ae2536c3edb6f881052bdbae16832a2deb /Src/Newtonsoft.Json.Tests
parentda7e4508d76151402f700b6cb615dc053df67d82 (diff)
-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
Diffstat (limited to 'Src/Newtonsoft.Json.Tests')
-rw-r--r--Src/Newtonsoft.Json.Tests/Converters/StringEnumConverterTests.cs33
-rw-r--r--Src/Newtonsoft.Json.Tests/JsonValidatingReaderTests.cs151
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs32
3 files changed, 216 insertions, 0 deletions
diff --git a/Src/Newtonsoft.Json.Tests/Converters/StringEnumConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/StringEnumConverterTests.cs
index 2d5c8f5..685495b 100644
--- a/Src/Newtonsoft.Json.Tests/Converters/StringEnumConverterTests.cs
+++ b/Src/Newtonsoft.Json.Tests/Converters/StringEnumConverterTests.cs
@@ -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);
+ }
}
} \ No newline at end of file
diff --git a/Src/Newtonsoft.Json.Tests/JsonValidatingReaderTests.cs b/Src/Newtonsoft.Json.Tests/JsonValidatingReaderTests.cs
index 72556ad..432cf9b 100644
--- a/Src/Newtonsoft.Json.Tests/JsonValidatingReaderTests.cs
+++ b/Src/Newtonsoft.Json.Tests/JsonValidatingReaderTests.cs
@@ -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
{
@@ -503,6 +505,36 @@ namespace Newtonsoft.Json.Tests
}
[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()
{
string schemaJson = @"{
@@ -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);
+ }
}
} \ No newline at end of file
diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
index 38ffce6..fd1930e 100644
--- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
+++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
@@ -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