Unverified Commit 904642f2 authored by Niels Lohmann's avatar Niels Lohmann
Browse files

rename internal_binary_t with binary_t

parent dead99eb
......@@ -1047,6 +1047,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
......
......@@ -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;
......@@ -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';
}
<a target="_blank" href="https://wandbox.org/permlink/EXdpfHah1530TPIE"><b>online</b></a>
\ No newline at end of file
<a target="_blank" href="https://wandbox.org/permlink/qO60NqUznleA1S7v"><b>online</b></a>
\ No newline at end of file
......@@ -6,3 +6,4 @@ false
false
true
false
false
#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';
}
<a target="_blank" href="https://wandbox.org/permlink/xR6eTQqSySLjtpn6"><b>online</b></a>
\ No newline at end of file
false
false
false
false
false
false
false
false
true
......@@ -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;
......@@ -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';
}
<a target="_blank" href="https://wandbox.org/permlink/7sniyNPobbQQdBHJ"><b>online</b></a>
\ No newline at end of file
<a target="_blank" href="https://wandbox.org/permlink/QVbXnPfNZdvuDHwy"><b>online</b></a>
\ No newline at end of file
......@@ -6,3 +6,4 @@ false
false
false
false
false
......@@ -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;
......@@ -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';
}
<a target="_blank" href="https://wandbox.org/permlink/hWqzRJtSjY0cSoQV"><b>online</b></a>
\ No newline at end of file
<a target="_blank" href="https://wandbox.org/permlink/jg033y5pdOFOst14"><b>online</b></a>
\ No newline at end of file
......@@ -6,3 +6,4 @@ false
false
false
false
false
......@@ -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;
......@@ -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';
}
<a target="_blank" href="https://wandbox.org/permlink/r0Z6mhqY20XowAPj"><b>online</b></a>
\ No newline at end of file
<a target="_blank" href="https://wandbox.org/permlink/uDLxYO1TseoNS5Iu"><b>online</b></a>
\ No newline at end of file
......@@ -6,3 +6,4 @@ false
false
false
false
false
......@@ -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;
......@@ -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';
}
<a target="_blank" href="https://wandbox.org/permlink/e0RMOkVT4QrwhPV9"><b>online</b></a>
\ No newline at end of file
<a target="_blank" href="https://wandbox.org/permlink/a7j4EG9Hvjbby3x0"><b>online</b></a>
\ No newline at end of file
......@@ -6,3 +6,4 @@ true
false
false
false
false
......@@ -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;
......@@ -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';
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment