Inheritance: IJsonSerializerInternal
示例#1
0
        /// <summary>
        /// Encrypts the specified property, changing its value in place.
        /// </summary>
        /// <param name="name">The name of the property to encrypt.</param>
        /// <returns>This instance of <see cref="JsonObject"/>.</returns>
        public JsonObject Encrypt(string name)
        {
            if (_info.EncryptionMechanism != null)
            {
                object value;
                if (_values.TryGetValue(name, out value) &&
                    value != null)
                {
                    var sb = new StringBuilder();

                    using (var stringwriter = new StringWriter(sb))
                    {
                        using (var writer = new JsonWriter(stringwriter, _info))
                        {
                            DynamicJsonSerializer.Get(false, JsonMappings.Empty).SerializeObject(writer, value, _info);
                        }
                    }

                    value         = _info.EncryptionMechanism.Encrypt(sb.ToString(), _info.EncryptKey, _info.SerializationState);
                    _values[name] = value;
                }
            }

            return(this);
        }
示例#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);
            }));
        }
示例#3
0
        /// <summary>
        /// Decrypts the specified property, changing its value in place.
        /// </summary>
        /// <param name="name">The name of the property to decrypt.</param>
        /// <returns>This instance of <see cref="JsonObject"/>.</returns>
        public JsonObject Decrypt(string name)
        {
            if (_info.EncryptionMechanism != null)
            {
                object value;
                if (_values.TryGetValue(name, out value) &&
                    value is string)
                {
                    var decryptedJson = _info.EncryptionMechanism.Decrypt(
                        (string)value, _info.EncryptKey, _info.SerializationState);

                    using (var stringReader = new StringReader(decryptedJson))
                    {
                        using (var reader = new JsonReader(stringReader, _info))
                        {
                            value = DynamicJsonSerializer.Get(false, JsonMappings.Empty).DeserializeObject(reader, _info);

                            if (value == null ||
                                value is bool ||
                                value is string ||
                                value is JsonArray ||
                                value is JsonObject)
                            {
                                _values[name] = value;
                                return(this);
                            }

                            var jsonNumber = value as JsonNumber;
                            if (jsonNumber != null)
                            {
                                _values[name] = jsonNumber.DoubleValue;
                                _numericStringValues[name] = jsonNumber.StringValue;
                                return(this);
                            }

                            throw new NotSupportedException("Unsupported value type: " + value.GetType());
                        }
                    }
                }
            }

            return(this);
        }
 public JsonObjectSerializer(DynamicJsonSerializer dynamicJsonSerializer)
 {
     _dynamicJsonSerializer = dynamicJsonSerializer;
 }
 public JsonArraySerializer(DynamicJsonSerializer dynamicJsonSerializer)
 {
     _dynamicJsonSerializer = dynamicJsonSerializer;
 }
示例#6
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);
            }));
        }
示例#7
0
 public JsonArraySerializer(DynamicJsonSerializer dynamicJsonSerializer)
 {
     _dynamicJsonSerializer = dynamicJsonSerializer;
 }
示例#8
0
 public JsonObjectSerializer(DynamicJsonSerializer dynamicJsonSerializer)
 {
     _dynamicJsonSerializer = dynamicJsonSerializer;
 }