-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Juan Cruz Viotti <[email protected]>
- Loading branch information
Showing
16 changed files
with
347 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
if(NOT JSONBinPack_FOUND) | ||
set(JSONBINPACK_INSTALL OFF CACHE BOOL "disable installation") | ||
set(JSONBINPACK_RUNTIME OFF CACHE BOOL "disable the JSON BinPack runtime module") | ||
add_subdirectory("${PROJECT_SOURCE_DIR}/vendor/jsonbinpack") | ||
set(JSONBinPack_FOUND ON) | ||
endif() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
Decode | ||
====== | ||
|
||
```sh | ||
jsonschema decode <output.binpack> <output.json> | ||
``` | ||
|
||
This command decodes a JSON document using [JSON | ||
BinPack](https://jsonbinpack.sourcemeta.com) schema-less mode. **Note this | ||
command is considered experimental and might not decode binary files produced | ||
by other versions of this CLI**. | ||
|
||
Examples | ||
-------- | ||
|
||
For example, consider the following encoded file: | ||
|
||
``` | ||
$ xxd output.binpack | ||
00000000: 1308 7665 7273 696f 6e37 02 ..version7. | ||
``` | ||
|
||
Decoding this file using JSON BinPack will result in the following document: | ||
|
||
```json | ||
{ | ||
"version": 2.0 | ||
} | ||
``` | ||
|
||
### Decode a binary file | ||
|
||
```sh | ||
jsonschema decode path/to/output.binpack path/to/my/output.json | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
Encode | ||
====== | ||
|
||
```sh | ||
jsonschema encode <document.json> <output.binpack> | ||
``` | ||
|
||
This command encodes a JSON document using [JSON | ||
BinPack](https://jsonbinpack.sourcemeta.com) schema-less mode. **Note this | ||
command is considered experimental and its output might not be decodable across | ||
versions of this CLI**. | ||
|
||
Examples | ||
-------- | ||
|
||
For example, consider the following simple document: | ||
|
||
```json | ||
{ | ||
"version": 2.0 | ||
} | ||
``` | ||
|
||
The JSON BinPack schema-less encoding will result in something like this: | ||
|
||
``` | ||
$ xxd output.binpack | ||
00000000: 1308 7665 7273 696f 6e37 02 ..version7. | ||
``` | ||
|
||
### Encode a JSON document | ||
|
||
```sh | ||
jsonschema encode path/to/my/document.json path/to/output.binpack | ||
``` |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <sourcemeta/jsonbinpack/compiler.h> | ||
#include <sourcemeta/jsonbinpack/runtime.h> | ||
#include <sourcemeta/jsontoolkit/json.h> | ||
#include <sourcemeta/jsontoolkit/jsonschema.h> | ||
|
||
#include <cassert> // assert | ||
#include <cstdlib> // EXIT_SUCCESS | ||
#include <filesystem> // std::filesystem | ||
#include <fstream> // std::ifstream | ||
#include <iostream> // std::cout, std::endl | ||
|
||
#include "command.h" | ||
#include "utils.h" | ||
|
||
auto sourcemeta::jsonschema::cli::decode( | ||
const std::span<const std::string> &arguments) -> int { | ||
const auto options{parse_options(arguments, {})}; | ||
|
||
if (options.at("").size() < 2) { | ||
std::cerr | ||
<< "error: This command expects a path to a binary file and an " | ||
"output path. For example:\n\n" | ||
<< " jsonschema decode path/to/output.binpack path/to/document.json\n"; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
// TODO: Take a real schema as argument | ||
auto schema{sourcemeta::jsontoolkit::parse(R"JSON({ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema" | ||
})JSON")}; | ||
|
||
sourcemeta::jsonbinpack::compile( | ||
schema, sourcemeta::jsontoolkit::default_schema_walker, | ||
resolver(options, options.contains("h") || options.contains("http"))); | ||
const auto encoding{sourcemeta::jsonbinpack::load(schema)}; | ||
|
||
std::ifstream input_stream{std::filesystem::canonical(options.at("").front()), | ||
std::ios::binary}; | ||
input_stream.exceptions(std::ifstream::failbit | std::ifstream::badbit); | ||
assert(!input_stream.fail()); | ||
assert(input_stream.is_open()); | ||
sourcemeta::jsonbinpack::Decoder decoder{input_stream}; | ||
const auto document{decoder.read(encoding)}; | ||
|
||
std::ofstream output_stream( | ||
std::filesystem::weakly_canonical(options.at("").at(1)), | ||
std::ios::binary); | ||
output_stream.exceptions(std::ios_base::badbit); | ||
sourcemeta::jsontoolkit::prettify( | ||
document, output_stream, sourcemeta::jsontoolkit::schema_format_compare); | ||
output_stream << "\n"; | ||
output_stream.flush(); | ||
output_stream.close(); | ||
return EXIT_SUCCESS; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include <sourcemeta/jsonbinpack/compiler.h> | ||
#include <sourcemeta/jsonbinpack/runtime.h> | ||
#include <sourcemeta/jsontoolkit/json.h> | ||
#include <sourcemeta/jsontoolkit/jsonschema.h> | ||
|
||
#include <cstdlib> // EXIT_SUCCESS | ||
#include <filesystem> // std::filesystem | ||
#include <fstream> // std::ofstream | ||
#include <iostream> // std::cout, std::endl | ||
|
||
#include "command.h" | ||
#include "utils.h" | ||
|
||
auto sourcemeta::jsonschema::cli::encode( | ||
const std::span<const std::string> &arguments) -> int { | ||
const auto options{parse_options(arguments, {})}; | ||
|
||
if (options.at("").size() < 2) { | ||
std::cerr | ||
<< "error: This command expects a path to a JSON document and an " | ||
"output path. For example:\n\n" | ||
<< " jsonschema encode path/to/document.json path/to/output.binpack\n"; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
// TODO: Take a real schema as argument | ||
auto schema{sourcemeta::jsontoolkit::parse(R"JSON({ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema" | ||
})JSON")}; | ||
|
||
sourcemeta::jsonbinpack::compile( | ||
schema, sourcemeta::jsontoolkit::default_schema_walker, | ||
resolver(options, options.contains("h") || options.contains("http"))); | ||
const auto encoding{sourcemeta::jsonbinpack::load(schema)}; | ||
|
||
const auto document{ | ||
sourcemeta::jsontoolkit::from_file(options.at("").front())}; | ||
|
||
std::ofstream output_stream( | ||
std::filesystem::weakly_canonical(options.at("").at(1)), | ||
std::ios::binary); | ||
output_stream.exceptions(std::ios_base::badbit); | ||
sourcemeta::jsonbinpack::Encoder encoder{output_stream}; | ||
encoder.write(document, encoding); | ||
output_stream.flush(); | ||
const auto size{output_stream.tellp()}; | ||
output_stream.close(); | ||
std::cerr << "size: " << size << " bytes\n"; | ||
return EXIT_SUCCESS; | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/sh | ||
|
||
set -o errexit | ||
set -o nounset | ||
|
||
TMP="$(mktemp -d)" | ||
clean() { rm -rf "$TMP"; } | ||
trap clean EXIT | ||
|
||
cat << 'EOF' > "$TMP/document.json" | ||
{ "version": 2.0 } | ||
EOF | ||
|
||
"$1" decode 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?" | ||
test "$CODE" = "1" || exit 1 | ||
|
||
cat << 'EOF' > "$TMP/expected.txt" | ||
error: This command expects a path to a binary file and an output path. For example: | ||
jsonschema decode path/to/output.binpack path/to/document.json | ||
EOF | ||
|
||
diff "$TMP/stderr.txt" "$TMP/expected.txt" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/bin/sh | ||
|
||
set -o errexit | ||
set -o nounset | ||
|
||
TMP="$(mktemp -d)" | ||
clean() { rm -rf "$TMP"; } | ||
trap clean EXIT | ||
|
||
cat << 'EOF' > "$TMP/document.json" | ||
{ "version": 2.0 } | ||
EOF | ||
|
||
"$1" encode "$TMP/document.json" "$TMP/output.binpack" | ||
"$1" decode "$TMP/output.binpack" 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?" | ||
test "$CODE" = "1" || exit 1 | ||
|
||
cat << 'EOF' > "$TMP/expected.txt" | ||
error: This command expects a path to a binary file and an output path. For example: | ||
jsonschema decode path/to/output.binpack path/to/document.json | ||
EOF | ||
|
||
diff "$TMP/stderr.txt" "$TMP/expected.txt" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/sh | ||
|
||
set -o errexit | ||
set -o nounset | ||
|
||
TMP="$(mktemp -d)" | ||
clean() { rm -rf "$TMP"; } | ||
trap clean EXIT | ||
|
||
cat << 'EOF' > "$TMP/document.json" | ||
{ "version": 2.0 } | ||
EOF | ||
|
||
"$1" encode "$TMP/document.json" "$TMP/output.binpack" | ||
"$1" decode "$TMP/output.binpack" "$TMP/decode.json" | ||
|
||
cat << 'EOF' > "$TMP/expected.json" | ||
{ | ||
"version": 2.0 | ||
} | ||
EOF | ||
|
||
diff "$TMP/decode.json" "$TMP/expected.json" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/sh | ||
|
||
set -o errexit | ||
set -o nounset | ||
|
||
TMP="$(mktemp -d)" | ||
clean() { rm -rf "$TMP"; } | ||
trap clean EXIT | ||
|
||
cat << 'EOF' > "$TMP/document.json" | ||
{ "version": 2.0 } | ||
EOF | ||
|
||
"$1" encode 2> "$TMP/stderr.txt" && CODE="$?" || CODE="$?" | ||
test "$CODE" = "1" || exit 1 | ||
|
||
cat << 'EOF' > "$TMP/expected.txt" | ||
error: This command expects a path to a JSON document and an output path. For example: | ||
jsonschema encode path/to/document.json path/to/output.binpack | ||
EOF | ||
|
||
diff "$TMP/stderr.txt" "$TMP/expected.txt" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/sh | ||
|
||
set -o errexit | ||
set -o nounset | ||
|
||
TMP="$(mktemp -d)" | ||
clean() { rm -rf "$TMP"; } | ||
trap clean EXIT | ||
|
||
cat << 'EOF' > "$TMP/document.json" | ||
{ "version": 2.0 } | ||
EOF | ||
|
||
"$1" encode "$TMP/document.json" 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?" | ||
test "$CODE" = "1" || exit 1 | ||
|
||
cat << 'EOF' > "$TMP/expected.txt" | ||
error: This command expects a path to a JSON document and an output path. For example: | ||
jsonschema encode path/to/document.json path/to/output.binpack | ||
EOF | ||
|
||
diff "$TMP/stderr.txt" "$TMP/expected.txt" |
Oops, something went wrong.