/// <summary>
        /// Deserialize the json string token to the desired object
        /// </summary>
        /// <param name="jsonToken">The json string 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 || dataType == null)
            {
                return(null);
            }

            if (jsonToken.Type == LazyJsonType.Null)
            {
                if (dataType == typeof(Char))
                {
                    return('\0');
                }
                if (dataType == typeof(String))
                {
                    return(null);
                }
                if (dataType == typeof(Object))
                {
                    return(null);
                }
                if (dataType == typeof(Nullable <Char>))
                {
                    return(null);
                }
            }
            else if (jsonToken.Type == LazyJsonType.String)
            {
                LazyJsonString jsonString = (LazyJsonString)jsonToken;

                if (dataType == typeof(Char))
                {
                    return(jsonString.Value == null ? '\0' : Convert.ToChar(jsonString.Value));
                }
                if (dataType == typeof(String))
                {
                    return(jsonString.Value);
                }
                if (dataType == typeof(Object))
                {
                    return(jsonString.Value);
                }
                if (dataType == typeof(Nullable <Char>))
                {
                    return(jsonString.Value == null ? null : Convert.ToChar(jsonString.Value));
                }
            }

            return(null);
        }
        /// <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;
            }
        }
        /// <summary>
        /// Write the root node of the json
        /// </summary>
        /// <param name="stringBuilder">The string builder</param>
        /// <param name="jsonWriterOptions">The json writer options</param>
        /// <param name="jsonToken">The root token</param>
        private static void WriteRoot(StringBuilder stringBuilder, LazyJsonWriterOptions jsonWriterOptions, LazyJsonToken jsonToken)
        {
            switch (jsonToken.Type)
            {
            case LazyJsonType.Null:
                #region LazyJsonType.Null

                stringBuilder.Append("null");

                #endregion LazyJsonType.Null
                break;

            case LazyJsonType.Boolean:
                #region LazyJsonType.Boolean

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

                #endregion LazyJsonType.Boolean
                break;

            case LazyJsonType.Integer:
                #region LazyJsonType.Integer

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

                #endregion LazyJsonType.Integer
                break;

            case LazyJsonType.Decimal:
                #region LazyJsonType.Decimal

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

                #endregion LazyJsonType.Decimal
                break;

            case LazyJsonType.String:
                #region LazyJsonType.String

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

                #endregion LazyJsonType.String
                break;

            case LazyJsonType.Object:
                #region LazyJsonType.Object

                WriteObject(stringBuilder, jsonWriterOptions, (LazyJsonObject)jsonToken, LazyJsonPathType.Property);

                #endregion LazyJsonType.Object
                break;

            case LazyJsonType.Array:
                #region LazyJsonType.Array

                WriteArray(stringBuilder, jsonWriterOptions, (LazyJsonArray)jsonToken, LazyJsonPathType.Property);

                #endregion LazyJsonType.Array
                break;
            }
        }
        /// <summary>
        /// Write an array on the json
        /// </summary>
        /// <param name="stringBuilder">The string builder</param>
        /// <param name="jsonWriterOptions">The json writer options</param>
        /// <param name="jsonArray">The json array</param>
        /// <param name="jsonPathType">The json path type</param>
        private static void WriteArray(StringBuilder stringBuilder, LazyJsonWriterOptions jsonWriterOptions, LazyJsonArray jsonArray, LazyJsonPathType jsonPathType)
        {
            String value        = String.Empty;
            String openBracket  = "{0}{1}{2}";
            String closeBracket = "{0}{1}{2}";

            if (jsonWriterOptions.Indent == true)
            {
                jsonWriterOptions.IndentLevel++;
                value = Environment.NewLine + jsonWriterOptions.IndentValue + "{0}";
                jsonWriterOptions.IndentLevel--;

                if (jsonPathType == LazyJsonPathType.ArrayIndex)
                {
                    openBracket  = String.Format(openBracket, Environment.NewLine, jsonWriterOptions.IndentValue, "[");
                    closeBracket = String.Format(closeBracket, Environment.NewLine, jsonWriterOptions.IndentValue, "]");
                }
                else if (jsonPathType == LazyJsonPathType.Property)
                {
                    openBracket  = "[";
                    closeBracket = String.Format(closeBracket, Environment.NewLine, jsonWriterOptions.IndentValue, "]");
                }
            }
            else
            {
                value        = "{0}";
                openBracket  = "[";
                closeBracket = "]";
            }

            stringBuilder.Append(openBracket);
            for (int i = 0; i < jsonArray.TokenList.Count; i++)
            {
                switch (jsonArray.TokenList[i].Type)
                {
                case LazyJsonType.Null:
                    #region LazyJsonType.Null

                    stringBuilder.Append(String.Format(value, "null"));

                    #endregion LazyJsonType.Null
                    break;

                case LazyJsonType.Boolean:
                    #region LazyJsonType.Boolean

                    LazyJsonBoolean jsonBoolean = (LazyJsonBoolean)jsonArray.TokenList[i];
                    stringBuilder.Append(String.Format(value, jsonBoolean.Value == null ? "null" : jsonBoolean.Value.ToString().ToLower()));

                    #endregion LazyJsonType.Boolean
                    break;

                case LazyJsonType.Integer:
                    #region LazyJsonType.Integer

                    LazyJsonInteger jsonInteger = (LazyJsonInteger)jsonArray.TokenList[i];
                    stringBuilder.Append(String.Format(value, jsonInteger.Value == null ? "null" : jsonInteger.Value));

                    #endregion LazyJsonType.Integer
                    break;

                case LazyJsonType.Decimal:
                    #region LazyJsonType.Decimal

                    LazyJsonDecimal jsonDecimal = (LazyJsonDecimal)jsonArray.TokenList[i];
                    stringBuilder.Append(String.Format(value, jsonDecimal.Value == null ? "null" : jsonDecimal.Value.ToString().Replace(',', '.')));

                    #endregion LazyJsonType.Decimal
                    break;

                case LazyJsonType.String:
                    #region LazyJsonType.String

                    LazyJsonString jsonString = (LazyJsonString)jsonArray.TokenList[i];
                    stringBuilder.Append(String.Format(value, jsonString.Value == null ? "null" : "\"" + jsonString.Value + "\""));

                    #endregion LazyJsonType.String
                    break;

                case LazyJsonType.Object:
                    #region LazyJsonType.Object

                    jsonWriterOptions.IndentLevel++;
                    WriteObject(stringBuilder, jsonWriterOptions, (LazyJsonObject)jsonArray.TokenList[i], LazyJsonPathType.ArrayIndex);
                    jsonWriterOptions.IndentLevel--;

                    #endregion LazyJsonType.Object
                    break;

                case LazyJsonType.Array:
                    #region LazyJsonType.Array

                    jsonWriterOptions.IndentLevel++;
                    WriteArray(stringBuilder, jsonWriterOptions, (LazyJsonArray)jsonArray.TokenList[i], LazyJsonPathType.ArrayIndex);
                    jsonWriterOptions.IndentLevel--;

                    #endregion LazyJsonType.Array
                    break;
                }
                stringBuilder.Append(",");
            }

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

            stringBuilder.Append(closeBracket);
        }
