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

Clean up implementation of binary type #2125

Merged
merged 16 commits into from May 20, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/macos.yml
Expand Up @@ -14,4 +14,4 @@ jobs:
- name: build
run: cmake --build build --parallel 10
- name: test
run: cd build ; ctest -j 10
run: cd build ; ctest -j 10 --output-on-failure
2 changes: 1 addition & 1 deletion .github/workflows/ubuntu.yml
Expand Up @@ -14,4 +14,4 @@ jobs:
- name: build
run: cmake --build build --parallel 10
- name: test
run: cd build ; ctest -j 10
run: cd build ; ctest -j 10 --output-on-failure
2 changes: 1 addition & 1 deletion .github/workflows/windows.yml
Expand Up @@ -14,4 +14,4 @@ jobs:
- name: build
run: cmake --build build --parallel 10
- name: test
run: cd build ; ctest -j 10 -C Debug --exclude-regex "test-unicode"
run: cd build ; ctest -j 10 -C Debug --exclude-regex "test-unicode" --output-on-failure
30 changes: 30 additions & 0 deletions README.md
Expand Up @@ -1050,6 +1050,36 @@ std::vector<std::uint8_t> v_ubjson = json::to_ubjson(j);
json j_from_ubjson = json::from_ubjson(v_ubjson);
```

The library also supports binary types from BSON, CBOR (byte strings), and MessagePack (bin, ext, fixext). They are stored by default as `std::vector<std::uint8_t>` to be processed outside of the library.

```cpp
// CBOR byte string with payload 0xCAFE
std::vector<std::uint8_t> v = {0x42, 0xCA, 0xFE};

// read value
json j = json::from_cbor(v);

// the JSON value has type binary
j.is_binary(); // true

// get reference to stored binary value
auto& binary = j.get_binary();

// the binary value has no subtype (CBOR has no binary subtypes)
binary.has_subtype(); // false

// access std::vector<std::uint8_t> member functions
binary.size(); // 2
binary[0]; // 0xCA
binary[1]; // 0xFE

// set subtype to 0x10
binary.set_subtype(0x10);

