コード例 #1
0
 public override Expression GetExpression(object data, JsonPath currentPath, IExpressionBuilder serializer)
 {
     DataTable table = (DataTable) data;
     ObjectExpression tableExpr = new ObjectExpression();
     tableExpr.Add("TableName", serializer.Serialize(table.TableName, currentPath.Append("TableName")));
     tableExpr.Add("Columns", GetColumnsExpression(table, currentPath.Append("Columns"), serializer));
     tableExpr.Add("Rows", GetRowsExpression(table, currentPath, serializer));
     return tableExpr;
 }
コード例 #2
0
ファイル: ExpressionBuilder.cs プロジェクト: vfioox/ENULib
        /// <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();
                }
            }
        }
コード例 #3
0
ファイル: ReferenceVisitor.cs プロジェクト: vfioox/ENULib
 public void VisitComplexBase(ComplexExpressionBase expression)
 {
     if (_refID.Top == JsonPath.Root)
     {
         if (expression.Parent != null)
         {
             throw new ArgumentException("Reference for this passed to object that is not at the root", "refID");
         }
     }
     else
     {
         // have to assume that the parent checked that we were the right reference
         // should only get here if we have a parent, if no parent we're not valid
         if (expression.Parent == null)
             throw new ArgumentException("Invalid reference", "refID");
     }
     // it is this object, check if we need to go further
     _refID = _refID.ChildReference();
     if (_refID.IsEmpty) {
         _expr = expression;
     }
 }
コード例 #4
0
 /// <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);
 }
コード例 #5
0
        /// <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;
        }
コード例 #6
0
 public override Expression GetExpression(object data, JsonPath currentPath, IExpressionBuilder serializer)
 {
     string value = ((DateTime)data).ToString(dateFormat, Culture);
     return new ValueExpression(value);
 }
コード例 #7
0
 /// <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, IExpressionBuilder serializer)
 {
     throw new InvalidOperationException("CastObjectHandler should not be called during Serialization");
 }
コード例 #8
0
 protected virtual Expression GetColumnsExpression(DataTable table, JsonPath currentPath, IExpressionBuilder serializer)
 {
     ArrayExpression columns = new ArrayExpression();
     int colCount = 0;
     foreach (DataColumn dc in table.Columns)
     {
         columns.Add(GetColumnExpression(dc, currentPath.Append(colCount), serializer));
         colCount++;
     }
     return columns;
 }
コード例 #9
0
ファイル: ReferenceExpression.cs プロジェクト: vfioox/ENULib
        private Expression _reference; // the expression that is referenced

        #endregion Fields

        #region Constructors

        public ReferenceExpression(string ReferencePath)
        {
            this._path = new JsonPath(ReferencePath);
        }
コード例 #10
0
ファイル: ReferenceVisitor.cs プロジェクト: vfioox/ENULib
 public ReferenceVisitor(JsonPath RefID)
 {
     _refID = RefID;
 }
コード例 #11
0
ファイル: Parser.cs プロジェクト: vfioox/ENULib
        /// <summary>
        /// Parses a reference to an object
        /// </summary>
        /// <returns></returns>
        private Expression ParseReference()
        {
            JsonPath refID = new JsonPath();
            Token tok = ReadToken();
            if (tok != ReferenceStartToken && tok != OldReferenceStartToken)
                throw new ParseException(string.Format("Invalid starting token for ParseReference at Line: {0}, Position: {1}, Expected: {2} or {3}, got: {4}", tok.linenumber, tok.position, ReferenceStartToken, OldReferenceStartToken, tok));
            int line = tok.linenumber;
            int pos = tok.position;
            while (PeekToken() == PeriodToken || PeekToken() == LSquareToken)
            {
                tok = ReadToken(); // separator "."
                if (tok == PeriodToken)
                    tok = ReadToken(); // type part

                if (tok == LSquareToken)
                {
                    refID = refID.Append(ReadToken().value); // index
                    ReadToken(); // ]
                }
                else if (tok.type == TokenType.Identifier)
                {
                    refID = refID.Append(tok.value);
                }
                else
                {
                    throw new ParseException(string.Format("Invalid Reference at Line: {0}, Position: {1}, must be an identifier or array value: {2}", tok.linenumber, tok.position, tok));
                }
            }
            return new ReferenceExpression(refID) {  LineNumber = line, CharacterPosition = pos };
        }
