/// <summary>
        /// Serialize a datarow to a json datarow object token
        /// </summary>
        /// <param name="dataRow">The datarow to be serialized</param>
        /// <param name="dataRowVersion">The datarow version to be serialized</param>
        /// <returns>The json datarow object token</returns>
        private LazyJsonObject SerializeDataRow(DataRow dataRow, DataRowVersion dataRowVersion)
        {
            LazyJsonObject jsonObjectDataRowColumns = new LazyJsonObject();

            foreach (DataColumn dataColumn in dataRow.Table.Columns)
            {
                if (dataRow[dataColumn.ColumnName, dataRowVersion] == null || dataRow[dataColumn.ColumnName, dataRowVersion] == DBNull.Value)
                {
                    jsonObjectDataRowColumns.Add(new LazyJsonProperty(dataColumn.ColumnName, new LazyJsonNull()));
                }
                else if (dataColumn.DataType == typeof(Boolean))
                {
                    jsonObjectDataRowColumns.Add(new LazyJsonProperty(dataColumn.ColumnName, new LazyJsonBoolean(Convert.ToBoolean(dataRow[dataColumn.ColumnName, dataRowVersion]))));
                }
                else if (dataColumn.DataType == typeof(Char) || dataColumn.DataType == typeof(String))
                {
                    jsonObjectDataRowColumns.Add(new LazyJsonProperty(dataColumn.ColumnName, new LazyJsonString(Convert.ToString(dataRow[dataColumn.ColumnName, dataRowVersion]))));
                }
                else if (dataColumn.DataType == typeof(Int16) || dataColumn.DataType == typeof(Int32) || dataColumn.DataType == typeof(Int64))
                {
                    jsonObjectDataRowColumns.Add(new LazyJsonProperty(dataColumn.ColumnName, new LazyJsonInteger(Convert.ToInt64(dataRow[dataColumn.ColumnName, dataRowVersion]))));
                }
                else if (dataColumn.DataType == typeof(float) || dataColumn.DataType == typeof(Double) || dataColumn.DataType == typeof(Decimal))
                {
                    jsonObjectDataRowColumns.Add(new LazyJsonProperty(dataColumn.ColumnName, new LazyJsonDecimal(Convert.ToDecimal(dataRow[dataColumn.ColumnName, dataRowVersion]))));
                }
                else
                {
                    jsonObjectDataRowColumns.Add(new LazyJsonProperty(dataColumn.ColumnName, new LazyJsonString(Convert.ToString(dataRow[dataColumn.ColumnName, dataRowVersion]))));
                }
            }

            return(jsonObjectDataRowColumns);
        }
        /// <summary>
        /// Serialize an object to a json datatable object token
        /// </summary>
        /// <param name="data">The object to be serialized</param>
        /// <returns>The json datatable object token</returns>
        public override LazyJsonToken Serialize(Object data)
        {
            if (data == null || data is not DataTable)
            {
                return(new LazyJsonNull());
            }

            DataTable      dataTable           = (DataTable)data;
            LazyJsonObject jsonObjectDataTable = new LazyJsonObject();

            // Rows
            LazyJsonArray jsonArrayDataTableRows = new LazyJsonArray();

            jsonObjectDataTable.Add(new LazyJsonProperty("Rows", jsonArrayDataTableRows));

            LazyJsonSerializerDataRow jsonSerializerDataRow = new LazyJsonSerializerDataRow();

            foreach (DataRow dataRow in dataTable.Rows)
            {
                LazyJsonToken jsonTokenDataTableRow = jsonSerializerDataRow.Serialize(dataRow);

                if (jsonTokenDataTableRow != null && jsonTokenDataTableRow.Type != LazyJsonType.Null)
                {
                    jsonArrayDataTableRows.Add(jsonTokenDataTableRow);
                }
            }

            return(jsonObjectDataTable);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Deserialize the json datatable token to the desired object
        /// </summary>
        /// <param name="jsonToken">The json datatable token to be deserialized</param>
        /// <param name="dataType">The type of the desired object</param>
        /// <returns>The desired object instance</returns>
        public override Object Deserialize(LazyJsonToken jsonToken, Type dataType)
        {
            if (jsonToken == null || jsonToken.Type != LazyJsonType.Object || dataType == null || dataType != typeof(DataTable))
            {
                return(null);
            }

            LazyJsonObject jsonObjectDataTable = (LazyJsonObject)jsonToken;
            DataTable      dataTable           = new DataTable();

            // Rows
            if (jsonObjectDataTable["Rows"] != null && jsonObjectDataTable["Rows"].Token.Type == LazyJsonType.Array)
            {
                LazyJsonArray jsonArrayDataTableRows = (LazyJsonArray)jsonObjectDataTable["Rows"].Token;
                LazyJsonDeserializerDataRow jsonDeserializerDataRow = new LazyJsonDeserializerDataRow();

                foreach (LazyJsonToken jsonTokenDataTableRow in jsonArrayDataTableRows.TokenList)
                {
                    if (jsonTokenDataTableRow.Type == LazyJsonType.Object)
                    {
                        jsonDeserializerDataRow.Deserialize(jsonTokenDataTableRow, dataTable);
                    }
                }
            }

            return(dataTable);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Deserialize the json dataset token to the desired object
        /// </summary>
        /// <param name="jsonToken">The json dataset token to be deserialized</param>
        /// <param name="dataType">The type of the desired object</param>
        /// <returns>The desired object instance</returns>
        public override Object Deserialize(LazyJsonToken jsonToken, Type dataType)
        {
            if (jsonToken == null || jsonToken.Type != LazyJsonType.Object || dataType == null || dataType != typeof(DataSet))
            {
                return(null);
            }

            LazyJsonObject jsonObjectDataSet = (LazyJsonObject)jsonToken;
            DataSet        dataSet           = new DataSet();

            // Name
            if (jsonObjectDataSet["Name"] != null && jsonObjectDataSet["Name"].Token.Type == LazyJsonType.String)
            {
                dataSet.DataSetName = ((LazyJsonString)jsonObjectDataSet["Name"].Token).Value;
            }

            // Tables
            if (jsonObjectDataSet["Tables"] != null && jsonObjectDataSet["Tables"].Token.Type == LazyJsonType.Object)
            {
                LazyJsonObject jsonObjectDataSetTables = (LazyJsonObject)jsonObjectDataSet["Tables"].Token;
                LazyJsonDeserializerDataTable jsonDeserializerDataTable = new LazyJsonDeserializerDataTable();

                foreach (LazyJsonProperty jsonPropertyDataSetTable in jsonObjectDataSetTables.PropertyList)
                {
                    DataTable dataTable = jsonDeserializerDataTable.Deserialize <DataTable>(jsonPropertyDataSetTable);

                    if (dataTable != null)
                    {
                        dataSet.Tables.Add(dataTable);
                    }
                }
            }

            return(dataSet);
        }
        /// <summary>
        /// Serialize an object to a json dataset object token
        /// </summary>
        /// <param name="data">The object to be serialized</param>
        /// <returns>The json dataset object token</returns>
        public override LazyJsonToken Serialize(Object data)
        {
            if (data == null || data is not DataSet)
            {
                return(new LazyJsonNull());
            }

            DataSet        dataSet           = (DataSet)data;
            LazyJsonObject jsonObjectDataSet = new LazyJsonObject();

            // Name
            jsonObjectDataSet.Add(new LazyJsonProperty("Name", new LazyJsonString(dataSet.DataSetName)));

            // Tables
            LazyJsonObject jsonObjectDataSetTables = new LazyJsonObject();

            jsonObjectDataSet.Add(new LazyJsonProperty("Tables", jsonObjectDataSetTables));

            LazyJsonSerializerDataTable jsonSerializerDataTable = new LazyJsonSerializerDataTable();

            foreach (DataTable dataTable in dataSet.Tables)
            {
                LazyJsonToken jsonTokenDataSetTable = jsonSerializerDataTable.Serialize(dataTable);

                if (jsonTokenDataSetTable != null)
                {
                    jsonObjectDataSetTables.Add(new LazyJsonProperty(dataTable.TableName, jsonTokenDataSetTable));
                }
            }

            return(jsonObjectDataSet);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Deserialize the json datarow token to the desired object
        /// </summary>
        /// <param name="jsonToken">The json datarow token</param>
        /// <param name="dataTable">The datatable</param>
        public void Deserialize(LazyJsonToken jsonToken, DataTable dataTable)
        {
            LazyJsonObject jsonObjectDataRow = (LazyJsonObject)jsonToken;
            DataRow        dataRow           = dataTable.NewRow();

            dataTable.Rows.Add(dataRow);
            dataRow.AcceptChanges();

            // State
            DataRowState dataRowState = DataRowState.Unchanged;

            if (jsonObjectDataRow["State"] != null && jsonObjectDataRow["State"].Token.Type == LazyJsonType.String)
            {
                switch (((LazyJsonString)jsonObjectDataRow["State"].Token).Value.ToLower())
                {
                case "added": dataRowState = DataRowState.Added; break;

                case "modified": dataRowState = DataRowState.Modified; break;

                case "deleted": dataRowState = DataRowState.Deleted; break;
                }
            }

            // Values
            if (jsonObjectDataRow["Values"] != null && jsonObjectDataRow["Values"].Token.Type == LazyJsonType.Object)
            {
                LazyJsonObject jsonObjectDataRowValues = (LazyJsonObject)jsonObjectDataRow["Values"].Token;

                // Values Original
                if (dataRowState == DataRowState.Modified)
                {
                    if (jsonObjectDataRowValues["Original"] != null && jsonObjectDataRowValues["Original"].Token.Type == LazyJsonType.Object)
                    {
                        DeserializeDataRow((LazyJsonObject)jsonObjectDataRowValues["Original"].Token, dataRow);
                    }

                    dataRow.AcceptChanges();
                }

                // Values Current
                if (jsonObjectDataRowValues["Current"] != null && jsonObjectDataRowValues["Current"].Token.Type == LazyJsonType.Object)
                {
                    DeserializeDataRow((LazyJsonObject)jsonObjectDataRowValues["Current"].Token, dataRow);
                }
            }

            switch (dataRowState)
            {
            case DataRowState.Added: dataRow.AcceptChanges(); dataRow.SetAdded(); break;

            case DataRowState.Deleted: dataRow.AcceptChanges(); dataRow.Delete(); break;

            case DataRowState.Unchanged: dataRow.AcceptChanges(); break;
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Deserialize the json datarow object to the desired object
        /// </summary>
        /// <param name="jsonObjectDataRowColumns">The json datarow object</param>
        /// <param name="dataRow">The datarow</param>
        private void DeserializeDataRow(LazyJsonObject jsonObjectDataRowColumns, DataRow dataRow)
        {
            foreach (LazyJsonProperty jsonPropertyDataRowColumn in jsonObjectDataRowColumns.PropertyList)
            {
                if (jsonPropertyDataRowColumn.Token.Type == LazyJsonType.Null)
                {
                    if (dataRow.Table.Columns.Contains(jsonPropertyDataRowColumn.Name) == false)
                    {
                        dataRow.Table.Columns.Add(jsonPropertyDataRowColumn.Name, typeof(String));
                    }

                    dataRow[jsonPropertyDataRowColumn.Name] = DBNull.Value;
                }
                else if (jsonPropertyDataRowColumn.Token.Type == LazyJsonType.Boolean)
                {
                    if (dataRow.Table.Columns.Contains(jsonPropertyDataRowColumn.Name) == false)
                    {
                        dataRow.Table.Columns.Add(jsonPropertyDataRowColumn.Name, typeof(Boolean));
                    }

                    dataRow[jsonPropertyDataRowColumn.Name] = ((LazyJsonBoolean)jsonPropertyDataRowColumn.Token).Value;
                }
                else if (jsonPropertyDataRowColumn.Token.Type == LazyJsonType.String)
                {
                    if (dataRow.Table.Columns.Contains(jsonPropertyDataRowColumn.Name) == false)
                    {
                        dataRow.Table.Columns.Add(jsonPropertyDataRowColumn.Name, typeof(String));
                    }

                    dataRow[jsonPropertyDataRowColumn.Name] = ((LazyJsonString)jsonPropertyDataRowColumn.Token).Value;
                }
                else if (jsonPropertyDataRowColumn.Token.Type == LazyJsonType.Integer)
                {
                    if (dataRow.Table.Columns.Contains(jsonPropertyDataRowColumn.Name) == false)
                    {
                        dataRow.Table.Columns.Add(jsonPropertyDataRowColumn.Name, typeof(Int64));
                    }

                    dataRow[jsonPropertyDataRowColumn.Name] = ((LazyJsonInteger)jsonPropertyDataRowColumn.Token).Value;
                }
                else if (jsonPropertyDataRowColumn.Token.Type == LazyJsonType.Decimal)
                {
                    if (dataRow.Table.Columns.Contains(jsonPropertyDataRowColumn.Name) == false)
                    {
                        dataRow.Table.Columns.Add(jsonPropertyDataRowColumn.Name, typeof(Decimal));
                    }

                    dataRow[jsonPropertyDataRowColumn.Name] = ((LazyJsonDecimal)jsonPropertyDataRowColumn.Token).Value;
                }
            }
        }
        /// <summary>
        /// Serialize an object to a json datarow object token
        /// </summary>
        /// <param name="data">The object to be serialized</param>
        /// <returns>The json datarow object token</returns>
        public LazyJsonToken Serialize(DataRow dataRow)
        {
            LazyJsonObject jsonObjectDataRow = new LazyJsonObject();

            // State
            jsonObjectDataRow.Add(new LazyJsonProperty("State", new LazyJsonString(Enum.GetName(typeof(DataRowState), dataRow.RowState))));

            // Values
            LazyJsonObject jsonObjectDataRowValues = new LazyJsonObject();

            jsonObjectDataRow.Add(new LazyJsonProperty("Values", jsonObjectDataRowValues));

            // Values Original
            jsonObjectDataRowValues.Add(new LazyJsonProperty("Original", dataRow.RowState != DataRowState.Modified ? new LazyJsonNull() : SerializeDataRow(dataRow, DataRowVersion.Original)));

            // Values Current
            jsonObjectDataRowValues.Add(new LazyJsonProperty("Current", SerializeDataRow(dataRow, DataRowVersion.Current)));

            return(jsonObjectDataRow);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Read the root node of the json
        /// </summary>
        /// <param name="json">The json</param>
        /// <param name="jsonReaderOptions">The json reader options</param>
        /// <param name="lazyJson">The lazy json</param>
        private static void ReadRoot(String json, LazyJsonReaderOptions jsonReaderOptions, LazyJson lazyJson)
        {
            Int32 line  = 1;
            Int32 index = 0;

            while (index < json.Length)
            {
                if (json[index] == ' ')
                {
                    index++; continue;
                }
                if (json[index] == '\r')
                {
                    index++; continue;
                }
                if (json[index] == '\n')
                {
                    index++; line++; continue;
                }
                if (json[index] == '\t')
                {
                    index++; continue;
                }

                if (json[index] == '/')
                {
                    index++;
                    LazyJsonComments jsonComments = new LazyJsonComments();
                    ReadComments(json, jsonReaderOptions, lazyJson.ReaderResult, jsonComments, ref line, ref index);

                    if (index >= json.Length && jsonComments.IsInBlock == true && jsonComments.IsInBlockComplete == false)
                    {
                        lazyJson.ReaderResult.Success = false;
                        lazyJson.ReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedCharacter, line, "*/", String.Empty));
                    }
                }
                else if (json[index] == '[')
                {
                    index++;
                    LazyJsonArray jsonArray = new LazyJsonArray();
                    ReadArray(json, jsonReaderOptions, lazyJson.ReaderResult, jsonArray, ref line, ref index);

                    if (lazyJson.ReaderResult.Success == true)
                    {
                        lazyJson.Root = jsonArray;
                    }

                    break;
                }
                else if (json[index] == '{')
                {
                    index++;
                    LazyJsonObject jsonObject = new LazyJsonObject();
                    ReadObject(json, jsonReaderOptions, lazyJson.ReaderResult, jsonObject, ref line, ref index);

                    if (lazyJson.ReaderResult.Success == true)
                    {
                        lazyJson.Root = jsonObject;
                    }

                    break;
                }
                else if (json[index] == '\"')
                {
                    index++;
                    LazyJsonString jsonString = new LazyJsonString();
                    ReadString(json, jsonReaderOptions, lazyJson.ReaderResult, jsonString, ref line, ref index);

                    if (lazyJson.ReaderResult.Success == true)
                    {
                        lazyJson.Root = jsonString;
                    }

                    break;
                }
                else if (json[index] == '-')
                {
                    LazyJsonToken jsonToken = null;
                    ReadIntegerOrDecimal(json, jsonReaderOptions, lazyJson.ReaderResult, out jsonToken, ref line, ref index);

                    if (lazyJson.ReaderResult.Success == true)
                    {
                        lazyJson.Root = jsonToken;
                    }

                    break;
                }
                else if (Char.IsDigit(json[index]) == true)
                {
                    LazyJsonToken jsonToken = null;
                    ReadIntegerOrDecimal(json, jsonReaderOptions, lazyJson.ReaderResult, out jsonToken, ref line, ref index);

                    if (lazyJson.ReaderResult.Success == true)
                    {
                        lazyJson.Root = jsonToken;
                    }

                    break;
                }
                else if (Char.IsLetter(json[index]) == true)
                {
                    LazyJsonToken jsonToken = null;
                    ReadNullOrBoolean(json, jsonReaderOptions, lazyJson.ReaderResult, out jsonToken, ref line, ref index);

                    if (lazyJson.ReaderResult.Success == true)
                    {
                        lazyJson.Root = jsonToken;
                    }

                    break;
                }
                else
                {
                    lazyJson.Root = new LazyJsonNull();

                    lazyJson.ReaderResult.Success = false;
                    lazyJson.ReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedCharacter, line, "//", json[index]));
                    index = json.Length; // Exit

                    break;
                }
            }

            while (index < json.Length)
            {
                if (json[index] == ' ')
                {
                    index++; continue;
                }
                if (json[index] == '\r')
                {
                    index++; continue;
                }
                if (json[index] == '\n')
                {
                    index++; line++; continue;
                }
                if (json[index] == '\t')
                {
                    index++; continue;
                }

                if (json[index] == '/')
                {
                    index++;
                    LazyJsonComments jsonComments = new LazyJsonComments();
                    ReadComments(json, jsonReaderOptions, lazyJson.ReaderResult, jsonComments, ref line, ref index);

                    if (index >= json.Length && jsonComments.IsInBlock == true && jsonComments.IsInBlockComplete == false)
                    {
                        lazyJson.ReaderResult.Success = false;
                        lazyJson.ReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedCharacter, line, "*/", String.Empty));
                    }
                }
                else
                {
                    lazyJson.Root = new LazyJsonNull();

                    lazyJson.ReaderResult.Success = false;
                    lazyJson.ReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedCharacter, line, "//", json[index]));
                    index = json.Length; // Exit

                    break;
                }
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Write an object on the json
        /// </summary>
        /// <param name="stringBuilder">The string builder</param>
        /// <param name="jsonWriterOptions">The json writer options</param>
        /// <param name="jsonObject">The json object</param>
        /// <param name="jsonPathType">The json path type</param>
        private static void WriteObject(StringBuilder stringBuilder, LazyJsonWriterOptions jsonWriterOptions, LazyJsonObject jsonObject, LazyJsonPathType jsonPathType)
        {
            String openKey  = "{0}{1}{2}";
            String closeKey = "{0}{1}{2}";

            if (jsonWriterOptions.Indent == true)
            {
                if (jsonPathType == LazyJsonPathType.ArrayIndex)
                {
                    openKey  = String.Format(openKey, Environment.NewLine, jsonWriterOptions.IndentValue, "{");
                    closeKey = String.Format(closeKey, Environment.NewLine, jsonWriterOptions.IndentValue, "}");
                }
                else if (jsonPathType == LazyJsonPathType.Property)
                {
                    openKey  = "{";
                    closeKey = String.Format(closeKey, Environment.NewLine, jsonWriterOptions.IndentValue, "}");
                }
            }
            else
            {
                openKey  = "{";
                closeKey = "}";
            }

            stringBuilder.Append(openKey);
            jsonWriterOptions.IndentLevel++;
            for (int i = 0; i < jsonObject.PropertyList.Count; i++)
            {
                WriteProperty(stringBuilder, jsonWriterOptions, jsonObject.PropertyList[i]);
                stringBuilder.Append(",");
            }
            jsonWriterOptions.IndentLevel--;

            if (stringBuilder[stringBuilder.Length - 1] == ',')
            {
                stringBuilder.Remove(stringBuilder.Length - 1, 1);
            }

            stringBuilder.Append(closeKey);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Find token at the specified jPath
        /// </summary>
        /// <param name="jPath">The jPath location of the token</param>
        /// <param name="renameWith">A new property name to the token at specified jPath if it's an object</param>
        /// <param name="replaceWith">A new token to override the token at specified jPath</param>
        /// <param name="remove">Indicate if the token should be removed</param>
        /// <returns>The located token</returns>
        private LazyJsonToken InternalFind(String jPath, String renameWith = null, LazyJsonToken replaceWith = null, Boolean remove = false)
        {
            List <LazyJsonPathNode> pathNodeList = LazyJsonPath.Parse(jPath);

            if (pathNodeList.Count == 0)
            {
                return(null);
            }

            LazyJsonToken jsonTokenParent = this.Root;
            LazyJsonToken jsonToken       = null;
            Int32         pathIndex       = 0;

            if (pathNodeList.Count == 1)
            {
                if (replaceWith != null)
                {
                    this.Root = replaceWith;
                }

                return(jsonTokenParent);
            }

            pathNodeList.RemoveAt(0);

            #region Find parent token

            for (pathIndex = 0; pathIndex < (pathNodeList.Count - 1); pathIndex++)
            {
                if (pathNodeList[pathIndex].Type == LazyJsonPathType.Property)
                {
                    if (jsonTokenParent.Type != LazyJsonType.Object)
                    {
                        return(null);
                    }

                    LazyJsonProperty jsonProperty = ((LazyJsonObject)jsonTokenParent)[((LazyJsonPathProperty)pathNodeList[pathIndex]).Name];

                    if (jsonProperty == null)
                    {
                        return(null);
                    }

                    jsonTokenParent = jsonProperty.Token;
                }
                else if (pathNodeList[pathIndex].Type == LazyJsonPathType.ArrayIndex)
                {
                    if (jsonTokenParent.Type != LazyJsonType.Array)
                    {
                        return(null);
                    }

                    jsonTokenParent = ((LazyJsonArray)jsonTokenParent)[Convert.ToInt32(((LazyJsonPathArrayIndex)pathNodeList[pathIndex]).Index)];
                }
            }

            #endregion Find parent token

            #region Find token

            if (pathNodeList[pathIndex].Type == LazyJsonPathType.Property)
            {
                if (jsonTokenParent.Type != LazyJsonType.Object)
                {
                    return(null);
                }

                LazyJsonObject   jsonObjectParent = (LazyJsonObject)jsonTokenParent;
                LazyJsonProperty jsonProperty     = jsonObjectParent[((LazyJsonPathProperty)pathNodeList[pathIndex]).Name];

                if (jsonProperty == null)
                {
                    return(null);
                }

                jsonToken = jsonProperty.Token;

                if (remove == true)
                {
                    jsonObjectParent.Remove(jsonProperty.Name);
                }
                else
                {
                    if (renameWith != null)
                    {
                        jsonProperty.Name = renameWith;
                    }

                    if (replaceWith != null)
                    {
                        jsonProperty.Token = replaceWith;
                    }
                }
            }
            else if (pathNodeList[pathIndex].Type == LazyJsonPathType.ArrayIndex)
            {
                if (jsonTokenParent.Type != LazyJsonType.Array)
                {
                    return(null);
                }

                LazyJsonArray jsonArrayParent = (LazyJsonArray)jsonTokenParent;
                Int32         index           = Convert.ToInt32(((LazyJsonPathArrayIndex)pathNodeList[pathIndex]).Index);

                jsonToken = jsonArrayParent[index];

                if (jsonToken != null)
                {
                    if (remove == true)
                    {
                        jsonArrayParent.TokenList.RemoveAt(index);
                    }
                    else
                    {
                        if (replaceWith != null)
                        {
                            jsonArrayParent[index] = replaceWith;
                        }
                    }
                }
            }

            #endregion Find token

            return(jsonToken);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Append a json token at specified jPath
        /// </summary>
        /// <param name="jPath">The jPath location to append the token</param>
        /// <param name="jsonToken">The json token</param>
        public void AppendToken(String jPath, LazyJsonToken jsonToken)
        {
            if (jPath == "$")
            {
                this.Root = jsonToken != null ? jsonToken : new LazyJsonNull();
            }
            else
            {
                List <LazyJsonPathNode> jsonPathNodeList = LazyJsonPath.Parse(jPath);
                String jPathParent = String.Join('.', jsonPathNodeList.ToJsonPathNodeArray(), 0, jsonPathNodeList.Count - 1);

                LazyJsonToken jsonTokenParent = Find(jPathParent);

                if (jsonTokenParent != null)
                {
                    if (jsonPathNodeList[jsonPathNodeList.Count - 1].Type == LazyJsonPathType.ArrayIndex)
                    {
                        if (jsonTokenParent.Type == LazyJsonType.Array)
                        {
                            LazyJsonPathArrayIndex jsonPathArrayIndex = (LazyJsonPathArrayIndex)jsonPathNodeList[jsonPathNodeList.Count - 1];
                            ((LazyJsonArray)jsonTokenParent)[Convert.ToInt32(jsonPathArrayIndex.Index)] = jsonToken;
                        }
                        else
                        {
                            LazyJsonArray jsonArray = new LazyJsonArray();
                            jsonArray.Add(jsonToken);

                            Find(jPathParent, replaceWith: jsonArray);
                        }
                    }
                    else if (jsonPathNodeList[jsonPathNodeList.Count - 1].Type == LazyJsonPathType.Property)
                    {
                        if (jsonTokenParent.Type == LazyJsonType.Object)
                        {
                            LazyJsonPathProperty jsonPathProperty = (LazyJsonPathProperty)jsonPathNodeList[jsonPathNodeList.Count - 1];
                            ((LazyJsonObject)jsonTokenParent)[jsonPathProperty.Name] = new LazyJsonProperty(jsonPathProperty.Name, jsonToken);
                        }
                        else
                        {
                            LazyJsonObject jsonObject = new LazyJsonObject();
                            jsonObject.Add(new LazyJsonProperty(((LazyJsonPathProperty)jsonPathNodeList[jsonPathNodeList.Count - 1]).Name, jsonToken));

                            Find(jPathParent, replaceWith: jsonObject);
                        }
                    }
                }
                else
                {
                    if (jsonPathNodeList[jsonPathNodeList.Count - 1].Type == LazyJsonPathType.ArrayIndex)
                    {
                        LazyJsonArray jsonArray = new LazyJsonArray();
                        jsonArray.Add(jsonToken);

                        AppendToken(jPathParent, jsonArray);
                    }
                    else if (jsonPathNodeList[jsonPathNodeList.Count - 1].Type == LazyJsonPathType.Property)
                    {
                        LazyJsonObject jsonObject = new LazyJsonObject();
                        jsonObject.Add(new LazyJsonProperty(((LazyJsonPathProperty)jsonPathNodeList[jsonPathNodeList.Count - 1]).Name, jsonToken));

                        AppendToken(jPathParent, jsonObject);
                    }
                }
            }
        }
Exemplo n.º 13
0
 /// <summary>
 /// Append a json object token at specified jPath
 /// </summary>
 /// <param name="jPath">The jPath location to append the token</param>
 /// <param name="jsonObject">The json object token</param>
 public void AppendObject(String jPath, LazyJsonObject jsonObject)
 {
     AppendToken(jPath, jsonObject);
 }
Exemplo n.º 14
0
        /// <summary>
        /// Read a property on the json
        /// </summary>
        /// <param name="json">The json</param>
        /// <param name="jsonReaderOptions">The json reader options</param>
        /// <param name="jsonReaderResult">The json reader result</param>
        /// <param name="jsonProperty">The json property</param>
        /// <param name="line">The current line</param>
        /// <param name="index">The current index</param>
        private static void ReadProperty(String json, LazyJsonReaderOptions jsonReaderOptions, LazyJsonReaderResult jsonReaderResult, LazyJsonProperty jsonProperty, ref Int32 line, ref Int32 index)
        {
            LazyJsonString jsonStringPropertyName = new LazyJsonString();

            ReadString(json, jsonReaderOptions, jsonReaderResult, jsonStringPropertyName, ref line, ref index);
            jsonProperty.Name = jsonStringPropertyName.Value;

            if (index < json.Length)
            {
                while (index < json.Length)
                {
                    if (json[index] == ' ')
                    {
                        index++; continue;
                    }
                    if (json[index] == '\r')
                    {
                        index++; continue;
                    }
                    if (json[index] == '\n')
                    {
                        index++; line++; continue;
                    }
                    if (json[index] == '\t')
                    {
                        index++; continue;
                    }

                    if (json[index] == '/')
                    {
                        index++;
                        LazyJsonComments jsonComments = new LazyJsonComments();
                        ReadComments(json, jsonReaderOptions, jsonReaderResult, jsonComments, ref line, ref index);

                        if (index >= json.Length)
                        {
                            jsonReaderResult.Success = false;
                            jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedCharacter, line, " : ", String.Empty));
                        }
                    }
                    else if (json[index] == ':')
                    {
                        index++;
                        break;
                    }
                    else
                    {
                        jsonReaderResult.Success = false;
                        jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderInvalidObjectPropertyValue, line, json[index]));
                        index = json.Length; // Exit
                    }
                }

                if (index < json.Length)
                {
                    while (index < json.Length)
                    {
                        if (json[index] == ' ')
                        {
                            index++; continue;
                        }
                        if (json[index] == '\r')
                        {
                            index++; continue;
                        }
                        if (json[index] == '\n')
                        {
                            index++; line++; continue;
                        }
                        if (json[index] == '\t')
                        {
                            index++; continue;
                        }

                        if (json[index] == '/')
                        {
                            index++;
                            LazyJsonComments jsonComments = new LazyJsonComments();
                            ReadComments(json, jsonReaderOptions, jsonReaderResult, jsonComments, ref line, ref index);

                            if (index >= json.Length)
                            {
                                jsonReaderResult.Success = false;
                                jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedCharacter, line, " : ", String.Empty));
                            }
                        }
                        else if (json[index] == '[')
                        {
                            index++;
                            LazyJsonArray jsonArray = new LazyJsonArray();
                            ReadArray(json, jsonReaderOptions, jsonReaderResult, jsonArray, ref line, ref index);
                            jsonProperty.Token = jsonArray;
                            break;
                        }
                        else if (json[index] == '{')
                        {
                            index++;
                            LazyJsonObject jsonObject = new LazyJsonObject();
                            ReadObject(json, jsonReaderOptions, jsonReaderResult, jsonObject, ref line, ref index);
                            jsonProperty.Token = jsonObject;
                            break;
                        }
                        else if (json[index] == '\"')
                        {
                            index++;
                            LazyJsonString jsonString = new LazyJsonString();
                            ReadString(json, jsonReaderOptions, jsonReaderResult, jsonString, ref line, ref index);
                            jsonProperty.Token = jsonString;
                            break;
                        }
                        else if (json[index] == '-')
                        {
                            LazyJsonToken jsonToken = null;
                            ReadIntegerOrDecimal(json, jsonReaderOptions, jsonReaderResult, out jsonToken, ref line, ref index);
                            jsonProperty.Token = jsonToken;
                            break;
                        }
                        else if (Char.IsDigit(json[index]) == true)
                        {
                            LazyJsonToken jsonToken = null;
                            ReadIntegerOrDecimal(json, jsonReaderOptions, jsonReaderResult, out jsonToken, ref line, ref index);
                            jsonProperty.Token = jsonToken;
                            break;
                        }
                        else if (Char.IsLetter(json[index]) == true)
                        {
                            LazyJsonToken jsonToken = null;
                            ReadNullOrBoolean(json, jsonReaderOptions, jsonReaderResult, out jsonToken, ref line, ref index);
                            jsonProperty.Token = jsonToken;
                            break;
                        }
                        else
                        {
                            jsonReaderResult.Success = false;
                            jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderInvalidObjectPropertyValue, line, json[index]));
                            index = json.Length; // Exit
                        }
                    }
                }
                else
                {
                    jsonReaderResult.Success = false;
                    jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderInvalidObjectPropertyValue, line, String.Empty));
                }
            }
            else
            {
                jsonReaderResult.Success = false;
                jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderInvalidObjectPropertyValue, line, String.Empty));
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// Serialize an object to a json token by object property info
        /// </summary>
        /// <param name="data">The object to be serialized</param>
        /// <returns>The json token</returns>
        public static LazyJsonToken SerializeObject(Object data)
        {
            if (data == null)
            {
                return(new LazyJsonNull());
            }

            Type dataType = data.GetType();

            if (dataType.IsArray == true || dataType.IsGenericType == true)
            {
                return(SerializeToken(data));
            }

            LazyJsonObject jsonObject        = new LazyJsonObject();
            List <String>  alreadySerializer = new List <String>();

            PropertyInfo[] propertyInfoArray = dataType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo propertyInfo in propertyInfoArray)
            {
                if (propertyInfo.GetMethod == null)
                {
                    continue;
                }

                if (alreadySerializer.Contains(propertyInfo.Name) == true)
                {
                    continue;
                }

                if (propertyInfo.GetCustomAttributes(typeof(LazyJsonAttributePropertyIgnore), false).Length > 0)
                {
                    continue;
                }

                alreadySerializer.Add(propertyInfo.Name);

                Type     jsonSerializerType     = null;
                String   propertyName           = propertyInfo.Name;
                Object[] jsonAttributeBaseArray = propertyInfo.GetCustomAttributes(typeof(LazyJsonAttributeBase), false);

                foreach (Object attribute in jsonAttributeBaseArray)
                {
                    if (attribute is LazyJsonAttributePropertyRename)
                    {
                        propertyName = ((LazyJsonAttributePropertyRename)attribute).Name;
                    }
                    else if (attribute is LazyJsonAttributeSerializer)
                    {
                        LazyJsonAttributeSerializer jsonAttributeSerializer = (LazyJsonAttributeSerializer)attribute;

                        if (jsonAttributeSerializer.SerializerType != null && jsonAttributeSerializer.SerializerType.IsSubclassOf(typeof(LazyJsonSerializerBase)) == true)
                        {
                            jsonSerializerType = jsonAttributeSerializer.SerializerType;
                        }
                    }
                }

                if (jsonSerializerType != null)
                {
                    jsonObject.Add(new LazyJsonProperty(propertyName, ((LazyJsonSerializerBase)Activator.CreateInstance(jsonSerializerType)).Serialize(propertyInfo.GetValue(data))));
                }
                else
                {
                    jsonObject.Add(new LazyJsonProperty(propertyName, SerializeToken(propertyInfo.GetValue(data))));
                }
            }

            return(jsonObject);
        }
