/// <summary>
        /// Serialize an object implementing IDictionary.  The serialized data is similar to a regular
        /// object, except that the keys of the dictionary are used instead of properties.
        /// </summary>
        /// <param name="data">the dictionary object</param>
        /// <param name="currentPath">object's path</param>
        /// <param name="serializer">the serializer instance, used to serialize keys and values</param>
        public override Expression GetExpression(object data, JsonPath currentPath, IExpressionBuilder serializer)
        {
            IDictionary dictionary = (IDictionary)data;
            Type itemType = typeof(object);
            Type genericDictionary = null;

            if ((genericDictionary = dictionary.GetType().GetInterface(typeof(IDictionary<,>).Name)) != null)
            {
                itemType = genericDictionary.GetGenericArguments()[1];
            }

            ObjectExpression expression = new ObjectExpression();
            foreach (DictionaryEntry pair in dictionary)
            {
                //may not work in all cases
                object value = pair.Value;
                Expression valueExpr = serializer.Serialize(value, currentPath.Append(pair.Key.ToString()));
                if (value != null && !ReflectionUtils.AreEquivalentTypes(value.GetType(), itemType))
                {
                    valueExpr = new CastExpression(value.GetType(), valueExpr);
                }
                expression.Add(pair.Key.ToString(), valueExpr);
            }
            return expression;
        }
 /// <summary>
 /// Serialize the given object
 /// </summary>
 /// <param name="value">object to serialize</param>
 public Expression Serialize(object value)
 {
     Expression expr = Serialize(value, new JsonPath(), null);
     if (value != null && !ReflectionUtils.AreEquivalentTypes(value.GetType(), _serializedType))
     {
         expr = new CastExpression(value.GetType(), expr);
     }
     return expr;
 }
        protected virtual Expression GetExpression(IEnumerable Items, Type ItemType, JsonPath CurrentPath, ISerializerHandler serializer)
        {
            int index = 0;

            ArrayExpression expression = new ArrayExpression();
            foreach (object value in Items)
            {
                Expression itemExpr = serializer.Serialize(value, CurrentPath.Append(index));
                if (value != null && !ReflectionUtils.AreEquivalentTypes(value.GetType(), ItemType))
                {
                    itemExpr = new CastExpression(value.GetType(), itemExpr);
                }
                expression.Add(itemExpr);
                index++;
            }
            return expression;
        }
 /// <summary>
 /// Generates an expression for an item and adds it to the object
 /// </summary>
 /// <param name="data">the item being serialized</param>
 /// <param name="currentPath">the current path to the object</param>
 /// <param name="serializer">serializer instance</param>
 /// <param name="expression">the object expression</param>
 /// <param name="prop">the property being serialized</param>
 protected virtual void GenerateItemExpression(object data, JsonPath currentPath, IExpressionBuilder serializer, ObjectExpression expression, IPropertyData prop)
 {
     object value = prop.GetValue(data);
     if (!prop.ShouldWriteValue(this.Config, value))
         return;
     Expression valueExpr;
     if (prop.HasConverter)
     {
         valueExpr = serializer.Serialize(value, currentPath.Append(prop.Alias), prop.TypeConverter);
     }
     else
     {
         valueExpr = serializer.Serialize(value, currentPath.Append(prop.Alias));
     }
     if (value != null && !ReflectionUtils.AreEquivalentTypes(value.GetType(), prop.PropertyType))
     {
         valueExpr = new CastExpression(value.GetType(), valueExpr);
     }
     expression.Add(prop.Alias, valueExpr);
 }
        /// <summary>
        /// Serializes the data into a json array expression.
        /// </summary>
        /// <param name="data">the data to serialize</param>
        /// <param name="currentPath">the current path to the data</param>
        /// <param name="serializer">serializer instance to use to serialize list items</param>
        /// <returns>a json array expression representation</returns>
        public override Expression GetExpression(object data, JsonPath currentPath, IExpressionBuilder serializer)
        {
            TypeData handler = Config.GetTypeHandler(data.GetType());

            CollectionHandler collectionHandler = handler.CollectionHandler;
            Type elemType = collectionHandler.GetItemType(handler.ForType);

            int index = 0;

            ArrayExpression expression = new ArrayExpression();
            foreach (object value in collectionHandler.GetEnumerable(data))
            {
                Expression itemExpr = serializer.Serialize(value, currentPath.Append(index));
                if (value != null && !ReflectionUtils.AreEquivalentTypes(value.GetType(), elemType))
                {
                    itemExpr = new CastExpression(value.GetType(), itemExpr);
                }
                expression.Add(itemExpr);
                index++;
            }
            return expression;
        }
 public override void OnObjectStart(ObjectExpression expression)
 {
     // inspect the "type" property if available and set the correct type on the "value"
     if (expression["type"] != null && expression["value"] != null)
     {
         string typeIndc = ((ValueExpression)expression["type"]).StringValue;
         Type newType = null;
         if (typeIndc == "Type1")
             newType = typeof(Type1);
         else if (typeIndc == "Type2")
             newType = typeof(Type2);
         if (newType != null)
             expression["value"] = new CastExpression(newType, expression["value"]);
     }
 }
 /// <summary>
 /// Serialize the given object
 /// </summary>
 /// <param name="value">object to serialize</param>
 public void Serialize(object value)
 {
     if (value != null && _context.OutputTypeComment)
     {
         string comment = "";
         comment += "/*" + "\r\n";
         comment += "  Created by JsonExSerializer" + "\r\n";
         comment += "  Assembly: " + value.GetType().Assembly.ToString() + "\r\n";
         comment += "  Type: " + value.GetType().FullName + "\r\n";
         comment += "*/" + "\r\n";
         this.WriteComment(comment);
     }
     Expression expr = Serialize(value, new JsonPath(), null);
     if (value != null && !ReflectionUtils.AreEquivalentTypes(value.GetType(), _serializedType))
     {
         expr = new CastExpression(value.GetType(), expr);
     }
     ExpressionWriter.Write(this, _context, expr);
 }
