コード例 #1
0
ファイル: FormLogic.cs プロジェクト: eltomjan/ETEhomeTools
        public void LoadJSON(String fileName)
        {
            JSONdata = File.ReadAllBytes(fileName);
            JsonNode result;
            int      endPos = -1;

            JsonErrno e = jsonParser.Parse(JSONdata, ref endPos, out result);

            if (JsonErrno.OK == e)
            {
                JSON = new VisualNode3(ref result, JSONdata, 10000);
            }
        }
コード例 #2
0
ファイル: test-suite.cs プロジェクト: eltomjan/Gason
    static void Parse(String csource, int no, bool ok)
    {
        Byte[]      utf, source, dest;
        int         endptr = -1;
        ValueWriter wr     = new ValueWriter();
        Parser      json   = new Parser(true);
        Boolean     shrink = false;
        Regex       r      = new Regex(@"[ \t\r\n]");
        String      print;

        do
        {
            if (shrink)
            {
                csource = r.Replace(csource, "");
            }
            utf    = Encoding.UTF8.GetBytes(csource);
            source = new byte[utf.Length + 1];
            dest   = new byte[utf.Length * 2 + 1];
            utf.CopyTo(source, 0);
            endptr = -1;
            JsonErrno result = json.Parse(source, ref endptr, out JsonNode value
#if KEY_SPLIT
                                          , new ByteString[] { }, 0, 0, -1
#endif
                                          );
            if (shrink || no > 100)
            {
                if (json != null && result == JsonErrno.OK)
                {
                    using (MemoryStream memory = new MemoryStream(dest))
                        using (StreamWriter sw = new StreamWriter(memory))
                        //using (StreamWriter sw = new StreamWriter(Console.OpenStandardOutput()))
                        {
                            sw.NewLine = "\n"; sw.AutoFlush = true;
                            wr.DumpValueIterative(sw, value, source, no > 100 ? 0 : -1); // print formatted JSON
                            sw.Flush();
                            int size = (int)memory.Position;
                            memory.Position = 0;
                            memory.Read(dest, 0, size);
                            print = Encoding.UTF8.GetString(dest, 0, size);
                            if (csource.TrimEnd('\n') != print.TrimEnd('\n'))
                            {
                                Console.WriteLine($"{no%100}:Dump bug:\n{csource}\nvs:\n{print}\n");
                                ++failed;
                            }
                        }
                }
            }
            else
            {
                if (ok && result != JsonErrno.OK)
                {
                    Console.WriteLine($"{no}:FAILED { parsed }: {result}\\{csource}\n{(int)(endptr + 1)} - \\{Encoding.UTF8.GetString(source,0,endptr)}\n");
                    ++failed;
                }
                if (!ok && result == JsonErrno.OK)
                {
                    Console.WriteLine($"{no}:PASSED {parsed}:\n{csource}\n");
                    ++failed;
                }
            }
            if (no <= 100)
            {
                shrink = !shrink;
            }
        } while (shrink);
        ++parsed;
    }
