Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpret instances as JSONL in JSON Toolkit #2

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions implementations/jsontoolkit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ add_subdirectory(../../dist/temp/jsontoolkit/repo jsontoolkit)
add_executable(jsontoolkit_benchmark main.cc)
noa_add_default_options(PRIVATE jsontoolkit_benchmark)
target_link_libraries(jsontoolkit_benchmark PRIVATE sourcemeta::jsontoolkit::json)
target_link_libraries(jsontoolkit_benchmark PRIVATE sourcemeta::jsontoolkit::jsonl)
target_link_libraries(jsontoolkit_benchmark PRIVATE sourcemeta::jsontoolkit::jsonschema)
22 changes: 16 additions & 6 deletions implementations/jsontoolkit/main.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <sourcemeta/jsontoolkit/json.h>
#include <sourcemeta/jsontoolkit/jsonl.h>
#include <sourcemeta/jsontoolkit/jsonschema.h>

#include <chrono>
#include <iostream>
#include <filesystem>
#include <vector>

int main(int argc, char **argv) {
if (argc < 2) {
Expand All @@ -13,21 +15,29 @@ int main(int argc, char **argv) {

const std::filesystem::path example{argv[1]};
const auto schema{sourcemeta::jsontoolkit::from_file(example / "schema.json")};
const auto instance{sourcemeta::jsontoolkit::from_file(example / "instances.jsonl")};
auto stream{sourcemeta::jsontoolkit::read_file(example / "instances.jsonl")};
std::vector<sourcemeta::jsontoolkit::JSON> instances;
for (const auto &instance : sourcemeta::jsontoolkit::JSONL{stream}) {
instances.push_back(instance);
}

const auto schema_template{sourcemeta::jsontoolkit::compile(
schema, sourcemeta::jsontoolkit::default_schema_walker,
sourcemeta::jsontoolkit::official_resolver,
sourcemeta::jsontoolkit::default_schema_compiler)};

const auto timestamp_start{std::chrono::high_resolution_clock::now()};
const auto result{sourcemeta::jsontoolkit::evaluate(
schema_template, instance)};
const auto timestamp_end{std::chrono::high_resolution_clock::now()};
if (!result) {
return EXIT_FAILURE;

for (const auto &instance : instances) {
const auto result{sourcemeta::jsontoolkit::evaluate(
schema_template, instance)};
if (!result) {
return EXIT_FAILURE;
}
}

const auto timestamp_end{std::chrono::high_resolution_clock::now()};

const auto duration{
std::chrono::duration_cast<std::chrono::nanoseconds>(timestamp_end - timestamp_start)};
std::cout << duration.count() << "\n";
Expand Down