Get() public static method

public static Get ( Type type, bool encrypt ) : NumberJsonSerializer
type System.Type
encrypt bool
return NumberJsonSerializer
Example #1
0
        private IJsonSerializerInternal GetSerializer(Type concreteType)
        {
            // Note that no checks are made for nullable types. The concreteType parameter is retrieved
            // from a call to .GetType() on a non-null instance.

            if (concreteType == typeof(string) ||
                concreteType == typeof(DateTime) ||
                concreteType == typeof(DateTimeOffset) ||
                concreteType == typeof(Guid))
            {
                return(StringJsonSerializer.Get(concreteType, _encrypt));
            }

            if (concreteType == typeof(double) ||
                concreteType == typeof(int) ||
                concreteType == typeof(float) ||
                concreteType == typeof(long) ||
                concreteType == typeof(decimal) ||
                concreteType == typeof(byte) ||
                concreteType == typeof(sbyte) ||
                concreteType == typeof(short) ||
                concreteType == typeof(ushort) ||
                concreteType == typeof(uint) ||
                concreteType == typeof(ulong))
            {
                return(NumberJsonSerializer.Get(concreteType, _encrypt));
            }

            if (concreteType == typeof(bool))
            {
                return(BooleanJsonSerializer.Get(_encrypt, false));
            }

            if (concreteType == typeof(JsonObject))
            {
                return(_jsonObjectSerializer);
            }

            if (concreteType == typeof(JsonArray))
            {
                return(_jsonArraySerializer);
            }

            if (concreteType.IsAssignableToGenericIDictionaryOfStringToAnything())
            {
                return(DictionaryJsonSerializer.Get(concreteType, _encrypt, _mappings));
            }

            if (typeof(IEnumerable).IsAssignableFrom(concreteType))
            {
                return(ListJsonSerializer.Get(concreteType, _encrypt, _mappings));
            }

            return(CustomJsonSerializer.Get(concreteType, _encrypt, _mappings));
        }
Example #2
0
        public static IJsonSerializerInternal GetSerializer(
            Type type,
            bool encrypt,
            JsonMappings mappings,
            bool shouldUseAttributeDefinedInInterface)
        {
            return(_cache.GetOrAdd(
                       Tuple.Create(type, encrypt, mappings),
                       tuple =>
            {
                if (type == typeof(object))
                {
                    return DynamicJsonSerializer.Get(encrypt, mappings, shouldUseAttributeDefinedInInterface);
                }

                if (type.IsJsonStringType())
                {
                    return StringJsonSerializer.Get(type, encrypt);
                }

                if (type.IsJsonNumericType())
                {
                    return NumberJsonSerializer.Get(type, encrypt);
                }

                if (type.IsJsonBooleanType())
                {
                    return BooleanJsonSerializer.Get(encrypt, type == typeof(bool?));
                }

                if (type.IsAssignableToGenericIDictionary() ||
                    typeof(IDictionary).IsAssignableFrom(type))
                {
                    return DictionaryJsonSerializer.Get(type, encrypt, mappings, shouldUseAttributeDefinedInInterface);
                }

                if (typeof(IEnumerable).IsAssignableFrom(type))
                {
                    return ListJsonSerializer.Get(type, encrypt, mappings, shouldUseAttributeDefinedInInterface);
                }

                // TODO: Handle more types or possibly black-list some types or types of types.

                return CustomJsonSerializer.Get(type, encrypt, mappings, shouldUseAttributeDefinedInInterface);
            }));
        }
Example #3
0
        public static IJsonSerializerInternal GetSerializer(
            Type type,
            bool encrypt,
            JsonMappings mappings)
        {
            return(_cache.GetOrAdd(
                       Tuple.Create(type, encrypt, mappings),
                       tuple =>
            {
                if (type == typeof(object))
                {
                    return DynamicJsonSerializer.Get(encrypt, mappings);
                }

                if (type == typeof(string) ||
                    type == typeof(DateTime) ||
                    type == typeof(DateTime?) ||
                    type == typeof(DateTimeOffset) ||
                    type == typeof(DateTimeOffset?) ||
                    type == typeof(Guid) ||
                    type == typeof(Guid?) ||
                    type.IsEnum ||
                    (type.IsNullableType() && Nullable.GetUnderlyingType(type).IsEnum) ||
                    type == typeof(Type) ||
                    type == typeof(Uri))
                {
                    return StringJsonSerializer.Get(type, encrypt);
                }

                if (type == typeof(double) ||
                    type == typeof(double?) ||
                    type == typeof(int) ||
                    type == typeof(int?) ||
                    type == typeof(float) ||
                    type == typeof(float?) ||
                    type == typeof(long) ||
                    type == typeof(long?) ||
                    type == typeof(decimal) ||
                    type == typeof(decimal?) ||
                    type == typeof(byte) ||
                    type == typeof(byte?) ||
                    type == typeof(sbyte) ||
                    type == typeof(sbyte?) ||
                    type == typeof(short) ||
                    type == typeof(short?) ||
                    type == typeof(ushort) ||
                    type == typeof(ushort?) ||
                    type == typeof(uint) ||
                    type == typeof(uint?) ||
                    type == typeof(ulong) ||
                    type == typeof(ulong?))        // TODO: handle more number types.
                {
                    return NumberJsonSerializer.Get(type, encrypt);
                }

                if (type == typeof(bool) ||
                    type == typeof(bool?))
                {
                    return BooleanJsonSerializer.Get(encrypt, type == typeof(bool?));
                }

                if (type.IsAssignableToGenericIDictionary() ||
                    typeof(IDictionary).IsAssignableFrom(type))
                {
                    return DictionaryJsonSerializer.Get(type, encrypt, mappings);
                }

                if (typeof(IEnumerable).IsAssignableFrom(type))
                {
                    return ListJsonSerializer.Get(type, encrypt, mappings);
                }

                // TODO: Handle more types or possibly black-list some types or types of types.

                return CustomJsonSerializer.Get(type, encrypt, mappings);
            }));
        }