Ejemplo n.º 1
0
        JsonValue ReadCore(bool objectWithoutBraces = false)
        {
            int c = objectWithoutBraces?'{':SkipPeekChar();

            if (c < 0)
            {
                throw ParseError("Incomplete input");
            }
            switch (c)
            {
            case '[':
                JsonArray    list;
                WscJsonArray wscL = null;
                ReadChar();
                ResetWhite();
                if (ReadWsc)
                {
                    list = wscL = new WscJsonArray();
                }
                else
                {
                    list = new JsonArray();
                }
                SkipPeekChar();
                if (ReadWsc)
                {
                    wscL.Comments.Add(GetWhite());
                }
                for (int i = 0; ; i++)
                {
                    if (SkipPeekChar() == ']')
                    {
                        ReadChar(); break;
                    }
                    if (HasReader)
                    {
                        Reader.Index(i);
                    }
                    var value = ReadCore();
                    if (HasReader)
                    {
                        Reader.Value(value);
                    }
                    list.Add(value);
                    ResetWhite();
                    if (SkipPeekChar() == ',')
                    {
                        ReadChar(); ResetWhite(); SkipPeekChar();
                    }
                    if (ReadWsc)
                    {
                        wscL.Comments.Add(GetWhite());
                    }
                }
                return(list);

            case '{':
                JsonObject    obj;
                WscJsonObject wsc = null;
                if (!objectWithoutBraces)
                {
                    ReadChar();
                    ResetWhite();
                }
                if (ReadWsc)
                {
                    obj = wsc = new WscJsonObject()
                    {
                        RootBraces = !objectWithoutBraces
                    }
                }
                ;
                else
                {
                    obj = new JsonObject();
                }
                SkipPeekChar();
                if (ReadWsc)
                {
                    wsc.Comments[""] = GetWhite();
                }
                for (; ;)
                {
                    if (objectWithoutBraces)
                    {
                        if (SkipPeekChar() < 0)
                        {
                            break;
                        }
                    }
                    else if (SkipPeekChar() == '}')
                    {
                        ReadChar(); break;
                    }
                    string name = readKeyName();
                    skipWhite2();
                    Expect(':');
                    skipWhite2();
                    if (HasReader)
                    {
                        Reader.Key(name);
                    }
                    var value = ReadCore();
                    if (HasReader)
                    {
                        Reader.Value(value);
                    }
                    obj.Add(new JsonPair(name, value));
                    ResetWhite();
                    if (SkipPeekChar() == ',')
                    {
                        ReadChar(); ResetWhite(); SkipPeekChar();
                    }
                    if (ReadWsc)
                    {
                        wsc.Comments[name] = GetWhite(); wsc.Order.Add(name);
                    }
                }
                return(obj);

            case '"': return(ReadStringLiteral());

            default: return(readTfnns(c));
            }
        }
