Exemplo n.º 1
0
    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;
    }
Exemplo n.º 2
0
    public static void Main()
    {
        Tests.TestAll();
        Byte[] raw;
#if DoubleLinked
        int        endPos = -1;
        JsonNode   jsn;
        BrowseNode v1, v2;
        DepthFirst bf1, bf2;
#endif
        Parser  jsonParser = new Parser(true); // FloatAsDecimal
        Printer prn        = new Printer();

#if DoubleLinked
        raw = Encoding.UTF8.GetBytes(Strings.JSONnetPart1);
        jsonParser.Parse(raw, ref endPos, out JsonNode jsn1
#if KEY_SPLIT
                         , new ByteString[] { }, 0, 0, 0
#endif
                         );
        bf1 = new DepthFirst(jsn1, raw);
        v1  = new BrowseNode(ref jsn1, raw);

        raw = Encoding.UTF8.GetBytes(Strings.JSONnetPart2);
        jsonParser.Parse(raw, ref endPos, out JsonNode jsn2
#if KEY_SPLIT
                         , new ByteString[] { }, 0, 0, 0
#endif
                         );
        jsonParser.SortPaths(jsn2, raw, "id");
        v2 = new BrowseNode(ref jsn2, raw);

        bf2 = new DepthFirst(jsn2, raw);
        jsonParser.RemoveTwins(ref bf1, ref bf2);

        Console.WriteLine("RemoveTwins result 1/2:");
        Console.WriteLine(prn.Print(ref v1, 0).ToString());
        Console.WriteLine("RemoveTwins result 2/2:");
        Console.WriteLine(prn.Print(ref v2, 0).ToString());

        raw = Encoding.UTF8.GetBytes(Strings.JSONnetComplete);
        ByteString[] keys = new ByteString[]
        {
            new ByteString("batters"),
            null
        };
        jsonParser.Parse(raw, ref endPos, out jsn
#if KEY_SPLIT
                         , keys, 2, 0, 2
#endif
                         ); // batters / null path, read only 1st 2
#if !KEY_SPLIT
        jsonParser.SortPaths(jsn, raw, null);
        v1 = new BrowseNode(ref jsn, raw);
        String check = prn.Print(ref v1, 0).ToString();
        if (Strings.Sort1 != check)
        {
            Console.WriteLine($"SortPaths 2.1 error:\n{Strings.Sort1}!=\n{check}<-");
        }
        else
        {
            Console.WriteLine("SortPaths 2.1 OK");
        }
        jsonParser.SortPaths(jsn, raw, "id");
        check = prn.Print(ref v1, 0).ToString();
        if (Strings.Sort2 != check)
        {
            Console.WriteLine($"SortPaths 2.2 error:\n{Strings.Sort2}");
        }
        else
        {
            Console.WriteLine("SortPaths 2.2 OK");
        }
        endPos = -1;
#else
        Console.WriteLine("1st 2 rows of batters only:");
        jsonParser.Parse(raw, ref endPos, out jsn
                         , keys, 2, endPos, 2
                         ); // and now following 2
        ValueWriter wr = new ValueWriter();
        using (StreamWriter sw = new StreamWriter(Console.OpenStandardOutput()))
        {
            sw.AutoFlush = true;
            wr.DumpValueIterative(sw, jsn, raw);
        }
#endif

        // Twiter complex json -> TC
        raw = Encoding.UTF8.GetBytes(Tests.ReadFile("pass6.json"));
        jsonParser.Parse(raw, ref endPos, out JsonNode jsn01
#if KEY_SPLIT
                         , new ByteString[] { }, 0, 0, 0
#endif
                         );
        jsonParser.Parse(raw, ref endPos, out JsonNode jsn02
#if KEY_SPLIT
                         , new ByteString[] { }, 0, 0, 0
#endif
                         );
        v1  = new BrowseNode(ref jsn01, raw);
        v2  = new BrowseNode(ref jsn02, raw);
        bf1 = new DepthFirst(jsn01, raw);
        bf2 = new DepthFirst(jsn02, raw);
        Tests.ModifyTwitter(ref bf1, ref bf2, raw);
        jsonParser.RemoveTwins(ref bf1, ref bf2);
        if (v1.NodeRawData == null)
        {
            Console.WriteLine("Bug - 1st modified Twitter JSON empty");
        }
        else if (Strings.Twitter1 == prn.Print(ref v1, 0).ToString())
        {
            Console.WriteLine($"Twitter check 1/2 OK - 1st variant has expected content:");
        }
        else
        {
            Console.WriteLine($"Twiter RemoveTwins result 1/2 differs:\n{prn.Print(ref v1, 0).ToString()}");
        }
        if (v2.NodeRawData == null)
        {
            Console.WriteLine("Bug - 2nd modified Twitter JSON empty");
        }
        else if (Strings.Twitter2 == prn.Print(ref v2, 0).ToString())
        {
            Console.WriteLine($"Twitter check 2/2 OK - 2nd variant has expected content:");
        }
        else
        {
            Console.WriteLine($"Twiter RemoveTwins result 2/2 differs:\n{prn.Print(ref v2, 0).ToString()}");
        }
#endif

        raw = File.ReadAllBytes(@"citylots.json");
        Benchmark b = new Benchmark(raw);
        b.Run(); // < 30s
        return;
    }