Merge pull request #40 from ranrav/master

Fix bug in JSON parsing when string value is a date
This commit is contained in:
Adam Sapek 2015-02-27 08:15:33 -08:00
Родитель ebf9175eef 3294b3e1d3
Коммит 7c84c4fa7e
4 изменённых файлов: 27 добавлений и 10 удалений

Просмотреть файл

@ -121,10 +121,11 @@ namespace Bond.Expressions.Json
convertedValue = Reader.Value;
}
var errorMessage = string.Format(
CultureInfo.InvariantCulture,
"Invalid input, expected JSON token of type {0}",
scalarTokenType);
var errorMessage =
StringExpression.Format(
"Invalid input, expected JSON token of type {0}, encountered {1}",
Expression.Constant(scalarTokenType, typeof(object)),
Expression.Convert(Reader.TokenType, typeof(object)));
return
Expression.Block(
@ -197,16 +198,21 @@ namespace Bond.Expressions.Json
Expression.Convert(Reader.LinePosition, typeof(object))));
}
Expression ThrowUnexpectedInput(string errorMessage)
Expression ThrowUnexpectedInput(Expression errorMessage)
{
return ThrowExpression.InvalidDataException(
StringExpression.Format(
"{0} (line {1} position {2})",
Expression.Constant(errorMessage),
errorMessage,
Expression.Convert(Reader.LineNumber, typeof(object)),
Expression.Convert(Reader.LinePosition, typeof(object))));
}
Expression ThrowUnexpectedInput(string errorMessage)
{
return ThrowUnexpectedInput(Expression.Constant(errorMessage));
}
Expression ProcessField(ParameterExpression requiredFields, IEnumerable<TransformSchemaPair> transforms)
{
// unknown fields are skipped (read past the unknown PropertyName then skip the value)

Просмотреть файл

@ -20,6 +20,9 @@ namespace Bond.Protocols
public SimpleJsonReader(TextReader reader)
{
this.reader = new JsonTextReader(reader);
this.reader.DateParseHandling = DateParseHandling.None;
this.reader.FloatParseHandling = FloatParseHandling.Double;
eof = false;
}

Просмотреть файл

@ -2,13 +2,10 @@
{
using System;
using System.IO;
using Bond;
using Bond.Protocols;
using NUnit.Framework;
using Newtonsoft.Json;
using NUnit.Framework;
[TestFixture]
public class JsonParsingTests
@ -681,6 +678,16 @@ World", target._str);
Assert.AreEqual(11, target.nb.Array[target.nb.Offset + 1]);
}
[Test]
public void JsonParsing_DateAsString()
{
const string json = @"{""value"":""2015-02-26T13:18:13.1521765-08:00""}";
var target = ParseJson<Box<string>>(json);
Assert.AreEqual("2015-02-26T13:18:13.1521765-08:00", target.value);
}
private static T ParseJson<T>(string json) where T : new()
{

Просмотреть файл

@ -652,6 +652,7 @@ struct IdAttribute
{
0: TypeAttribute field = DefaultAttribute;
}
struct classT {}
struct structT {}
struct GenericConflict<structT : value>