Exemplo n.º 1
0
 /// <summary>
 /// Emits a System.Decimal as a JSON Number to the specified text writer.
 /// </summary>
 /// <param name="writer">The text writer to append the System.Decimal to.</param>
 /// <param name="value">The System.Decimal value to emit.</param>
 /// <param name="_">The emitter context.</param>
 internal static void EmitDecimal(System.IO.TextWriter writer, Decimal value, EmitterContext _ = null) => writer.Write(value.ToString(CultureInfo.InvariantCulture));
Exemplo n.º 2
0
        /// <summary>
        /// Emits a nullable System.Int16 to the specified text writer.
        /// </summary>
        /// <param name="writer">The text writer to append the nullable System.Int16 to.</param>
        /// <param name="value">The nullable System.Int16 value to emit.</param>
        /// <param name="context">The emitter context.</param>
        internal static void EmitNullableInt16(System.IO.TextWriter writer, Int16?value, EmitterContext context)
        {
            if (value == null)
            {
                //
                // CONSIDER: Add a flag to EmitterContext to reveal whether the TextWriter is one of the known writers
                //           which has an efficient implementation of Write(string) that doesn't call ToCharArray on
                //           the given string (see base class behavior of Write).
                //

                writer.Write("null");
            }
            else
            {
                EmitInt16(writer, value.Value, context);
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Emits a System.Decimal as a JSON Number to the specified string builder.
 /// </summary>
 /// <param name="builder">The string builder to append the System.Decimal to.</param>
 /// <param name="value">The System.Decimal value to emit.</param>
 /// <param name="_">The emitter context.</param>
 internal static void EmitDecimal(StringBuilder builder, Decimal value, EmitterContext _ = null) => builder.Append(value.ToString(CultureInfo.InvariantCulture));
Exemplo n.º 4
0
 /// <summary>
 /// Emits a nullable System.DateTimeOffset to the specified string builder.
 /// </summary>
 /// <param name="builder">The string builder to append the nullable System.DateTimeOffset to.</param>
 /// <param name="value">The nullable System.DateTimeOffset value to emit.</param>
 /// <param name="context">The emitter context.</param>
 internal static void EmitNullableDateTimeOffset(StringBuilder builder, DateTimeOffset?value, EmitterContext context)
 {
     if (value == null)
     {
         builder.Append("null");
     }
     else
     {
         EmitDateTimeOffset(builder, value.Value, context);
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// Emits a nullable System.Decimal to the specified string builder.
 /// </summary>
 /// <param name="builder">The string builder to append the nullable System.Decimal to.</param>
 /// <param name="value">The nullable System.Decimal value to emit.</param>
 /// <param name="context">The emitter context.</param>
 internal static void EmitNullableDecimal(StringBuilder builder, Decimal?value, EmitterContext context)
 {
     if (value == null)
     {
         builder.Append("null");
     }
     else
     {
         EmitDecimal(builder, value.Value, context);
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// Emits a nullable System.Boolean to the specified string builder.
 /// </summary>
 /// <param name="builder">The string builder to append the nullable System.Boolean to.</param>
 /// <param name="value">The nullable System.Boolean value to emit.</param>
 /// <param name="context">The emitter context.</param>
 internal static void EmitNullableBoolean(StringBuilder builder, Boolean?value, EmitterContext context)
 {
     if (value == null)
     {
         builder.Append("null");
     }
     else
     {
         EmitBoolean(builder, value.Value, context);
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// Emits a homogeneously typed dictionary as a JSON Object to the specified text writer.
        /// </summary>
        /// <typeparam name="TValue">The type of the values in the dictionary.</typeparam>
        /// <typeparam name="TDictionary">The type of the dictionary to serialize.</typeparam>
        /// <param name="writer">The text writer to write the dictionary to.</param>
        /// <param name="value">The homogeneously typed dictionary to emit.</param>
        /// <param name="context">The emitter context.</param>
        /// <param name="emitValue">The emitter to use for values.</param>
        internal static void EmitAnyObject <TValue, TDictionary>(System.IO.TextWriter writer, TDictionary value, EmitterContext context, EmitWriterAction <TValue> emitValue)
            where TDictionary : IEnumerable <KeyValuePair <string, TValue> >
        {
            if (value == null)
            {
                writer.Write("null");
                return;
            }

            writer.Write('{');

            var first = true;

            foreach (var kv in value)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    writer.Write(',');
                }

                var key = kv.Key;
                var val = kv.Value;

                if (key == null)
                {
                    throw new InvalidOperationException("Encountered a key value of 'null' which can not be used for a property in a JSON Object.");
                }

                EmitString(writer, key, context);

                writer.Write(':');

                emitValue(writer, val, context);
            }

            writer.Write('}');
        }
Exemplo n.º 8
0
        /// <summary>
        /// Emits an object as a JSON value by using its runtime type, or as a JSON null literal if it's null.
        /// </summary>
        /// <param name="builder">The string builder to append the object to.</param>
        /// <param name="value">The value to emit.</param>
        /// <param name="context">The emitter context.</param>
        internal static void EmitAny(StringBuilder builder, object value, EmitterContext context)
        {
            //
            // CONSIDER: Expose a serialization option to disallow late bound serialization.
            //

            if (value == null)
            {
                builder.Append("null");
                return;
            }

            var type = value.GetType();

            //
            // NB: We first try to dispatch quickly to an emitter based on a check for well-known types.
            //

            switch (Type.GetTypeCode(type))
            {
            case TypeCode.Boolean:
                EmitBoolean(builder, (bool)value, context);
                return;

            case TypeCode.Char:
                EmitChar(builder, (char)value, context);
                return;

            case TypeCode.SByte:
                EmitSByte(builder, (sbyte)value, context);
                return;

            case TypeCode.Byte:
                EmitByte(builder, (byte)value, context);
                return;

            case TypeCode.Int16:
                EmitInt16(builder, (short)value, context);
                return;

            case TypeCode.UInt16:
                EmitUInt16(builder, (ushort)value, context);
                return;

            case TypeCode.Int32:
                EmitInt32(builder, (int)value, context);
                return;

            case TypeCode.UInt32:
                EmitUInt32(builder, (uint)value, context);
                return;

            case TypeCode.Int64:
                EmitInt64(builder, (long)value, context);
                return;

            case TypeCode.UInt64:
                EmitUInt64(builder, (ulong)value, context);
                return;

            case TypeCode.Single:
                EmitSingle(builder, (float)value, context);
                return;

            case TypeCode.Double:
                EmitDouble(builder, (double)value, context);
                return;

            case TypeCode.Decimal:
                EmitDecimal(builder, (decimal)value, context);
                return;

            case TypeCode.DateTime:
                EmitDateTime(builder, (DateTime)value, context);
                return;

            case TypeCode.String:
                EmitString(builder, (string)value, context);
                return;
            }

            if (type == typeof(DateTimeOffset))
            {
                EmitDateTimeOffset(builder, (DateTimeOffset)value, context);
                return;
            }

            //
            // NB: Serialization of the same types is hopefully common, so we build a polymorphic inline cache and store it in
            //     the emitter context.
            //
            // CONSIDER: What should be the scope for the polymorphic inline cache? Per serializer or per invocation?
            //
            // TODO: Add object reference cycle detection.
            //

            context.EmitAnyString(builder, value, context);
        }
Exemplo n.º 9
0
 /// <summary>
 /// Creates a new serializer given the specified emitter implementation.
 /// </summary>
 /// <param name="emitterString">The emitter to use to serialize objects.</param>
 /// <param name="builderString">The builder to use to create emitters for objects based on their runtime type.</param>
 public Serializer(EmitStringAction <T> emitterString, EmitterBuilder builderString)
     : base(emitterString)
 {
     _context = new EmitterContext(builderString);
 }
Exemplo n.º 10
0
 /// <summary>
 /// Creates a new serializer given the specified emitter implementation.
 /// </summary>
 /// <param name="emitterString">The emitter to use to serialize objects to string outputs.</param>
 /// <param name="builderString">The builder to use to create emitters for objects based on their runtime type.</param>
 /// <param name="emitterWriter">The emitter to use to serialize objects to text writers.</param>
 /// <param name="builderWriter">The builder to use to create emitters for objects based on their runtime type.</param>
 public Serializer(EmitStringAction <T> emitterString, EmitterStringBuilder builderString, EmitWriterAction <T> emitterWriter, EmitterWriterBuilder builderWriter)
     : base(emitterString, emitterWriter)
 {
     _context = new EmitterContext(builderString, builderWriter);
 }