Last active
October 25, 2023 20:44
-
-
Save lemire/90f1476bc6ab904e5ce1831552848792 to your computer and use it in GitHub Desktop.
demosimdjson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "simdjson.h" | |
#include <fstream> | |
#include <iostream> | |
#include <sstream> | |
#include <string> | |
using namespace simdjson; | |
std::string read_file(const std::string &filename) { | |
std::ifstream file(filename); | |
std::stringstream buffer; | |
buffer << file.rdbuf(); | |
return buffer.str(); | |
} | |
void GetPackageJSON(std::string input) { | |
size_t start = 0; | |
input.reserve(input.size() + simdjson::SIMDJSON_PADDING); | |
std::string_view json_string(input); | |
if (input.size() >= 3 && 0 == memcmp(input.data(), "\xEF\xBB\xBF", 3)) { | |
json_string.remove_prefix(3); // Skip UTF-8 BOM. | |
} | |
simdjson::ondemand::parser parser; | |
simdjson::ondemand::document document; | |
simdjson::ondemand::object main_object; | |
simdjson::error_code error = | |
parser.iterate(json_string, input.capacity() - start).get(document); | |
if (error) { | |
std::cout << "error" << error << std::endl; | |
abort(); | |
} | |
if (error || document.get_object().get(main_object)) { | |
std::cout << "error" << std::endl; | |
abort(); | |
} | |
simdjson::ondemand::raw_json_string key; | |
simdjson::ondemand::value value; | |
for (auto field : main_object) { | |
// Throw error if getting key or value fails. | |
if (field.key().get(key) || field.value().get(value)) { | |
abort(); | |
} | |
if (key == "name") { | |
std::string_view name; | |
if (!value.get_string().get(name)) { | |
std::cout << std::string(name) << std::endl; | |
} | |
} else if (key == "main") { | |
std::string_view main; | |
if (!value.get_string().get(main)) { | |
std::cout << std::string(main) << std::endl; | |
} | |
} else if (key == "exports") { | |
simdjson::ondemand::json_type exports_type; | |
if (!value.type().get(exports_type)) { | |
std::string_view exports; | |
switch (exports_type) { | |
case simdjson::ondemand::json_type::object: { | |
simdjson::ondemand::object exports_object; | |
if (!value.get_object().get(exports_object) && | |
!exports_object.raw_json().get(exports)) { | |
std::cout << std::string(exports) << std::endl; | |
} | |
break; | |
} | |
case simdjson::ondemand::json_type::array: { | |
simdjson::ondemand::array exports_array; | |
if (!value.get_array().get(exports_array) && | |
!exports_array.raw_json().get(exports)) { | |
std::cout << std::string(exports) << std::endl; | |
} | |
break; | |
} | |
case simdjson::ondemand::json_type::string: { | |
if (!value.get_string().get(exports)) { | |
std::cout << std::string(exports) << std::endl; | |
} | |
break; | |
} | |
default: | |
break; | |
} | |
} | |
} else if (key == "imports") { | |
simdjson::ondemand::json_type imports_type; | |
if (!value.type().get(imports_type)) { | |
std::string_view imports; | |
switch (imports_type) { | |
case simdjson::ondemand::json_type::object: { | |
simdjson::ondemand::object imports_object; | |
if (!value.get_object().get(imports_object) && | |
!imports_object.raw_json().get(imports)) { | |
std::cout << std::string(imports) << std::endl; | |
} | |
break; | |
} | |
case simdjson::ondemand::json_type::array: { | |
simdjson::ondemand::array imports_array; | |
if (!value.get_array().get(imports_array) && | |
!imports_array.raw_json().get(imports)) { | |
std::cout << std::string(imports) << std::endl; | |
} | |
break; | |
} | |
case simdjson::ondemand::json_type::string: { | |
if (!value.get_string().get(imports)) { | |
std::cout << std::string(imports) << std::endl; | |
} | |
break; | |
} | |
default: | |
break; | |
} | |
} | |
} else if (key == "type") { | |
std::string_view type; | |
// Only update type if it is "commonjs" or "module". | |
if (!value.get_string().get(type) && | |
(type == "commonjs" || type == "module")) { | |
std::cout << std::string(type) << std::endl; | |
} | |
} | |
} | |
} | |
int main(int arc, char *argv[]) { GetPackageJSON(read_file(argv[1])); } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment