Пример #1
0
        /// <summary>
        /// Serialize an object to json
        /// </summary>
        /// <param name="data">The object to be serialized</param>
        /// <returns>The json</returns>
        public static String Serialize(Object data)
        {
            LazyJson lazyJson = new LazyJson();

            lazyJson.AppendRoot(SerializeToken(data));
            return(LazyJsonWriter.Write(lazyJson));
        }
Пример #2
0
        /// <summary>
        /// Write a json
        /// </summary>
        /// <param name="lazyJson">The lazy json</param>
        /// <param name="jsonWriterOptions">The json writer options</param>
        /// <returns>The json</returns>
        public static String Write(LazyJson lazyJson, LazyJsonWriterOptions jsonWriterOptions = null)
        {
            StringBuilder stringBuilder = new StringBuilder();

            if (jsonWriterOptions == null)
            {
                jsonWriterOptions = new LazyJsonWriterOptions();
            }

            WriteRoot(stringBuilder, jsonWriterOptions, lazyJson.Root);

            return(stringBuilder.ToString());
        }
Пример #3
0
        /// <summary>
        /// Read a json
        /// </summary>
        /// <param name="json">The json</param>
        /// <param name="jsonReaderOptions">The json reader options</param>
        /// <returns>The lazy json</returns>
        public static LazyJson Read(String json, LazyJsonReaderOptions jsonReaderOptions = null)
        {
            LazyJson lazyJson = new LazyJson();

            if (jsonReaderOptions == null)
            {
                jsonReaderOptions = new LazyJsonReaderOptions();
            }

            ReadRoot(json, jsonReaderOptions, lazyJson);

            return(lazyJson);
        }
Пример #4
0
        /// <summary>
        /// Deserialize the json to the desired object
        /// </summary>
        /// <param name="json">The json to be deserialized</param>
        /// <param name="dataType">The type of the desired object</param>
        /// <returns>The desired object instance</returns>
        public static Object Deserialize(String json, Type dataType)
        {
            LazyJson lazyJson = LazyJsonReader.Read(json);

            return(DeserializeToken(lazyJson.Root, dataType));
        }
Пример #5
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;
                }
            }
        }