Exemple #1
0
        /// <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));
        }
        /// <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);
        }
Exemple #3
0
        /// <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);
        }
        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>
        /// 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, ISerializerHandler serializer)
        {
            TypeData handler = Context.GetTypeHandler(data.GetType());

            CollectionHandler collectionHandler = handler.GetCollectionHandler();
            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 Expression GetExpression(object data, JsonPath CurrentPath, ISerializerHandler serializer)
 {
     return GetExpression((IEnumerable)data, GetItemType(data.GetType()), CurrentPath, serializer);
 }
 /// <summary>
 /// Returns a reference expression to the current data
 /// </summary>
 /// <param name="data">the object data</param>
 /// <param name="currentPath">current path to this object</param>
 /// <param name="serializer">serializer instance</param>
 /// <returns>reference expression</returns>
 public override Expression GetExpression(object data, JsonPath currentPath, ISerializerHandler serializer)
 {
     return serializer.HandleReference(data, currentPath);
 }
 /// <summary>
 /// Take an object and convert it to an expression to be serialized.  Child names/indexes should be appended to the
 /// currentPath before serializing child objects.
 /// </summary>
 /// <param name="data">the object to convert</param>
 /// <param name="CurrentPath">the current path to this object from the root, used for tracking references</param>
 /// <param name="serializer">serializer instance for serializing child objects</param>
 /// <returns>an expression which represents a json structure</returns>
 public abstract Expression GetExpression(object data, JsonPath currentPath, ISerializerHandler serializer);
Exemple #9
0
        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);
        }
Exemple #10
0
 /// <summary>
 /// Converts a boolean value into a BooleanExpression
 /// </summary>
 /// <param name="data">the data to convert to an expression</param>
 /// <param name="currentPath">the current path to the object, ignored for BooleanExpression</param>
 /// <param name="serializer">serializer instance, ignored</param>
 /// <returns>a BooleanExpression</returns>
 public override Expression GetExpression(object data, JsonPath currentPath, ISerializerHandler serializer)
 {
     return(new BooleanExpression(data));
 }
 /// <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);
 }
 public override Expression GetExpression(object data, JsonPath currentPath, ISerializerHandler serializer)
 {
     string value = ((DateTime)data).ToString(dateFormat, Culture);
     return new ValueExpression(value);
 }
Exemple #13
0
        public override Expression GetExpression(object data, JsonPath currentPath, ISerializerHandler serializer)
        {
            string value = ((DateTime)data).ToString(dateFormat, Culture);

            return(new ValueExpression(value));
        }
 /// <summary>
 /// GetExpression is not valid for a CastExpression.  CastExpressions should be created directly
 /// during serialization whenever type information is needed.
 /// </summary>
 /// <param name="data">data to serialize</param>
 /// <param name="currentPath">the current path to the object</param>
 /// <param name="serializer">serializer instance</param>
 /// <returns>expression</returns>
 /// <exception cref="InvalidOperationException">This will throw an exception if called</exception>
 public override Expression GetExpression(object data, JsonPath currentPath, ISerializerHandler serializer)
 {
     throw new InvalidOperationException("CastObjectHandler should not be called during Serialization");
 }
Exemple #15
0
        /// <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>
        /// 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, ISerializerHandler serializer)
        {
            TypeData handler = Context.GetTypeHandler(data.GetType());

            CollectionHandler collectionHandler = handler.GetCollectionHandler();
            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;
        }
 /// <summary>
 /// Creates a null expression
 /// </summary>
 /// <param name="data">the data</param>
 /// <param name="currentPath">current path</param>
 /// <param name="serializer">serializer instance</param>
 /// <returns>NullExpression</returns>
 public override Expression GetExpression(object data, JsonPath currentPath, ISerializerHandler serializer)
 {
     return new NullExpression();
 }
 /// <summary>
 /// GetExpression is not valid for a CastExpression.  CastExpressions should be created directly
 /// during serialization whenever type information is needed.
 /// </summary>
 /// <param name="data">data to serialize</param>
 /// <param name="currentPath">the current path to the object</param>
 /// <param name="serializer">serializer instance</param>
 /// <returns>expression</returns>
 /// <exception cref="InvalidOperationException">This will throw an exception if called</exception>
 public override Expression GetExpression(object data, JsonPath currentPath, ISerializerHandler serializer)
 {
     throw new InvalidOperationException("CastObjectHandler should not be called during Serialization");
 }
 /// <summary>
 /// Take an object and convert it to an expression to be serialized.  Child names/indexes should be appended to the
 /// currentPath before serializing child objects.
 /// </summary>
 /// <param name="data">the object to convert</param>
 /// <param name="CurrentPath">the current path to this object from the root, used for tracking references</param>
 /// <param name="serializer">serializer instance for serializing child objects</param>
 /// <returns>an expression which represents a json structure</returns>
 public abstract Expression GetExpression(object data, JsonPath currentPath, ISerializerHandler serializer);
        /// <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;
        }
 /// <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>
        /// 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;
        }
 /// <summary>
 /// Returns a reference expression to the current data
 /// </summary>
 /// <param name="data">the object data</param>
 /// <param name="currentPath">current path to this object</param>
 /// <param name="serializer">serializer instance</param>
 /// <returns>reference expression</returns>
 public override Expression GetExpression(object data, JsonPath currentPath, ISerializerHandler serializer)
 {
     return(serializer.HandleReference(data, currentPath));
 }
Exemple #24
0
 public override Expression GetExpression(object data, JsonPath CurrentPath, ISerializerHandler serializer)
 {
     return(GetExpression((IEnumerable)data, GetItemType(data.GetType()), CurrentPath, serializer));
 }