-Fixed JsonValidatingReader.ReadAsBytes throwing exception

-Minor NuGet spec updates
This commit is contained in:
JamesNK
2012-02-11 07:00:18 +00:00
parent 2dd26baa77
commit 7b908df64d
3 changed files with 78 additions and 5 deletions
+4 -3
View File
@@ -1,13 +1,14 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<package> <package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata> <metadata>
<id>Newtonsoft.Json</id> <id>Newtonsoft.Json</id>
<title>Json.NET</title>
<version>4.0.8</version> <version>4.0.8</version>
<authors>James Newton-King</authors> <title>Json.NET</title>
<description>Json.NET is a popular high-performance JSON framework for .NET</description> <description>Json.NET is a popular high-performance JSON framework for .NET</description>
<authors>James Newton-King</authors>
<language>en-US</language> <language>en-US</language>
<projectUrl>http://james.newtonking.com/projects/json-net.aspx</projectUrl> <projectUrl>http://james.newtonking.com/projects/json-net.aspx</projectUrl>
<licenseUrl>http://json.codeplex.com/license</licenseUrl>
<tags>json</tags> <tags>json</tags>
</metadata> </metadata>
</package> </package>
@@ -1360,5 +1360,77 @@ namespace Newtonsoft.Json.Tests
Assert.AreEqual(1, validationEventArgs.Count); Assert.AreEqual(1, validationEventArgs.Count);
} }
[Test]
public void ReadAsBytes()
{
JsonSchema s = new JsonSchemaGenerator().Generate(typeof (byte[]));
byte[] data = Encoding.UTF8.GetBytes("Hello world");
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"""" + Convert.ToBase64String(data) + @"""")))
{
Schema = s
};
byte[] bytes = reader.ReadAsBytes();
Assert.AreEqual(data, bytes);
}
[Test]
public void ReadAsInt32()
{
JsonSchema s = new JsonSchemaGenerator().Generate(typeof(int));
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"1")))
{
Schema = s
};
int? i = reader.ReadAsInt32();
Assert.AreEqual(1, i);
}
[Test]
[ExpectedException(typeof(JsonSchemaException), ExpectedMessage = "Integer 5 exceeds maximum value of 2. Line 1, position 1.")]
public void ReadAsInt32Failure()
{
JsonSchema s = new JsonSchemaGenerator().Generate(typeof(int));
s.Maximum = 2;
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"5")))
{
Schema = s
};
reader.ReadAsInt32();
}
[Test]
public void ReadAsDecimal()
{
JsonSchema s = new JsonSchemaGenerator().Generate(typeof(decimal));
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"1.5")))
{
Schema = s
};
decimal? d = reader.ReadAsDecimal();
Assert.AreEqual(1.5, d);
}
[Test]
[ExpectedException(typeof(JsonSchemaException), ExpectedMessage = "Float 5.5 is not evenly divisible by 1. Line 1, position 3.")]
public void ReadAsDecimalFailure()
{
JsonSchema s = new JsonSchemaGenerator().Generate(typeof(decimal));
s.DivisibleBy = 1;
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"5.5")))
{
Schema = s
};
reader.ReadAsDecimal();
}
} }
} }
+2 -2
View File
@@ -423,8 +423,6 @@ namespace Newtonsoft.Json
_model = builder.Build(_schema); _model = builder.Build(_schema);
} }
//ValidateValueToken();
switch (_reader.TokenType) switch (_reader.TokenType)
{ {
case JsonToken.StartObject: case JsonToken.StartObject:
@@ -503,6 +501,8 @@ namespace Newtonsoft.Json
Pop(); Pop();
break; break;
case JsonToken.Date: case JsonToken.Date:
case JsonToken.Bytes:
// these have no equivalent in JSON schema
break; break;
default: default:
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();