public static Expression Convert(Expression valueAsString, BondDataType type) { if (type == BondDataType.BT_STRING || type == BondDataType.BT_WSTRING) { return valueAsString; } return Expression.Call(methods[type], valueAsString, invariantCulture); }
public void ReadContainerBegin(out int count, out BondDataType keyType, out BondDataType valueType) { keyType = (BondDataType)input.ReadUInt8(); valueType = (BondDataType)input.ReadUInt8(); count = (int)input.ReadVarUInt32(); }
public void WriteFieldOmitted(BondDataType dataType, ushort id, Metadata metadata) { }
public Expression Scalar(Expression valueType, BondDataType expectedType, ValueHandler handler) { Debug.Assert(expectedType == schemaType.GetBondDataType()); return(handler(typeAlias.Convert(value, schemaType))); }
public void WriteFieldBegin(BondDataType dataType, ushort id, Metadata metadata) { writer.WriteStartElement(Prefix, metadata.name, null); }
public void WriteFieldOmitted(BondDataType type, ushort id, Metadata metadata) {}
public Expression Container(BondDataType? expectedType, ContainerHandler handler) { if (schemaType.IsBondNullable()) return Nullable(handler); if (schemaType.IsBondBlob()) return BlobContainer(handler); var itemType = schemaType.GetValueType(); ContainerItemHandler itemHandler = (item, next, count) => handler( new ObjectParser(this, item, itemType), Expression.Constant(itemType.GetBondDataType()), next, count); if (value.Type.IsArray) return ArrayContainer(itemHandler); if (value.Type.IsGenericType()) { if (typeof(IList<>).MakeGenericType(value.Type.GetGenericArguments()[0]).IsAssignableFrom(value.Type)) return ListContainer(itemHandler); if (typeof(LinkedList<>) == value.Type.GetGenericTypeDefinition()) return LinkedListContainer(itemHandler); } return EnumerableContainer(itemHandler); }
public void WriteContainerBegin(int count, BondDataType elementType) { }
static Expression InvalidType(BondDataType expectedType, Expression valueType) { return(ThrowExpression.InvalidTypeException(Expression.Constant(expectedType), valueType)); }
public void WriteContainerBegin(int count, BondDataType keyType, BondDataType valueType) { throw new NotImplementedException(); }
public void WriteFieldOmitted(BondDataType type, ushort id, Metadata metadata) { throw new NotImplementedException(); }
public void WriteContainerBegin(int count, BondDataType elementType) { writer.WriteStartArray(); }
public void WriteContainerBegin(int count, BondDataType keyType, BondDataType valueType) { writer.WriteStartArray(); }
public void WriteFieldBegin(BondDataType type, ushort id, Metadata metadata) { string name; writer.WritePropertyName(metadata.attributes.TryGetValue(NameAttribute, out name) ? name : metadata.name); }
public Expression Scalar(Expression valueType, BondDataType expectedType, ValueHandler handler) { return(MatchOrCompatible(valueType, expectedType, type => handler(reader.Read(type)))); }
public abstract Expression Scalar(Expression valueType, BondDataType expectedType, ValueHandler handler);
public Expression Container(BondDataType? expectedType, ContainerHandler handler) { throw new System.NotImplementedException(); }
public Expression Scalar(Expression valueType, BondDataType expectedType, ValueHandler handler) { Debug.Assert(expectedType == schemaType.GetBondDataType()); return handler(typeAlias.Convert(value, schemaType)); }
public Expression Map(BondDataType? expectedKeyType, BondDataType? expectedValueType, MapHandler handler) { throw new System.NotImplementedException(); }
public Expression Scalar(Expression valueType, BondDataType expectedType, ValueHandler handler) { throw new System.NotImplementedException(); }
public void ReadContainerBegin(out int count, out BondDataType elementType) { elementType = (BondDataType)input.ReadUInt8(); count = (int)input.ReadVarUInt32(); }
static void ThrowInvalidTypeException(BondDataType expectedType, BondDataType actualType) { throw new InvalidDataException(string.Format("Invalid type {0}, expected {1}", actualType, expectedType)); }
public Expression Scalar(Expression valueType, BondDataType expectedType, ValueHandler handler) { Debug.Assert(valueType is ConstantExpression); return(handler(reader.Read((BondDataType)(valueType as ConstantExpression).Value))); }
public void WriteFieldOmitted(BondDataType dataType, ushort id, Metadata metadata) { // Simple doesn't support omitting fields so instead we write the default value Audit.ArgRule(!metadata.default_value.nothing, "Field set to nothing can't be serialized."); switch (dataType) { case BondDataType.BT_BOOL: WriteBool(0 != metadata.default_value.uint_value); break; case BondDataType.BT_UINT8: WriteUInt8((byte)metadata.default_value.uint_value); break; case BondDataType.BT_UINT16: WriteUInt16((UInt16)metadata.default_value.uint_value); break; case BondDataType.BT_UINT32: WriteUInt32((UInt32)metadata.default_value.uint_value); break; case BondDataType.BT_UINT64: WriteUInt64(metadata.default_value.uint_value); break; case BondDataType.BT_FLOAT: WriteFloat((float)metadata.default_value.double_value); break; case BondDataType.BT_DOUBLE: WriteDouble(metadata.default_value.double_value); break; case BondDataType.BT_STRING: WriteString(metadata.default_value.string_value); break; case BondDataType.BT_LIST: case BondDataType.BT_SET: case BondDataType.BT_MAP: WriteContainerBegin(0, dataType); break; case BondDataType.BT_INT8: WriteInt8((sbyte)metadata.default_value.int_value); break; case BondDataType.BT_INT16: WriteInt16((Int16)metadata.default_value.int_value); break; case BondDataType.BT_INT32: WriteInt32((Int32)metadata.default_value.int_value); break; case BondDataType.BT_INT64: WriteInt64(metadata.default_value.int_value); break; case BondDataType.BT_WSTRING: WriteWString(metadata.default_value.wstring_value); break; default: Throw.InvalidBondDataType(dataType); break; } }
public void WriteContainerBegin(int count, BondDataType keyType, BondDataType valueType) { output.WriteUInt8((byte)keyType); output.WriteUInt8((byte)valueType); output.WriteVarUInt32((uint)count); }
public static void InvalidBondDataType(BondDataType type) { throw new InvalidDataException(string.Format("Invalid BondDataType {0}", type)); }
public void WriteFieldBegin(BondDataType dataType, ushort id, Metadata metadata) { }
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())); }
// Generate expression to handle exact match or compatible type private static Expression MatchOrCompatible(Expression valueType, BondDataType expectedType, TypeHandlerCompiletime handler) { return(MatchOrElse(valueType, expectedType, handler, TryCompatible(valueType, expectedType, handler))); }
public void WriteContainerBegin(int count, BondDataType keyType, BondDataType valueType) { }
public void WriteFieldBegin(BondDataType type, ushort id, Metadata metadata) { if (id <= 5) { AddBytes(1); } else if (id <= 0xFF) { AddBytes(2); } else { AddBytes(3); } }
public void WriteContainerBegin(int count, BondDataType elementType) { if (count < 7) { AddBytes(1); } else { AddBytes(1); AddVarUInt32((uint)count); } }
public Expression Map(BondDataType? expectedKeyType, BondDataType? expectedValueType, MapHandler handler) { Debug.Assert(schemaType.IsBondMap()); var itemType = schemaType.GetKeyValueType(); return EnumerableContainer((item, next, count) => handler( new ObjectParser(this, Expression.Property(item, "Key"), itemType.Key), new ObjectParser(this, Expression.Property(item, "Value"), itemType.Value), Expression.Constant(itemType.Key.GetBondDataType()), Expression.Constant(itemType.Value.GetBondDataType()), next, Expression.Empty(), count)); }
public void WriteContainerBegin(int count, BondDataType keyType, BondDataType valueType) { AddBytes(2); AddVarUInt32((uint)count); }
public Expression WriteFieldOmitted(BondDataType type, ushort id, Metadata metadata) { return(Expression.Call(writer, fieldOmitted, Expression.Constant(type), Expression.Constant(id), Expression.Constant(metadata))); }
public void WriteFieldBegin(BondDataType type, ushort id, Metadata metadata) { output.WriteUInt8((byte)type); output.WriteUInt16(id); }
public void WriteContainerBegin(int count, BondDataType elementType) { output.WriteUInt8((byte)elementType); output.WriteVarUInt32((uint)count); }