// serialize to MessagePack
auto cbor = json::to_msgpack(j); // 0xD5 (fixext2), 0x10, 0xCA, 0xFE
```


## Supported compilers

Expand Down
2 changes: 2 additions & 0 deletions doc/examples/is_array.cpp
Expand Up @@ -14,6 +14,7 @@ int main()
json j_object = {{"one", 1}, {"two", 2}};
json j_array = {1, 2, 4, 8, 16};
json j_string = "Hello, world";
json j_binary = json::binary_array({1, 2, 3});

// call is_array()
std::cout << std::boolalpha;
Expand All @@ -25,4 +26,5 @@ int main()
std::cout << j_object.is_array() << '\n';
std::cout << j_array.is_array() << '\n';
std::cout << j_string.is_array() << '\n';
std::cout << j_binary.is_array() << '\n';
}
2 changes: 1 addition & 1 deletion doc/examples/is_array.link
@@ -1 +1 @@
<a target="_blank" href="https://wandbox.org/permlink/EXdpfHah1530TPIE"><b>online</b></a>
<a target="_blank" href="https://wandbox.org/permlink/qO60NqUznleA1S7v"><b>online</b></a>
1 change: 1 addition & 0 deletions doc/examples/is_array.output
Expand Up @@ -6,3 +6,4 @@ false
false
true
false
false
30 changes: 30 additions & 0 deletions doc/examples/is_binary.cpp
@@ -0,0 +1,30 @@
#include <iostream>
#include <nlohmann/json.hpp>

using json = nlohmann::json;

int main()
{
// create JSON values
json j_null;
json j_boolean = true;
json j_number_integer = 17;
json j_number_unsigned_integer = 12345678987654321u;
json j_number_float = 23.42;
json j_object = {{"one", 1}, {"two", 2}};
json j_array = {1, 2, 4, 8, 16};
json j_string = "Hello, world";
json j_binary = json::binary_array({1, 2, 3});

// call is_binary()
std::cout << std::boolalpha;
std::cout << j_null.is_binary() << '\n';
std::cout << j_boolean.is_binary() << '\n';
std::cout << j_number_integer.is_binary() << '\n';
std::cout << j_number_unsigned_integer.is_binary() << '\n';
std::cout << j_number_float.is_binary() << '\n';
std::cout << j_object.is_binary() << '\n';
std::cout << j_array.is_binary() << '\n';
std::cout << j_string.is_binary() << '\n';
std::cout << j_binary.is_binary() << '\n';
}
1 change: 1 addition & 0 deletions doc/examples/is_binary.link
@@ -0,0 +1 @@
<a target="_blank" href="https://wandbox.org/permlink/xR6eTQqSySLjtpn6"><b>online</b></a>
9 changes: 9 additions & 0 deletions doc/examples/is_binary.output
@@ -0,0 +1,9 @@
false
false
false
false
false
false
false
false
true
2 changes: 2 additions & 0 deletions doc/examples/is_boolean.cpp
Expand Up @@ -14,6 +14,7 @@ int main()
json j_object = {{"one", 1}, {"two", 2}};
json j_array = {1, 2, 4, 8, 16};
json j_string = "Hello, world";
json j_binary = json::binary_array({1, 2, 3});

// call is_boolean()
std::cout << std::boolalpha;
Expand All @@ -25,4 +26,5 @@ int main()
std::cout << j_object.is_boolean() << '\n';
std::cout << j_array.is_boolean() << '\n';
std::cout << j_string.is_boolean() << '\n';
std::cout << j_binary.is_boolean() << '\n';
}
2 changes: 1 addition & 1 deletion doc/examples/is_boolean.link
@@ -1 +1 @@
<a target="_blank" href="https://wandbox.org/permlink/7sniyNPobbQQdBHJ"><b>online</b></a>
<a target="_blank" href="https://wandbox.org/permlink/QVbXnPfNZdvuDHwy"><b>online</b></a>
1 change: 1 addition & 0 deletions doc/examples/is_boolean.output
Expand Up @@ -6,3 +6,4 @@ false
false
false
false
false
2 changes: 2 additions & 0 deletions doc/examples/is_discarded.cpp
Expand Up @@ -14,6 +14,7 @@ int main()
json j_object = {{"one", 1}, {"two", 2}};
json j_array = {1, 2, 4, 8, 16};
json j_string = "Hello, world";
json j_binary = json::binary_array({1, 2, 3});

// call is_discarded()
std::cout << std::boolalpha;
Expand All @@ -25,4 +26,5 @@ int main()
std::cout << j_object.is_discarded() << '\n';
std::cout << j_array.is_discarded() << '\n';
std::cout << j_string.is_discarded() << '\n';
std::cout << j_binary.is_discarded() << '\n';
}
2 changes: 1 addition & 1 deletion doc/examples/is_discarded.link
@@ -1 +1 @@
<a target="_blank" href="https://wandbox.org/permlink/hWqzRJtSjY0cSoQV"><b>online</b></a>
<a target="_blank" href="https://wandbox.org/permlink/jg033y5pdOFOst14"><b>online</b></a>
1 change: 1 addition & 0 deletions doc/examples/is_discarded.output
Expand Up @@ -6,3 +6,4 @@ false
false
false
false
false
2 changes: 2 additions & 0 deletions doc/examples/is_null.cpp
Expand Up @@ -14,6 +14,7 @@ int main()
json j_object = {{"one", 1}, {"two", 2}};
json j_array = {1, 2, 4, 8, 16};
json j_string = "Hello, world";
json j_binary = json::binary_array({1, 2, 3});

// call is_null()
std::cout << std::boolalpha;
Expand All @@ -25,4 +26,5 @@ int main()
std::cout << j_object.is_null() << '\n';
std::cout << j_array.is_null() << '\n';
std::cout << j_string.is_null() << '\n';
std::cout << j_binary.is_null() << '\n';
}
2 changes: 1 addition & 1 deletion doc/examples/is_null.link
@@ -1 +1 @@
<a target="_blank" href="https://wandbox.org/permlink/r0Z6mhqY20XowAPj"><b>online</b></a>
<a target="_blank" href="https://wandbox.org/permlink/uDLxYO1TseoNS5Iu"><b>online</b></a>
1 change: 1 addition & 0 deletions doc/examples/is_null.output
Expand Up @@ -6,3 +6,4 @@ false
false
false
false
false
2 changes: 2 additions & 0 deletions doc/examples/is_number.cpp
Expand Up @@ -14,6 +14,7 @@ int main()
json j_object = {{"one", 1}, {"two", 2}};
json j_array = {1, 2, 4, 8, 16};
json j_string = "Hello, world";
json j_binary = json::binary_array({1, 2, 3});

// call is_number()
std::cout << std::boolalpha;
Expand All @@ -25,4 +26,5 @@ int main()
std::cout << j_object.is_number() << '\n';
std::cout << j_array.is_number() << '\n';
std::cout << j_string.is_number() << '\n';
std::cout << j_binary.is_number() << '\n';
}
2 changes: 1 addition & 1 deletion doc/examples/is_number.link
@@ -1 +1 @@
<a target="_blank" href="https://wandbox.org/permlink/e0RMOkVT4QrwhPV9"><b>online</b></a>
<a target="_blank" href="https://wandbox.org/permlink/a7j4EG9Hvjbby3x0"><b>online</b></a>
1 change: 1 addition & 0 deletions doc/examples/is_number.output
Expand Up @@ -6,3 +6,4 @@ true
false
false
false
false
2 changes: 2 additions & 0 deletions doc/examples/is_number_float.cpp
Expand Up @@ -14,6 +14,7 @@ int main()
json j_object = {{"one", 1}, {"two", 2}};
json j_array = {1, 2, 4, 8, 16};
json j_string = "Hello, world";
json j_binary = json::binary_array({1, 2, 3});

// call is_number_float()
std::cout << std::boolalpha;
Expand All @@ -25,4 +26,5 @@ int main()
std::cout << j_object.is_number_float() << '\n';
std::cout << j_array.is_number_float() << '\n';
std::cout << j_string.is_number_float() << '\n';
std::cout << j_binary.is_number_float() << '\n';
}
2 changes: 1 addition & 1 deletion doc/examples/is_number_float.link
@@ -1 +1 @@
<a target="_blank" href="https://wandbox.org/permlink/Thh18DVuOoaiYidD"><b>online</b></a>
<a target="_blank" href="https://wandbox.org/permlink/cT9J60hwwflg88M9"><b>online</b></a>
1 change: 1 addition & 0 deletions doc/examples/is_number_float.output
Expand Up @@ -6,3 +6,4 @@ true
false
false
false
false
2 changes: 2 additions & 0 deletions doc/examples/is_number_integer.cpp
Expand Up @@ -14,6 +14,7 @@ int main()
json j_object = {{"one", 1}, {"two", 2}};
json j_array = {1, 2, 4, 8, 16};
json j_string = "Hello, world";
json j_binary = json::binary_array({1, 2, 3});

// call is_number_integer()
std::cout << std::boolalpha;
Expand All @@ -25,4 +26,5 @@ int main()
std::cout << j_object.is_number_integer() << '\n';
std::cout << j_array.is_number_integer() << '\n';
std::cout << j_string.is_number_integer() << '\n';
std::cout << j_binary.is_number_integer() << '\n';
}
2 changes: 1 addition & 1 deletion doc/examples/is_number_integer.link
@@ -1 +1 @@
<a target="_blank" href="https://wandbox.org/permlink/wFZSC6RswWXwSncb"><b>online</b></a>
<a target="_blank" href="https://wandbox.org/permlink/j0TgXy0oyXxKkhLN"><b>online</b></a>
1 change: 1 addition & 0 deletions doc/examples/is_number_integer.output
Expand Up @@ -6,3 +6,4 @@ false
false
false
false
false
2 changes: 2 additions & 0 deletions doc/examples/is_number_unsigned.cpp
Expand Up @@ -14,6 +14,7 @@ int main()
json j_object = {{"one", 1}, {"two", 2}};
json j_array = {1, 2, 4, 8, 16};
json j_string = "Hello, world";
json j_binary = json::binary_array({1, 2, 3});

// call is_number_unsigned()
std::cout << std::boolalpha;
Expand All @@ -25,4 +26,5 @@ int main()
std::cout << j_object.is_number_unsigned() << '\n';
std::cout << j_array.is_number_unsigned() << '\n';
std::cout << j_string.is_number_unsigned() << '\n';
std::cout << j_binary.is_number_unsigned() << '\n';
}
2 changes: 1 addition & 1 deletion doc/examples/is_number_unsigned.link
@@ -1 +1 @@
<a target="_blank" href="https://wandbox.org/permlink/ajo1F1VJwoszcD7Y"><b>online</b></a>
<a target="_blank" href="https://wandbox.org/permlink/u5wlpVX9Za6lEC2f"><b>online</b></a>
1 change: 1 addition & 0 deletions doc/examples/is_number_unsigned.output
Expand Up @@ -6,3 +6,4 @@ false
false
false
false
false
2 changes: 2 additions & 0 deletions doc/examples/is_object.cpp
Expand Up @@ -14,6 +14,7 @@ int main()
json j_object = {{"one", 1}, {"two", 2}};
json j_array = {1, 2, 4, 8, 16};
json j_string = "Hello, world";
json j_binary = json::binary_array({1, 2, 3});

// call is_object()
std::cout << std::boolalpha;
Expand All @@ -25,4 +26,5 @@ int main()
std::cout << j_object.is_object() << '\n';
std::cout << j_array.is_object() << '\n';
std::cout << j_string.is_object() << '\n';
std::cout << j_binary.is_object() << '\n';
}
2 changes: 1 addition & 1 deletion doc/examples/is_object.link
@@ -1 +1 @@
<a target="_blank" href="https://wandbox.org/permlink/ojKk5AMVK9xF9bmQ"><b>online</b></a>
<a target="_blank" href="https://wandbox.org/permlink/QJQ2avqtJEd4uI23"><b>online</b></a>
1 change: 1 addition & 0 deletions doc/examples/is_object.output
Expand Up @@ -6,3 +6,4 @@ false
true
false
false
false
2 changes: 2 additions & 0 deletions doc/examples/is_primitive.cpp
Expand Up @@ -14,6 +14,7 @@ int main()
json j_object = {{"one", 1}, {"two", 2}};
json j_array = {1, 2, 4, 8, 16};
json j_string = "Hello, world";
json j_binary = json::binary_array({1, 2, 3});

// call is_primitive()
std::cout << std::boolalpha;
Expand All @@ -25,4 +26,5 @@ int main()
std::cout << j_object.is_primitive() << '\n';
std::cout << j_array.is_primitive() << '\n';
std::cout << j_string.is_primitive() << '\n';
std::cout << j_binary.is_primitive() << '\n';
}
2 changes: 1 addition & 1 deletion doc/examples/is_primitive.link
@@ -1 +1 @@
<a target="_blank" href="https://wandbox.org/permlink/B7F0eMkW0EKdZGcC"><b>online</b></a>
<a target="_blank" href="https://wandbox.org/permlink/a4WQ1RXZbD1YQELx"><b>online</b></a>
1 change: 1 addition & 0 deletions doc/examples/is_primitive.output
Expand Up @@ -6,3 +6,4 @@ true
false
false
true
true
2 changes: 2 additions & 0 deletions doc/examples/is_string.cpp
Expand Up @@ -14,6 +14,7 @@ int main()
json j_object = {{"one", 1}, {"two", 2}};
json j_array = {1, 2, 4, 8, 16};
json j_string = "Hello, world";
json j_binary = json::binary_array({1, 2, 3});

// call is_string()
std::cout << std::boolalpha;
Expand All @@ -25,4 +26,5 @@ int main()
std::cout << j_object.is_string() << '\n';
std::cout << j_array.is_string() << '\n';
std::cout << j_string.is_string() << '\n';
std::cout << j_binary.is_string() << '\n';
}
2 changes: 1 addition & 1 deletion doc/examples/is_string.link
@@ -1 +1 @@
<a target="_blank" href="https://wandbox.org/permlink/2Iq9wtaxEvrb5B68"><b>online</b></a>
<a target="_blank" href="https://wandbox.org/permlink/vj3Wo1roaNjE3fPo"><b>online</b></a>
1 change: 1 addition & 0 deletions doc/examples/is_string.output
Expand Up @@ -6,3 +6,4 @@ false
false
false
true
false
2 changes: 2 additions & 0 deletions doc/examples/is_structured.cpp
Expand Up @@ -14,6 +14,7 @@ int main()
json j_object = {{"one", 1}, {"two", 2}};
json j_array = {1, 2, 4, 8, 16};
json j_string = "Hello, world";
json j_binary = json::binary_array({1, 2, 3});

// call is_structured()
std::cout << std::boolalpha;
Expand All @@ -25,4 +26,5 @@ int main()
std::cout << j_object.is_structured() << '\n';
std::cout << j_array.is_structured() << '\n';
std::cout << j_string.is_structured() << '\n';
std::cout << j_binary.is_structured() << '\n';
}
2 changes: 1 addition & 1 deletion doc/examples/is_structured.link
@@ -1 +1 @@
<a target="_blank" href="https://wandbox.org/permlink/44jkAs0G7D0XB24j"><b>online</b></a>
<a target="_blank" href="https://wandbox.org/permlink/BoS03RLCyI6oDMbc"><b>online</b></a>
1 change: 1 addition & 0 deletions doc/examples/is_structured.output
Expand Up @@ -6,3 +6,4 @@ false
true
true
false
false
2 changes: 1 addition & 1 deletion doc/examples/sax_parse.cpp
Expand Up @@ -79,7 +79,7 @@ class sax_event_consumer : public json::json_sax_t
return true;
}

bool binary(binary_t& val) override
bool binary(json::binary_t& val) override
{
events.push_back("binary");
return true;
Expand Down