/// <summary> /// Deserializes the object from the passed data dictionary. /// </summary> /// <param name="type">Type of the object to deserialize.</param> /// <param name="data">Data dictionary containing the values of the object to deserialize.</param> /// <returns>Object deserialized from the passed data dictionary.</returns> public object Deserialize(Type type, object data) { if (data == null) { return(null); } // Check if raw serialization possible. if (this.IsRawSerializationPossible(type)) { return(Convert.ChangeType(data, type)); } // Get serializer for object. IDictionarySerializer serializer = this.GetSerializer(type); if (serializer == null) { // Check if nullable. Type nullableType = Nullable.GetUnderlyingType(type); if (nullableType != null) { return(this.Deserialize(nullableType, data)); } // Check if generic type. if (type.IsGenericType()) { Type genericType = type.GetGenericTypeDefinition(); if (genericType != null && this.genericSerializerMap.TryGetValue(genericType, out serializer)) { return(serializer.Deserialize(this, (Dictionary <string, object>)data)); } } // Check if enum type. if (type.IsEnum()) { // Avoid recursive deserialization of ValueWithType, whose value is an enum in this case. return(Enum.Parse(type, (string)data)); } // No custom serializer found - try reflection. if (type.IsAttributeDefined <DictionarySerializableAttribute>()) { return(this.DeserializeReflection(type, (Dictionary <string, object>)data)); } throw new ArgumentException(string.Format("Unsupported type for dictionary serialization: {0}", type)); } return(serializer.Deserialize(this, (Dictionary <string, object>)data)); }
public DeleteCommand(IDictionaryDeserializer deserializer, IDictionarySerializer serializer, string storeFile, string[] remainingArgs) { _deserializer = deserializer; _serializer = serializer; _storeFile = storeFile; if (!remainingArgs.Any()) { throw new CommandParseException("Key not provided."); } _key = remainingArgs[0]; }
private object Serialize(Type type, object obj) { // Check if raw serialization is possible. if (this.IsRawSerializationPossible(type)) { return(obj); } // Get serializer for object. IDictionarySerializer serializer = this.GetSerializer(type); if (serializer == null) { // Check if nullable. Type nullableType = Nullable.GetUnderlyingType(type); if (nullableType != null) { return(this.Serialize(nullableType, obj)); } // Check if generic type. if (type.IsGenericType()) { Type genericType = type.GetGenericTypeDefinition(); if (genericType != null && this.genericSerializerMap.TryGetValue(genericType, out serializer)) { return(serializer.Serialize(this, obj)); } } // Check if enum type. if (type.IsEnum()) { return(obj.ToString()); } // No custom serializer found - try reflection. if (type.IsAttributeDefined <DictionarySerializableAttribute>()) { return(this.SerializeReflection(obj)); } throw new ArgumentException( string.Format("Unsupported type for dictionary serialization: {0}", type), "obj"); } return(serializer.Serialize(this, obj)); }
public SetCommand(IDictionaryDeserializer deserializer, IDictionarySerializer serializer, string storeFile, string[] remainingArgs) { _deserializer = deserializer; _serializer = serializer; _storeFile = storeFile; if (remainingArgs.Length == 0) { throw new CommandParseException("Key not provided."); } if (remainingArgs.Length < 2) { throw new CommandParseException("Value not provided."); } _key = remainingArgs[0]; _value = remainingArgs[1]; }
/// <summary> /// Checks whether this context is able to serialize the specified type. /// </summary> /// <param name="type">Type to check.</param> /// <returns> /// <c>true</c>, if this context can serialize the specified type with custom serializers or reflection, and <c>false</c> otherwise. /// </returns> public bool CanSerialize(Type type) { IDictionarySerializer serializer = this.GetSerializer(type); return(serializer != null || type.IsAttributeDefined <DictionarySerializableAttribute>()); }
/// <summary> /// Sets the custom serializer of this context for the specified type. /// </summary> /// <param name="type">Type to set the custom serializer of.</param> /// <param name="serializer">Custom serializer for the specified type.</param> public void SetSerializer(Type type, IDictionarySerializer serializer) { this.serializerMap[type] = serializer; }
public CommandLineParser(IDictionaryDeserializer deserializer, IDictionarySerializer serializer, IConsole console) { _deserializer = deserializer; _serializer = serializer; _console = console; }