Beispiel #1
0
        /// <summary>Loads Hjson/JSON from a TextReader.</summary>
        public static JsonValue Load(TextReader textReader, IJsonReader jsonReader = null)
        {
            if (textReader == null)
            {
                throw new ArgumentNullException("textReader");
            }
            var ret = new HjsonReader(textReader, jsonReader).Read();

            return(ret);
        }
Beispiel #2
0
        void writeString(string value, TextWriter tw, int level, bool hasComment, string separator)
        {
            if (value == "")
            {
                tw.Write(separator + "\"\""); return;
            }

            char      left = value[0], right = value[value.Length - 1];
            char      left1 = value.Length > 1?value[1]:'\0', left2 = value.Length > 2?value[2]:'\0';
            bool      doEscape = hasComment || value.Any(c => needsQuotes(c));
            JsonValue dummy;

            if (doEscape ||
                BaseReader.IsWhite(left) ||
                left == '"' ||
                left == '#' ||
                left == '\'' && left1 == '\'' && left2 == '\'' ||
                left == '/' && (left1 == '*' || left1 == '/') ||
                left == '{' ||
                left == '[' ||
                BaseReader.IsWhite(right) ||
                HjsonReader.TryParseNumericLiteral(value, true, out dummy) ||
                startsWithKeyword(value))
            {
                // If the string contains no control characters, no quote characters, and no
                // backslash characters, then we can safely slap some quotes around it.
                // Otherwise we first check if the string can be expressed in multiline
                // format or we must replace the offending characters with safe escape
                // sequences.

                if (!value.Any(c => needsEscape(c)))
                {
                    tw.Write(separator + "\"" + value + "\"");
                }
                else if (!value.Any(c => needsEscapeML(c)) && !value.Contains("'''"))
                {
                    writeMLString(value, tw, level, separator);
                }
                else
                {
                    tw.Write(separator + "\"" + JsonWriter.EscapeString(value) + "\"");
                }
            }
            else
            {
                tw.Write(separator + value);
            }
        }