Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
 public void ReadContainerBegin(out int count, out BondDataType keyType, out BondDataType valueType)
 {
     keyType   = (BondDataType)input.ReadUInt8();
     valueType = (BondDataType)input.ReadUInt8();
     count     = (int)input.ReadVarUInt32();
 }
Exemplo n.º 3
0
 public void WriteFieldOmitted(BondDataType dataType, ushort id, Metadata metadata)
 {
 }
Exemplo n.º 4
0
 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);
 }
Exemplo n.º 6
0
 public void WriteFieldOmitted(BondDataType type, ushort id, Metadata metadata)
 {}
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
 public void WriteContainerBegin(int count, BondDataType elementType)
 { }
Exemplo n.º 9
0
 static Expression InvalidType(BondDataType expectedType, Expression valueType)
 {
     return(ThrowExpression.InvalidTypeException(Expression.Constant(expectedType), valueType));
 }
Exemplo n.º 10
0
 public void WriteContainerBegin(int count, BondDataType keyType, BondDataType valueType)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 11
0
 public void WriteFieldOmitted(BondDataType type, ushort id, Metadata metadata)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 12
0
 public void WriteContainerBegin(int count, BondDataType elementType)
 {
     writer.WriteStartArray();
 }
Exemplo n.º 13
0
 public void WriteContainerBegin(int count, BondDataType keyType, BondDataType valueType)
 {
     writer.WriteStartArray();
 }
Exemplo n.º 14
0
        public void WriteFieldBegin(BondDataType type, ushort id, Metadata metadata)
        {
            string name;

            writer.WritePropertyName(metadata.attributes.TryGetValue(NameAttribute, out name) ? name : metadata.name);
        }
 public void WriteContainerBegin(int count, BondDataType keyType, BondDataType valueType)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 16
0
 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);
Exemplo n.º 18
0
 public void WriteFieldBegin(BondDataType dataType, ushort id, Metadata metadata)
 {
     writer.WriteStartElement(Prefix, metadata.name, null);
 }
Exemplo n.º 19
0
 public Expression Container(BondDataType? expectedType, ContainerHandler handler)
 {
     throw new System.NotImplementedException();
 }
Exemplo n.º 20
0
 public Expression Scalar(Expression valueType, BondDataType expectedType, ValueHandler handler)
 {
     Debug.Assert(expectedType == schemaType.GetBondDataType());
     return handler(typeAlias.Convert(value, schemaType));
 }
Exemplo n.º 21
0
 public Expression Map(BondDataType? expectedKeyType, BondDataType? expectedValueType, MapHandler handler)
 {
     throw new System.NotImplementedException();
 }
Exemplo n.º 22
0
 public void WriteContainerBegin(int count, BondDataType elementType)
 {
     writer.WriteStartArray();
 }
Exemplo n.º 23
0
 public Expression Scalar(Expression valueType, BondDataType expectedType, ValueHandler handler)
 {
     throw new System.NotImplementedException();
 }
Exemplo n.º 24
0
 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));
 }
Exemplo n.º 26
0
        public Expression Scalar(Expression valueType, BondDataType expectedType, ValueHandler handler)
        {
            Debug.Assert(valueType is ConstantExpression);

            return(handler(reader.Read((BondDataType)(valueType as ConstantExpression).Value)));
        }
Exemplo n.º 27
0
        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;
            }
        }
Exemplo n.º 28
0
 public void WriteContainerBegin(int count, BondDataType keyType, BondDataType valueType)
 {
     output.WriteUInt8((byte)keyType);
     output.WriteUInt8((byte)valueType);
     output.WriteVarUInt32((uint)count);
 }
Exemplo n.º 29
0
 public static void InvalidBondDataType(BondDataType type)
 {
     throw new InvalidDataException(string.Format("Invalid BondDataType {0}", type));
 }
 public void WriteFieldOmitted(BondDataType type, ushort id, Metadata metadata)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 31
0
 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()));
        }
Exemplo n.º 33
0
 public void WriteContainerBegin(int count, BondDataType elementType)
 {
 }
Exemplo n.º 34
0
 // 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)));
 }
Exemplo n.º 35
0
 public void WriteContainerBegin(int count, BondDataType keyType, BondDataType valueType)
 {
 }
Exemplo n.º 36
0
 public void WriteContainerBegin(int count, BondDataType keyType, BondDataType valueType)
 { }
Exemplo n.º 37
0
 public void WriteFieldBegin(BondDataType type, ushort id, Metadata metadata)
 {
     if (id <= 5)
     {
         AddBytes(1);
     }
     else if (id <= 0xFF)
     {
         AddBytes(2);
     }
     else
     {
         AddBytes(3);
     }
 }
Exemplo n.º 38
0
 static void ThrowInvalidTypeException(BondDataType expectedType, BondDataType actualType)
 {
     throw new InvalidDataException(string.Format("Invalid type {0}, expected {1}", actualType, expectedType));
 }
Exemplo n.º 39
0
 public void WriteContainerBegin(int count, BondDataType elementType)
 {
     if (count < 7)
     {
         AddBytes(1);
     }
     else
     {
         AddBytes(1);
         AddVarUInt32((uint)count);
     }
 }
Exemplo n.º 40
0
        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));
        }
Exemplo n.º 41
0
 public void WriteContainerBegin(int count, BondDataType keyType, BondDataType valueType)
 {
     AddBytes(2);
     AddVarUInt32((uint)count);
 }
Exemplo n.º 42
0
 public void WriteFieldBegin(BondDataType type, ushort id, Metadata metadata)
 {
     string name;
     writer.WritePropertyName(metadata.attributes.TryGetValue(NameAttribute, out name) ? name : metadata.name);
 }
Exemplo n.º 43
0
 public Expression WriteFieldOmitted(BondDataType type, ushort id, Metadata metadata)
 {
     return(Expression.Call(writer, fieldOmitted, Expression.Constant(type), Expression.Constant(id), Expression.Constant(metadata)));
 }
Exemplo n.º 44
0
 public void WriteContainerBegin(int count, BondDataType keyType, BondDataType valueType)
 {
     writer.WriteStartArray();
 }
Exemplo n.º 45
0
 public void WriteFieldBegin(BondDataType type, ushort id, Metadata metadata)
 {
     output.WriteUInt8((byte)type);
     output.WriteUInt16(id);
 }
Exemplo n.º 46
0
 public void WriteContainerBegin(int count, BondDataType elementType)
 {
     output.WriteUInt8((byte)elementType);
     output.WriteVarUInt32((uint)count);
 }
Exemplo n.º 47
0
 public void WriteContainerBegin(int count, BondDataType keyType, BondDataType valueType)
 {
     AddBytes(2);
     AddVarUInt32((uint)count);
 }