Exemplo n.º 1
0
        private static JsonWriter.WriteDelegate <T> GetObjectWriter <T, VE>(Type pt, VE ve)
            where VE : IValueExtractorFactory <T>
        {
            var checkNull = !pt.IsValueType || Nullable.GetUnderlyingType(pt) != null;
            var propType  = Nullable.GetUnderlyingType(pt) ?? pt;

            JsonWriter.WriteDelegate <T> action = null;

            if (KnownTypes.Contains(propType))
            {
                var makeActionMI =
                    typeof(TypeSerializer).GetMethod("MakeAction", BindingFlags.NonPublic | BindingFlags.Static);
                var makeAction = makeActionMI.MakeGenericMethod(typeof(T), propType, typeof(VE));
                action = (JsonWriter.WriteDelegate <T>)makeAction.Invoke(null, new object[] { ve });
            }
            else if (propType.IsArray)
            {
                var makeActionMI =
                    typeof(TypeSerializer).GetMethod("MakeArrayAction", BindingFlags.NonPublic | BindingFlags.Static);
                var makeAction = makeActionMI.MakeGenericMethod(typeof(T), propType, typeof(VE));
                action = (JsonWriter.WriteDelegate <T>)makeAction.Invoke(null, new object[] { ve });
            }
            else if (typeof(IEnumerable).IsAssignableFrom(propType))
            {
                var makeActionMI =
                    typeof(TypeSerializer).GetMethod("MakeEnumerableAction",
                                                     BindingFlags.NonPublic | BindingFlags.Static);
                var makeAction = makeActionMI.MakeGenericMethod(typeof(T), propType, typeof(VE));
                action = (JsonWriter.WriteDelegate <T>)makeAction.Invoke(null, new object[] { ve });
            }
            else
            {
                var makeActionMI =
                    typeof(TypeSerializer).GetMethod("MakeAction", BindingFlags.NonPublic | BindingFlags.Static);
                var makeAction = makeActionMI.MakeGenericMethod(typeof(T), propType, typeof(VE));
                action = (JsonWriter.WriteDelegate <T>)makeAction.Invoke(null, new object[] { ve });
            }

            if (checkNull)
            {
                var action1 = action;
                var valFunc = ve.ValueExtractor <object>();
                action = (t, w) =>
                {
                    if (valFunc(t) == null)
                    {
                        JsonWriter.WritePrimitive(Token.Null, w);
                        return(default);
Exemplo n.º 2
0
 public TypeSerializer(JsonWriter.WriteDelegate <object> serializer)
 {
     _serializer = serializer;
 }