/// <summary>
            /// Compiles the call site with efficient polymorphic inline cache dispatch behavior.
            /// </summary>
            public void Compile()
            {
                var body =
                    Expression.Block(
                        _locals,
                        _getType,
                        _tests
                        );

                var expr = Expression.Lambda <EmitWriterAction <object> >(body, _writer, _value, _context);

                Action = expr.Compile();
            }
 /// <summary>
 /// Creates a new serializer given the specified emitter implementations.
 /// </summary>
 /// <param name="emitterString">The emitter to use to serialize objects to string outputs.</param>
 /// <param name="emitterWriter">The emitter to use to serialize objects to text writers.</param>
 public SerializerBase(EmitStringAction <T> emitterString, EmitWriterAction <T> emitterWriter)
 {
     _emitterString = emitterString;
     _emitterWriter = emitterWriter;
 }
Ejemplo n.º 3
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('}');
        }
 /// <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 SafeSerializer(EmitStringAction <T> emitterString, EmitterStringBuilder builderString, EmitWriterAction <T> emitterWriter, EmitterWriterBuilder builderWriter)
     : base(emitterString, emitterWriter)
 {
     _contextPool = new ObjectPool <EmitterContext>(() => new EmitterContext(builderString, builderWriter));
 }
Ejemplo n.º 5
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);
 }