2018-11-28 03:37:59 +08:00
|
|
|
#include <unistd.h>
|
2018-10-18 00:00:44 +08:00
|
|
|
|
2020-03-03 06:23:19 +08:00
|
|
|
#include "simdjson.h"
|
2018-10-18 00:00:44 +08:00
|
|
|
|
2020-03-28 01:52:13 +08:00
|
|
|
SIMDJSON_PUSH_DISABLE_ALL_WARNINGS
|
|
|
|
|
2018-10-18 00:00:44 +08:00
|
|
|
// #define RAPIDJSON_SSE2 // bad
|
|
|
|
// #define RAPIDJSON_SSE42 // bad
|
2019-05-10 08:51:33 +08:00
|
|
|
#include "fastjson.cpp"
|
|
|
|
#include "fastjson_dom.cpp"
|
|
|
|
#include "gason.cpp"
|
|
|
|
#include "json11.cpp"
|
2018-10-18 00:00:44 +08:00
|
|
|
#include "rapidjson/document.h"
|
|
|
|
#include "rapidjson/reader.h" // you have to check in the submodule
|
|
|
|
#include "rapidjson/stringbuffer.h"
|
|
|
|
#include "rapidjson/writer.h"
|
|
|
|
#include "sajson.h"
|
2019-05-10 08:51:33 +08:00
|
|
|
extern "C" {
|
2019-01-18 06:24:29 +08:00
|
|
|
#include "cJSON.c"
|
2019-05-10 08:51:33 +08:00
|
|
|
#include "cJSON.h"
|
2019-01-18 06:24:29 +08:00
|
|
|
#include "jsmn.c"
|
2019-05-10 08:51:33 +08:00
|
|
|
#include "jsmn.h"
|
|
|
|
#include "ujdecode.h"
|
|
|
|
#include "ultrajsondec.c"
|
2018-11-21 09:08:02 +08:00
|
|
|
}
|
2019-01-25 03:28:26 +08:00
|
|
|
#include "jsoncpp.cpp"
|
2019-05-10 08:51:33 +08:00
|
|
|
#include "json/json.h"
|
2018-10-18 00:00:44 +08:00
|
|
|
|
2020-03-28 01:52:13 +08:00
|
|
|
SIMDJSON_POP_DISABLE_WARNINGS
|
|
|
|
|
2018-11-21 09:08:02 +08:00
|
|
|
// fastjson has a tricky interface
|
2020-03-28 01:52:13 +08:00
|
|
|
void on_json_error(void *, UNUSED const fastjson::ErrorContext &ec) {
|
2019-05-10 08:51:33 +08:00
|
|
|
// std::cerr<<"ERROR: "<<ec.mesg<<std::endl;
|
2018-11-21 09:08:02 +08:00
|
|
|
}
|
|
|
|
bool fastjson_parse(const char *input) {
|
|
|
|
fastjson::Token token;
|
|
|
|
fastjson::dom::Chunk chunk;
|
2019-05-10 08:51:33 +08:00
|
|
|
return fastjson::dom::parse_string(input, &token, &chunk, 0, &on_json_error,
|
|
|
|
NULL);
|
2018-11-21 09:08:02 +08:00
|
|
|
}
|
|
|
|
// end of fastjson stuff
|
2018-10-18 00:00:44 +08:00
|
|
|
|
|
|
|
using namespace rapidjson;
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2018-11-28 03:37:59 +08:00
|
|
|
bool verbose = false;
|
2019-07-31 05:18:10 +08:00
|
|
|
bool just_favorites = false;
|
2019-05-10 08:51:33 +08:00
|
|
|
int c;
|
|
|
|
while ((c = getopt(argc, argv, "vm")) != -1)
|
|
|
|
switch (c) {
|
|
|
|
case 'v':
|
|
|
|
verbose = true;
|
|
|
|
break;
|
|
|
|
case 'm':
|
2019-07-31 05:18:10 +08:00
|
|
|
just_favorites = true;
|
2019-05-10 08:51:33 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
2018-11-28 03:37:59 +08:00
|
|
|
if (optind >= argc) {
|
2019-05-10 05:59:51 +08:00
|
|
|
std::cerr << "Usage: " << argv[0] << " <jsonfile>" << std::endl;
|
|
|
|
std::cerr << "Or " << argv[0] << " -v <jsonfile>" << std::endl;
|
2018-10-18 00:00:44 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2019-05-10 08:51:33 +08:00
|
|
|
const char *filename = argv[optind];
|
2020-03-28 01:52:13 +08:00
|
|
|
auto [p, loaderr] = simdjson::padded_string::load(filename);
|
|
|
|
if (loaderr) {
|
|
|
|
std::cerr << "Could not load the file " << filename << ": " << loaderr << std::endl;
|
2018-11-28 03:37:59 +08:00
|
|
|
return EXIT_FAILURE;
|
2018-10-18 00:00:44 +08:00
|
|
|
}
|
|
|
|
if (verbose) {
|
|
|
|
std::cout << "Input has ";
|
2018-12-01 09:27:16 +08:00
|
|
|
if (p.size() > 1024 * 1024)
|
|
|
|
std::cout << p.size() / (1024 * 1024) << " MB ";
|
|
|
|
else if (p.size() > 1024)
|
|
|
|
std::cout << p.size() / 1024 << " KB ";
|
2018-10-18 00:00:44 +08:00
|
|
|
else
|
2018-12-01 09:27:16 +08:00
|
|
|
std::cout << p.size() << " B ";
|
2018-10-18 00:00:44 +08:00
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
2020-03-29 02:43:41 +08:00
|
|
|
simdjson::dom::parser parser;
|
2020-04-15 08:26:26 +08:00
|
|
|
auto err = parser.parse(p).error();
|
2018-10-18 00:00:44 +08:00
|
|
|
|
|
|
|
rapidjson::Document d;
|
|
|
|
|
2018-12-01 09:27:16 +08:00
|
|
|
char *buffer = (char *)malloc(p.size() + 1);
|
2018-12-01 10:31:05 +08:00
|
|
|
memcpy(buffer, p.data(), p.size());
|
2018-12-01 09:27:16 +08:00
|
|
|
buffer[p.size()] = '\0';
|
2019-05-10 08:51:33 +08:00
|
|
|
bool rapid_correct_checkencoding =
|
|
|
|
(d.Parse<kParseValidateEncodingFlag>((const char *)buffer)
|
|
|
|
.HasParseError() == false);
|
|
|
|
bool sajson_correct =
|
|
|
|
sajson::parse(sajson::dynamic_allocation(),
|
|
|
|
sajson::mutable_string_view(p.size(), buffer))
|
|
|
|
.is_valid();
|
2019-07-31 05:18:10 +08:00
|
|
|
if (just_favorites) {
|
2019-05-10 08:51:33 +08:00
|
|
|
printf("our parser : %s \n",
|
2020-03-27 05:15:33 +08:00
|
|
|
(err == simdjson::error_code::SUCCESS) ? "correct" : "invalid");
|
2019-05-10 08:51:33 +08:00
|
|
|
printf("rapid (check encoding) : %s \n",
|
|
|
|
rapid_correct_checkencoding ? "correct" : "invalid");
|
|
|
|
printf("sajson : %s \n",
|
|
|
|
sajson_correct ? "correct" : "invalid");
|
2020-03-27 05:15:33 +08:00
|
|
|
if (err == simdjson::DEPTH_ERROR) {
|
2019-06-27 07:48:51 +08:00
|
|
|
printf("simdjson encountered a DEPTH_ERROR, it was parametrized to "
|
|
|
|
"reject documents with depth exceeding %zu.\n",
|
2020-03-27 05:15:33 +08:00
|
|
|
parser.max_depth());
|
2019-05-10 08:51:33 +08:00
|
|
|
}
|
2020-03-27 05:15:33 +08:00
|
|
|
if (((err == simdjson::error_code::SUCCESS) != rapid_correct_checkencoding) ||
|
2019-05-10 08:51:33 +08:00
|
|
|
(rapid_correct_checkencoding != sajson_correct) ||
|
2020-03-27 05:15:33 +08:00
|
|
|
((err == simdjson::SUCCESS) != sajson_correct)) {
|
2019-05-10 08:51:33 +08:00
|
|
|
printf("WARNING: THEY DISAGREE\n\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
free(buffer);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2018-10-18 00:00:44 +08:00
|
|
|
bool rapid_correct = (d.Parse((const char *)buffer).HasParseError() == false);
|
2019-05-10 08:51:33 +08:00
|
|
|
|
2018-11-21 09:08:02 +08:00
|
|
|
std::string json11err;
|
2019-05-10 08:51:33 +08:00
|
|
|
bool dropbox_correct = ((json11::Json::parse(buffer, json11err).is_null()) ||
|
|
|
|
(!json11err.empty())) == false;
|
|
|
|
bool fastjson_correct = fastjson_parse(buffer);
|
2018-11-21 09:08:02 +08:00
|
|
|
JsonValue value;
|
|
|
|
JsonAllocator allocator;
|
|
|
|
char *endptr;
|
2019-05-10 08:51:33 +08:00
|
|
|
bool gason_correct =
|
|
|
|
(jsonParse(buffer, &endptr, &value, allocator) == JSON_OK);
|
2018-11-21 09:08:02 +08:00
|
|
|
void *state;
|
2019-05-10 08:51:33 +08:00
|
|
|
bool ultrajson_correct =
|
|
|
|
((UJDecode(buffer, p.size(), NULL, &state) == NULL) == false);
|
2019-04-17 10:07:52 +08:00
|
|
|
|
2019-05-10 05:59:51 +08:00
|
|
|
auto tokens = std::make_unique<jsmntok_t[]>(p.size());
|
2019-04-17 10:07:52 +08:00
|
|
|
bool jsmn_correct = false;
|
2019-05-10 08:51:33 +08:00
|
|
|
if (tokens == nullptr) {
|
2019-01-18 06:24:29 +08:00
|
|
|
printf("Failed to alloc memory for jsmn\n");
|
|
|
|
} else {
|
2020-03-27 05:15:33 +08:00
|
|
|
jsmn_parser jsmnparser;
|
|
|
|
jsmn_init(&jsmnparser);
|
2019-01-18 06:24:29 +08:00
|
|
|
memcpy(buffer, p.data(), p.size());
|
|
|
|
buffer[p.size()] = '\0';
|
2020-04-22 06:43:35 +08:00
|
|
|
int r = jsmn_parse(&jsmnparser, buffer, p.size(), tokens.get(), static_cast<unsigned int>(p.size()));
|
2019-02-26 04:03:20 +08:00
|
|
|
tokens = nullptr;
|
2019-01-18 06:24:29 +08:00
|
|
|
jsmn_correct = (r > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buffer, p.data(), p.size());
|
|
|
|
buffer[p.size()] = '\0';
|
2019-05-10 08:51:33 +08:00
|
|
|
cJSON *tree = cJSON_Parse(buffer);
|
2019-01-18 06:24:29 +08:00
|
|
|
bool cjson_correct = (tree != NULL);
|
|
|
|
if (tree != NULL) {
|
2019-05-10 08:51:33 +08:00
|
|
|
cJSON_Delete(tree);
|
2019-01-18 06:24:29 +08:00
|
|
|
}
|
|
|
|
|
2019-01-25 03:28:26 +08:00
|
|
|
Json::CharReaderBuilder b;
|
2019-07-31 05:18:10 +08:00
|
|
|
Json::CharReader *json_cpp_reader = b.newCharReader();
|
2019-01-25 03:28:26 +08:00
|
|
|
Json::Value root;
|
|
|
|
Json::String errs;
|
2019-07-31 05:18:10 +08:00
|
|
|
bool is_json_cpp_ok =
|
|
|
|
json_cpp_reader->parse(buffer, buffer + p.size(), &root, &errs);
|
|
|
|
delete json_cpp_reader;
|
2019-01-18 06:24:29 +08:00
|
|
|
|
2019-05-10 08:51:33 +08:00
|
|
|
printf("our parser : %s \n",
|
2020-03-27 05:15:33 +08:00
|
|
|
(err == simdjson::error_code::SUCCESS) ? "correct" : "invalid");
|
2019-05-10 08:51:33 +08:00
|
|
|
printf("rapid : %s \n",
|
|
|
|
rapid_correct ? "correct" : "invalid");
|
|
|
|
printf("rapid (check encoding) : %s \n",
|
|
|
|
rapid_correct_checkencoding ? "correct" : "invalid");
|
|
|
|
printf("sajson : %s \n",
|
|
|
|
sajson_correct ? "correct" : "invalid");
|
|
|
|
printf("dropbox : %s \n",
|
|
|
|
dropbox_correct ? "correct" : "invalid");
|
|
|
|
printf("fastjson : %s \n",
|
|
|
|
fastjson_correct ? "correct" : "invalid");
|
|
|
|
printf("gason : %s \n",
|
|
|
|
gason_correct ? "correct" : "invalid");
|
|
|
|
printf("ultrajson : %s \n",
|
|
|
|
ultrajson_correct ? "correct" : "invalid");
|
|
|
|
printf("jsmn : %s \n",
|
|
|
|
jsmn_correct ? "correct" : "invalid");
|
|
|
|
printf("cjson : %s \n",
|
|
|
|
cjson_correct ? "correct" : "invalid");
|
|
|
|
printf("jsoncpp : %s \n",
|
2019-07-31 05:18:10 +08:00
|
|
|
is_json_cpp_ok ? "correct" : "invalid");
|
2019-01-18 06:24:29 +08:00
|
|
|
|
2018-10-18 00:00:44 +08:00
|
|
|
free(buffer);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|