Skip to content

Commit b71f82b

Browse files
Added support for UBJSON; resolves #245 (#251)
1 parent 716de7b commit b71f82b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+2277
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
*.flexbuf
4949
*.msgpack
5050
*.toml
51+
*.ubjson
5152
*.xml
5253
*.yml
5354
*.yaml

CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ option(REFLECTCPP_FLEXBUFFERS "Enable flexbuffers support" OFF)
99
option(REFLECTCPP_MSGPACK "Enable msgpack support" OFF)
1010
option(REFLECTCPP_XML "Enable XML support" OFF)
1111
option(REFLECTCPP_TOML "Enable TOML support" OFF)
12+
option(REFLECTCPP_UBJSON "Enable UBJSON support" OFF)
1213
option(REFLECTCPP_YAML "Enable YAML support" OFF)
1314

1415
option(REFLECTCPP_BUILD_BENCHMARKS "Build benchmarks" OFF)
@@ -25,11 +26,12 @@ if(REFLECTCPP_BUILD_BENCHMARKS)
2526
set(REFLECTCPP_MSGPACK ON CACHE BOOL "" FORCE)
2627
set(REFLECTCPP_XML ON CACHE BOOL "" FORCE)
2728
set(REFLECTCPP_TOML ON CACHE BOOL "" FORCE)
29+
set(REFLECTCPP_UBJSON ON CACHE BOOL "" FORCE)
2830
set(REFLECTCPP_YAML ON CACHE BOOL "" FORCE)
2931
endif()
3032
if (REFLECTCPP_BUILD_TESTS OR REFLECTCPP_BUILD_BENCHMARKS OR
3133
(REFLECTCPP_JSON AND NOT REFLECTCPP_USE_BUNDLED_DEPENDENCIES) OR
32-
REFLECTCPP_BSON OR REFLECTCPP_CBOR OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_MSGPACK OR REFLECTCPP_XML OR REFLECTCPP_TOML OR REFLECTCPP_YAML)
34+
REFLECTCPP_BSON OR REFLECTCPP_CBOR OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_MSGPACK OR REFLECTCPP_XML OR REFLECTCPP_TOML OR REFLECTCPP_UBJSON OR REFLECTCPP_YAML)
3335
# enable vcpkg per default if require features other than JSON
3436
set(REFLECTCPP_USE_VCPKG_DEFAULT ON)
3537
endif()
@@ -147,6 +149,14 @@ if (REFLECTCPP_TOML)
147149
endif ()
148150
endif()
149151

152+
if (REFLECTCPP_UBJSON)
153+
list(APPEND REFLECT_CPP_SOURCES
154+
src/reflectcpp_ubjson.cpp
155+
)
156+
target_include_directories(reflectcpp SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include")
157+
find_package(jsoncons CONFIG REQUIRED)
158+
endif ()
159+
150160
if (REFLECTCPP_XML)
151161
list(APPEND REFLECT_CPP_SOURCES
152162
src/reflectcpp_xml.cpp

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ The following table lists the serialization formats currently supported by refle
3636
| flexbuffers | [flatbuffers](https://github.com/google/flatbuffers) | >= 23.5.26 | Apache 2.0 | Schema-less version of flatbuffers, binary format |
3737
| msgpack | [msgpack-c](https://github.com/msgpack/msgpack-c) | >= 6.0.0 | BSL 1.0 | JSON-like binary format |
3838
| TOML | [toml++](https://github.com/marzer/tomlplusplus) | >= 3.4.0 | MIT | Textual format with an emphasis on readability |
39+
| UBJSON | [jsoncons](https://github.com/danielaparker/jsoncons)| >= 0.176.0 | BSL 1.0 | JSON-like binary format |
3940
| XML | [pugixml](https://github.com/zeux/pugixml) | >= 1.14 | MIT | Textual format used in many legacy projects |
4041
| YAML | [yaml-cpp](https://github.com/jbeder/yaml-cpp) | >= 0.8.0 | MIT | Textual format with an emphasis on readability |
4142

@@ -471,7 +472,7 @@ In addition, it supports the following custom containers:
471472
472473
- `rfl::Binary`: Used to express numbers in binary format.
473474
- `rfl::Box`: Similar to `std::unique_ptr`, but (almost) guaranteed to never be null.
474-
- `rfl::Bytestring`: An alias for `std::basic_string<std::byte>`. Supported by BSON, CBOR, flexbuffers and msgpack.
475+
- `rfl::Bytestring`: An alias for `std::basic_string<std::byte>`. Supported by BSON, CBOR, flexbuffers, msgpack and UBJSON.
475476
- `rfl::Generic`: A catch-all type that can represent (almost) anything.
476477
- `rfl::Hex`: Used to express numbers in hex format.
477478
- `rfl::Literal`: An explicitly enumerated string.
@@ -569,6 +570,7 @@ set(REFLECTCPP_CBOR ON) # Optional
569570
set(REFLECTCPP_FLEXBUFFERS ON) # Optional
570571
set(REFLECTCPP_MSGPACK ON) # Optional
571572
set(REFLECTCPP_TOML ON) # Optional
573+
set(REFLECTCPP_UBJSON ON) # Optional
572574
set(REFLECTCPP_XML ON) # Optional
573575
set(REFLECTCPP_YAML ON) # Optional
574576
@@ -619,7 +621,7 @@ To run the tests, do the following:
619621
To compile the tests with serialization formats other than JSON, do the following:
620622

621623
```bash
622-
cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_MSGPACK=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_TOML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release
624+
cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_MSGPACK=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_TOML=ON -DREFLECTCPP_UBJSON=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release
623625
cmake --build build -j 4 # gcc, clang
624626
cmake --build build --config Release -j 4 # MSVC
625627
```
@@ -633,6 +635,7 @@ To run the tests, do the following:
633635
./build/tests/msgpack/reflect-cpp-msgpack-tests
634636
./build/tests/json/reflect-cpp-json-tests
635637
./build/tests/toml/reflect-cpp-toml-tests
638+
./build/tests/toml/reflect-cpp-ubjson-tests
636639
./build/tests/xml/reflect-cpp-xml-tests
637640
./build/tests/yaml/reflect-cpp-yaml-tests
638641
```

benchmarks/all/canada_read.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <rfl/json.hpp>
1010
#include <rfl/msgpack.hpp>
1111
#include <rfl/toml.hpp>
12+
#include <rfl/ubjson.hpp>
1213
#include <rfl/yaml.hpp>
1314
#include <type_traits>
1415
#include <vector>
@@ -164,6 +165,30 @@ static void BM_canada_read_reflect_cpp_toml(benchmark::State &state) {
164165
}
165166
BENCHMARK(BM_canada_read_reflect_cpp_toml);
166167

168+
static void BM_canada_read_reflect_cpp_ubjson(benchmark::State &state) {
169+
const auto data = rfl::ubjson::write(load_data());
170+
for (auto _ : state) {
171+
const auto res = rfl::ubjson::read<FeatureCollection>(data);
172+
if (!res) {
173+
std::cout << res.error()->what() << std::endl;
174+
}
175+
}
176+
}
177+
BENCHMARK(BM_canada_read_reflect_cpp_ubjson);
178+
179+
static void BM_canada_read_reflect_cpp_ubjson_without_field_names(
180+
benchmark::State &state) {
181+
const auto data = rfl::ubjson::write<rfl::NoFieldNames>(load_data());
182+
for (auto _ : state) {
183+
const auto res =
184+
rfl::ubjson::read<FeatureCollection, rfl::NoFieldNames>(data);
185+
if (!res) {
186+
std::cout << res.error()->what() << std::endl;
187+
}
188+
}
189+
}
190+
BENCHMARK(BM_canada_read_reflect_cpp_ubjson_without_field_names);
191+
167192
static void BM_canada_read_reflect_cpp_yaml(benchmark::State &state) {
168193
const auto data = rfl::yaml::write(load_data());
169194
for (auto _ : state) {

benchmarks/all/canada_write.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <rfl/json.hpp>
1010
#include <rfl/msgpack.hpp>
1111
#include <rfl/toml.hpp>
12+
#include <rfl/ubjson.hpp>
1213
#include <rfl/yaml.hpp>
1314
#include <type_traits>
1415
#include <vector>
@@ -149,6 +150,29 @@ static void BM_canada_write_reflect_cpp_toml(benchmark::State &state) {
149150
}
150151
BENCHMARK(BM_canada_write_reflect_cpp_toml);
151152

153+
static void BM_canada_write_reflect_cpp_ubjson(benchmark::State &state) {
154+
const auto data = load_data();
155+
for (auto _ : state) {
156+
const auto output = rfl::ubjson::write(data);
157+
if (output.size() == 0) {
158+
std::cout << "No output" << std::endl;
159+
}
160+
}
161+
}
162+
BENCHMARK(BM_canada_write_reflect_cpp_ubjson);
163+
164+
static void BM_canada_write_reflect_cpp_ubjson_without_field_names(
165+
benchmark::State &state) {
166+
const auto data = load_data();
167+
for (auto _ : state) {
168+
const auto output = rfl::ubjson::write<rfl::NoFieldNames>(data);
169+
if (output.size() == 0) {
170+
std::cout << "No output" << std::endl;
171+
}
172+
}
173+
}
174+
BENCHMARK(BM_canada_write_reflect_cpp_ubjson_without_field_names);
175+
152176
static void BM_canada_write_reflect_cpp_yaml(benchmark::State &state) {
153177
const auto data = load_data();
154178
for (auto _ : state) {

benchmarks/all/licenses_read.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <rfl/json.hpp>
1010
#include <rfl/msgpack.hpp>
1111
#include <rfl/toml.hpp>
12+
#include <rfl/ubjson.hpp>
1213
#include <rfl/xml.hpp>
1314
#include <rfl/yaml.hpp>
1415
#include <type_traits>
@@ -187,6 +188,29 @@ static void BM_licenses_read_reflect_cpp_toml(benchmark::State &state) {
187188
}
188189
BENCHMARK(BM_licenses_read_reflect_cpp_toml);
189190

191+
static void BM_licenses_read_reflect_cpp_ubjson(benchmark::State &state) {
192+
const auto data = rfl::ubjson::write(load_data());
193+
for (auto _ : state) {
194+
const auto res = rfl::ubjson::read<Licenses>(data);
195+
if (!res) {
196+
std::cout << res.error()->what() << std::endl;
197+
}
198+
}
199+
}
200+
BENCHMARK(BM_licenses_read_reflect_cpp_ubjson);
201+
202+
static void BM_licenses_read_reflect_cpp_ubjson_without_field_names(
203+
benchmark::State &state) {
204+
const auto data = rfl::ubjson::write<rfl::NoFieldNames>(load_data());
205+
for (auto _ : state) {
206+
const auto res = rfl::ubjson::read<Licenses, rfl::NoFieldNames>(data);
207+
if (!res) {
208+
std::cout << res.error()->what() << std::endl;
209+
}
210+
}
211+
}
212+
BENCHMARK(BM_licenses_read_reflect_cpp_ubjson_without_field_names);
213+
190214
static void BM_licenses_read_reflect_cpp_yaml(benchmark::State &state) {
191215
const auto data = rfl::yaml::write(load_data());
192216
for (auto _ : state) {

benchmarks/all/licenses_write.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <rfl/json.hpp>
1010
#include <rfl/msgpack.hpp>
1111
#include <rfl/toml.hpp>
12+
#include <rfl/ubjson.hpp>
1213
#include <rfl/xml.hpp>
1314
#include <rfl/yaml.hpp>
1415
#include <type_traits>
@@ -176,6 +177,29 @@ static void BM_licenses_write_reflect_cpp_toml(benchmark::State &state) {
176177
}
177178
BENCHMARK(BM_licenses_write_reflect_cpp_toml);
178179

180+
static void BM_licenses_write_reflect_cpp_ubjson(benchmark::State &state) {
181+
const auto data = load_data();
182+
for (auto _ : state) {
183+
const auto output = rfl::ubjson::write(data);
184+
if (output.size() == 0) {
185+
std::cout << "No output" << std::endl;
186+
}
187+
}
188+
}
189+
BENCHMARK(BM_licenses_write_reflect_cpp_ubjson);
190+
191+
static void BM_licenses_write_reflect_cpp_ubjson_without_field_names(
192+
benchmark::State &state) {
193+
const auto data = load_data();
194+
for (auto _ : state) {
195+
const auto output = rfl::ubjson::write<rfl::NoFieldNames>(data);
196+
if (output.size() == 0) {
197+
std::cout << "No output" << std::endl;
198+
}
199+
}
200+
}
201+
BENCHMARK(BM_licenses_write_reflect_cpp_ubjson_without_field_names);
202+
179203
static void BM_licenses_write_reflect_cpp_xml(benchmark::State &state) {
180204
const auto data = load_data();
181205
for (auto _ : state) {

benchmarks/all/person_read.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
#include <rfl/json.hpp>
1010
#include <rfl/msgpack.hpp>
1111
#include <rfl/toml.hpp>
12+
#include <rfl/ubjson.hpp>
1213
#include <rfl/xml.hpp>
1314
#include <rfl/yaml.hpp>
1415
#include <type_traits>
1516
#include <vector>
16-
1717
namespace person_read {
1818

1919
// ----------------------------------------------------------------------------
@@ -156,6 +156,29 @@ static void BM_person_read_reflect_cpp_toml(benchmark::State &state) {
156156
}
157157
BENCHMARK(BM_person_read_reflect_cpp_toml);
158158

159+
static void BM_person_read_reflect_cpp_ubjson(benchmark::State &state) {
160+
const auto data = rfl::ubjson::write(load_data());
161+
for (auto _ : state) {
162+
const auto res = rfl::ubjson::read<Person>(data);
163+
if (!res) {
164+
std::cout << res.error()->what() << std::endl;
165+
}
166+
}
167+
}
168+
BENCHMARK(BM_person_read_reflect_cpp_ubjson);
169+
170+
static void BM_person_read_reflect_cpp_ubjson_without_field_names(
171+
benchmark::State &state) {
172+
const auto data = rfl::ubjson::write<rfl::NoFieldNames>(load_data());
173+
for (auto _ : state) {
174+
const auto res = rfl::ubjson::read<Person, rfl::NoFieldNames>(data);
175+
if (!res) {
176+
std::cout << res.error()->what() << std::endl;
177+
}
178+
}
179+
}
180+
BENCHMARK(BM_person_read_reflect_cpp_ubjson_without_field_names);
181+
159182
static void BM_person_read_reflect_cpp_xml(benchmark::State &state) {
160183
const auto data = rfl::xml::write(load_data());
161184
for (auto _ : state) {

benchmarks/all/person_write.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <rfl/json.hpp>
1010
#include <rfl/msgpack.hpp>
1111
#include <rfl/toml.hpp>
12+
#include <rfl/ubjson.hpp>
1213
#include <rfl/xml.hpp>
1314
#include <rfl/yaml.hpp>
1415
#include <type_traits>
@@ -156,6 +157,29 @@ static void BM_person_write_reflect_cpp_toml(benchmark::State &state) {
156157
}
157158
BENCHMARK(BM_person_write_reflect_cpp_toml);
158159

160+
static void BM_person_write_reflect_cpp_ubjson(benchmark::State &state) {
161+
const auto data = load_data();
162+
for (auto _ : state) {
163+
const auto output = rfl::ubjson::write(data);
164+
if (output.size() == 0) {
165+
std::cout << "No output" << std::endl;
166+
}
167+
}
168+
}
169+
BENCHMARK(BM_person_write_reflect_cpp_ubjson);
170+
171+
static void BM_person_write_reflect_cpp_ubjson_without_field_names(
172+
benchmark::State &state) {
173+
const auto data = load_data();
174+
for (auto _ : state) {
175+
const auto output = rfl::ubjson::write<rfl::NoFieldNames>(data);
176+
if (output.size() == 0) {
177+
std::cout << "No output" << std::endl;
178+
}
179+
}
180+
}
181+
BENCHMARK(BM_person_write_reflect_cpp_ubjson_without_field_names);
182+
159183
static void BM_person_write_reflect_cpp_xml(benchmark::State &state) {
160184
const auto data = load_data();
161185
for (auto _ : state) {

docs/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@
8888

8989
6.6) [TOML](https://github.com/getml/reflect-cpp/blob/main/docs/toml.md)
9090

91-
6.7) [XML](https://github.com/getml/reflect-cpp/blob/main/docs/xml.md)
91+
6.7) [UBJSON](https://github.com/getml/reflect-cpp/blob/main/docs/ubjson.md)
9292

93-
6.8) [YAML](https://github.com/getml/reflect-cpp/blob/main/docs/yaml.md)
93+
6.8) [XML](https://github.com/getml/reflect-cpp/blob/main/docs/xml.md)
94+
95+
6.9) [YAML](https://github.com/getml/reflect-cpp/blob/main/docs/yaml.md)
9496

9597
## 7) Advanced topics
9698

0 commit comments

Comments
 (0)