Exemple #5
0
 /// <summary>
 /// Append a json string token at specified jPath
 /// </summary>
 /// <param name="jPath">The jPath location to append the token</param>
 /// <param name="jsonString">The json string token</param>
 public void AppendString(String jPath, LazyJsonString jsonString)
 {
     AppendToken(jPath, jsonString);
 }
Exemple #6
0
        /// <summary>
        /// Read a string 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="jsonString">The json string</param>
        /// <param name="line">The current line</param>
        /// <param name="index">The current index</param>
        private static void ReadString(String json, LazyJsonReaderOptions jsonReaderOptions, LazyJsonReaderResult jsonReaderResult, LazyJsonString jsonString, ref Int32 line, ref Int32 index)
        {
            Int32 startIndex = index;

            while (index < json.Length && json[index] != '\"')
            {
                index = json[index] == '\\' ? index + 2 : index + 1;
            }

            if (index < json.Length || (index == json.Length && json[index - 1] == '\"'))
            {
                jsonString.Value = json.Substring(startIndex, index - startIndex);
                index++; // Jump last "
            }
            else
            {
                jsonReaderResult.Success = false;
                jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedCharacter, line, " \" ", String.Empty));
            }
        }
Exemple #7
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));
            }
        }
Exemple #8
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;
                }
            }
        }