Ejemplo n.º 1
0
        private static bool startsWithKeyword(string text)
        {
            int p;

            if (text.StartsWith("true") || text.StartsWith("null"))
            {
                p = 4;
            }
            else if (text.StartsWith("false"))
            {
                p = 5;
            }
            else
            {
                return(false);
            }
            while (p < text.Length && BaseReader.IsWhite(text[p]))
            {
                p++;
            }
            if (p == text.Length)
            {
                return(true);
            }
            var ch = text[p];

            return(ch == ',' || ch == '}' || ch == ']' || ch == '#' || ch == '/' && (text.Length > p + 1 && (text[p + 1] == '/' || text[p + 1] == '*')));
        }
Ejemplo n.º 2
0
        void writeString(string value, TextWriter tw, int level, bool hasComment)
        {
            if (value == "")
            {
                tw.Write("\"\""); return;
            }

            char first = value[0], last = value[value.Length - 1];
            bool doEscape = value.Any(c => shouldEscapeChar(c));

            if (hasComment ||
                BaseReader.IsWhite(first) ||
                char.IsDigit(first) ||
                first == '#' ||
                first == '-' ||
                first == '{' ||
                first == '[' ||
                BaseReader.IsWhite(last) ||
                doEscape ||
                isKeyword(value))
            {
                if (doEscape && !value.Any(c => shouldEscapeCharExceptLF(c)))
                {
                    writeMLString(value, tw, level);
                }
                else
                {
                    tw.Write("\"" + JsonWriter.EscapeString(value) + "\"");
                }
            }
            else
            {
                tw.Write(value);
            }
        }
Ejemplo n.º 3
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);
            }
        }