/// <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;
        }
Esempio n. 2
0
 /// <summary>
 /// Parses a javascript object
 /// </summary>
 /// <returns></returns>
 private ObjectExpression ParseObject()
 {
     Token tok = ReadToken();
     Debug.Assert(tok == LBraceToken);
     ObjectExpression value = new ObjectExpression();
     Expression item;
     while (ReadAhead(CommaToken, RBraceToken, new ExpressionMethod(ParseKeyValue), out item))
     {
         value.Add((KeyValueExpression)item);
     }
     return value;
 }
        /// <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, ISerializerHandler 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;
        }