/// <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 <EmitStringAction <object> >(body, _builder, _value, _context);

                Action = expr.Compile();
            }
Exemple #2
0
        private static void AssertEmit <T>(EmitStringAction <T> action, T value, string expected)
        {
            var sb = new StringBuilder();

            action(sb, value, GetEmitterContext());
            Assert.AreEqual(expected, sb.ToString());

#if !NO_IO
            var sw = new System.IO.StringWriter();

            var emitWriterMtd = action.Method.DeclaringType.GetMethod(action.Method.Name, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static, binder: null, new[] { typeof(System.IO.TextWriter), typeof(T), typeof(EmitterContext) }, modifiers: null);
            var emitWriterFnc = (EmitWriterAction <T>)Delegate.CreateDelegate(typeof(EmitWriterAction <T>), emitWriterMtd);
            emitWriterFnc(sw, value, GetEmitterContext());

            Assert.AreEqual(expected, sw.ToString());
#endif
        }
 /// <summary>
 /// Creates a new serializer given the specified emitter implementation.
 /// </summary>
 /// <param name="emitterString">The emitter to use to serialize objects.</param>
 public SerializerBase(EmitStringAction <T> emitterString)
 {
     _emitterString = emitterString;
 }
 /// <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;
 }
 /// <summary>
 /// Creates a new serializer given the specified emitter implementation.
 /// </summary>
 /// <param name="emitter">The emitter to use to serialize objects.</param>
 /// <param name="builder">The builder to use to create emitters for objects based on their runtime type.</param>
 public SafeSerializer(EmitStringAction <T> emitter, EmitterStringBuilder builder)
     : base(emitter)
 {
     _contextPool = new ObjectPool <EmitterContext>(() => new EmitterContext(builder));
 }
Exemple #6
0
        /// <summary>
        /// Emits a homogeneously typed dictionary as a JSON Object to the specified string builder.
        /// </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="builder">The string builder to append 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>(StringBuilder builder, TDictionary value, EmitterContext context, EmitStringAction <TValue> emitValue)
            where TDictionary : IEnumerable <KeyValuePair <string, TValue> >
        {
            if (value == null)
            {
                builder.Append("null");
                return;
            }

            builder.Append('{');

            var first = true;

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

                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(builder, key, context);

                builder.Append(':');

                emitValue(builder, val, context);
            }

            builder.Append('}');
        }
Exemple #7
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);
 }
Exemple #8
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);
 }