/// <summary> /// Gets an expression for a value by first converting it with its registered type converter and then calling Serialize /// </summary> /// <param name="value">the value to generate an expression for</param> /// <param name="currentPath">the current path to the value</param> /// <param name="serializer">serializer instance</param> /// <returns>an expression for the value</returns> public override Expression GetExpression(object value, JsonPath currentPath, ISerializerHandler serializer) { TypeData handler = Context.TypeHandlerFactory[value.GetType()]; IJsonTypeConverter converter = (handler.HasConverter ? handler.TypeConverter : (IJsonTypeConverter)value); return(GetExpression(value, converter, currentPath, serializer)); }
private IJsonTypeConverter GetConverter(object value) { ITypeData handler = Settings.Types[value.GetType()]; IJsonTypeConverter converter = (handler.HasConverter ? handler.TypeConverter : (IJsonTypeConverter)value); return(converter); }
/// <summary> /// Handles the TypeConverters/add tag of the config file /// </summary> /// <param name="tagName">name of the tag, should be "add"</param> /// <param name="values">attribute values in the order: type, property, converter</param> private void AddTypeConverter(string tagName, string[] values) { string type = values[0]; string property = values[1]; string converter = values[2]; if (string.IsNullOrEmpty(type)) { throw new ArgumentException("Must specify 'type' for TypeConverters add"); } if (string.IsNullOrEmpty(converter)) { throw new ArgumentException("Must specify 'converter' for TypeConverters add"); } // load the specified types Type objectType = Type.GetType(type, true); Type converterType = Type.GetType(converter, true); // check for the property element, if it exists, the converter is for a property on the type IJsonTypeConverter converterObj = (IJsonTypeConverter)Activator.CreateInstance(converterType); if (!string.IsNullOrEmpty(property)) { context.RegisterTypeConverter(objectType, property, converterObj); } else { context.RegisterTypeConverter(objectType, converterObj); } }
public void RegisterTypeConverter(Type forType, IJsonTypeConverter converter) { if (forType.IsPrimitive || forType == typeof(string)) { throw new ArgumentException("Converters can not be registered for primitive types or string. " + forType, "forType"); } this[forType].TypeConverter = converter; }
/// <summary> /// Serialize the given object at the current indent level. The path to the object is represented by /// currentPath such as "this.name", etc. This is an internal method that can be called recursively. /// </summary> /// <param name="value">the object to serialize</param> /// <param name="currentPath">the current path for reference writing</param> public Expression Serialize(object value, JsonPath currentPath, IJsonTypeConverter converter) { if (value == null) { return(new NullExpression()); } else { IExpressionHandler objHandler; bool isReferencable = _config.IsReferenceableType(value.GetType()); if (converter != null) { TypeConverterExpressionHandler converterHandler = (TypeConverterExpressionHandler)_config.ExpressionHandlers.Find(typeof(TypeConverterExpressionHandler)); isReferencable = converterHandler.IsReferenceable(value, converter); objHandler = converterHandler; } else { objHandler = _config.ExpressionHandlers.GetHandler(value); isReferencable = objHandler.IsReferenceable(value); } if (isReferencable) { Expression expr = HandleReference(value, currentPath); if (expr != null) { return(expr); } } ISerializationCallback callback = value as ISerializationCallback; if (callback != null) { callback.OnBeforeSerialization(); } try { if (converter != null) { return(((TypeConverterExpressionHandler)objHandler).GetExpression(value, converter, currentPath, this)); } else { SetCanReference(value); return(objHandler.GetExpression(value, currentPath, this)); } } finally { if (callback != null) { callback.OnAfterSerialization(); } } } }
/// <summary> /// Gets an expression for a value by first converting it with a specific type converter and then calling Serialize. This /// method can be called directly when using a Property Converter /// </summary> /// <param name="value">the value to generate an expression for</param> /// <param name="converter">the type converter to use for conversion</param> /// <param name="currentPath">the current path to the value</param> /// <param name="serializer">serializer instance</param> /// <returns>an expression for the value</returns> public Expression GetExpression(object value, IJsonTypeConverter converter, JsonPath currentPath, ISerializerHandler serializer) { object convertedObject = converter.ConvertFrom(value, Context); // call serialize again in case the new type has a converter Expression expr = serializer.Serialize(convertedObject, currentPath, null); serializer.SetCanReference(value); // can't reference inside the object return(expr); }
public override void Process(IMetaData metaData, ICustomAttributeProvider attributeProvider, ISerializerSettings config) { IJsonTypeConverter converter = CreateTypeConverter(attributeProvider); if (converter != null) { metaData.TypeConverter = converter; } }
public void TestRegisterTypeConverter() { Serializer s = new Serializer("TestRegisterTypeConverter"); IJsonTypeConverter typeConverter = s.Settings.Type <SimpleObject>().TypeConverter; IJsonTypeConverter propConverter = s.Settings.Type <SimpleObject>().FindProperty("BoolValue").TypeConverter; Assert.IsNotNull(typeConverter, "No converter for simple object registered"); Assert.IsNotNull(propConverter, "No converter for simple object, BoolValue property registered"); }
/// <summary> /// Gets an expression for a value by first converting it with a specific type converter and then calling Serialize. This /// method can be called directly when using a Property Converter /// </summary> /// <param name="value">the value to generate an expression for</param> /// <param name="converter">the type converter to use for conversion</param> /// <param name="currentPath">the current path to the value</param> /// <param name="serializer">serializer instance</param> /// <returns>an expression for the value</returns> public Expression GetExpression(object value, IJsonTypeConverter converter, JsonPath currentPath, IExpressionBuilder serializer) { //TODO: Cast for now to avoid breaking compatibility object convertedObject = converter.ConvertFrom(value, (SerializerSettings)Settings); // call serialize again in case the new type has a converter Expression expr = serializer.Serialize(convertedObject, currentPath, null); serializer.SetCanReference(value); // can't reference inside the object return(expr); }
/// <summary> /// Constructs a converter from the convert attribute /// </summary> /// <param name="attribute">the JsonConvertAttribute decorating a property or class</param> /// <returns>converter</returns> private static IJsonTypeConverter CreateTypeConverter(JsonConvertAttribute attribute) { IJsonTypeConverter converter = (IJsonTypeConverter)Activator.CreateInstance(attribute.Converter); if (attribute.Context != null) { converter.Context = attribute.Context; } return(converter); }
/// <summary> /// Serialize the given object at the current indent level. The path to the object is represented by /// currentPath such as "this.name", etc. This is an internal method that can be called recursively. /// </summary> /// <param name="value">the object to serialize</param> /// <param name="currentPath">the current path for reference writing</param> public Expression Serialize(object value, JsonPath currentPath, IJsonTypeConverter converter) { if (value == null) { return new NullExpression(); } else { IExpressionHandler objHandler; bool isReferencable = _config.IsReferenceableType(value.GetType()); if (converter != null) { TypeConverterExpressionHandler converterHandler = (TypeConverterExpressionHandler)_config.ExpressionHandlers.Find(typeof(TypeConverterExpressionHandler)); isReferencable = converterHandler.IsReferenceable(value, converter); objHandler = converterHandler; } else { objHandler = _config.ExpressionHandlers.GetHandler(value); isReferencable = objHandler.IsReferenceable(value); } if (isReferencable) { Expression expr = HandleReference(value, currentPath); if (expr != null) return expr; } ISerializationCallback callback = value as ISerializationCallback; if (callback != null) callback.OnBeforeSerialization(); try { if (converter != null) { return ((TypeConverterExpressionHandler)objHandler).GetExpression(value, converter, currentPath, this); } else { SetCanReference(value); return objHandler.GetExpression(value, currentPath, this); } } finally { if (callback != null) callback.OnAfterSerialization(); } } }
/// <summary> /// Converts an expression to an object by first Evaluating the expression as its converted type and /// then converting that result using the specified type converter. /// </summary> /// <param name="expression">the expression to convert</param> /// <param name="deserializer">deserializer instance</param> /// <param name="converter">the converter to use to convert the object</param> /// <returns>an object created from the expression</returns> public object Evaluate(Expression expression, IDeserializerHandler deserializer, IJsonTypeConverter converter) { Type sourceType = expression.ResultType; Type destType = converter.GetSerializedType(sourceType); expression.ResultType = destType; object tempResult = deserializer.Evaluate(expression); object result = converter.ConvertTo(tempResult, sourceType, Context); expression.OnObjectConstructed(result); if (result is IDeserializationCallback) { ((IDeserializationCallback)result).OnAfterDeserialization(); } return result; }
protected override IJsonTypeConverter CreateTypeConverter() { IJsonTypeConverter converter = CreateTypeConverter(ForType); if (converter == null) { return(TypeConverterAdapter.GetAdapter(ForType)); } //return null; else { return(converter); } }
/// <summary> /// Converts an expression to an object by first Evaluating the expression as its converted type and /// then converting that result using the specified type converter. /// </summary> /// <param name="expression">the expression to convert</param> /// <param name="deserializer">deserializer instance</param> /// <param name="converter">the converter to use to convert the object</param> /// <returns>an object created from the expression</returns> public object Evaluate(Expression expression, IDeserializerHandler deserializer, IJsonTypeConverter converter) { Type sourceType = expression.ResultType; Type destType = converter.GetSerializedType(sourceType); expression.ResultType = destType; object tempResult = deserializer.Evaluate(expression); //TODO: Cast for now to avoid breaking compatibility object result = converter.ConvertTo(tempResult, sourceType, (SerializationContext) Config); expression.OnObjectConstructed(result); if (result is IDeserializationCallback) { ((IDeserializationCallback)result).OnAfterDeserialization(); } return result; }
/// <summary> /// Serialize the given object at the current indent level. The path to the object is represented by /// currentPath such as "this.name", etc. This is an internal method that can be called recursively. /// </summary> /// <param name="value">the object to serialize</param> /// <param name="currentPath">the current path for reference writing</param> public Expression Serialize(object value, JsonPath currentPath, IJsonTypeConverter converter) { if (value == null) { return(new NullExpression()); } else { Expression expr = HandleReference(value, currentPath); if (expr != null) { return(expr); } ISerializationCallback callback = value as ISerializationCallback; if (callback != null) { callback.OnBeforeSerialization(); } try { //TODO: this is too early for converters SetCanReference(value); // regular object, can reference at any time IExpressionHandler objHandler; if (converter != null) { TypeConverterExpressionHandler converterHandler = (TypeConverterExpressionHandler)_context.ExpressionHandlers.Find(typeof(TypeConverterExpressionHandler)); //TODO: make sure it exists return(converterHandler.GetExpression(value, converter, currentPath, this)); } objHandler = _context.ExpressionHandlers.GetHandler(value); return(objHandler.GetExpression(value, currentPath, this)); } finally { if (callback != null) { callback.OnAfterSerialization(); } } } }
/// <summary> /// Converts an expression to an object by first Evaluating the expression as its converted type and /// then converting that result using the specified type converter. /// </summary> /// <param name="expression">the expression to convert</param> /// <param name="deserializer">deserializer instance</param> /// <param name="converter">the converter to use to convert the object</param> /// <returns>an object created from the expression</returns> public object Evaluate(Expression expression, IDeserializerHandler deserializer, IJsonTypeConverter converter) { Type sourceType = expression.ResultType; Type destType = converter.GetSerializedType(sourceType); expression.ResultType = destType; object tempResult = deserializer.Evaluate(expression); //TODO: Cast for now to avoid breaking compatibility object result = converter.ConvertTo(tempResult, sourceType, (SerializerSettings)Settings); expression.OnObjectConstructed(result); if (result is IDeserializationCallback) { ((IDeserializationCallback)result).OnAfterDeserialization(); } return(result); }
/// <summary> /// Register a type converter with the DefaultConverterFactory. /// </summary> /// <param name="forType">the property to register</param> /// <param name="converter">the converter</param> public void RegisterTypeConverter(Type forType, string propertyName, IJsonTypeConverter converter) { TypeHandlerFactory.RegisterTypeConverter(forType, propertyName, converter); }
/// <summary> /// Creates the type converter represented by this attribute. /// </summary> /// <returns></returns> public virtual IJsonTypeConverter CreateTypeConverter() { IJsonTypeConverter converter = (IJsonTypeConverter)Activator.CreateInstance(Converter); return(converter); }
public virtual void RegisterTypeConverter(Type forType, string PropertyName, IJsonTypeConverter converter) { this[forType].FindProperty(PropertyName).TypeConverter = converter; }
/// <summary> /// Register a type converter with the DefaultConverterFactory. /// </summary> /// <param name="forType">the type to register</param> /// <param name="converter">the converter</param> public void RegisterTypeConverter(Type forType, IJsonTypeConverter converter) { TypeHandlerFactory.RegisterTypeConverter(forType, converter); }
protected virtual void EvaluateItem(object existingObject, IDeserializerHandler deserializer, ITypeData typeHandler, KeyValueExpression Item) { // evaluate the item and let it assign itself? IPropertyData hndlr = typeHandler.FindPropertyByAlias(Item.Key); if (hndlr == null) { switch (this.Settings.MissingPropertyAction) { case MissingPropertyOptions.Ignore: return; case MissingPropertyOptions.ThrowException: throw new Exception(string.Format("Could not find property {0} for type {1}", Item.Key, typeHandler.ForType)); default: throw new InvalidOperationException("Unhandled MissingPropertyAction: " + this.Settings.MissingPropertyAction); } } if (hndlr.Ignored) { switch (Settings.IgnoredPropertyAction) { case IgnoredPropertyOption.Ignore: return; case IgnoredPropertyOption.SetIfPossible: if (!hndlr.CanWrite) { return; } break; case IgnoredPropertyOption.ThrowException: throw new Exception(string.Format("Can not set property {0} for type {1} because it is ignored and IgnorePropertyAction is set to ThrowException", Item.Key, typeHandler.ForType)); } } Expression valueExpression = Item.ValueExpression; valueExpression.ResultType = hndlr.PropertyType; object result = null; TypeConverterExpressionHandler converterHandler = null; IJsonTypeConverter converter = null; if (hndlr.HasConverter) { converterHandler = (TypeConverterExpressionHandler)Settings.ExpressionHandlers.Find(typeof(TypeConverterExpressionHandler)); converter = hndlr.TypeConverter; } if (!hndlr.CanWrite) { result = hndlr.GetValue(existingObject); if (converterHandler != null) { converterHandler.Evaluate(valueExpression, result, deserializer, converter); } else { deserializer.Evaluate(valueExpression, result); } } else { if (hndlr.HasConverter) { hndlr.SetValue(existingObject, converterHandler.Evaluate(valueExpression, deserializer, converter)); } else { hndlr.SetValue(existingObject, deserializer.Evaluate(valueExpression)); } } }
public virtual void RegisterTypeConverter(Type forType, IJsonTypeConverter converter) { if (forType.IsPrimitive || forType == typeof(string)) throw new ArgumentException("Converters can not be registered for primitive types or string. " + forType, "forType"); this[forType].TypeConverter = converter; }
/// <summary> /// This method is invalid for TypeConverterExpressionHandler /// </summary> /// <exception cref="NotSupportedException">Evaluating an existing object is not supported by TypeConverterExpressionHandler</exception> public void Evaluate(Expression valueExpression, object result, IDeserializerHandler deserializer, IJsonTypeConverter converter) { //TODO: possibly allow this if the type implements IJsonTypeConverter itself throw new NotSupportedException("Cannot convert an existing object."); }
/// <summary> /// Evaluates the expression and populates an existing object with the expression's properties /// </summary> /// <param name="expression">json object expression</param> /// <param name="existingObject">the existing object to populate</param> /// <param name="deserializer">deserializer for deserializing key values</param> /// <returns>deserialized object</returns> public override object Evaluate(Expression expression, object existingObject, IDeserializerHandler deserializer) { TypeData typeHandler = Context.GetTypeHandler(existingObject.GetType()); ObjectExpression objectExpression = (ObjectExpression)expression; foreach (KeyValueExpression Item in objectExpression.Properties) { // evaluate the item and let it assign itself? IPropertyData hndlr = typeHandler.FindProperty(Item.Key); if (hndlr == null) { throw new Exception(string.Format("Could not find property {0} for type {1}", Item.Key, typeHandler.ForType)); } if (hndlr.Ignored) { switch (Context.IgnoredPropertyAction) { case SerializationContext.IgnoredPropertyOption.Ignore: continue; case SerializationContext.IgnoredPropertyOption.SetIfPossible: if (!hndlr.CanWrite) { continue; } break; case SerializationContext.IgnoredPropertyOption.ThrowException: throw new Exception(string.Format("Can not set property {0} for type {1} because it is ignored and IgnorePropertyAction is set to ThrowException", Item.Key, typeHandler.ForType)); } } Expression valueExpression = Item.ValueExpression; valueExpression.ResultType = hndlr.PropertyType; object result = null; TypeConverterExpressionHandler converterHandler = null; IJsonTypeConverter converter = null; if (hndlr.HasConverter) { converterHandler = (TypeConverterExpressionHandler)Context.ExpressionHandlers.Find(typeof(TypeConverterExpressionHandler)); converter = hndlr.TypeConverter; } if (!hndlr.CanWrite) { result = hndlr.GetValue(existingObject); if (converterHandler != null) { converterHandler.Evaluate(valueExpression, result, deserializer, converter); } else { deserializer.Evaluate(valueExpression, result); } } else { if (hndlr.HasConverter) { hndlr.SetValue(existingObject, converterHandler.Evaluate(valueExpression, deserializer, converter)); } else { hndlr.SetValue(existingObject, deserializer.Evaluate(valueExpression)); } } } return(existingObject); }
/// <summary> /// Gets an expression for a value by first converting it with its registered type converter and then calling Serialize /// </summary> /// <param name="value">the value to generate an expression for</param> /// <param name="currentPath">the current path to the value</param> /// <param name="serializer">serializer instance</param> /// <returns>an expression for the value</returns> public override Expression GetExpression(object value, JsonPath currentPath, IExpressionBuilder serializer) { IJsonTypeConverter converter = GetConverter(value); return(GetExpression(value, converter, currentPath, serializer)); }
public JsonTypeConverterWrapper(IJsonTypeConverter typeConverter, JsonConverter jsonConverter) { this.typeConverter = typeConverter; this.jsonConverter = jsonConverter; }
public void RegisterTypeConverter(Type objectType, IJsonTypeConverter typeConverter) { typeConverters[objectType] = typeConverter; }
public virtual bool IsReferenceable(object value, IJsonTypeConverter converter) { //TODO: Cast for now to avoid breaking compatibility return converter.SupportsReferences(value.GetType(), (SerializationContext) this.Config); }
public virtual bool IsReferenceable(object value, IJsonTypeConverter converter) { //TODO: Cast for now to avoid breaking compatibility return(converter.SupportsReferences(value.GetType(), (SerializerSettings)this.Settings)); }
/// <summary> /// Converts an expression to an object by first Evaluating the expression as its converted type and /// then converting that result using the specified type converter. /// </summary> /// <param name="expression">the expression to convert</param> /// <param name="deserializer">deserializer instance</param> /// <param name="converter">the converter to use to convert the object</param> /// <returns>an object created from the expression</returns> public object Evaluate(Expression expression, IDeserializerHandler deserializer, IJsonTypeConverter converter) { Type sourceType = expression.ResultType; Type destType = converter.GetSerializedType(sourceType); expression.ResultType = destType; object tempResult = deserializer.Evaluate(expression); object result = converter.ConvertTo(tempResult, sourceType, Context); expression.OnObjectConstructed(result); if (result is IDeserializationCallback) { ((IDeserializationCallback)result).OnAfterDeserialization(); } return(result); }
public void RegisterTypeConverter(Type forType, string PropertyName, IJsonTypeConverter converter) { this[forType].FindProperty(PropertyName).TypeConverter = converter; }
/// <summary> /// Registers a type converter for a type /// </summary> /// <typeparam name="T">the type to register for</typeparam> /// <param name="typeSettings">the type settings object of the serializer</param> /// <param name="converter">the converter to register</param> public static void RegisterTypeConverter <T>(this ITypeSettings typeSettings, IJsonTypeConverter converter) { typeSettings.RegisterTypeConverter(typeof(T), converter); }
/// <summary> /// Gets an expression for a value by first converting it with a specific type converter and then calling Serialize. This /// method can be called directly when using a Property Converter /// </summary> /// <param name="value">the value to generate an expression for</param> /// <param name="converter">the type converter to use for conversion</param> /// <param name="currentPath">the current path to the value</param> /// <param name="serializer">serializer instance</param> /// <returns>an expression for the value</returns> public Expression GetExpression(object value, IJsonTypeConverter converter, JsonPath currentPath, ISerializerHandler serializer) { object convertedObject = converter.ConvertFrom(value, Context); // call serialize again in case the new type has a converter Expression expr = serializer.Serialize(convertedObject, currentPath, null); serializer.SetCanReference(value); // can't reference inside the object return expr; }
/// <summary> /// Registers a type converter for a property /// </summary> /// <typeparam name="T">the type that contains the property</typeparam> /// <typeparam name="P">the type of the property</typeparam> /// <param name="typeSettings">the type settings object of the serializer</param> /// <param name="propertyExpression">An expression that references the property</param> /// <param name="converter">the converter to register</param> public static void RegisterTypeConverter <T, P>(this ITypeSettings typeSettings, Expression <Func <T, P> > propertyExpression, IJsonTypeConverter converter) { typeSettings.RegisterTypeConverter(typeof(T), ReflectionUtils.GetPropertyName(propertyExpression), converter); }
/// <summary> /// Serialize the given object at the current indent level. The path to the object is represented by /// currentPath such as "this.name", etc. This is an internal method that can be called recursively. /// </summary> /// <param name="value">the object to serialize</param> /// <param name="currentPath">the current path for reference writing</param> public Expression Serialize(object value, JsonPath currentPath, IJsonTypeConverter converter) { if (value == null) { return new NullExpression(); } else { Expression expr = HandleReference(value, currentPath); if (expr != null) return expr; ISerializationCallback callback = value as ISerializationCallback; if (callback != null) callback.OnBeforeSerialization(); try { //TODO: this is too early for converters SetCanReference(value); // regular object, can reference at any time IExpressionHandler objHandler; if (converter != null) { TypeConverterExpressionHandler converterHandler = (TypeConverterExpressionHandler)_context.ExpressionHandlers.Find(typeof(TypeConverterExpressionHandler)); //TODO: make sure it exists return converterHandler.GetExpression(value, converter, currentPath, this); } objHandler = _context.ExpressionHandlers.GetHandler(value); return objHandler.GetExpression(value, currentPath, this); } finally { if (callback != null) callback.OnAfterSerialization(); } } }
/// <summary> /// Gets an expression for a value by first converting it with a specific type converter and then calling Serialize. This /// method can be called directly when using a Property Converter /// </summary> /// <param name="value">the value to generate an expression for</param> /// <param name="converter">the type converter to use for conversion</param> /// <param name="currentPath">the current path to the value</param> /// <param name="serializer">serializer instance</param> /// <returns>an expression for the value</returns> public Expression GetExpression(object value, IJsonTypeConverter converter, JsonPath currentPath, IExpressionBuilder serializer) { //TODO: Cast for now to avoid breaking compatibility object convertedObject = converter.ConvertFrom(value, (SerializationContext) Config); // call serialize again in case the new type has a converter Expression expr = serializer.Serialize(convertedObject, currentPath, null); serializer.SetCanReference(value); // can't reference inside the object return expr; }