Ejemplo n.º 2
0
        JsonValue ReadCore()
        {
            int c = SkipPeekChar(), next;

            if (c < 0)
            {
                throw ParseError("Incomplete input");
            }
            switch (c)
            {
            case '[':
                JsonArray    list;
                WscJsonArray wscL = null;
                ReadChar();
                ResetWhite();
                if (ReadWsc)
                {
                    list = wscL = new WscJsonArray();
                }
                else
                {
                    list = new JsonArray();
                }
                next = SkipPeekChar();
                if (ReadWsc)
                {
                    wscL.Comments.Add(GetWhite());
                }
                if (next == ']')
                {
                    ReadChar();
                    return(list);
                }
                for (int i = 0; ; i++)
                {
                    if (HasReader)
                    {
                        Reader.Index(i);
                    }
                    var value = ReadCore();
                    if (HasReader)
                    {
                        Reader.Value(value);
                    }
                    list.Add(value);
                    ResetWhite();
                    c = SkipPeekChar();
                    if (c == ',')
                    {
                        ReadChar(); ResetWhite(); c = SkipPeekChar();
                    }
                    if (ReadWsc)
                    {
                        wscL.Comments.Add(GetWhite());
                    }
                    if (c == ']')
                    {
                        ReadChar(); break;
                    }
                }
                return(list);

            case '{':
                JsonObject    obj;
                WscJsonObject wsc = null;
                ReadChar();
                ResetWhite();
                if (ReadWsc)
                {
                    obj = wsc = new WscJsonObject();
                }
                else
                {
                    obj = new JsonObject();
                }
                next = SkipPeekChar();
                if (ReadWsc)
                {
                    wsc.Comments[""] = GetWhite();
                }
                if (next == '}')
                {
                    ReadChar(); return(obj);
                }
                for (; ;)
                {
                    if (SkipPeekChar() == '}')
                    {
                        ReadChar(); break;
                    }
                    string name = readName();
                    skipWhite2();
                    Expect(':');
                    skipWhite2();
                    if (HasReader)
                    {
                        Reader.Key(name);
                    }
                    var value = ReadCore();
                    if (HasReader)
                    {
                        Reader.Value(value);
                    }
                    obj.Add(new JsonPair(name, value));
                    ResetWhite();
                    c = SkipPeekChar();
                    if (c == ',')
                    {
                        ReadChar(); ResetWhite(); c = SkipPeekChar();
                    }
                    if (ReadWsc)
                    {
                        wsc.Comments[name] = GetWhite(); wsc.Order.Add(name);
                    }
                    if (c == '}')
                    {
                        ReadChar(); break;
                    }
                }
                return(obj);

            case '-': return(ReadNumericLiteral());

            default: return(readPrimitive(c));
            }
        }
Ejemplo n.º 3
0
        public void Save(JsonValue value, TextWriter tw, int level, bool hasComment, string separator, bool noIndent = false, bool isRootObject = false)
        {
            if (value == null)
            {
                tw.Write(separator);
                tw.Write("null");
                return;
            }
            switch (value.JsonType)
            {
            case JsonType.Object:
                var           obj        = value.Qo();
                WscJsonObject kw         = writeWsc?obj as WscJsonObject:null;
                bool          showBraces = !isRootObject || (kw != null?kw.RootBraces:emitRootBraces);
                if (!noIndent)
                {
                    if (obj.Count > 0)
                    {
                        nl(tw, level);
                    }
                    else
                    {
                        tw.Write(separator);
                    }
                }
                if (showBraces)
                {
                    tw.Write('{');
                }
                else
                {
                    level--; // reduce level for root
                }
                if (kw != null)
                {
                    var kwl = getWsc(kw.Comments, "");
                    foreach (string key in kw.Order.Concat(kw.Keys).Distinct())
                    {
                        if (!obj.ContainsKey(key))
                        {
                            continue;
                        }
                        var val = obj[key];
                        tw.Write(kwl);
                        nl(tw, level + 1);
                        kwl = getWsc(kw.Comments, key);

                        tw.Write(escapeName(key));
                        tw.Write(":");
                        Save(val, tw, level + 1, testWsc(kwl), " ");
                    }
                    tw.Write(kwl);
                    if (showBraces)
                    {
                        nl(tw, level);
                    }
                }
                else
                {
                    bool skipFirst = !showBraces;
                    foreach (JsonPair pair in obj)
                    {
                        if (!skipFirst)
                        {
                            nl(tw, level + 1);
                        }
                        else
                        {
                            skipFirst = false;
                        }
                        tw.Write(escapeName(pair.Key));
                        tw.Write(":");
                        Save(pair.Value, tw, level + 1, false, " ");
                    }
                    if (showBraces && obj.Count > 0)
                    {
                        nl(tw, level);
                    }
                }
                if (showBraces)
                {
                    tw.Write('}');
                }
                break;

            case JsonType.Array:
                int i = 0, n = value.Count;
                if (!noIndent)
                {
                    if (n > 0)
                    {
                        nl(tw, level);
                    }
                    else
                    {
                        tw.Write(separator);
                    }
                }
                tw.Write('[');
                WscJsonArray whiteL = null;
                string       wsl    = null;
                if (writeWsc)
                {
                    whiteL = value as WscJsonArray;
                    if (whiteL != null)
                    {
                        wsl = getWsc(whiteL.Comments, 0);
                    }
                }
                for (; i < n; i++)
                {
                    var v = value[i];
                    if (whiteL != null)
                    {
                        tw.Write(wsl);
                        wsl = getWsc(whiteL.Comments, i + 1);
                    }
                    nl(tw, level + 1);
                    Save(v, tw, level + 1, wsl != null && testWsc(wsl), "", true);
                }
                if (whiteL != null)
                {
                    tw.Write(wsl);
                }
                if (n > 0)
                {
                    nl(tw, level);
                }
                tw.Write(']');
                break;

            case JsonType.Boolean:
                tw.Write(separator);
                tw.Write((bool)value?"true":"false");
                break;

            case JsonType.String:
                writeString(((JsonPrimitive)value).GetRawString(), tw, level, hasComment, separator);
                break;

            default:
                tw.Write(separator);
                tw.Write(((JsonPrimitive)value).GetRawString());
                break;
            }
        }
