/// <summary> /// Evaluates the expression and populates an existing object with keys and values. /// </summary> /// <param name="expression">the expression to evaluate</param> /// <param name="existingObject">the existing object to populate</param> /// <param name="deserializer">the deserializer instance to use to deserialize other expressions</param> /// <returns>a populated object</returns> public override object Evaluate(Expression expression, object existingObject, IDeserializerHandler deserializer) { Type _dictionaryKeyType = typeof(string); Type _dictionaryValueType = null; Type genDict = existingObject.GetType().GetInterface(typeof(IDictionary<,>).Name); // attempt to figure out what the types of the values are, if no type is set already if (genDict != null) { Type[] genArgs = genDict.GetGenericArguments(); _dictionaryKeyType = genArgs[0]; _dictionaryValueType = genArgs[1]; } ObjectExpression objectExpression = (ObjectExpression)expression; foreach (KeyValueExpression keyValue in objectExpression.Properties) { // if no type set, set one keyValue.KeyExpression.ResultType = _dictionaryKeyType; if (_dictionaryValueType != null) keyValue.ValueExpression.ResultType = _dictionaryValueType; object keyObject = deserializer.Evaluate(keyValue.KeyExpression); object result = deserializer.Evaluate(keyValue.ValueExpression); ((IDictionary)existingObject)[keyObject] = result; } return existingObject; }
/// <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; }
public Expression Execute(Expression root) { IList<ReferenceExpression> references = CollectReferences(root); foreach (ReferenceExpression reference in references) ResolveReference(reference, root); return root; }
private void WriteCast(Expression Expression) { CastExpression cast = (CastExpression)Expression; if (_context.OutputTypeInformation && !(cast.Expression is ReferenceExpression)) _jsonWriter.WriteCast(cast.ResultType); Write(cast.Expression); }
private static void ResolveReference(ReferenceExpression reference, Expression root) { ReferenceVisitor visitor = new ReferenceVisitor(reference.Path); visitor.Visit(root); if (visitor.ReferencedExpression == null) throw new ParseException("Unable to resolve reference to " + reference.Path); reference.ReferencedExpression = visitor.ReferencedExpression; }
private void WriteList(Expression Expression) { ArrayExpression list = (ArrayExpression)Expression; _jsonWriter.WriteArrayStart(); foreach (Expression item in list.Items) Write(item); _jsonWriter.WriteArrayEnd(); }
/// <summary> /// Evaluates the expression and deserializes it. /// </summary> /// <param name="expression">json object expression</param> /// <param name="deserializer">deserializer for deserializing key values</param> /// <returns>deserialized object</returns> public override object Evaluate(Expression expression, IDeserializerHandler deserializer) { object value = ConstructObject((ObjectExpression)expression, deserializer); value = Evaluate(expression, value, deserializer); if (value is IDeserializationCallback) ((IDeserializationCallback)value).OnAfterDeserialization(); return value; }
public override object Evaluate(Expression Expression, object existingObject, IDeserializerHandler deserializer) { Expression.OnObjectConstructed(existingObject); Type itemType = GetItemType(existingObject.GetType()); EvaluateItems((ArrayExpression) Expression, existingObject, itemType, deserializer); if (existingObject is IDeserializationCallback) ((IDeserializationCallback)existingObject).OnAfterDeserialization(); return existingObject; }
public override object Evaluate(Expression expression, IDeserializerHandler deserializer) { ValueExpression value = (ValueExpression)expression; if (value.ResultType.IsEnum) return Enum.Parse(value.ResultType, value.StringValue); else if (value.ResultType == typeof(object)) return value.StringValue; else if (value.ResultType == typeof(string)) return value.StringValue; else return Convert.ChangeType(value.Value, value.ResultType, CultureInfo.InvariantCulture); }
/// <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 override object Evaluate(Expression expression, IDeserializerHandler deserializer) { DataTable table = new DataTable(); ObjectExpression tableExpr = (ObjectExpression)expression; EvaluateColumns(table, (ArrayExpression)tableExpr["Columns"], deserializer); // remove the columns expression, it's been evaluated already tableExpr.Properties.RemoveAt(tableExpr.IndexOf("Columns")); // now the rows EvaluateRows(table, (ArrayExpression)tableExpr["Rows"], deserializer); // remove the rows expression, it's been evaluated already tableExpr.Properties.RemoveAt(tableExpr.IndexOf("Rows")); // fill in any remaining properties return base.Evaluate(expression, table, deserializer); }
/// <summary> /// Converts an expression to an object by first Evaluating the expression as its converted type and /// then converting that result using a type converter. /// </summary> /// <param name="expression">the expression to convert</param> /// <param name="deserializer">deserializer instance</param> /// <returns>an object created from the expression</returns> public override object Evaluate(Expression expression, IDeserializerHandler deserializer) { Type sourceType = expression.ResultType; TypeData handler = Context.TypeHandlerFactory[sourceType]; IJsonTypeConverter converter; if (typeof(IJsonTypeConverter).IsAssignableFrom(sourceType)) { converter = (IJsonTypeConverter) Activator.CreateInstance(sourceType); } else { converter = handler.TypeConverter; } return Evaluate(expression, deserializer, converter); }
protected virtual void WriteNumeric(Expression expression) { NumericExpression numeric = (NumericExpression)expression; object value = numeric.Value; switch (Type.GetTypeCode(value.GetType())) { case TypeCode.Double: jsonWriter.WriteValue((double)value); break; case TypeCode.Single: jsonWriter.WriteValue((float)value); break; case TypeCode.Int64: jsonWriter.WriteValue((long)value); break; case TypeCode.Decimal: case TypeCode.UInt64: jsonWriter.WriteSpecialValue(string.Format("{0}", value)); break; default: jsonWriter.WriteValue((long)Convert.ChangeType(value, typeof(long), CultureInfo.InvariantCulture)); break; } }
protected virtual void WriteList(Expression Expression) { ArrayExpression list = (ArrayExpression)Expression; jsonWriter.WriteArrayStart(); foreach (Expression item in list.Items) Write(item); jsonWriter.WriteArrayEnd(); }
protected virtual void WriteNull(Expression expression) { if (!(expression is NullExpression)) throw new ArgumentException("Expression should be a NullExpression"); jsonWriter.WriteSpecialValue("null"); }
protected virtual void WriteValue(Expression expression) { ValueExpression value = (ValueExpression)expression; jsonWriter.WriteQuotedValue(value.StringValue); }
protected virtual void WriteBoolean(Expression expression) { this.jsonWriter.WriteValue((bool)((BooleanExpression)expression).Value); }
public static void Write(IJsonWriter writer, bool outputTypeInfo, Expression expression) { new ExpressionWriter(writer, outputTypeInfo).Write(expression); }
public virtual void Write(Expression expression) { this.actions[expression.GetType()](expression); }
protected virtual void WriteCast(Expression Expression) { CastExpression cast = (CastExpression)Expression; if (outputTypeInfo && !(cast.Expression is ReferenceExpression)) jsonWriter.WriteCast(cast.ResultType); Write(cast.Expression); }
private void WriteBoolean(Expression expression) { _jsonWriter.WriteValue((bool)((BooleanExpression)expression).Value); }
/// <summary> /// Add a property to this object /// </summary> /// <param name="key">the key for the property</param> /// <param name="value">the value for the property</param> /// <returns>KeyValueExpression that was added</returns> public KeyValueExpression Add(string key, Expression value) { return Add(new ValueExpression(key), value); }
/// <summary> /// Converts an existing object /// </summary> /// <param name="expression">the expression</param> /// <param name="existingObject">an existing object</param> /// <param name="deserializer">deserializer</param> /// <returns>the existing object</returns> public override object Evaluate(Expression expression, object existingObject, IDeserializerHandler deserializer) { return existingObject; }
/// <summary> /// Determines whether this handler can deserialize a specific expression. The NullExpressionHandler /// only handles NullExpressions. /// </summary> /// <param name="expression">the expression</param> /// <returns>true if this handler handles the expression</returns> public override bool CanHandle(Expression expression) { return (expression is NullExpression); }
/// <summary> /// Converts the expression back into an object /// </summary> /// <param name="expression">the expression</param> /// <param name="deserializer">the deserializer</param> /// <returns>null</returns> public override object Evaluate(Expression expression, IDeserializerHandler deserializer) { if (!(expression is NullExpression)) throw new ArgumentException("expression should be NullExpression"); return null; }
private void WriteNull(Expression expression) { if (!(expression is NullExpression)) throw new ArgumentException("Expression should be a NullExpression"); _jsonWriter.WriteSpecialValue("null"); }
public Expression Execute(Expression root) { CustomTypeResolverVisitor visitor = new CustomTypeResolverVisitor(); root.Accept(visitor); return root; }
protected virtual void WriteObject(Expression Expression) { ObjectExpression obj = (ObjectExpression)Expression; bool hasConstructor = false; if (obj.ConstructorArguments.Count > 0) { hasConstructor = true; jsonWriter.WriteConstructorStart(obj.ResultType); jsonWriter.WriteConstructorArgsStart(); foreach (Expression ctorArg in obj.ConstructorArguments) { Write(ctorArg); } jsonWriter.WriteConstructorArgsEnd(); } if (!hasConstructor || obj.Properties.Count > 0) { jsonWriter.WriteObjectStart(); foreach (KeyValueExpression keyValue in obj.Properties) { jsonWriter.WriteKey(keyValue.Key); Write(keyValue.ValueExpression); } jsonWriter.WriteObjectEnd(); } if (hasConstructor) jsonWriter.WriteConstructorEnd(); }
/// <summary> /// Add a property to this object /// </summary> /// <param name="key">the key for the property</param> /// <param name="value">the value for the property</param> /// <returns>KeyValueExpression that was added</returns> public KeyValueExpression Add(Expression key, Expression value) { return Add(new KeyValueExpression(key, value)); }
protected virtual void WriteReference(Expression Expression) { ReferenceExpression refExpr = (ReferenceExpression)Expression; jsonWriter.WriteSpecialValue(refExpr.Path.ToString()); }