Exemple #1
0
        /// <summary>Saves as Hjson to a string.</summary>
        public string ToString(HjsonOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            var sw = new StringWriter();

            HjsonValue.Save(this, sw, options);
            return(sw.ToString());
        }
Exemple #2
0
        string readKeyName()
        {
            // quotes for keys are optional in Hjson
            // unless they include {}[],: or whitespace.

            int c = PeekChar();

            if (c == '"' || c == '\'')
            {
                return(ReadStringLiteral(null));
            }

            sb.Length = 0;
            int space = -1;

            for (; ;)
            {
                c = PeekChar();
                if (c < 0)
                {
                    throw ParseError("Name is not closed");
                }
                char ch = (char)c;
                if (ch == ':')
                {
                    if (sb.Length == 0)
                    {
                        throw ParseError("Found ':' but no key name (for an empty key name use quotes)");
                    }
                    else if (space >= 0 && space != sb.Length)
                    {
                        throw ParseError("Found whitespace in your key name (use quotes to include)");
                    }
                    return(sb.ToString());
                }
                else if (IsWhite(ch))
                {
                    if (space < 0)
                    {
                        space = sb.Length;
                    }
                    ReadChar();
                }
                else if (HjsonValue.IsPunctuatorChar(ch))
                {
                    throw ParseError("Found '" + ch + "' where a key name was expected (check your syntax or use quotes if the key name includes {}[],: or whitespace)");
                }
                else
                {
                    ReadChar();
                    sb.Append(ch);
                }
            }
        }
Exemple #3
0
 /// <summary>Saves the JSON to a TextWriter.</summary>
 public void Save(TextWriter textWriter, Stringify format = Stringify.Plain)
 {
     if (textWriter == null)
     {
         throw new ArgumentNullException("textWriter");
     }
     if (format == Stringify.Hjson)
     {
         HjsonValue.Save(this, textWriter);
     }
     else
     {
         new JsonWriter(format == Stringify.Formatted).Save(this, textWriter, 0);
     }
     textWriter.Flush();
 }
Exemple #4
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) || BaseReader.IsWhite(right) ||
                left == '"' ||
                left == '\'' ||
                left == '#' ||
                left == '/' && (left1 == '*' || left1 == '/') ||
                HjsonValue.IsPunctuatorChar(left) ||
                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("'''") && !value.All(c => BaseReader.IsWhite(c)))
                {
                    writeMLString(value, tw, level, separator);
                }
                else
                {
                    tw.Write(separator + "\"" + JsonWriter.EscapeString(value) + "\"");
                }
            }
            else
            {
                tw.Write(separator + value);
            }
        }
Exemple #5
0
        JsonValue readTfnns(int c)
        {
            if (HjsonValue.IsPunctuatorChar((char)c))
            {
                throw ParseError("Found a punctuator character '" + c + "' when expecting a quoteless string (check your syntax)");
            }

            sb.Length = 0;
            for (; ;)
            {
                bool isEol = c < 0 || c == '\n';
                if (isEol || c == ',' ||
                    c == '}' || c == ']' ||
                    c == '#' ||
                    c == '/' && (PeekChar(1) == '/' || PeekChar(1) == '*'))
                {
                    if (sb.Length > 0)
                    {
                        char ch = sb[0];
                        switch (ch)
                        {
                        case 'f': if (sb.ToString().Trim() == "false")
                            {
                                return(false);
                            }
                            break;

                        case 'n': if (sb.ToString().Trim() == "null")
                            {
                                return(null);
                            }
                            break;

                        case 't': if (sb.ToString().Trim() == "true")
                            {
                                return(true);
                            }
                            break;

                        default:
                            if (ch == '-' || ch >= '0' && ch <= '9')
                            {
                                JsonValue res;
                                if (TryParseNumericLiteral(sb.ToString(), false, out res))
                                {
                                    return(res);
                                }
                            }
                            break;
                        }
                    }
                    if (isEol)
                    {
                        // remove any whitespace at the end (ignored in quoteless strings)
                        return(HjsonDsf.Parse(dsfProviders, sb.ToString().Trim()));
                    }
                }
                ReadChar();
                if (c != '\r')
                {
                    sb.Append((char)c);
                }
                c = PeekChar();
            }
        }