Exemplo n.º 16
0
        /// <summary>
        /// Deserialize the json object token to the desired object
        /// </summary>
        /// <param name="jsonObject">The json object token to be deserialized</param>
        /// <param name="dataType">The type of the desired object</param>
        /// <returns>The desired object instance</returns>
        public static Object DeserializeObject(LazyJsonObject jsonObject, Type dataType)
        {
            if (jsonObject == null || dataType == null)
            {
                return(null);
            }

            Object        data = Activator.CreateInstance(dataType);
            List <String> alreadyDeserializer = new List <String>();

            PropertyInfo[] propertyInfoArray = dataType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo propertyInfo in propertyInfoArray)
            {
                if (propertyInfo.SetMethod == null)
                {
                    continue;
                }

                if (alreadyDeserializer.Contains(propertyInfo.Name) == true)
                {
                    continue;
                }

                if (propertyInfo.GetCustomAttributes(typeof(LazyJsonAttributePropertyIgnore), false).Length > 0)
                {
                    continue;
                }

                alreadyDeserializer.Add(propertyInfo.Name);

                Type     jsonDeserializerType   = null;
                String   propertyName           = propertyInfo.Name;
                Object[] jsonAttributeBaseArray = propertyInfo.GetCustomAttributes(typeof(LazyJsonAttributeBase), false);

                foreach (Object attribute in jsonAttributeBaseArray)
                {
                    if (attribute is LazyJsonAttributePropertyRename)
                    {
                        propertyName = ((LazyJsonAttributePropertyRename)attribute).Name;
                    }
                    else if (attribute is LazyJsonAttributeDeserializer)
                    {
                        LazyJsonAttributeDeserializer jsonAttributeDeserializer = (LazyJsonAttributeDeserializer)attribute;

                        if (jsonAttributeDeserializer.DeserializerType != null && jsonAttributeDeserializer.DeserializerType.IsSubclassOf(typeof(LazyJsonDeserializerBase)) == true)
                        {
                            jsonDeserializerType = jsonAttributeDeserializer.DeserializerType;
                        }
                    }
                }

                if (jsonObject[propertyName] != null)
                {
                    if (jsonDeserializerType != null)
                    {
                        propertyInfo.SetValue(data, ((LazyJsonDeserializerBase)Activator.CreateInstance(jsonDeserializerType)).Deserialize(jsonObject[propertyName], propertyInfo.PropertyType));
                    }
                    else
                    {
                        propertyInfo.SetValue(data, DeserializeToken(jsonObject[propertyName].Token, propertyInfo.PropertyType));
                    }
                }
            }

            return(data);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Read an object on the json
        /// </summary>
        /// <param name="json">The json</param>
        /// <param name="jsonReaderOptions">The json reader options</param>
        /// <param name="jsonReaderResult">The json reader result</param>
        /// <param name="jsonObject">The json object</param>
        /// <param name="line">The current line</param>
        /// <param name="index">The current index</param>
        private static void ReadObject(String json, LazyJsonReaderOptions jsonReaderOptions, LazyJsonReaderResult jsonReaderResult, LazyJsonObject jsonObject, ref Int32 line, ref Int32 index)
        {
            while (index < json.Length)
            {
                if (json[index] == ' ')
                {
                    index++; continue;
                }
                if (json[index] == '\r')
                {
                    index++; continue;
                }
                if (json[index] == '\n')
                {
                    index++; line++; continue;
                }
                if (json[index] == '\t')
                {
                    index++; continue;
                }

                if (json[index] == '/')
                {
                    index++;
                    LazyJsonComments jsonComments = new LazyJsonComments();
                    ReadComments(json, jsonReaderOptions, jsonReaderResult, jsonComments, ref line, ref index);

                    if (index >= json.Length)
                    {
                        jsonReaderResult.Success = false;
                        jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedCharacter, line, " \" | } ", String.Empty));
                    }
                }
                else if (json[index] == '\"')
                {
                    index++;
                    LazyJsonProperty jsonProperty = new LazyJsonProperty(LazyJson.UNNAMED_PROPERTY, new LazyJsonNull());
                    ReadProperty(json, jsonReaderOptions, jsonReaderResult, jsonProperty, ref line, ref index);

                    if (jsonObject[jsonProperty.Name] == null)
                    {
                        jsonObject.Add(jsonProperty);
                    }
                    else
                    {
                        jsonReaderResult.Success = false;
                        jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderObjectPropertyDuplicated, line, jsonProperty.Name));
                        index = json.Length; // Exit
                    }

                    while (index < json.Length)
                    {
                        if (json[index] == ' ')
                        {
                            index++; continue;
                        }
                        if (json[index] == '\r')
                        {
                            index++; continue;
                        }
                        if (json[index] == '\n')
                        {
                            index++; line++; continue;
                        }
                        if (json[index] == '\t')
                        {
                            index++; continue;
                        }

                        if (json[index] == '/')
                        {
                            index++;
                            LazyJsonComments jsonComments = new LazyJsonComments();
                            ReadComments(json, jsonReaderOptions, jsonReaderResult, jsonComments, ref line, ref index);

                            if (index >= json.Length)
                            {
                                jsonReaderResult.Success = false;
                                jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedCharacter, line, ", | }", String.Empty));
                            }
                        }
                        else if (json[index] == ',' || json[index] == '}')
                        {
                            break;
                        }
                        else
                        {
                            jsonReaderResult.Success = false;
                            jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedCharacter, line, ", | }", json[index]));
                            index = json.Length; // Exit
                        }
                    }

                    if (index < json.Length)
                    {
                        if (json[index] == ',')
                        {
                            index++; continue;
                        }
                        if (json[index] == '}')
                        {
                            index++; break;
                        }
                    }
                }
                else if (json[index] == '}' && jsonObject.PropertyList.Count == 0)
                {
                    index++;
                    break;
                }
                else
                {
                    jsonReaderResult.Success = false;
                    jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedCharacter, line, " \" ", String.Empty));
                    index = json.Length; // Exit
                }
            }
        }