コード例 #3
0
ファイル: Gason.cs プロジェクト: eltomjan/Gason
        public JsonErrno Parse(Byte[] s, ref int endPos, out JsonNode value
#if KEY_SPLIT
                               , ByteString[] keysLog, int level, int startPos, int length
#endif
                               )
        {
            int endPosMem = endPos;

            Init(ref s, (endPos > 0));
            value = null;
            while (strPos < len)
            {
#if !SKIP_VALIDATION
                if (type > 1)
                {
                    prevType = type;
                }
#endif
                type = SearchTables.valTypes[s[strPos]];
                if (type <= 1)
                {
                    strPos++;
                    continue; // white space
                }
#if DebugPrint
                o.startPos = strPos;
#endif
                endPos = strPos++;
                switch (type) // switch (**endptr) {
                {
                case 2:       // case '-':
                    if (FloatAsDecimal)
                    {
                        endPos = o.String2decimal(ref strPos, s, true);
                    }
                    else
                    {
                        endPos = o.String2double(ref strPos, s, true);
                    }
#if !SKIP_VALIDATION
                    if (0 == (SearchTables.specialTypes[s[endPos]] & 3))     // isdelim
                    {
                        endPos = strPos;
                        return(JsonErrno.BAD_NUMBER);
                    }
#endif
                    break;

                case 4:     // 0-9
                    strPos--;
                    if (FloatAsDecimal)
                    {
                        endPos = o.String2decimal(ref strPos, s);
                    }
                    else
                    {
                        endPos = o.String2double(ref strPos, s);
                    }
#if !SKIP_VALIDATION
                    if (0 == (SearchTables.specialTypes[s[endPos]] & 3))     // isdelim
                    {
                        endPos = strPos;
                        return(JsonErrno.BAD_NUMBER);
                    }
#endif
                    break;

                case 3:                                       // case '"':
                    JsonErrno e = o.GetString(ref strPos, s); // new ByteString(s, o.doubleOrString).ToString()
                    if (e != JsonErrno.OK)
                    {
                        return(e);
                    }
#if !SKIP_VALIDATION
                    if (0 == (SearchTables.specialTypes[s[strPos]] & 3))     // !isdelim
                    {
                        endPos = strPos;
                        return(JsonErrno.BAD_STRING);
                    }
#endif
                    break;

                case 7:     // 't'
                    if ((SearchTables.specialTypes[s[strPos + 3]] & 3) != 0 &&  // isdelim
                        (s[strPos + 0] == 'r') &&
                        (s[strPos + 1] == 'u') &&
                        (s[strPos + 2] == 'e'))
                    {
                        o.Tag   = JsonTag.JSON_TRUE;
                        strPos += 3;
#if !SKIP_VALIDATION
                    }
                    else
                    {
                        return(JsonErrno.BAD_IDENTIFIER);
#endif
                    }
                    break;

                case 6:     // 'f'
                    if ((SearchTables.specialTypes[s[strPos + 4]] & 3) != 0 &&  // isdelim
                        (s[strPos + 0] == 'a') &&
                        (s[strPos + 1] == 'l') &&
                        (s[strPos + 2] == 's') &&
                        (s[strPos + 3] == 'e'))
                    {
                        o.Tag   = JsonTag.JSON_FALSE;
                        strPos += 4;
#if !SKIP_VALIDATION
                    }
                    else
                    {
                        return(JsonErrno.BAD_IDENTIFIER);
#endif
                    }
                    break;

                case 8:                              // 'n'
#if !SKIP_VALIDATION
                    if (prevType == 3 && !separator) // {"Missing colon" null} + fail19.json
                    {
                        return(JsonErrno.UNEXPECTED_CHARACTER);
                    }
#endif
                    if ((SearchTables.specialTypes[s[strPos + 3]] & 3) != 0 &&  // isdelim
                        (s[strPos + 0] == 'u') &&
                        (s[strPos + 1] == 'l') &&
                        (s[strPos + 2] == 'l'))
                    {
                        o.Tag   = JsonTag.JSON_NULL;
                        strPos += 3;
#if !SKIP_VALIDATION
                    }
                    else
                    {
                        return(JsonErrno.BAD_IDENTIFIER);
#endif
                    }
                    break;

                case 12:     // ']'
#if !SKIP_VALIDATION
                    if (pos == -1)
                    {
                        return(JsonErrno.STACK_UNDERFLOW);
                    }
                    if (tags[pos] != JsonTag.JSON_ARRAY)
                    {
                        return(JsonErrno.MISMATCH_BRACKET);
                    }
                    if (separator && prevType != 11)            // '['
                    {
                        return(JsonErrno.UNEXPECTED_CHARACTER); // fail4
                    }
#endif
#if DEBUGGING
                    o.uniqueNo = ++uniqueNo;
                    o.ListToValue(JsonTag.JSON_ARRAY, tails[pos]?.NodeRawData);
                    pos--;
#else
                    o.ListToValue(JsonTag.JSON_ARRAY, tails[pos--]);
#endif
#if !SKIP_VALIDATION
                    if (type > 1)
                    {
                        prevType = type;
                    }
#endif
                    break;

                case 13:     // '}'
#if !SKIP_VALIDATION
                    if (pos == -1)
                    {
                        return(JsonErrno.STACK_UNDERFLOW);
                    }
                    if (tags[pos] != JsonTag.JSON_OBJECT)
                    {
                        return(JsonErrno.MISMATCH_BRACKET);
                    }
                    if (keys[pos].length != -1)
                    {
                        return(JsonErrno.UNEXPECTED_CHARACTER);
                    }
                    if (separator && prevType != 10)     // '{'
                    {
                        return(JsonErrno.UNEXPECTED_CHARACTER);
                    }
#endif
#if DebugPrint
                    o.endPos = strPos;
#endif
#if DEBUGGING
                    o.uniqueNo = ++uniqueNo;
                    o.ListToValue(JsonTag.JSON_OBJECT, tails[pos]?.NodeRawData);     // o=parent, tails[pos]=last, 1st, ...
                    pos--;
#else
                    o.ListToValue(JsonTag.JSON_OBJECT, tails[pos--]);
#endif
#if KEY_SPLIT
                    if (insideLimitBlock && (level == pos + 1))
                    {
                        if (length == 1)
                        {
                            bubbleOut = true;
                        }
                        else if (length > 1)
                        {
                            length--;
                        }
                    }
#endif
                    break;

                case 11:     // '['
#if !SKIP_VALIDATION
                    if (++pos == JSON_STACK_SIZE)
                    {
                        return(JsonErrno.STACK_OVERFLOW);
                    }
#else
                    pos++;
#endif
                    tails[pos]       = null;
                    tags[pos]        = JsonTag.JSON_ARRAY;
                    keys[pos].length = -1;
#if !SKIP_VALIDATION
                    separator = true;
#endif
                    continue;

                case 10:     // '{'
#if !SKIP_VALIDATION
                    if (++pos == JSON_STACK_SIZE)
                    {
                        return(JsonErrno.STACK_OVERFLOW);
                    }
#else
                    pos++;
#endif
#if KEY_SPLIT
                    if (pos == level && !insideLimitBlock)
                    {
                        int i = level - 1;
                        while (i >= 0)
                        {
                            if (keys[i].length != -1)
                            {
#if DEBUGGING
                                if (keysLog[i].Equals(s, keys[i].idxes))
                                {
                                    i--;
                                }
#else
                                if (keysLog[i].Equals(s, keys[i]))
                                {
                                    i--;
                                }
#endif
                                else
                                {
                                    break;
                                }
                            }
                            else if (keysLog[i] == null)
                            {
                                i--;
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (i == -1)
                        {     // Keys & level match
                            insideLimitBlock = true;
                            if (startPos > 0)
                            {
                                strPos = endPosMem;
                            }
                        }
                    }
#endif
                    tails[pos]       = null;
                    tags[pos]        = JsonTag.JSON_OBJECT;
                    keys[pos].length = -1;
#if !SKIP_VALIDATION
                    separator = true;
#endif
                    continue;

                case 14:     // ':'
#if !SKIP_VALIDATION
                    if (separator || keys[pos].length == -1)
                    {
                        return(JsonErrno.UNEXPECTED_CHARACTER);
                    }
                    separator = true;
#endif
                    continue;

                case 15:     // ','
#if !SKIP_VALIDATION
                    if (separator || keys[pos].length != -1)
                    {
                        return(JsonErrno.UNEXPECTED_CHARACTER);
                    }
                    separator = true;
#endif
                    continue;

#if !SKIP_VALIDATION
                default:
                    return(JsonErrno.UNEXPECTED_CHARACTER);
#endif
                }
#if !SKIP_VALIDATION
                separator = false;
#endif

                if (pos == -1)
                {
                    value = o;
#if !SKIP_VALIDATION
                    while (strPos < len - 1)
                    {
                        if (type == 13)   // '}' / {"Extra value after close": true} "misplaced quoted value"
                        {
                            type = 0;
                            while (strPos < len - 1)
                            {
                                type = SearchTables.valTypes[s[strPos]];
                                if (type <= 1)
                                {
                                    strPos++;
                                    continue;
                                }
                                if (type == 3)
                                {
                                    strPos++;
                                    JsonNode  trash = new JsonNode();
                                    JsonErrno e     = trash.GetString(ref strPos, s);
                                    if (e != JsonErrno.OK)
                                    {
                                        return(e);
                                    }
                                    endPos = strPos;
                                    if (strPos != len - 1 || 0 == (SearchTables.specialTypes[s[strPos]] & 3)) // !isdelim
                                    {
                                        return(JsonErrno.BAD_STRING);
                                    }
                                    else
                                    {
                                        return(JsonErrno.OK);
                                    }
                                }
                                else
                                {
                                    return(JsonErrno.UNEXPECTED_CHARACTER);
                                }
                            }
                        }
                        if ((strPos < len - 1) && SearchTables.valTypes[s[strPos]] <= 1)
                        {
                            strPos++;
                        }
                        else
                        {
                            if (s[strPos] == 0)
                            {
                                break;
                            }
                            if (prevType != 12 || s[strPos] != ',') // ']'
                            {
                                return(JsonErrno.BREAKING_BAD);
                            }
                            else
                            {
                                strPos++;
                            }
                        }
                    }
#endif
                    return(JsonErrno.OK);
                }
#if KEY_SPLIT
                do
                {
#endif
                if (tags[pos] == JsonTag.JSON_OBJECT)
                {
                    if (keys[pos].length == -1)
                    {
#if !SKIP_VALIDATION
                        if (o.Tag != JsonTag.JSON_STRING)
                        {
                            return(JsonErrno.UNQUOTED_KEY);
                        }
#endif
#if DEBUGGING
                        keys[pos] = new LinkedByteString(o.doubleOrString);
#else
                        keys[pos].data = o.doubleOrString.data;
#endif
#if KEY_SPLIT
                        if (bubbleOut)
                        {
                            continue;
                        }
                        else
                        {
                            break;
                        }
#else
                        continue;
#endif
                    }
#if DebugPrint
                    o.endPos = strPos;
#endif
#if DEBUGGING
                    o.uniqueNo = ++uniqueNo;
                    o.InsertAfter(tails[pos]?.NodeRawData, ref keys[pos].idxes); // next2 last & before first
#else
                    o.InsertAfter(tails[pos] != null ? tails[pos] : null, ref keys[pos]);
#endif
                }
                else
                {
#if DebugPrint
                    o.endPos = strPos;
#endif
#if DEBUGGING
                    o.uniqueNo = ++uniqueNo;
                    o.InsertAfter(tails[pos]?.NodeRawData);
#else
                    o.InsertAfter(tails[pos]);
#endif
                }
                tails[pos] =
#if DEBUGGING
                    new VisualNode3(ref o, s, 3000);
#else
                    o;
#endif
                o = new JsonNode();
#if DEBUGGING
                root.ChangeNode(ref o);
#endif

#if KEY_SPLIT
                if (bubbleOut)
                {
                    if (tags[pos] == JsonTag.JSON_ARRAY ||
                        tags[pos] == JsonTag.JSON_OBJECT)
                    { // lists close brackets
#if DEBUGGING
                        o.ListToValue(tags[pos], tails[pos]?.NodeRawData);
#else
                        o.ListToValue(tags[pos], tails[pos]);
#endif
                    }
                    if (pos-- == 0)
                    {
                        while ((strPos < len) && s[strPos++] != ',')
                        {
                            ;                                          // find array separator
                        }
                        while ((strPos < len) && ((SearchTables.specialTypes[s[strPos]] & 3) != 0))
                        {
                            strPos++;                                                                         // skip delims
                        }
                        while ((strPos < len) && (s[strPos] != '{'))
                        {
                            strPos++;                                          // array start
                        }
                        if (strPos < len)
                        {
                            strPos++;
                        }
                        endPos = strPos;
                        value  = o;
                        return(JsonErrno.OK);
                    }
                }
                else
                {
                    break;
                }
            }
            while (true)
            {
                ;           // exit by breaks
            }
#endif
            }