Exemplo n.º 1
0
        /// <summary>
        /// Test for a typical json-style message
        /// </summary>
        public static void Combined1(IIonReader reader)
        {
            Assert.AreEqual(IonType.Struct, reader.MoveNext());
            reader.StepIn();
            Assert.IsTrue(reader.IsInStruct);
            Assert.AreEqual(IonType.Struct, reader.MoveNext());
            Assert.AreEqual("menu", reader.CurrentFieldName);
            reader.StepIn();
            Assert.AreEqual(IonType.String, reader.MoveNext());
            Assert.AreEqual("id", reader.CurrentFieldName);
            Assert.AreEqual("file", reader.StringValue());
            Assert.AreEqual(IonType.List, reader.MoveNext());
            Assert.AreEqual("popup", reader.CurrentFieldName);
            reader.StepIn();
            Assert.AreEqual(IonType.String, reader.MoveNext());
            Assert.AreEqual("Open", reader.StringValue());
            Assert.AreEqual(IonType.String, reader.MoveNext());
            Assert.AreEqual("Load", reader.StringValue());
            Assert.AreEqual(IonType.String, reader.MoveNext());
            Assert.AreEqual("Close", reader.StringValue());
            reader.StepOut();

            Assert.AreEqual(IonType.Struct, reader.MoveNext());
            Assert.AreEqual("deep1", reader.CurrentFieldName);
            reader.StepIn();
            Assert.IsTrue(reader.IsInStruct);
            Assert.AreEqual(IonType.Struct, reader.MoveNext());
            Assert.AreEqual("deep2", reader.CurrentFieldName);
            reader.StepIn();
            Assert.AreEqual(IonType.Struct, reader.MoveNext());
            Assert.AreEqual("deep3", reader.CurrentFieldName);
            reader.StepIn();
            Assert.AreEqual(IonType.String, reader.MoveNext());
            Assert.AreEqual("deep4val", reader.CurrentFieldName);
            Assert.AreEqual("enddeep", reader.StringValue());
            reader.StepOut();
            reader.StepOut();
            reader.StepOut();

            Assert.AreEqual(IonType.List, reader.MoveNext());
            Assert.AreEqual("positions", reader.CurrentFieldName);
            reader.StepIn();
            Assert.AreEqual(IonType.Int, reader.MoveNext());
            Assert.AreEqual(1234, reader.IntValue());
            Assert.AreEqual(IonType.Int, reader.MoveNext());
            Assert.AreEqual(5678, reader.IntValue());
            Assert.AreEqual(IonType.Int, reader.MoveNext());
            Assert.AreEqual(90, reader.IntValue());
            reader.StepOut();
            reader.StepOut();
            reader.StepOut();

            Assert.AreEqual(0, reader.CurrentDepth);
        }
Exemplo n.º 2
0
        /// <remarks>See text/twolayer.ion for content</remarks>
        public static void TwoLayer_TestStepoutSkip(IIonReader reader)
        {
            Assert.AreEqual(IonType.Struct, reader.MoveNext());
            reader.StepIn();
            Assert.AreEqual(IonType.String, reader.MoveNext());
            Assert.AreEqual("open", reader.CurrentFieldName);

            Assert.AreEqual(IonType.Struct, reader.MoveNext());
            Assert.AreEqual("structure", reader.CurrentFieldName);
            reader.StepIn();

            Assert.AreEqual(IonType.String, reader.MoveNext());
            Assert.AreEqual("open", reader.CurrentFieldName);
            Assert.AreEqual(IonType.Struct, reader.MoveNext());
            Assert.AreEqual("structure", reader.CurrentFieldName);
            reader.StepIn();

            Assert.AreEqual(IonType.Int, reader.MoveNext());
            Assert.AreEqual("int", reader.CurrentFieldName);
            //1st skip
            reader.StepOut();

            Assert.AreEqual(IonType.Int, reader.MoveNext());
            Assert.AreEqual("int", reader.CurrentFieldName);
            //2nd skip
            reader.StepOut();

            Assert.AreEqual(IonType.String, reader.MoveNext());
            Assert.AreEqual("this is a string", reader.StringValue());
            Assert.AreEqual(IonType.Bool, reader.MoveNext());
            Assert.AreEqual(true, reader.BoolValue());
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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;
            }
        }
