/// <summary>
        /// Deserialize the json string property to the desired object
        /// </summary>
        /// <param name="jsonProperty">The json string property to be deserialized</param>
        /// <param name="dataType">The type of the desired object</param>
        /// <returns>The desired object instance</returns>
        public override Object Deserialize(LazyJsonProperty jsonProperty, Type dataType)
        {
            if (jsonProperty == null)
            {
                return(null);
            }

            return(Deserialize(jsonProperty.Token, dataType));
        }
示例#2
0
        /// <summary>
        /// Deserialize the json datatable property to the desired object
        /// </summary>
        /// <param name="jsonProperty">The json datatable property to be deserialized</param>
        /// <param name="dataType">The type of the desired object</param>
        /// <returns>The desired object instance</returns>
        public override Object Deserialize(LazyJsonProperty jsonProperty, Type dataType)
        {
            if (jsonProperty == null)
            {
                return(null);
            }

            DataTable dataTable = (DataTable)Deserialize(jsonProperty.Token, dataType);

            if (dataTable != null)
            {
                dataTable.TableName = jsonProperty.Name;
            }

            return(dataTable);
        }
示例#3
0
 /// <summary>
 /// Deserialize the json property to the desired object
 /// </summary>
 /// <param name="jsonProperty">The json property to be deserialized</param>
 /// <param name="dataType">The type of the desired object</param>
 /// <returns>The desired object instance</returns>
 public abstract Object Deserialize(LazyJsonProperty jsonProperty, Type dataType);
示例#4
0
 /// <summary>
 /// Deserialize the json property to the desired object
 /// </summary>
 /// <typeparam name="T">The type of the desired object</typeparam>
 /// <param name="jsonProperty">The json property to be deserialized</param>
 /// <returns>The desired object instance</returns>
 public T Deserialize <T>(LazyJsonProperty jsonProperty)
 {
     return((T)Deserialize(jsonProperty, typeof(T)));
 }
示例#5
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);
        }
        /// <summary>
        /// Write a property on the json
        /// </summary>
        /// <param name="stringBuilder">The string builder</param>
        /// <param name="jsonWriterOptions">The json writer options</param>
        /// <param name="jsonProperty">The json property</param>
        private static void WriteProperty(StringBuilder stringBuilder, LazyJsonWriterOptions jsonWriterOptions, LazyJsonProperty jsonProperty)
        {
            String property = "{0}{1}\"{2}\"{3}";

            if (jsonWriterOptions.Indent == true)
            {
                property = String.Format(property, Environment.NewLine, jsonWriterOptions.IndentValue, jsonProperty.Name, ": ");
            }
            else
            {
                property = String.Format(property, String.Empty, String.Empty, jsonProperty.Name, ":");
            }

            stringBuilder.Append(property);

            switch (jsonProperty.Token.Type)
            {
            case LazyJsonType.Null:
                #region LazyJsonType.Null

                stringBuilder.Append("null");

                #endregion LazyJsonType.Null
                break;

            case LazyJsonType.Boolean:
                #region LazyJsonType.Boolean

                LazyJsonBoolean jsonBoolean = (LazyJsonBoolean)jsonProperty.Token;
                stringBuilder.Append(jsonBoolean.Value == null ? "null" : jsonBoolean.Value.ToString().ToLower());

                #endregion LazyJsonType.Boolean
                break;

            case LazyJsonType.Integer:
                #region LazyJsonType.Integer

                LazyJsonInteger jsonInteger = (LazyJsonInteger)jsonProperty.Token;
                stringBuilder.Append(jsonInteger.Value == null ? "null" : jsonInteger.Value);

                #endregion LazyJsonType.Integer
                break;

            case LazyJsonType.Decimal:
                #region LazyJsonType.Decimal

                LazyJsonDecimal jsonDecimal = (LazyJsonDecimal)jsonProperty.Token;
                stringBuilder.Append(jsonDecimal.Value == null ? "null" : jsonDecimal.Value.ToString().Replace(',', '.'));

                #endregion LazyJsonType.Decimal
                break;

            case LazyJsonType.String:
                #region LazyJsonType.String

                LazyJsonString jsonString = (LazyJsonString)jsonProperty.Token;
                stringBuilder.Append(jsonString.Value == null ? "null" : "\"" + jsonString.Value + "\"");

                #endregion LazyJsonType.String
                break;

            case LazyJsonType.Object:
                #region LazyJsonType.Object

                WriteObject(stringBuilder, jsonWriterOptions, (LazyJsonObject)jsonProperty.Token, LazyJsonPathType.Property);

                #endregion LazyJsonType.Object
                break;

            case LazyJsonType.Array:
                #region LazyJsonType.Array

                WriteArray(stringBuilder, jsonWriterOptions, (LazyJsonArray)jsonProperty.Token, LazyJsonPathType.Property);

                #endregion LazyJsonType.Array
                break;
            }
        }
示例#7
0
 /// <summary>
 /// Add a new property to the object
 /// </summary>
 /// <param name="property">The json property</param>
 public void Add(LazyJsonProperty property)
 {
     this[property.Name] = property;
 }
示例#8
0
 /// <summary>
 /// Add a new property to the object
 /// </summary>
 /// <param name="name">The property name</param>
 /// <param name="jsonToken">The json token</param>
 public void Add(String name, LazyJsonToken jsonToken)
 {
     this[name] = new LazyJsonProperty(name, jsonToken);
 }
示例#9
0
 /// <summary>
 /// Add a new property to the object
 /// </summary>
 /// <param name="name">The property name</param>
 /// <param name="json">The json that will be transformed on the desired token</param>
 public void Add(String name, String json)
 {
     this[name] = new LazyJsonProperty(name, LazyJsonReader.Read(json).Root);
 }
示例#10
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));
            }
        }
示例#11
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
                }
            }
        }