Ejemplo n.º 4
0
        public void Save(JsonValue value, TextWriter tw, int level, bool hasComment)
        {
            switch (value.JsonType)
            {
            case JsonType.Object:
                var           obj = value.Qo();
                WscJsonObject kw  = WriteWsc?obj as WscJsonObject:null;
                if (level > 0)
                {
                    nl(tw, level);
                }
                tw.Write('{');
                if (kw != null)
                {
                    var kwl = getWsc(kw.Comments, "");
                    foreach (string key in kw.Order.Concat(kw.Keys).Distinct())
                    {
                        if (!obj.ContainsKey(key))
                        {
                            continue;
                        }
                        var val = obj[key];
                        tw.Write(kwl);
                        nl(tw, level + 1);
                        kwl = getWsc(kw.Comments, key);

                        tw.Write(escapeName(key));
                        tw.Write(":");
                        var nextType = val != null?(JsonType?)val.JsonType:null;
                        if (nextType != JsonType.Array && nextType != JsonType.Object)
                        {
                            tw.Write(" ");
                        }
                        if (val == null)
                        {
                            tw.Write("null");
                        }
                        else
                        {
                            Save(val, tw, level + 1, testWsc(kwl));
                        }
                    }
                    tw.Write(kwl);
                    nl(tw, level);
                }
                else
                {
                    foreach (JsonPair pair in ((JsonObject)value))
                    {
                        nl(tw, level + 1);
                        tw.Write(escapeName(pair.Key));
                        tw.Write(":");
                        var nextType = pair.Value != null?(JsonType?)pair.Value.JsonType:null;
                        if (nextType != JsonType.Array && nextType != JsonType.Object)
                        {
                            tw.Write(" ");
                        }
                        if (pair.Value == null)
                        {
                            tw.Write("null");
                        }
                        else
                        {
                            Save(pair.Value, tw, level + 1, false);
                        }
                    }
                    nl(tw, level);
                }
                tw.Write('}');
                break;

            case JsonType.Array:
                if (level > 0)
                {
                    nl(tw, level);
                }
                tw.Write('[');
                int          i = 0, n = value.Count;
                WscJsonArray whiteL = null;
                string       wsl    = null;
                if (WriteWsc)
                {
                    whiteL = value as WscJsonArray;
                    if (whiteL != null)
                    {
                        wsl = getWsc(whiteL.Comments, 0);
                    }
                }
                for (; i < n; i++)
                {
                    var v = value[i];
                    if (whiteL != null)
                    {
                        tw.Write(wsl);
                        wsl = getWsc(whiteL.Comments, i + 1);
                    }
                    if (v.JsonType != JsonType.Array && v.JsonType != JsonType.Object)
                    {
                        nl(tw, level + 1);
                    }
                    if (v != null)
                    {
                        Save(v, tw, level + 1, wsl != null && testWsc(wsl));
                    }
                    else
                    {
                        tw.Write("null");
                    }
                }
                if (whiteL != null)
                {
                    tw.Write(wsl);
                }
                nl(tw, level);
                tw.Write(']');
                break;

            case JsonType.Boolean:
                tw.Write((bool)value?"true":"false");
                break;

            case JsonType.String:
                writeString(((JsonPrimitive)value).GetRawString(), tw, level, hasComment);
                break;

            default:
                tw.Write(((JsonPrimitive)value).GetRawString());
                break;
            }
        }