Exemplo n.º 1
0
        public static void SingleNumber(IIonReader reader, long value)
        {
            //a single number
            Assert.AreEqual(IonType.Int, reader.MoveNext());
            switch (reader.GetIntegerSize())
            {
            case IntegerSize.Unknown:
                break;

            case IntegerSize.Int:
                Assert.AreEqual(value, reader.IntValue());
                break;

            case IntegerSize.Long:
                Assert.AreEqual(value, reader.LongValue());
                break;

            case IntegerSize.BigInteger:
                Assert.Fail("not testing big int");
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemplo n.º 2
0
        private void WriteValueRecursively(IonType type, IIonReader reader)
        {
            this.TryWriteFieldName(reader);
            this.TryWriteAnnotationSymbols(reader);

            if (reader.CurrentIsNull)
            {
                this.WriteNull(type);
                return;
            }

            switch (type)
            {
            case IonType.Bool:
                this.WriteBool(reader.BoolValue());
                break;

            case IonType.Int:
                switch (reader.GetIntegerSize())
                {
                case IntegerSize.Int:
                    this.WriteInt(reader.IntValue());
                    break;

                case IntegerSize.Long:
                    this.WriteInt(reader.LongValue());
                    break;

                case IntegerSize.BigInteger:
                    this.WriteInt(reader.BigIntegerValue());
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                break;

            case IonType.Float:
                this.WriteFloat(reader.DoubleValue());
                break;

            case IonType.Decimal:
                this.WriteDecimal(reader.DecimalValue());
                break;

            case IonType.Timestamp:
                this.WriteTimestamp(reader.TimestampValue());
                break;

            case IonType.Symbol:
                this.WriteSymbolToken(reader.SymbolValue());
                break;

            case IonType.String:
                this.WriteString(reader.StringValue());
                break;

            case IonType.Clob:
                this.WriteClob(reader.NewByteArray());
                break;

            case IonType.Blob:
                this.WriteBlob(reader.NewByteArray());
                break;

            case IonType.List:
            case IonType.Sexp:
            case IonType.Struct:
                this.WriteContainerRecursively(type, reader);
                break;
            }
        }
        private static bool TryDeserializeScalar(IIonReader reader, Type type, IScalarConverter scalarConverter, ref object result)
        {
            if (type == typeof(string))
            {
                if (reader.CurrentType != IonType.String && reader.CurrentType != IonType.Null)
                {
                    return(false);
                }
                result = reader.CurrentIsNull ? null : reader.StringValue();
                return(true);
            }

            if (reader.CurrentIsNull)
            {
                if (type.IsValueType)
                {
                    return(false);
                }

                result = null;
                return(true);
            }

            //check for enum/symbol
            if (type.IsEnum)
            {
                if (reader.CurrentType != IonType.Symbol)
                {
                    goto NoMatch;
                }
                var symbolText = reader.SymbolValue().Text;
                return(Enum.TryParse(type, symbolText, out result));
            }

            if (type == typeof(bool))
            {
                if (reader.CurrentType != IonType.Bool)
                {
                    goto NoMatch;
                }
                result = reader.BoolValue();
                return(true);
            }

            if (type == typeof(int))
            {
                if (reader.CurrentType != IonType.Int)
                {
                    goto NoMatch;
                }
                switch (reader.GetIntegerSize())
                {
                case IntegerSize.Int:
                    result = reader.IntValue();
                    return(true);

                case IntegerSize.Long:
                case IntegerSize.BigInteger:
                    throw new OverflowException($"Encoded value is too big for int32");

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            if (type == typeof(long))
            {
                if (reader.CurrentType != IonType.Int)
                {
                    goto NoMatch;
                }
                switch (reader.GetIntegerSize())
                {
                case IntegerSize.Int:
                    result = (long)reader.IntValue();
                    return(true);

                case IntegerSize.Long:
                    result = reader.LongValue();
                    return(true);

                case IntegerSize.BigInteger:
                    throw new OverflowException($"Encoded value is too big for int32");

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            if (type == typeof(BigInteger))
            {
                if (reader.CurrentType != IonType.Int)
                {
                    goto NoMatch;
                }
                switch (reader.GetIntegerSize())
                {
                case IntegerSize.Int:
                    result = new BigInteger(reader.IntValue());
                    return(true);

                case IntegerSize.Long:
                    result = new BigInteger(reader.LongValue());
                    return(true);

                case IntegerSize.BigInteger:
                    result = reader.BigIntegerValue();
                    return(true);

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            if (type == typeof(float))
            {
                if (reader.CurrentType != IonType.Float)
                {
                    goto NoMatch;
                }
                result = (float)reader.DoubleValue();
                return(true);
            }

            if (type == typeof(double))
            {
                if (reader.CurrentType != IonType.Float)
                {
                    goto NoMatch;
                }
                result = reader.DoubleValue();
                return(true);
            }

            if (type == typeof(decimal))
            {
                if (reader.CurrentType != IonType.Decimal)
                {
                    goto NoMatch;
                }
                result = reader.DecimalValue();
                return(true);
            }

            if (type == typeof(DateTime))
            {
                if (reader.CurrentType != IonType.Timestamp)
                {
                    goto NoMatch;
                }
                result = reader.TimestampValue().DateTime;
                return(true);
            }

            if (type == typeof(DateTimeOffset))
            {
                if (reader.CurrentType != IonType.Timestamp)
                {
                    goto NoMatch;
                }
                result = reader.TimestampValue().AsDateTimeOffset();
                return(true);
            }


NoMatch:
            //here means we don't know , try the scalar converter
            return(scalarConverter != null && reader.TryConvertTo(type, scalarConverter, out result));
        }