in cs/src/json/expressions/json/SimpleJsonParser.cs [103:166]
public override Expression Scalar(Expression valueType, BondDataType expectedType, ValueHandler handler)
{
JsonToken scalarTokenType;
if (!ScalarTokenTypes.TryGetValue(expectedType, out scalarTokenType))
{
Debug.Assert(false, "Scalar should be called only on scalar expected types.");
}
Expression convertedValue;
if (scalarTokenType == JsonToken.Integer)
{
// reader.Value is a boxed long and must be unboxed to the right type in order to
// avoid an InvalidCastException.
convertedValue = Expression.Convert(Reader.Value, typeof(long));
}
else if (scalarTokenType == JsonToken.Float)
{
convertedValue = Expression.Convert(Reader.Value, typeof(double));
}
else
{
convertedValue = Reader.Value;
}
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)));
Expression embeddedExpression = handler(convertedValue);
#if SUPPORTS_BIGINTEGER
if (expectedType == BondDataType.BT_UINT64 && scalarTokenType == JsonToken.Integer)
{
embeddedExpression =
Expression.IfThenElse(
Expression.TypeIs(Reader.Value, typeof(long)),
embeddedExpression,
handler(Expression.Convert(Reader.Value, typeof(BigInteger))));
}
#endif
var handleValue =
Expression.IfThenElse(
JsonTokenEquals(scalarTokenType),
embeddedExpression,
ThrowUnexpectedInput(errorMessage));
// If a floating point value is expected also accept an integer
if (scalarTokenType == JsonToken.Float)
{
handleValue = Expression.IfThenElse(
JsonTokenEquals(JsonToken.Integer),
handler(Expression.Convert(Reader.Value, typeof(long))),
handleValue);
}
return
Expression.Block(
handleValue,
Reader.Read());
}