File: unsafe_parse_many.cpp

package info (click to toggle)
simdjson 4.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,936 kB
  • sloc: cpp: 171,612; ansic: 19,122; sh: 1,126; python: 842; makefile: 47; ruby: 25; javascript: 13
file content (47 lines) | stat: -rw-r--r-- 1,282 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <string>
#include <vector>
#include <iostream>

#include "simdjson.h"

bool single_document() {
    std::cout << "Running " << __func__ << std::endl;
    simdjson::dom::parser parser;
    simdjson::dom::document_stream stream;

#if COMPILATION_TEST_USE_FAILING_CODE
    auto error = parser.parse_many(json).get(R"({"hello": "world"})"_padded);
#else
    auto json = R"({"hello": "world"})"_padded;
    auto error = parser.parse_many(json).get(stream);
#endif
    if(error) {
        std::cerr << error << std::endl;
        return false;
    }
    size_t count = 0;
    for (auto doc : stream) {
        if(doc.error()) {
          std::cerr << "Unexpected error: " << doc.error() << std::endl;
          return false;
        }
        std::string expected = R"({"hello":"world"})";
        simdjson::dom::element this_document;
        error = doc.get(this_document);
        if(error) {
          std::cerr << error << std::endl;
          return false;
        }
        std::string answer = simdjson::minify(this_document);
        if(answer != expected) {
          std::cout << this_document << std::endl;
          return false;
        }
        count += 1;
    }
    return count == 1;
}

int main() {
    return single_document() ? EXIT_SUCCESS : EXIT_FAILURE;
}