Esempio n. 1
0
        private void SerializeObject(object target, StringBuilder localBuffer)
        {
            var type   = target.GetType();
            var mapper = mappers.GetMapper(type);

            if (mapper.Serializer != null)
            {
                localBuffer.Append(mapper.Serializer(target).ToString());
            }
            else
            {
                var manager = types[type];
                localBuffer.Append(JsonTokens.LeftBrace);
                var getters = manager.Getters;
                for (var i = 0; i < getters.Count; i++)
                {
                    var prop     = getters[i];
                    var itemName = prop.Item1.Name;
                    if (mapper.IsPropertyIgnored(itemName))
                    {
                        continue;
                    }
                    var propValue = prop.Item2(target);
                    localBuffer.Append(JsonTokens.Quoter);
                    localBuffer.Append(mapper.GetKey(itemName));
                    localBuffer.Append(JsonTokens.Quoter);
                    localBuffer.Append(JsonTokens.Colon);
                    var serializer = GetSerializer(propValue);
                    serializer(propValue, localBuffer);
                    localBuffer.Append(JsonTokens.Comma);
                }
                localBuffer.Remove(localBuffer.Length - 1, 1);
                localBuffer.Append(JsonTokens.RightBrace);
            }
        }
Esempio n. 2
0
        private Func <Type, JValue, object> GetDeserializer(Type type)
        {
            Func <Type, JValue, object> deserializer;

            if (deserializers.TryGetValue(type, out deserializer))
            {
                return(deserializer);
            }


            var mapper = mappers.GetMapper(type);

            // for manual create now, assuming underlying type == type
            if (mapper.ManualCreate != null)
            {
                deserializer = (t, v) => mapper.ManualCreate(v);
            }
            else if ((type.IsInterface() || type.IsAbstract()) && mapper.Create == null)
            {
                deserializer = BuildNull;
            }
            else if (type == typeof(bool))
            {
                deserializer = BuildBool;
            }
            else if (type == typeof(bool?))
            {
                deserializer = BuildNullableBool;
            }
            else if (type == typeof(double))
            {
                deserializer = BuildDouble;
            }
            else if (type == typeof(double?))
            {
                deserializer = BuildNullableDouble;
            }
            else if (type == typeof(decimal))
            {
                deserializer = BuildDecimal;
            }
            else if (type == typeof(decimal?))
            {
                deserializer = BuildNullableDecimal;
            }
            else if (type == typeof(float))
            {
                deserializer = BuildFloat;
            }
            else if (type == typeof(float?))
            {
                deserializer = BuildNullableFloat;
            }
            else if (type == typeof(int))
            {
                deserializer = BuildInt;
            }
            else if (type == typeof(int?))
            {
                deserializer = BuildNullableInt;
            }
            else if (type == typeof(long))
            {
                deserializer = BuildLong;
            }
            else if (type == typeof(long?))
            {
                deserializer = BuildNullableLong;
            }
            else if (type == typeof(short))
            {
                deserializer = BuildShort;
            }
            else if (type == typeof(short?))
            {
                deserializer = BuildNullableShort;
            }
            else if (type == typeof(uint))
            {
                deserializer = BuildUint;
            }
            else if (type == typeof(uint?))
            {
                deserializer = BuildNullableUint;
            }
            else if (type == typeof(ulong))
            {
                deserializer = BuildUlong;
            }
            else if (type == typeof(ulong?))
            {
                deserializer = BuildNullableUlong;
            }
            else if (type == typeof(ushort))
            {
                deserializer = BuildUshort;
            }
            else if (type == typeof(ushort?))
            {
                deserializer = BuildNullableUshort;
            }
            else if (type == typeof(byte))
            {
                deserializer = BuildByte;
            }
            else if (type == typeof(byte?))
            {
                deserializer = BuildNullableByte;
            }
            else if (type == typeof(sbyte))
            {
                deserializer = BuildSbyte;
            }
            else if (type == typeof(sbyte?))
            {
                deserializer = BuildNullableSbyte;
            }
            else if (type.IsEnum())
            {
                deserializer = BuildEnum;
            }
            else if (type == typeof(Guid))
            {
                deserializer = BuildGuid;
            }
            else if (type == typeof(Guid?))
            {
                deserializer = BuildNullableGuid;
            }
            else if (type == typeof(char))
            {
                deserializer = BuildChar;
            }
            else if (type == typeof(char?))
            {
                deserializer = BuildNullableChar;
            }
            else if (type == typeof(string))
            {
                deserializer = BuildString;
            }
            else if (type == typeof(DateTime))
            {
                deserializer = BuildTime;
            }
            else if (type == typeof(DateTime?))
            {
                deserializer = BuildNullableTime;
            }
            else if (type == typeof(byte[]))
            {
                deserializer = BuildByteArray;
            }
            else if (type.IsDictionary())
            {
                deserializer = BuildDict;
            }
            else if (type.IsArray)
            {
                deserializer = BuildArray;
            }
            else if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                deserializer = BuildEnumerable;
            }
            else if (type.IsClass())
            {
                deserializer = BuildObject;
            }
            else
            {
                var underlying = Nullable.GetUnderlyingType(type);
                if (underlying != null)
                {
                    if (underlying.IsEnum())
                    {
                        deserializer = BuildNullableEnum;
                    }
                    else
                    {
                        deserializer = BuildObject;
                    }
                }
                else
                {
                    deserializer = BuildStruct;
                }
            }

            deserializers[type] = deserializer;

            return(deserializer);
        }