/// <summary>
 /// Deserialize CLOB value.
 /// </summary>
 ///
 /// <param name="reader">The Ion reader to be used for deserialization.</param>
 ///
 /// <returns>The deserialized CLOB value.</returns>
 public override string Deserialize(IIonReader reader)
 {
     byte[] clob = new byte[reader.GetLobByteSize()];
     reader.GetBytes(clob);
     return(Encoding.UTF8.GetString(clob));
 }
示例#2
0
 //empty content
 public static void Empty(IIonReader reader)
 {
     //also convieniently testing symtab
     Assert.IsNotNull(reader.GetSymbolTable());
     Assert.AreEqual(IonType.None, reader.MoveNext());
 }
        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));
        }
示例#4
0
 public static void AssertFlatStruct(IIonReader reader, IEnumerable <(string key, object value)> kvps)
        private void WriteValueRecursively(IonType type, IIonReader reader)
        {
            TryWriteFieldName(reader);
            TryWriteAnnotations(reader);

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

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

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

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

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

                default:
                    throw new ArgumentOutOfRangeException();
                }

                break;

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

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

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

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

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

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

            case IonType.List:
            case IonType.Sexp:
            case IonType.Struct:
                WriteContainerRecursively(type, reader);
                break;
            }
        }
 /// <summary>
 /// Deserialize byte array.
 /// </summary>
 ///
 /// <param name="reader">The Ion reader to be used for deserialization.</param>
 ///
 /// <returns>The deserialized byte array.</returns>
 public override byte[] Deserialize(IIonReader reader)
 {
     byte[] blob = new byte[reader.GetLobByteSize()];
     reader.GetBytes(blob);
     return(blob);
 }
 public override string Deserialize(IIonReader reader)
 {
     return("Springgina");
 }
 public override BigDecimal Deserialize(IIonReader reader)
 {
     return(-reader.DecimalValue());
 }
        public override SymbolToken Deserialize(IIonReader reader)
        {
            var token = reader.SymbolValue();

            return(new SymbolToken(token.Text.ToUpper(), token.Sid));
        }
示例#10
0
 public void WriteValues(IIonReader reader)
 {
     this.InternalWriteValues(reader);
 }
示例#11
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();
                }
            }
        }
示例#12
0
 /// <summary>
 /// Creates ref-struct that represents key-value property.
 /// </summary>
 /// <param name="reader">Instance of <see cref="IIonReader"/> with current state <see cref="IIonReader.IsProperty"/>=<see langword="true"/>.</param>
 /// <param name="propertySeparator">Character that separates key from value. By default it is used = character.</param>
 /// <returns>An instance of <see cref="IonProperty"/> that has access to key and value spans in current line.</returns>
 /// <exception cref="InvalidOperationException">Reader is in not correct state. That means <see cref="IIonReader.IsProperty"/> is <see langword="false"/>.</exception>
 public static IonProperty ReadProperty(this IIonReader reader, char propertySeparator = '=')
 {
     return(new IonProperty(reader, propertySeparator));
 }
 /// <inheritdoc/>
 object IIonSerializer.Deserialize(IIonReader reader)
 {
     return(this.Deserialize(reader));
 }
 /// <summary>
 /// Deserialize value.
 /// </summary>
 ///
 /// <param name="reader">The Ion reader to be used for deserialization.</param>
 ///
 /// <returns>The deserialized value.</returns>
 public abstract T Deserialize(IIonReader reader);
 public override float Deserialize(IIonReader reader)
 {
     return(-Convert.ToSingle(reader.DoubleValue()));
 }
 public override DateTime Deserialize(IIonReader reader)
 {
     return(reader.TimestampValue().DateTimeValue.AddDays(1));
 }
 public override double Deserialize(IIonReader reader)
 {
     return(-reader.DoubleValue());
 }
 public override Guid Deserialize(IIonReader reader)
 {
     byte[] blob = new byte[reader.GetLobByteSize()];
     return(new Guid(blob));
 }
 /// <summary>
 /// Deserialize decimal value.
 /// </summary>
 ///
 /// <param name="reader">The Ion reader to be used for deserialization.</param>
 ///
 /// <returns>The deserialized decimal value.</returns>
 public override decimal Deserialize(IIonReader reader)
 {
     return(reader.DecimalValue().ToDecimal());
 }
 public override string Deserialize(IIonReader reader)
 {
     return("BMW");
 }
 /// <summary>
 /// Deserialize Symbol Token value.
 /// </summary>
 ///
 /// <param name="reader">The Ion reader to be used for deserialization.</param>
 ///
 /// <returns>The deserialized Symbol Token value.</returns>
 public override SymbolToken Deserialize(IIonReader reader)
 {
     return(reader.SymbolValue());
 }
 public override bool Deserialize(IIonReader reader)
 {
     return(!reader.BoolValue());
 }
 /// <inheritdoc />
 /// <summary>
 /// Default implementation of writing reader value.
 /// Can be overriden to optimize.
 /// </summary>
 public void WriteValue(IIonReader reader) => WriteValueRecursively(reader.CurrentType, reader);
 public override byte[] Deserialize(IIonReader reader)
 {
     return(new byte[reader.GetLobByteSize()]);
 }
 /// <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());
 }
 public override int Deserialize(IIonReader reader)
 {
     return(-reader.IntValue());
 }
示例#27
0
 /// <summary>
 /// Test for single-value bool
 /// </summary>
 public static void SingleBool(IIonReader reader, bool value)
 {
     Assert.AreEqual(IonType.Bool, reader.MoveNext());
     Assert.AreEqual(value, reader.BoolValue());
 }
 public override long Deserialize(IIonReader reader)
 {
     return(-reader.LongValue());
 }
        private static bool TryDeserializeCollection(IIonReader reader, Type type, IScalarConverter scalarConverter, ref object result)
        {
            if (!typeof(IEnumerable).IsAssignableFrom(type))
            {
                return(false);
            }

            //special case of byte array
            if (TryDeserializeByteArray(reader, type, scalarConverter, ref result))
            {
                return(true);
            }

            if (reader.CurrentType != IonType.List)
            {
                return(false);
            }

            //figure out collection type
            Type elementType, constructedListType = null;

            if (type.IsArray)
            {
                elementType = type.GetElementType();
            }
            else
            {
                var generics = type.GetGenericArguments();
                if (generics.Length == 0)
                {
                    throw new IonException("Must specify collection type");
                }
                var listType = typeof(List <>);
                elementType         = generics[0];
                constructedListType = listType.MakeGenericType(elementType);
                if (!type.IsAssignableFrom(constructedListType))
                {
                    throw new IonException("Must be collection");
                }
            }

            reader.StepIn();
            var arrayList = new ArrayList();

            while (reader.MoveNext() != IonType.None)
            {
                var element = Deserialize(reader, elementType, scalarConverter);
                arrayList.Add(element);
            }

            if (type.IsArray)
            {
                var arr = Array.CreateInstance(elementType, arrayList.Count);
                for (var i = 0; i < arrayList.Count; i++)
                {
                    arr.SetValue(arrayList[i], i);
                }

                result = arr;
            }
            else
            {
                var list = (IList)Activator.CreateInstance(constructedListType);
                foreach (var item in arrayList)
                {
                    list.Add(item);
                }

                result = list;
            }

            reader.StepOut();
            return(true);
        }
示例#30
0
 void IIonWriter.WriteValues(IIonReader reader) => throw new NotSupportedException();