-Change BSON writer to write new binary subtype
-Fix BSON reader to read the old binary subtype correctly -Added JsonNet35BinaryCompadibility to BSON reader for incorrectly written BSON binary values -Changed JSON schema to use required flags instead of optional as defined in JSON schema spec 3.0 -Changed JSON schema to use divisibleBy instead of maxDecimals as defined in JSON schema spec 3.0 -Fixed detection of serialization specified flag properties -Updated to latest version of NuGet
This commit is contained in:
@@ -72,6 +72,7 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
byte[] data = MiscellaneousUtils.HexToBytes("8C-00-00-00-12-30-00-FF-FF-FF-FF-FF-FF-FF-7F-12-31-00-FF-FF-FF-FF-FF-FF-FF-7F-10-32-00-FF-FF-FF-7F-10-33-00-FF-FF-FF-7F-10-34-00-FF-00-00-00-10-35-00-7F-00-00-00-02-36-00-02-00-00-00-61-00-01-37-00-00-00-00-00-00-00-F0-45-01-38-00-FF-FF-FF-FF-FF-FF-EF-7F-01-39-00-00-00-00-E0-FF-FF-EF-47-08-31-30-00-01-05-31-31-00-05-00-00-00-02-00-01-02-03-04-09-31-32-00-40-C5-E2-BA-E3-00-00-00-09-31-33-00-40-C5-E2-BA-E3-00-00-00-00");
|
||||
MemoryStream ms = new MemoryStream(data);
|
||||
BsonReader reader = new BsonReader(ms);
|
||||
reader.JsonNet35BinaryCompadibility = true;
|
||||
reader.ReadRootValueAsArray = true;
|
||||
reader.DateTimeKindHandling = DateTimeKind.Utc;
|
||||
|
||||
@@ -250,6 +251,7 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
|
||||
MemoryStream ms = new MemoryStream(data);
|
||||
BsonReader reader = new BsonReader(ms, true, DateTimeKind.Utc);
|
||||
reader.JsonNet35BinaryCompadibility = true;
|
||||
|
||||
Assert.AreEqual(true, reader.ReadRootValueAsArray);
|
||||
Assert.AreEqual(DateTimeKind.Utc, reader.DateTimeKindHandling);
|
||||
@@ -378,11 +380,12 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
[Test]
|
||||
public void ReadNestedArrayIntoLinq()
|
||||
{
|
||||
string hexdoc = "87-00-00-00-05-5F-69-64-00-0C-00-00-00-02-4A-78-93-79-17-22-00-00-00-00-61-CF-04-61-00-5D-00-00-00-01-30-00-00-00-00-00-00-00-F0-3F-01-31-00-00-00-00-00-00-00-00-40-01-32-00-00-00-00-00-00-00-08-40-01-33-00-00-00-00-00-00-00-10-40-01-34-00-00-00-00-00-00-00-14-50-01-35-00-00-00-00-00-00-00-18-40-01-36-00-00-00-00-00-00-00-1C-40-01-37-00-00-00-00-00-00-00-20-40-00-02-62-00-05-00-00-00-74-65-73-74-00-00";
|
||||
string hexdoc = "87-00-00-00-05-5F-69-64-00-0C-00-00-00-00-4A-78-93-79-17-22-00-00-00-00-61-CF-04-61-00-5D-00-00-00-01-30-00-00-00-00-00-00-00-F0-3F-01-31-00-00-00-00-00-00-00-00-40-01-32-00-00-00-00-00-00-00-08-40-01-33-00-00-00-00-00-00-00-10-40-01-34-00-00-00-00-00-00-00-14-50-01-35-00-00-00-00-00-00-00-18-40-01-36-00-00-00-00-00-00-00-1C-40-01-37-00-00-00-00-00-00-00-20-40-00-02-62-00-05-00-00-00-74-65-73-74-00-00";
|
||||
|
||||
byte[] data = MiscellaneousUtils.HexToBytes(hexdoc);
|
||||
|
||||
BsonReader reader = new BsonReader(new MemoryStream(data));
|
||||
reader.JsonNet35BinaryCompadibility = true;
|
||||
|
||||
JObject o = (JObject)JToken.ReadFrom(reader);
|
||||
Assert.AreEqual(3, o.Count);
|
||||
@@ -403,6 +406,7 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
"82-00-00-00-07-5F-69-64-00-4A-78-93-79-17-22-00-00-00-00-61-CF-04-61-00-5D-00-00-00-01-30-00-00-00-00-00-00-00-F0-3F-01-31-00-00-00-00-00-00-00-00-40-01-32-00-00-00-00-00-00-00-08-40-01-33-00-00-00-00-00-00-00-10-40-01-34-00-00-00-00-00-00-00-14-50-01-35-00-00-00-00-00-00-00-18-40-01-36-00-00-00-00-00-00-00-1C-40-01-37-00-00-00-00-00-00-00-20-40-00-02-62-00-05-00-00-00-74-65-73-74-00-00");
|
||||
|
||||
BsonReader reader1 = new BsonReader(new MemoryStream(data1));
|
||||
reader1.JsonNet35BinaryCompadibility = true;
|
||||
|
||||
// oid
|
||||
JObject o1 = (JObject)JToken.ReadFrom(reader1);
|
||||
@@ -411,6 +415,7 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
"87-00-00-00-05-5F-69-64-00-0C-00-00-00-02-4A-78-93-79-17-22-00-00-00-00-61-CF-04-61-00-5D-00-00-00-01-30-00-00-00-00-00-00-00-F0-3F-01-31-00-00-00-00-00-00-00-00-40-01-32-00-00-00-00-00-00-00-08-40-01-33-00-00-00-00-00-00-00-10-40-01-34-00-00-00-00-00-00-00-14-50-01-35-00-00-00-00-00-00-00-18-40-01-36-00-00-00-00-00-00-00-1C-40-01-37-00-00-00-00-00-00-00-20-40-00-02-62-00-05-00-00-00-74-65-73-74-00-00");
|
||||
|
||||
BsonReader reader2 = new BsonReader(new MemoryStream(data2));
|
||||
reader2.JsonNet35BinaryCompadibility = true;
|
||||
|
||||
// bytes
|
||||
JObject o2 = (JObject)JToken.ReadFrom(reader2);
|
||||
|
||||
@@ -78,7 +78,7 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
writer.WriteEnd();
|
||||
|
||||
string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
|
||||
Assert.AreEqual("8C-00-00-00-12-30-00-FF-FF-FF-FF-FF-FF-FF-7F-12-31-00-FF-FF-FF-FF-FF-FF-FF-7F-10-32-00-FF-FF-FF-7F-10-33-00-FF-FF-FF-7F-10-34-00-FF-00-00-00-10-35-00-7F-00-00-00-02-36-00-02-00-00-00-61-00-01-37-00-00-00-00-00-00-00-F0-45-01-38-00-FF-FF-FF-FF-FF-FF-EF-7F-01-39-00-00-00-00-E0-FF-FF-EF-47-08-31-30-00-01-05-31-31-00-05-00-00-00-02-00-01-02-03-04-09-31-32-00-40-C5-E2-BA-E3-00-00-00-09-31-33-00-40-C5-E2-BA-E3-00-00-00-00", bson);
|
||||
Assert.AreEqual("8C-00-00-00-12-30-00-FF-FF-FF-FF-FF-FF-FF-7F-12-31-00-FF-FF-FF-FF-FF-FF-FF-7F-10-32-00-FF-FF-FF-7F-10-33-00-FF-FF-FF-7F-10-34-00-FF-00-00-00-10-35-00-7F-00-00-00-02-36-00-02-00-00-00-61-00-01-37-00-00-00-00-00-00-00-F0-45-01-38-00-FF-FF-FF-FF-FF-FF-EF-7F-01-39-00-00-00-00-E0-FF-FF-EF-47-08-31-30-00-01-05-31-31-00-05-00-00-00-00-00-01-02-03-04-09-31-32-00-40-C5-E2-BA-E3-00-00-00-09-31-33-00-40-C5-E2-BA-E3-00-00-00-00", bson);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -120,10 +120,19 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
|
||||
ms.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
string expected = "2B-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-05-32-00-0C-00-00-00-02-48-65-6C-6C-6F-20-77-6F-72-6C-64-21-00";
|
||||
string expected = "2B-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-05-32-00-0C-00-00-00-00-48-65-6C-6C-6F-20-77-6F-72-6C-64-21-00";
|
||||
string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
|
||||
|
||||
Assert.AreEqual(expected, bson);
|
||||
|
||||
BsonReader reader = new BsonReader(new MemoryStream(ms.ToArray()));
|
||||
reader.ReadRootValueAsArray = true;
|
||||
reader.Read();
|
||||
reader.Read();
|
||||
reader.Read();
|
||||
reader.Read();
|
||||
Assert.AreEqual(JsonToken.Bytes, reader.TokenType);
|
||||
Assert.AreEqual(data, reader.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -157,7 +166,7 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
|
||||
ms.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
string expected = "87-00-00-00-05-5F-69-64-00-0C-00-00-00-02-4A-78-93-79-17-22-00-00-00-00-61-CF-04-61-00-5D-00-00-00-01-30-00-00-00-00-00-00-00-F0-3F-01-31-00-00-00-00-00-00-00-00-40-01-32-00-00-00-00-00-00-00-08-40-01-33-00-00-00-00-00-00-00-10-40-01-34-00-00-00-00-00-00-00-14-50-01-35-00-00-00-00-00-00-00-18-40-01-36-00-00-00-00-00-00-00-1C-40-01-37-00-00-00-00-00-00-00-20-40-00-02-62-00-05-00-00-00-74-65-73-74-00-00";
|
||||
string expected = "87-00-00-00-05-5F-69-64-00-0C-00-00-00-00-4A-78-93-79-17-22-00-00-00-00-61-CF-04-61-00-5D-00-00-00-01-30-00-00-00-00-00-00-00-F0-3F-01-31-00-00-00-00-00-00-00-00-40-01-32-00-00-00-00-00-00-00-08-40-01-33-00-00-00-00-00-00-00-10-40-01-34-00-00-00-00-00-00-00-14-50-01-35-00-00-00-00-00-00-00-18-40-01-36-00-00-00-00-00-00-00-1C-40-01-37-00-00-00-00-00-00-00-20-40-00-02-62-00-05-00-00-00-74-65-73-74-00-00";
|
||||
string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
|
||||
|
||||
Assert.AreEqual(expected, bson);
|
||||
|
||||
@@ -470,7 +470,7 @@ namespace Newtonsoft.Json.Tests
|
||||
""type"":""array"",
|
||||
""items"":{
|
||||
""type"":""number"",
|
||||
""maxDecimal"":2
|
||||
""divisibleBy"":0.1
|
||||
}
|
||||
}";
|
||||
|
||||
@@ -494,7 +494,7 @@ namespace Newtonsoft.Json.Tests
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.Float, reader.TokenType);
|
||||
Assert.AreEqual(@"Float 4.001 exceeds the maximum allowed number decimal places of 2. Line 1, position 15.", validationEventArgs.Message);
|
||||
Assert.AreEqual(@"Float 4.001 is not evenly divisible by 0.1. Line 1, position 15.", validationEventArgs.Message);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
|
||||
@@ -700,7 +700,7 @@ namespace Newtonsoft.Json.Tests
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MissingNonoptionalProperties()
|
||||
public void MissingRequiredProperties()
|
||||
{
|
||||
string schemaJson = @"{
|
||||
""description"":""A person"",
|
||||
@@ -708,8 +708,8 @@ namespace Newtonsoft.Json.Tests
|
||||
""properties"":
|
||||
{
|
||||
""name"":{""type"":""string""},
|
||||
""hobbies"":{""type"":""string""},
|
||||
""age"":{""type"":""integer""}
|
||||
""hobbies"":{""type"":""string"",""required"":true},
|
||||
""age"":{""type"":""integer"",""required"":true}
|
||||
}
|
||||
}";
|
||||
|
||||
@@ -735,22 +735,22 @@ namespace Newtonsoft.Json.Tests
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
|
||||
Assert.AreEqual("Non-optional properties are missing from object: hobbies, age. Line 1, position 16.", validationEventArgs.Message);
|
||||
Assert.AreEqual("Required properties are missing from object: hobbies, age. Line 1, position 16.", validationEventArgs.Message);
|
||||
|
||||
Assert.IsNotNull(validationEventArgs);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MissingOptionalProperties()
|
||||
public void MissingNonRequiredProperties()
|
||||
{
|
||||
string schemaJson = @"{
|
||||
""description"":""A person"",
|
||||
""type"":""object"",
|
||||
""properties"":
|
||||
{
|
||||
""name"":{""type"":""string""},
|
||||
""hobbies"":{""type"":""string"",optional:true},
|
||||
""age"":{""type"":""integer"",optional:true}
|
||||
""name"":{""type"":""string"",""required"":true},
|
||||
""hobbies"":{""type"":""string"",""required"":false},
|
||||
""age"":{""type"":""integer""}
|
||||
}
|
||||
}";
|
||||
|
||||
@@ -873,7 +873,7 @@ namespace Newtonsoft.Json.Tests
|
||||
""type"":""object"",
|
||||
""properties"":
|
||||
{
|
||||
""firstproperty"":{""type"":""string""}
|
||||
""firstproperty"":{""type"":""string"",""required"":true}
|
||||
},
|
||||
""additionalProperties"":{}
|
||||
}";
|
||||
@@ -884,7 +884,7 @@ namespace Newtonsoft.Json.Tests
|
||||
""extends"":{""$ref"":""first""},
|
||||
""properties"":
|
||||
{
|
||||
""secondproperty"":{""type"":""string""}
|
||||
""secondproperty"":{""type"":""string"",""required"":true}
|
||||
},
|
||||
""additionalProperties"":false
|
||||
}";
|
||||
@@ -955,7 +955,7 @@ namespace Newtonsoft.Json.Tests
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ExtendsMissingNonoptionalProperties()
|
||||
public void ExtendsMissingRequiredProperties()
|
||||
{
|
||||
string json = "{}";
|
||||
|
||||
@@ -972,7 +972,7 @@ namespace Newtonsoft.Json.Tests
|
||||
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
|
||||
|
||||
Assert.AreEqual(1, errors.Count);
|
||||
Assert.AreEqual("Non-optional properties are missing from object: secondproperty, firstproperty. Line 1, position 2.", errors[0]);
|
||||
Assert.AreEqual("Required properties are missing from object: secondproperty, firstproperty. Line 1, position 2.", errors[0]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -89,13 +89,13 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
[Test]
|
||||
public void ValidateFailureWithOutLineInfoBecauseOfEndToken()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse("{'properties':{'lol':{}}}");
|
||||
JsonSchema schema = JsonSchema.Parse("{'properties':{'lol':{'required':true}}}");
|
||||
JObject o = JObject.Parse("{}");
|
||||
|
||||
List<string> errors = new List<string>();
|
||||
o.Validate(schema, (sender, args) => errors.Add(args.Message));
|
||||
|
||||
Assert.AreEqual("Non-optional properties are missing from object: lol.", errors[0]);
|
||||
Assert.AreEqual("Required properties are missing from object: lol.", errors[0]);
|
||||
Assert.AreEqual(1, errors.Count);
|
||||
}
|
||||
|
||||
|
||||
@@ -116,18 +116,18 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Optional()
|
||||
public void Required()
|
||||
{
|
||||
string json = @"{
|
||||
""description"":""Optional"",
|
||||
""optional"":true
|
||||
""description"":""Required"",
|
||||
""required"":true
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("Optional", schema.Description);
|
||||
Assert.AreEqual(true, schema.Optional);
|
||||
Assert.AreEqual("Required", schema.Description);
|
||||
Assert.AreEqual(true, schema.Required);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -279,7 +279,7 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""maxItems"":2,
|
||||
""minLength"":5,
|
||||
""maxLength"":50,
|
||||
""maxDecimal"":3,
|
||||
""divisibleBy"":3,
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
@@ -292,7 +292,7 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
Assert.AreEqual(2, schema.MaximumItems);
|
||||
Assert.AreEqual(5, schema.MinimumLength);
|
||||
Assert.AreEqual(50, schema.MaximumLength);
|
||||
Assert.AreEqual(3, schema.MaximumDecimals);
|
||||
Assert.AreEqual(3, schema.DivisibleBy);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -94,10 +94,12 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""additionalProperties"": false,
|
||||
""properties"": {
|
||||
""TestField1"": {
|
||||
""required"": true,
|
||||
""type"": ""integer"",
|
||||
""default"": 21
|
||||
},
|
||||
""TestProperty1"": {
|
||||
""required"": true,
|
||||
""type"": [
|
||||
""string"",
|
||||
""null""
|
||||
@@ -124,15 +126,18 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""type"": ""object"",
|
||||
""properties"": {
|
||||
""Name"": {
|
||||
""required"": true,
|
||||
""type"": [
|
||||
""string"",
|
||||
""null""
|
||||
]
|
||||
},
|
||||
""BirthDate"": {
|
||||
""required"": true,
|
||||
""type"": ""string""
|
||||
},
|
||||
""LastModified"": {
|
||||
""required"": true,
|
||||
""type"": ""string""
|
||||
}
|
||||
}
|
||||
@@ -151,36 +156,43 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""type"": ""object"",
|
||||
""properties"": {
|
||||
""Id"": {
|
||||
""required"": true,
|
||||
""type"": ""string""
|
||||
},
|
||||
""FName"": {
|
||||
""required"": true,
|
||||
""type"": [
|
||||
""string"",
|
||||
""null""
|
||||
]
|
||||
},
|
||||
""LName"": {
|
||||
""required"": true,
|
||||
""type"": [
|
||||
""string"",
|
||||
""null""
|
||||
]
|
||||
},
|
||||
""RoleId"": {
|
||||
""required"": true,
|
||||
""type"": ""integer""
|
||||
},
|
||||
""NullableRoleId"": {
|
||||
""required"": true,
|
||||
""type"": [
|
||||
""integer"",
|
||||
""null""
|
||||
]
|
||||
},
|
||||
""NullRoleId"": {
|
||||
""required"": true,
|
||||
""type"": [
|
||||
""integer"",
|
||||
""null""
|
||||
]
|
||||
},
|
||||
""Active"": {
|
||||
""required"": true,
|
||||
""type"": [
|
||||
""boolean"",
|
||||
""null""
|
||||
@@ -370,6 +382,7 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""id"": ""System.IO.DirectoryInfo"",
|
||||
""required"": true,
|
||||
""type"": [
|
||||
""object"",
|
||||
""null""
|
||||
@@ -377,6 +390,7 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""additionalProperties"": false,
|
||||
""properties"": {
|
||||
""Name"": {
|
||||
""required"": true,
|
||||
""type"": [
|
||||
""string"",
|
||||
""null""
|
||||
@@ -386,39 +400,49 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""$ref"": ""System.IO.DirectoryInfo""
|
||||
},
|
||||
""Exists"": {
|
||||
""required"": true,
|
||||
""type"": ""boolean""
|
||||
},
|
||||
""FullName"": {
|
||||
""required"": true,
|
||||
""type"": [
|
||||
""string"",
|
||||
""null""
|
||||
]
|
||||
},
|
||||
""Extension"": {
|
||||
""required"": true,
|
||||
""type"": [
|
||||
""string"",
|
||||
""null""
|
||||
]
|
||||
},
|
||||
""CreationTime"": {
|
||||
""required"": true,
|
||||
""type"": ""string""
|
||||
},
|
||||
""CreationTimeUtc"": {
|
||||
""required"": true,
|
||||
""type"": ""string""
|
||||
},
|
||||
""LastAccessTime"": {
|
||||
""required"": true,
|
||||
""type"": ""string""
|
||||
},
|
||||
""LastAccessTimeUtc"": {
|
||||
""required"": true,
|
||||
""type"": ""string""
|
||||
},
|
||||
""LastWriteTime"": {
|
||||
""required"": true,
|
||||
""type"": ""string""
|
||||
},
|
||||
""LastWriteTimeUtc"": {
|
||||
""required"": true,
|
||||
""type"": ""string""
|
||||
},
|
||||
""Attributes"": {
|
||||
""required"": true,
|
||||
""type"": ""integer""
|
||||
}
|
||||
}
|
||||
@@ -458,21 +482,27 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""additionalProperties"": false,
|
||||
""properties"": {
|
||||
""major"": {
|
||||
""required"": true,
|
||||
""type"": ""integer""
|
||||
},
|
||||
""minor"": {
|
||||
""required"": true,
|
||||
""type"": ""integer""
|
||||
},
|
||||
""build"": {
|
||||
""required"": true,
|
||||
""type"": ""integer""
|
||||
},
|
||||
""revision"": {
|
||||
""required"": true,
|
||||
""type"": ""integer""
|
||||
},
|
||||
""majorRevision"": {
|
||||
""required"": true,
|
||||
""type"": ""integer""
|
||||
},
|
||||
""minorRevision"": {
|
||||
""required"": true,
|
||||
""type"": ""integer""
|
||||
}
|
||||
}
|
||||
@@ -503,6 +533,7 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""type"": ""object"",
|
||||
""properties"": {
|
||||
""x"": {
|
||||
""required"": true,
|
||||
""type"": ""integer"",
|
||||
""enum"": [
|
||||
0,
|
||||
@@ -552,13 +583,13 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""id"": ""Newtonsoft.Json.Tests.TestObjects.CircularReferenceClass"",
|
||||
""optional"": true,
|
||||
""type"": [
|
||||
""object"",
|
||||
""null""
|
||||
],
|
||||
""properties"": {
|
||||
""Name"": {
|
||||
""required"": true,
|
||||
""type"": ""string""
|
||||
},
|
||||
""Child"": {
|
||||
@@ -579,13 +610,13 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""id"": ""Newtonsoft.Json.Tests.TestObjects.JsonPropertyWithHandlingValues"",
|
||||
""required"": true,
|
||||
""type"": [
|
||||
""object"",
|
||||
""null""
|
||||
],
|
||||
""properties"": {
|
||||
""DefaultValueHandlingIgnoreProperty"": {
|
||||
""optional"": true,
|
||||
""type"": [
|
||||
""string"",
|
||||
""null""
|
||||
@@ -593,6 +624,7 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""default"": ""Default!""
|
||||
},
|
||||
""DefaultValueHandlingIncludeProperty"": {
|
||||
""required"": true,
|
||||
""type"": [
|
||||
""string"",
|
||||
""null""
|
||||
@@ -600,13 +632,13 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""default"": ""Default!""
|
||||
},
|
||||
""NullValueHandlingIgnoreProperty"": {
|
||||
""optional"": true,
|
||||
""type"": [
|
||||
""string"",
|
||||
""null""
|
||||
]
|
||||
},
|
||||
""NullValueHandlingIncludeProperty"": {
|
||||
""required"": true,
|
||||
""type"": [
|
||||
""string"",
|
||||
""null""
|
||||
|
||||
@@ -139,7 +139,7 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Optional()
|
||||
public void Required()
|
||||
{
|
||||
string schemaJson = @"{
|
||||
""description"":""A person"",
|
||||
@@ -147,8 +147,8 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""properties"":
|
||||
{
|
||||
""name"":{""type"":""string""},
|
||||
""hobbies"":{""type"":""string"",optional:true},
|
||||
""age"":{""type"":""integer"",optional:true}
|
||||
""hobbies"":{""type"":""string"",required:true},
|
||||
""age"":{""type"":""integer"",required:true}
|
||||
}
|
||||
}";
|
||||
|
||||
@@ -158,9 +158,9 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
|
||||
Assert.AreEqual(JsonSchemaType.Object, model.Type);
|
||||
Assert.AreEqual(3, model.Properties.Count);
|
||||
Assert.AreEqual(false, model.Properties["name"].Optional);
|
||||
Assert.AreEqual(true, model.Properties["hobbies"].Optional);
|
||||
Assert.AreEqual(true, model.Properties["age"].Optional);
|
||||
Assert.AreEqual(false, model.Properties["name"].Required);
|
||||
Assert.AreEqual(true, model.Properties["hobbies"].Required);
|
||||
Assert.AreEqual(true, model.Properties["age"].Required);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3385,6 +3385,7 @@ keyword such as type of business.""
|
||||
public string Name { get; set; }
|
||||
public int Age { get; set; }
|
||||
public int Weight { get; set; }
|
||||
public int Height { get; set; }
|
||||
|
||||
// dummy. should never be used because it isn't of type bool
|
||||
[JsonIgnore]
|
||||
@@ -3399,6 +3400,10 @@ keyword such as type of business.""
|
||||
|
||||
[JsonIgnore]
|
||||
public bool WeightSpecified;
|
||||
|
||||
[JsonIgnore]
|
||||
[System.Xml.Serialization.XmlIgnoreAttribute]
|
||||
public bool HeightSpecified;
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -3419,28 +3424,36 @@ keyword such as type of business.""
|
||||
Assert.IsNull(deserialized.Name);
|
||||
Assert.IsFalse(deserialized.NameSpecified);
|
||||
Assert.IsFalse(deserialized.WeightSpecified);
|
||||
Assert.IsFalse(deserialized.HeightSpecified);
|
||||
Assert.AreEqual(27, deserialized.Age);
|
||||
|
||||
c.NameSpecified = true;
|
||||
c.WeightSpecified = true;
|
||||
c.HeightSpecified = true;
|
||||
json = JsonConvert.SerializeObject(c, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""Name"": ""James"",
|
||||
""Age"": 27,
|
||||
""Weight"": 0
|
||||
""Weight"": 0,
|
||||
""Height"": 0
|
||||
}", json);
|
||||
|
||||
deserialized = JsonConvert.DeserializeObject<SpecifiedTestClass>(json);
|
||||
Assert.AreEqual("James", deserialized.Name);
|
||||
Assert.IsTrue(deserialized.NameSpecified);
|
||||
Assert.IsTrue(deserialized.WeightSpecified);
|
||||
Assert.IsTrue(deserialized.HeightSpecified);
|
||||
Assert.AreEqual(27, deserialized.Age);
|
||||
}
|
||||
|
||||
// [Test]
|
||||
// public void XmlSerializerSpecifiedTrueTest()
|
||||
// {
|
||||
// XmlSerializer s = new XmlSerializer(typeof(OptionalOrder));
|
||||
|
||||
// StringWriter sw = new StringWriter();
|
||||
// s.Serialize(sw, new OptionalOrder() { FirstOrder = "First", FirstOrderSpecified = true});
|
||||
// s.Serialize(sw, new OptionalOrder() { FirstOrder = "First", FirstOrderSpecified = true });
|
||||
|
||||
// Console.WriteLine(sw.ToString());
|
||||
|
||||
@@ -3452,18 +3465,38 @@ keyword such as type of business.""
|
||||
// OptionalOrder o = (OptionalOrder)s.Deserialize(new StringReader(xml));
|
||||
// Console.WriteLine(o.FirstOrder);
|
||||
// Console.WriteLine(o.FirstOrderSpecified);
|
||||
}
|
||||
// }
|
||||
|
||||
//public class OptionalOrder
|
||||
//{
|
||||
// // This field shouldn't be serialized
|
||||
// // if it is uninitialized.
|
||||
// public string FirstOrder;
|
||||
// // Use the XmlIgnoreAttribute to ignore the
|
||||
// // special field named "FirstOrderSpecified".
|
||||
// [System.Xml.Serialization.XmlIgnoreAttribute]
|
||||
// public bool FirstOrderSpecified;
|
||||
//}
|
||||
// [Test]
|
||||
// public void XmlSerializerSpecifiedFalseTest()
|
||||
// {
|
||||
// XmlSerializer s = new XmlSerializer(typeof(OptionalOrder));
|
||||
|
||||
// StringWriter sw = new StringWriter();
|
||||
// s.Serialize(sw, new OptionalOrder() { FirstOrder = "First", FirstOrderSpecified = false });
|
||||
|
||||
// Console.WriteLine(sw.ToString());
|
||||
|
||||
//// string xml = @"<?xml version=""1.0"" encoding=""utf-16""?>
|
||||
////<OptionalOrder xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
|
||||
//// <FirstOrder>First</FirstOrder>
|
||||
////</OptionalOrder>";
|
||||
|
||||
//// OptionalOrder o = (OptionalOrder)s.Deserialize(new StringReader(xml));
|
||||
//// Console.WriteLine(o.FirstOrder);
|
||||
//// Console.WriteLine(o.FirstOrderSpecified);
|
||||
// }
|
||||
|
||||
public class OptionalOrder
|
||||
{
|
||||
// This field shouldn't be serialized
|
||||
// if it is uninitialized.
|
||||
public string FirstOrder;
|
||||
// Use the XmlIgnoreAttribute to ignore the
|
||||
// special field named "FirstOrderSpecified".
|
||||
[System.Xml.Serialization.XmlIgnoreAttribute]
|
||||
public bool FirstOrderSpecified;
|
||||
}
|
||||
|
||||
public class FamilyDetails
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user