Exemplo n.º 5
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");
            }
        }
        private static IonType buildData1(
            List <KeyValuePair <string, string> > document,
            IIonReader reader,
            IonType type)
        {
            if (type == IonType.Struct)
            {
                reader.StepIn();
                while ((type = reader.MoveNext()) != IonType.None)
                {
                    switch (type)
                    {
                    case IonType.Bool:
                        document.Add(new KeyValuePair <string, string>(reader.CurrentFieldName, Convert.ToString(reader.BoolValue())));
                        continue;

                    case IonType.Int:
                        document.Add(new KeyValuePair <string, string>(reader.CurrentFieldName, Convert.ToString(reader.IntValue())));
                        continue;

                    case IonType.Decimal:
                        document.Add(new KeyValuePair <string, string>(reader.CurrentFieldName, Convert.ToString((object)reader.DecimalValue())));
                        continue;

                    case IonType.Timestamp:
                        document.Add(new KeyValuePair <string, string>(reader.CurrentFieldName, Convert.ToString((object)reader.TimestampValue())));
                        continue;

                    case IonType.String:
                        document.Add(new KeyValuePair <string, string>(reader.CurrentFieldName, reader.StringValue()));
                        continue;

                    case IonType.Struct:
                        int num = (int)ConnectionService.buildData1(document, reader, type);
                        continue;

                    default:
                        continue;
                    }
                }
                reader.StepOut();
            }
            return(type);
        }
        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));
        }
 /// <summary>
 /// Deserialize string value.
 /// </summary>
 ///
 /// <param name="reader">The Ion reader to be used for deserialization.</param>
 ///
 /// <returns>The deserialized string value.</returns>
 public override string Deserialize(IIonReader reader)
 {
     return(reader.StringValue());
 }
Exemplo n.º 9
0
        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();
                }
            }
        }
Exemplo n.º 10
0
        public static ISymbolTable ImportReaderTable(IIonReader reader, ICatalog catalog, bool isOnStruct)
        {
            var table      = reader.GetSymbolTable() as ReaderLocalTable ?? new ReaderLocalTable(reader.GetSymbolTable());
            var imports    = table.Imports;
            var symbols    = table.OwnSymbols;
            var newSymbols = new List <string>();

            if (!isOnStruct)
            {
                reader.MoveNext();
            }

            Debug.Assert(
                reader.CurrentType == IonType.Struct,
                "Invalid symbol table image passed in reader " + reader.CurrentType + " encountered when a struct was expected.");

            Debug.Assert(
                SystemSymbols.IonSymbolTable.Equals(reader.GetTypeAnnotations()[0]),
                "Local symbol tables must be annotated by " + SystemSymbols.IonSymbolTable + ".");

            // Assume that we're standing before a struct.
            reader.StepIn();

            IonType fieldType;
            bool    foundImport = false;
            bool    foundLocals = false;

            while ((fieldType = reader.MoveNext()) != IonType.None)
            {
                if (reader.CurrentIsNull)
                {
                    continue;
                }

                var sid = reader.GetFieldNameSymbol().Sid;
                if (sid == SymbolToken.UnknownSid)
                {
                    // This is a user-defined IonReader or a pure DOM, fall
                    // back to text.
                    sid = SystemSymbols.ResolveSidForSymbolTableField(reader.CurrentFieldName);
                }

                switch (sid)
                {
                case SystemSymbols.SymbolsSid:
                    // As per the spec, other field types are treated as
                    // empty lists.
                    if (foundLocals)
                    {
                        throw new IonException("Multiple symbol fields found within a single local symbol table.");
                    }

                    foundLocals = true;
                    if (fieldType == IonType.List)
                    {
                        ReadSymbolList(reader, newSymbols);
                    }

                    break;

                case SystemSymbols.ImportsSid:
                    if (foundImport)
                    {
                        throw new IonException("Multiple imports fields found within a single local symbol table.");
                    }

                    foundImport = true;
                    if (fieldType == IonType.List)
                    {
                        // List of symbol tables to imports.
                        ReadImportList(reader, catalog, imports);
                    }

                    // Trying to import the current table.
                    else if (fieldType == IonType.Symbol &&
                             reader.GetSymbolTable().IsLocal &&
                             (SystemSymbols.IonSymbolTable.Equals(reader.StringValue()) || reader.IntValue() == SystemSymbols.IonSymbolTableSid))
                    {
                        var currentSymbolTable = reader.GetSymbolTable();
                        var declaredSymbols    = currentSymbolTable.GetDeclaredSymbolNames();

                        if (foundLocals)
                        {
                            // Ordering matters. Maintain order as 'symbols'
                            // and 'imports' fields can come in any order.
                            newSymbols.InsertRange(0, declaredSymbols);
                        }
                        else
                        {
                            newSymbols.AddRange(declaredSymbols);
                        }
                    }

                    break;

                default:
                    // As per the spec, any other field is ignored.
                    break;
                }
            }

            reader.StepOut();

            symbols.Clear();

            // If there were prior imports and now only a system table is
            // seen, then start fresh again as prior imports no longer matter.
            if (imports.Count > 1 && !foundImport && foundLocals)
            {
                imports.RemoveAll(symbolTable => symbolTable.IsSubstitute == true);
            }

            symbols.AddRange(newSymbols);

            table.Refresh();

            return(table);
        }
Exemplo n.º 11
0
        public override string Deserialize(IIonReader reader)
        {
            string result = reader.StringValue();

            return(result.Substring(1, result.Length - 2));
        }