internal static void WriteValue(EdiWriter writer, PrimitiveTypeCode typeCode, object value, Picture? picture, string format) { switch (typeCode) { case PrimitiveTypeCode.Char: writer.WriteValue((char)value); break; case PrimitiveTypeCode.CharNullable: writer.WriteValue((value == null) ? (char?)null : (char)value); break; case PrimitiveTypeCode.Boolean: writer.WriteValue((bool)value); break; case PrimitiveTypeCode.BooleanNullable: writer.WriteValue((value == null) ? (bool?)null : (bool)value); break; case PrimitiveTypeCode.SByte: writer.WriteValue((sbyte)value, picture); break; case PrimitiveTypeCode.SByteNullable: writer.WriteValue((value == null) ? (sbyte?)null : (sbyte)value, picture); break; case PrimitiveTypeCode.Int16: writer.WriteValue((short)value, picture); break; case PrimitiveTypeCode.Int16Nullable: writer.WriteValue((value == null) ? (short?)null : (short)value, picture); break; case PrimitiveTypeCode.UInt16: writer.WriteValue((ushort)value, picture); break; case PrimitiveTypeCode.UInt16Nullable: writer.WriteValue((value == null) ? (ushort?)null : (ushort)value, picture); break; case PrimitiveTypeCode.Int32: writer.WriteValue((int)value, picture); break; case PrimitiveTypeCode.Int32Nullable: writer.WriteValue((value == null) ? (int?)null : (int)value, picture); break; case PrimitiveTypeCode.Byte: writer.WriteValue((byte)value, picture); break; case PrimitiveTypeCode.ByteNullable: writer.WriteValue((value == null) ? (byte?)null : (byte)value, picture); break; case PrimitiveTypeCode.UInt32: writer.WriteValue((uint)value, picture); break; case PrimitiveTypeCode.UInt32Nullable: writer.WriteValue((value == null) ? (uint?)null : (uint)value); break; case PrimitiveTypeCode.Int64: writer.WriteValue((long)value, picture); break; case PrimitiveTypeCode.Int64Nullable: writer.WriteValue((value == null) ? (long?)null : (long)value, picture); break; case PrimitiveTypeCode.UInt64: writer.WriteValue((ulong)value, picture); break; case PrimitiveTypeCode.UInt64Nullable: writer.WriteValue((value == null) ? (ulong?)null : (ulong)value, picture); break; case PrimitiveTypeCode.Single: writer.WriteValue((float)value, picture); break; case PrimitiveTypeCode.SingleNullable: writer.WriteValue((value == null) ? (float?)null : (float)value); break; case PrimitiveTypeCode.Double: writer.WriteValue((double)value, picture); break; case PrimitiveTypeCode.DoubleNullable: writer.WriteValue((value == null) ? (double?)null : (double)value, picture); break; case PrimitiveTypeCode.DateTime: writer.WriteValue((DateTime)value, format); break; case PrimitiveTypeCode.DateTimeNullable: writer.WriteValue((value == null) ? (DateTime?)null : (DateTime)value, format); break; case PrimitiveTypeCode.DateTimeOffset: writer.WriteValue((DateTimeOffset)value, format); break; case PrimitiveTypeCode.DateTimeOffsetNullable: writer.WriteValue((value == null) ? (DateTimeOffset?)null : (DateTimeOffset)value, format); break; case PrimitiveTypeCode.Decimal: writer.WriteValue((decimal)value, picture); break; case PrimitiveTypeCode.DecimalNullable: writer.WriteValue((value == null) ? (decimal?)null : (decimal)value, picture); break; case PrimitiveTypeCode.Guid: writer.WriteValue((Guid)value); break; case PrimitiveTypeCode.GuidNullable: writer.WriteValue((value == null) ? (Guid?)null : (Guid)value); break; case PrimitiveTypeCode.TimeSpan: writer.WriteValue((TimeSpan)value); break; case PrimitiveTypeCode.TimeSpanNullable: writer.WriteValue((value == null) ? (TimeSpan?)null : (TimeSpan)value); break; #if !PORTABLE case PrimitiveTypeCode.BigInteger: // this will call to WriteValue(object) writer.WriteValue((BigInteger)value); break; case PrimitiveTypeCode.BigIntegerNullable: // this will call to WriteValue(object) writer.WriteValue((value == null) ? (BigInteger?)null : (BigInteger)value); break; #endif case PrimitiveTypeCode.Uri: writer.WriteValue((Uri)value); break; case PrimitiveTypeCode.String: writer.WriteValue((string)value, picture); break; case PrimitiveTypeCode.Bytes: writer.WriteValue((byte[])value); break; #if !(PORTABLE || DOTNET) case PrimitiveTypeCode.DBNull: writer.WriteNull(); break; #endif default: #if !PORTABLE if (value is IConvertible) { // the value is a non-standard IConvertible // convert to the underlying value and retry IConvertible convertable = (IConvertible)value; TypeInformation typeInformation = ConvertUtils.GetTypeInformation(convertable); // if convertable has an underlying typecode of Object then attempt to convert it to a string PrimitiveTypeCode resolvedTypeCode = (typeInformation.TypeCode == PrimitiveTypeCode.Object) ? PrimitiveTypeCode.String : typeInformation.TypeCode; Type resolvedType = (typeInformation.TypeCode == PrimitiveTypeCode.Object) ? typeof(string) : typeInformation.Type; object convertedValue = convertable.ToType(resolvedType, CultureInfo.InvariantCulture); WriteValue(writer, resolvedTypeCode, convertedValue, picture, format); break; } else #endif { WriteValue(writer, PrimitiveTypeCode.String, $"{value}", picture, format); break; // consider throwing some times... //throw CreateUnsupportedTypeException(writer, value); } } }
/// <summary> /// Writes a <see cref="Nullable{Decimal}"/> value. /// </summary> /// <param name="value">The <see cref="Nullable{Decimal}"/> value to write.</param> /// <param name="picture">The <see cref="Nullable{Picture}"/> picture that discribes the value.</param> public virtual void WriteValue(decimal? value, Picture? picture = null) { if (value == null) { WriteNull(); } else { WriteValue(value.GetValueOrDefault(), picture); } }
/// <summary> /// Writes a <see cref="Object"/> value. /// An error will raised if the value cannot be written as a single Edi token. /// </summary> /// <param name="value">The <see cref="Object"/> value to write.</param> /// <param name="picture"></param> /// <param name="format">traditional string format mask</param> public virtual void WriteValue(object value, Picture? picture, string format) { if (value == null) { WriteNull(); } else { #if !PORTABLE // this is here because adding a WriteValue(BigInteger) to EdiWriter will // mean the user has to add a reference to System.Numerics.dll if (value is BigInteger) { throw CreateUnsupportedTypeException(this, value); } #endif WriteValue(this, ConvertUtils.GetTypeCode(value.GetType()), value, picture, format); } }
/// <summary> /// Writes a <see cref="SByte"/> value. /// </summary> /// <param name="value">The <see cref="SByte"/> value to write.</param> public virtual void WriteValue(sbyte value, Picture? picture) { InternalWriteValue(EdiToken.Integer); }
/// <summary> /// Writes a <see cref="Decimal"/> value. /// </summary> /// <param name="value">The <see cref="Decimal"/> value to write.</param> public virtual void WriteValue(decimal value, Picture? picture) { InternalWriteValue(EdiToken.Float); }
private void WriteIntegerValue(ulong uvalue, Picture? picture) { if (uvalue <= 9) { _writer.Write((char)('0' + uvalue)); } else { EnsureWriteBuffer(); int totalLength = MathUtils.IntLength(uvalue); int length = 0; do { _writeBuffer[totalLength - ++length] = (char)('0' + (uvalue % 10)); uvalue /= 10; } while (uvalue != 0); _writer.Write(_writeBuffer, 0, length); } }
/// <summary> /// Writes a <see cref="String"/> value. /// </summary> /// <param name="value">The <see cref="String"/> value to write.</param> public virtual void WriteValue(string value, Picture? picture) { InternalWriteValue(EdiToken.String); }
/// <summary> /// Writes a <see cref="Decimal"/> value. /// </summary> /// <param name="value">The <see cref="Decimal"/> value to write.</param> public override void WriteValue(decimal value, Picture? picture) { InternalWriteValue(EdiToken.Float); _writer.Write(value.ToEdiString(picture, Grammar.DecimalMark)); }
private void WriteIntegerValue(long value, Picture? picture) { if (value >= 0 && value <= 9) { _writer.Write((char)('0' + value)); } else { ulong uvalue = (value < 0) ? (ulong)-value : (ulong)value; if (value < 0) { _writer.Write('-'); } WriteIntegerValue(uvalue, picture); } }
/// <summary> /// Writes a <see cref="Nullable{Double}"/> value. /// </summary> /// <param name="value">The <see cref="Nullable{Double}"/> value to write.</param> public override void WriteValue(double? value, Picture? picture = null) { if (value == null) { WriteNull(); } else { InternalWriteValue(EdiToken.Float); _writer.Write(value.ToEdiString(picture, Grammar.DecimalMark)); } }
/// <summary> /// Writes a <see cref="SByte"/> value. /// </summary> /// <param name="value">The <see cref="SByte"/> value to write.</param> public override void WriteValue(sbyte value, Picture? picture) { InternalWriteValue(EdiToken.Integer); _writer.Write(((int?)value).ToEdiString(picture)); //WriteIntegerValue(value, picture); }
/// <summary> /// Writes a <see cref="Int64"/> value. /// </summary> /// <param name="value">The <see cref="Int64"/> value to write.</param> public override void WriteValue(long value, Picture? picture = null) { InternalWriteValue(EdiToken.Float); _writer.Write(value.ToEdiString(picture)); //WriteIntegerValue(value, picture); }
/// <summary> /// Writes a <see cref="String"/> value. /// </summary> /// <param name="value">The <see cref="String"/> value to write.</param> public override void WriteValue(string value, Picture? picture) { InternalWriteValue(EdiToken.String); WriteEscapedString(value); }
internal decimal? ReadAsDecimalInternal(Picture? picture) { EdiToken t; if (!ReadInternal()) { SetToken(EdiToken.None); return null; } t = TokenType; if (t == EdiToken.Null) return null; if (t == EdiToken.String) { string s = (string)Value; if (s != null) { s = s.TrimStart('Z'); // Z suppresses leading zeros } if (string.IsNullOrEmpty(s)) { SetToken(EdiToken.Null); return null; } decimal d; if (s.TryParse(picture, Grammar.DecimalMark, out d)) { SetToken(EdiToken.Float, d, false); return d; } throw EdiReaderException.Create(this, "Could not convert string to decimal: {0}.".FormatWith(CultureInfo.InvariantCulture, Value)); } throw EdiReaderException.Create(this, "Error reading decimal. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, t)); }
/// <summary> /// Reads the next EDI token from the stream as a <see cref="Nullable{Decimal}"/>. /// </summary> /// <param name="picture">The <see cref="Nullable{Picture}"/> is the format information needed to parse this into a float</param> /// <returns>A <see cref="Nullable{Decimal}"/>. This method will return <c>null</c> at the end of an array.</returns> public abstract decimal? ReadAsDecimal(Picture? picture);
/// <summary> /// Reads the next EDI token from the stream as a <see cref="Nullable{Decimal}"/>. /// </summary> /// <returns>A <see cref="Nullable{Decimal}"/>. This method will return <c>null</c> at the end of an array.</returns> public override decimal? ReadAsDecimal(Picture? picture = null) { return ReadAsDecimalInternal(picture); }