示例#8
0
 /// <summary>
 /// Parses a type cast.  Will also parse the expression that the cast applies to.
 /// The result will be the expression following the cast with the ResultType set to
 /// the type of the cast.
 /// </summary>
 /// <returns>casted expression</returns>
 private Expression ParseCast()
 {
     Token tok = ReadToken();
     Debug.Assert(tok == LParenToken, "Invalid starting token for ParseCast: " + tok);
     Type desiredType = ParseTypeSpecifier();
     CastExpression cast = new CastExpression(desiredType);
     tok = ReadToken();
     RequireToken(RParenToken, tok, "Invalid Type Cast Syntax");
     cast.Expression = ParseExpression();
     return cast;
 }
 public virtual void Visit(CastExpression expression)
 {
     expression.Expression.Accept(this);
 }
        /// <summary>
        /// Creates an json object expression from object data.
        /// </summary>
        /// <param name="data">the data to serialize</param>
        /// <param name="currentPath">current path to the object</param>
        /// <param name="serializer">serializer instance used to serialize key values</param>
        /// <returns>json object expression</returns>
        public override Expression GetExpression(object data, JsonPath currentPath, ISerializerHandler serializer)
        {
            TypeData handler = Context.GetTypeHandler(data.GetType());

            ObjectExpression expression = new ObjectExpression();

            foreach (IPropertyData prop in handler.Properties)
            {
                object value = prop.GetValue(data);
                Expression valueExpr;
                if (prop.HasConverter)
                {
                    valueExpr = serializer.Serialize(value, currentPath.Append(prop.Name), prop.TypeConverter);
                }
                else
                {
                    valueExpr = serializer.Serialize(value, currentPath.Append(prop.Name));
                }
                if (value != null && !ReflectionUtils.AreEquivalentTypes(value.GetType(), prop.PropertyType))
                {
                    valueExpr = new CastExpression(value.GetType(), valueExpr);
                }
                expression.Add(prop.Name, valueExpr);
            }
            return expression;
        }
 public void VisitCast(CastExpression Cast)
 {
     Visit(Cast.Expression);
 }