コード例 #12
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, 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;
 }
コード例 #13
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, IExpressionBuilder serializer)
 {
     IJsonTypeConverter converter = GetConverter(value);
     return GetExpression(value, converter, currentPath, serializer);
 }
コード例 #14
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, IExpressionBuilder serializer)
        {
            TypeData handler = Config.GetTypeHandler(data.GetType());

            ObjectExpression expression = new ObjectExpression();

            foreach (IPropertyData prop in handler.Properties)
            {
                GenerateItemExpression(data, currentPath, serializer, expression, prop);
            }
            return expression;
        }
コード例 #15
0
ファイル: ReferenceExpression.cs プロジェクト: vfioox/ENULib
 public ReferenceExpression(JsonPath ReferencePath)
 {
     this._path = ReferencePath;
 }
コード例 #16
0
 protected virtual Expression GetRowsExpression(DataTable table, JsonPath currentPath, IExpressionBuilder serializer)
 {
     ArrayExpression rowsExpr = new ArrayExpression();
     for (int i = 0; i < table.Rows.Count; i++)
     {
         DataRow row = table.Rows[i];
         object[] values = row.ItemArray;
         JsonPath rowPath = currentPath.Append(i);
         ArrayExpression rowExpr = new ArrayExpression();
         for (int j = 0; j < values.Length; j++)
         {
             rowExpr.Add(serializer.Serialize(values[j], rowPath.Append(j)));
         }
         rowsExpr.Add(rowExpr);
     }
     return rowsExpr;
 }
コード例 #17
0
ファイル: ExpressionBuilder.cs プロジェクト: vfioox/ENULib
        protected virtual Expression HandleReference(object value, JsonPath CurrentPath)
        {
            ReferenceInfo refInfo = null;
            if (_refs.ContainsKey(value))
            {
                /*
                 * This object has already been seen by the serializer so
                 * determine what to do with it.  If its part of the current path
                 * then its a circular reference and an error needs to be thrown or it should
                 * be ignored depending on the option. Otherwise write a reference to it
                 */
                refInfo = _refs[value];
                JsonPath refPath = refInfo.Path;
                switch (_config.ReferenceWritingType)
                {
                    case SerializationContext.ReferenceOption.WriteIdentifier:
                        if (!refInfo.CanReference)
                            throw new InvalidOperationException("Can't reference object: " + refPath + " from " + CurrentPath + ", either it is a collection, or it has not been converted yet");

                        return new ReferenceExpression(refPath);
                    case SerializationContext.ReferenceOption.IgnoreCircularReferences:
                        if (CurrentPath.StartsWith(refPath))
                        {
                            return new NullExpression();
                        }
                        break;
                    case SerializationContext.ReferenceOption.ErrorCircularReferences:
                        if (CurrentPath.StartsWith(refPath))
                        {
                            throw new InvalidOperationException("Circular reference detected.  Current path: " + CurrentPath + ", reference to: " + refPath);
                        }
                        break;
                }
            }
            else
            {
                refInfo = new ReferenceInfo(CurrentPath);
                _refs[value] = refInfo;
            }
            return null;
        }
コード例 #18
0
 protected virtual Expression GetColumnExpression(DataColumn dc, JsonPath jsonPath, IExpressionBuilder serializer)
 {
     ObjectExpression column = new ObjectExpression();
     // just DataType and column for now
     column.Add("DataType", serializer.Serialize(dc.DataType, jsonPath.Append("DataType")));
     column.Add("ColumnName", serializer.Serialize(dc.ColumnName, jsonPath.Append("ColumnName")));
     return column;
 }
コード例 #19
0
ファイル: ExpressionBuilder.cs プロジェクト: vfioox/ENULib
 public ReferenceInfo(JsonPath Path)
 {
     this.Path = Path;
 }
コード例 #20
0
        /// <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;
        }
コード例 #21
0
ファイル: ExpressionBuilder.cs プロジェクト: vfioox/ENULib
 public Expression Serialize(object value, JsonPath currentPath)
 {
     return Serialize(value, currentPath, null);
 }
コード例 #22
0
 /// <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, IExpressionBuilder serializer)
 {
     return new NullExpression();
 }
コード例 #23
0
ファイル: JsonPath.cs プロジェクト: vfioox/ENULib
 public bool StartsWith(JsonPath value)
 {
     return this.ToString().StartsWith(value.ToString());
 }