Пример #1
0
        /// <summary>
        /// Parses the provided JSON string and outputs formatted information regarding it.
        /// </summary>
        /// <param name="jsonString">JSON string to parse.</param>
        /// <param name="name">Name to specify in the header of the output.</param>
        static void ExamineJson(string jsonString, string name = "Unspecified string")
        {
            var    strB = new StringBuilder();
            LWJson jsonObj;

            // Provide a header and preview of the JSON that will be processed
            strB.AppendLine("---------------------------------------")
            .AppendLine($"Start Examination ({name})")
            .AppendLine("---------------------------------------");

            strB.AppendLine("Preview of JSON string provided:")
            .Append(jsonString.Substring(0, Math.Min(jsonString.Length, 100))).AppendLine("...").AppendLine();

            // Exceptions will be thrown if invalid JSON formatting is encountered.
            strB.Append("Processing... ");
            jsonObj = LWJson.Parse(jsonString);
            strB.AppendLine("Done!").AppendLine();

            // Display details about the processed JSON
            strB.AppendLine("Details:")
            .AppendLine($" - IsString: {jsonObj.IsString}")
            .AppendLine($" - IsBoolean: {jsonObj.IsBoolean}")
            .AppendLine($" - IsInteger: {jsonObj.IsInteger}")
            .AppendLine($" - IsDouble: {jsonObj.IsDouble}")
            .AppendLine($" - IsObject: {jsonObj.IsObject}")
            .AppendLine($" - IsArray: {jsonObj.IsArray}")
            .AppendLine();

            strB.AppendLine("Processed JSON:").AppendLine(jsonObj.ToString());

            Console.WriteLine(strB.ToString());
        }
Пример #2
0
        /// <summary>
        /// Reads a text file containing JSON, converting it to a <see cref="LWJson"/> object and outputing the results to console.
        /// </summary>
        /// <param name="filename"></param>
        static void ReadFromFile(string filename)
        {
            var json = System.IO.File.ReadAllText(filename);

            Console.WriteLine(json);

            var jsonObj = LWJson.Parse(json);

            Console.WriteLine(jsonObj.ToString());
        }