|
| 1 | + |
| 2 | +#include "jsonparser/jsonparser.h" |
| 3 | + |
| 4 | + |
| 5 | +// #define RAPIDJSON_SSE2 // bad |
| 6 | +// #define RAPIDJSON_SSE42 // bad |
| 7 | +#include "rapidjson/document.h" |
| 8 | +#include "rapidjson/reader.h" // you have to check in the submodule |
| 9 | +#include "rapidjson/stringbuffer.h" |
| 10 | +#include "rapidjson/writer.h" |
| 11 | + |
| 12 | +#include "sajson.h" |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +using namespace rapidjson; |
| 18 | +using namespace std; |
| 19 | + |
| 20 | +int main(int argc, char *argv[]) { |
| 21 | + if (argc < 2) { |
| 22 | + cerr << "Usage: " << argv[0] << " <jsonfile>\n"; |
| 23 | + cerr << "Or " << argv[0] << " -v <jsonfile>\n"; |
| 24 | + exit(1); |
| 25 | + } |
| 26 | + bool verbose = false; |
| 27 | + if (argc > 2) { |
| 28 | + if (strcmp(argv[1], "-v")) |
| 29 | + verbose = true; |
| 30 | + } |
| 31 | + pair<u8 *, size_t> p = get_corpus(argv[argc - 1]); |
| 32 | + if (verbose) { |
| 33 | + std::cout << "Input has "; |
| 34 | + if (p.second > 1024 * 1024) |
| 35 | + std::cout << p.second / (1024 * 1024) << " MB "; |
| 36 | + else if (p.second > 1024) |
| 37 | + std::cout << p.second / 1024 << " KB "; |
| 38 | + else |
| 39 | + std::cout << p.second << " B "; |
| 40 | + std::cout << std::endl; |
| 41 | + } |
| 42 | + ParsedJson *pj_ptr = allocate_ParsedJson(p.second); |
| 43 | + if (pj_ptr == NULL) { |
| 44 | + std::cerr << "can't allocate memory" << std::endl; |
| 45 | + return EXIT_FAILURE; |
| 46 | + } |
| 47 | + ParsedJson &pj(*pj_ptr); |
| 48 | + |
| 49 | + bool ours_correct = json_parse(p.first, p.second, pj); |
| 50 | + |
| 51 | + rapidjson::Document d; |
| 52 | + |
| 53 | + char *buffer = (char *)malloc(p.second + 1); |
| 54 | + memcpy(buffer, p.first, p.second); |
| 55 | + buffer[p.second] = '\0'; |
| 56 | + bool rapid_correct = (d.Parse((const char *)buffer).HasParseError() == false); |
| 57 | + bool rapid_correct_checkencoding = (d.Parse<kParseValidateEncodingFlag>((const char *)buffer).HasParseError() == false); |
| 58 | + bool sajson_correct = sajson::parse(sajson::dynamic_allocation(), sajson::mutable_string_view(p.second, buffer)).is_valid(); |
| 59 | + printf("our parser : %s \n", ours_correct ? "correct":"invalid"); |
| 60 | + printf("rapid : %s \n", rapid_correct ? "correct":"invalid"); |
| 61 | + printf("rapid (check encoding) : %s \n", rapid_correct_checkencoding ? "correct":"invalid"); |
| 62 | + printf("sajson : %s \n", sajson_correct ? "correct":"invalid"); |
| 63 | + |
| 64 | + free(buffer); |
| 65 | + free(p.first); |
| 66 | + deallocate_ParsedJson(pj_ptr); |
| 67 | + return EXIT_SUCCESS; |
| 68 | +} |
0 commit comments