Exemple #1
0
        public static void FlatScalar(IIonReader reader)
        {
            //a flat struct of scalar values:
            //boolean:true
            //str:"yes"
            //integer:123456
            //longInt:int.Max*2
            //bigInt:long.Max*10
            //double:2213.1267567f
            reader.MoveNext();
            Assert.AreEqual(IonType.Struct, reader.CurrentType);
            reader.StepIn();
            Assert.IsTrue(reader.IsInStruct);
            Assert.AreEqual(1, reader.CurrentDepth);

            reader.MoveNext();
            Assert.AreEqual("boolean", reader.CurrentFieldName);
            Assert.AreEqual(IonType.Bool, reader.CurrentType);
            Assert.IsTrue(reader.BoolValue());

            reader.MoveNext();
            Assert.AreEqual("str", reader.CurrentFieldName);
            Assert.AreEqual(IonType.String, reader.CurrentType);
            Assert.AreEqual("yes", reader.StringValue());

            reader.MoveNext();
            Assert.AreEqual("integer", reader.CurrentFieldName);
            Assert.AreEqual(IonType.Int, reader.CurrentType);
            Assert.AreEqual(123456, reader.IntValue());

            reader.MoveNext();
            Assert.AreEqual("longInt", reader.CurrentFieldName);
            Assert.AreEqual(IonType.Int, reader.CurrentType);
            Assert.AreEqual((long)int.MaxValue * 2, reader.LongValue());

            reader.MoveNext();
            Assert.AreEqual("bigInt", reader.CurrentFieldName);
            Assert.AreEqual(IonType.Int, reader.CurrentType);
            Assert.AreEqual(BigInteger.Multiply(new BigInteger(long.MaxValue), 10), reader.BigIntegerValue());

            reader.MoveNext();
            Assert.AreEqual("double", reader.CurrentFieldName);
            Assert.AreEqual(IonType.Float, reader.CurrentType);
            Assert.AreEqual(2213.1267567, reader.DoubleValue());

            Assert.AreEqual(IonType.None, reader.MoveNext());
            reader.StepOut();
            Assert.AreEqual(0, reader.CurrentDepth);
        }
        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;
            }
        }
Exemple #3
0
        private static void CompareScalars(IonType t, bool isNull, IIonReader it1, IIonReader it2)
        {
            switch (t)
            {
            case IonType.Bool:
            {
                if (!isNull)
                {
                    Assert.AreEqual(it1.BoolValue(), it2.BoolValue());
                }

                break;
            }

            case IonType.Int:
            {
                if (!isNull)
                {
                    Assert.AreEqual(it1.BigIntegerValue(), it2.BigIntegerValue());
                }

                break;
            }

            case IonType.Float:
            {
                if (!isNull)
                {
                    double v1 = it1.DoubleValue();
                    double v2 = it2.DoubleValue();
                    if (double.IsNaN(v1) && double.IsNaN(v2))
                    {
                        Assert.AreEqual(v1, v2);
                    }
                    else if (double.IsNaN(v1) || double.IsNaN(v2))
                    {
                        Assert.AreNotEqual(v1, v2, 0);
                    }
                    else
                    {
                        Assert.AreEqual(v1, v2, 0);
                        // The last param is a delta, and we want exact match.
                    }
                }

                break;
            }

            case IonType.Decimal:
            {
                if (!isNull)
                {
                    BigDecimal bigDec1 = it1.DecimalValue();
                    BigDecimal bigDec2 = it2.DecimalValue();
                    AssertPreciselyEquals(bigDec1, bigDec2);

                    try
                    {
                        decimal dec1 = bigDec1.ToDecimal();
                        decimal dec2 = bigDec2.ToDecimal();
                        Assert.AreEqual(dec1, dec2);

                        Assert.AreEqual(decimal.ToDouble(dec1), decimal.ToDouble(dec2));
                        Assert.AreEqual(decimal.ToInt32(dec1), decimal.ToInt32(dec2));
                        Assert.AreEqual(decimal.ToInt64(dec1), decimal.ToInt64(dec2));
                    }
                    catch (OverflowException)
                    {
                        // Certain Ion Decimals in test file exceed C# decmial range. Just AssertPreciselyEquals.
                    }
                }

                break;
            }

            case IonType.Timestamp:
            {
                if (!isNull)
                {
                    Timestamp t1 = it1.TimestampValue();
                    Timestamp t2 = it2.TimestampValue();
                    Assert.AreEqual(t1, t2);
                }

                break;
            }

            case IonType.String:
            {
                string s1 = it1.StringValue();
                string s2 = it2.StringValue();
                Assert.AreEqual(s1, s2);
                break;
            }

            case IonType.Symbol:
            {
                SymbolToken tok1 = it1.SymbolValue();
                SymbolToken tok2 = it2.SymbolValue();
                if (isNull)
                {
                    Assert.IsNull(tok1.Text);
                    Assert.IsNull(tok2.Text);
                }
                else if (tok1.Text == null || tok2.Text == null)
                {
                    Assert.AreEqual(tok1.Sid, tok2.Sid, "sids");
                }
                else
                {
                    string s1 = tok1.Text;
                    string s2 = tok2.Text;
                    Assert.AreEqual(s1, s2);
                }

                break;
            }

            case IonType.Blob:
            case IonType.Clob:
            {
                if (!isNull)
                {
                    byte[] b1 = it1.NewByteArray();
                    byte[] b2 = it2.NewByteArray();
                    Assert.IsTrue(b1 != null && b2 != null);
                    Assert.IsTrue(b1.Length == b2.Length);
                    for (int ii = 0; ii < b1.Length; ii++)
                    {
                        byte v1 = b1[ii];
                        byte v2 = b2[ii];
                        Assert.AreEqual(v1, v2);
                    }
                }

                break;
            }

            default:
                throw new InvalidOperationException("iterated to a type that's not expected");
            }
        }
 /// <summary>
 /// Deserialize float value.
 /// </summary>
 ///
 /// <param name="reader">The Ion reader to be used for deserialization.</param>
 ///
 /// <returns>The deserialized float value.</returns>
 public override float Deserialize(IIonReader reader)
 {
     return(Convert.ToSingle(reader.DoubleValue()));
 }
        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));
        }
 public override double Deserialize(IIonReader reader)
 {
     return(-reader.DoubleValue());
 }
        private void InternalWriteValue(IIonReader reader, int depth = 0)
        {
            IonType type = reader.CurrentType;

            if (type == IonType.None)
            {
                return;
            }

            if (depth > 0)
            {
                string fieldName = reader.CurrentFieldName;
                if (fieldName != null)
                {
                    this.SetFieldName(fieldName);
                }
            }

            foreach (var annotation in reader.GetTypeAnnotations())
            {
                this.AddTypeAnnotationSymbol(annotation);
            }

            if (reader.CurrentIsNull)
            {
                this.WriteNull(type);
            }
            else
            {
                switch (type)
                {
                case IonType.Bool:
                    this.WriteBool(reader.BoolValue());
                    break;

                case IonType.Int:
                    this.WriteInt(reader.BigIntegerValue());
                    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:
                    this.StepIn(IonType.List);
                    break;

                case IonType.Sexp:
                    this.StepIn(IonType.Sexp);
                    break;

                case IonType.Struct:
                    this.StepIn(IonType.Struct);
                    break;

                default:
                    throw new InvalidOperationException("Unexpected type '" + type + "'");
                }

                if (type.IsContainer())
                {
                    reader.StepIn();
                    this.InternalWriteValues(reader, depth + 1);
                    this.StepOut();
                    reader.StepOut();
                }
            }
        }