コード例 #1
0
        public void TestJSONSerializeTwoTables()
        {
            string       expectedJson = UtilsForTesting.LoadJson("Resources/json/twotables.json");
            List <Table> tables       = this.GetTables();

            StringBuilder sb = new StringBuilder();

            (new JSONWriter()).Write(sb, tables);
            string s = sb.ToString();

            Assert.Equal(expectedJson, s);

            /*
             * using (var stream = new MemoryStream())
             * using (var sb = new StreamWriter(stream) { AutoFlush = true })
             * {
             *  (new JSONWriter()).write(sb, tables);
             *
             *  var reader = new StreamReader(stream);
             *  stream.Position = 0;
             *  var s = reader.ReadToEnd();
             *
             *  //File.WriteAllText("twotables_new.json", s);
             *
             *  Assert.Equal(expectedJson, s);
             *
             *  // Gson gson = new Gson();
             *  //JsonArray json = gson.fromJson(s, JsonArray.class);
             *  //assertEquals(2, json.size());
             *  var json = JsonConvert.DeserializeObject<List<Table>>(s);
             *  Assert.Equal(2, json.Count);
             * }
             */
        }
コード例 #2
0
        public void TestSpanningCells()
        {
            PageArea page                     = UtilsForTesting.GetPage("Resources/spanning_cells.pdf", 1);
            string   expectedJson             = UtilsForTesting.LoadJson("Resources/json/spanning_cells.json");
            SpreadsheetExtractionAlgorithm se = new SpreadsheetExtractionAlgorithm();
            List <Table> tables               = se.Extract(page);

            Assert.Equal(2, tables.Count);

            var expectedJObject = (JArray)JsonConvert.DeserializeObject(expectedJson);

            StringBuilder sb = new StringBuilder();

            (new JSONWriter()).Write(sb, tables);
            var actualJObject = (JArray)JsonConvert.DeserializeObject(sb.ToString());

            double pageHeight = 842;
            double precision  = 2;

            for (int i = 0; i < 2; i++)
            {
                Assert.Equal(expectedJObject[i]["extraction_method"], actualJObject[i]["extraction_method"]);

                Assert.True(Math.Abs(Math.Floor(pageHeight - expectedJObject[i]["top"].Value <double>()) - Math.Floor(actualJObject[i]["top"].Value <double>())) < precision);
                Assert.True(Math.Abs(Math.Floor(expectedJObject[i]["left"].Value <double>()) - Math.Floor(actualJObject[i]["left"].Value <double>())) < precision);
                Assert.True(Math.Abs(Math.Floor(expectedJObject[i]["width"].Value <double>()) - Math.Floor(actualJObject[i]["width"].Value <double>())) < precision);
                Assert.True(Math.Abs(Math.Floor(expectedJObject[i]["height"].Value <double>()) - Math.Floor(actualJObject[i]["height"].Value <double>())) < precision);
                Assert.True(Math.Abs(Math.Floor(expectedJObject[i]["right"].Value <double>()) - Math.Floor(actualJObject[i]["right"].Value <double>())) < precision);
                Assert.True(Math.Abs(Math.Floor(pageHeight - expectedJObject[i]["bottom"].Value <double>()) - Math.Floor(actualJObject[i]["bottom"].Value <double>())) < precision);

                var expectedData = (JArray)expectedJObject[i]["data"];
                var actualData   = (JArray)actualJObject[i]["data"];
                Assert.Equal(expectedData.Count, actualData.Count);

                for (int r = 0; r < expectedData.Count; r++)
                {
                    var rowExpected = (JArray)expectedData[r];
                    var rowActual   = (JArray)actualData[r];
                    Assert.Equal(rowExpected.Count, rowActual.Count);

                    for (int c = 0; c < rowExpected.Count; c++)
                    {
                        var cellExpected = (JObject)rowExpected[c];
                        var cellActual   = (JObject)rowActual[c];

                        if (string.IsNullOrEmpty(cellExpected["text"].Value <string>()))
                        {
                            continue;                                                             // empty cell have no coordinate data???
                        }
                        Assert.True(Math.Abs(Math.Floor(pageHeight - cellExpected["top"].Value <double>()) - Math.Floor(cellActual["top"].Value <double>())) < precision);
                        Assert.True(Math.Abs(Math.Floor(cellExpected["left"].Value <double>()) - Math.Floor(cellActual["left"].Value <double>())) < precision);
                        Assert.True(Math.Abs(Math.Floor(cellExpected["width"].Value <double>()) - Math.Floor(cellActual["width"].Value <double>())) < precision);
                        Assert.True(Math.Abs(Math.Floor(cellExpected["height"].Value <double>()) - Math.Floor(cellActual["height"].Value <double>())) < precision);
                        Assert.Equal(cellExpected["text"].Value <string>(), cellActual["text"].Value <string>());
                    }
                }
            }
            //Assert.Equal(expectedJson, sb.ToString());
        }
コード例 #3
0
        public void TestJSONSerializeInfinity()
        {
            string   expectedJson = UtilsForTesting.LoadJson("Resources/json/schools.json");
            PageArea page         = UtilsForTesting.GetAreaFromFirstPage("Resources/schools.pdf", new PdfRectangle(double.NaN, double.NaN, double.NaN, double.NaN)); // 53.74f, 16.97f, 548.74f, 762.3f);
            SpreadsheetExtractionAlgorithm sea = new SpreadsheetExtractionAlgorithm();
            Table table = sea.Extract(page)[0];                                                                                                                      //.get(0);

            StringBuilder sb = new StringBuilder();

            (new JSONWriter()).Write(sb, table);
            string s = sb.ToString();

            Assert.Equal(expectedJson, s);
        }
コード例 #4
0
        public void TestJSONWriter()
        {
            string expectedJson = UtilsForTesting.LoadJson("Resources/json/argentina_diputados_voting_record_new.json"); // argentina_diputados_voting_record.json
            Table  table        = this.GetTable();

            using (var stream = new MemoryStream())
                using (var sb = new StreamWriter(stream)
                {
                    AutoFlush = true
                })
                {
                    (new JSONWriter()).Write(sb, table);

                    var reader = new StreamReader(stream);
                    stream.Position = 0;
                    var s = reader.ReadToEnd();

                    Assert.Equal(expectedJson, s);
                }
        }