/// <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);
        }
        /// <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);
        }
        /// <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);
        }
        /// <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);
        }
Beispiel #5
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);
        }
Beispiel #6
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);
                    }
                }
            }
        }
Beispiel #7
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
                }
            }
        }