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

Added support for UBJSON; resolves #245 #251

Merged
merged 10 commits into from
Nov 10, 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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
*.flexbuf
*.msgpack
*.toml
*.ubjson
*.xml
*.yml
*.yaml
Expand Down
12 changes: 11 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ option(REFLECTCPP_FLEXBUFFERS "Enable flexbuffers support" OFF)
option(REFLECTCPP_MSGPACK "Enable msgpack support" OFF)
option(REFLECTCPP_XML "Enable XML support" OFF)
option(REFLECTCPP_TOML "Enable TOML support" OFF)
option(REFLECTCPP_UBJSON "Enable UBJSON support" OFF)
option(REFLECTCPP_YAML "Enable YAML support" OFF)

option(REFLECTCPP_BUILD_BENCHMARKS "Build benchmarks" OFF)
Expand All @@ -25,11 +26,12 @@ if(REFLECTCPP_BUILD_BENCHMARKS)
set(REFLECTCPP_MSGPACK ON CACHE BOOL "" FORCE)
set(REFLECTCPP_XML ON CACHE BOOL "" FORCE)
set(REFLECTCPP_TOML ON CACHE BOOL "" FORCE)
set(REFLECTCPP_UBJSON ON CACHE BOOL "" FORCE)
set(REFLECTCPP_YAML ON CACHE BOOL "" FORCE)
endif()
if (REFLECTCPP_BUILD_TESTS OR REFLECTCPP_BUILD_BENCHMARKS OR
(REFLECTCPP_JSON AND NOT REFLECTCPP_USE_BUNDLED_DEPENDENCIES) OR
REFLECTCPP_BSON OR REFLECTCPP_CBOR OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_MSGPACK OR REFLECTCPP_XML OR REFLECTCPP_TOML OR REFLECTCPP_YAML)
REFLECTCPP_BSON OR REFLECTCPP_CBOR OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_MSGPACK OR REFLECTCPP_XML OR REFLECTCPP_TOML OR REFLECTCPP_UBJSON OR REFLECTCPP_YAML)
# enable vcpkg per default if require features other than JSON
set(REFLECTCPP_USE_VCPKG_DEFAULT ON)
endif()
Expand Down Expand Up @@ -147,6 +149,14 @@ if (REFLECTCPP_TOML)
endif ()
endif()

if (REFLECTCPP_UBJSON)
list(APPEND REFLECT_CPP_SOURCES
src/reflectcpp_ubjson.cpp
)
target_include_directories(reflectcpp SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include")
find_package(jsoncons CONFIG REQUIRED)
endif ()

if (REFLECTCPP_XML)
list(APPEND REFLECT_CPP_SOURCES
src/reflectcpp_xml.cpp
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ The following table lists the serialization formats currently supported by refle
| flexbuffers | [flatbuffers](https://github.com/google/flatbuffers) | >= 23.5.26 | Apache 2.0 | Schema-less version of flatbuffers, binary format |
| msgpack | [msgpack-c](https://github.com/msgpack/msgpack-c) | >= 6.0.0 | BSL 1.0 | JSON-like binary format |
| TOML | [toml++](https://github.com/marzer/tomlplusplus) | >= 3.4.0 | MIT | Textual format with an emphasis on readability |
| UBJSON | [jsoncons](https://github.com/danielaparker/jsoncons)| >= 0.176.0 | BSL 1.0 | JSON-like binary format |
| XML | [pugixml](https://github.com/zeux/pugixml) | >= 1.14 | MIT | Textual format used in many legacy projects |
| YAML | [yaml-cpp](https://github.com/jbeder/yaml-cpp) | >= 0.8.0 | MIT | Textual format with an emphasis on readability |

Expand Down Expand Up @@ -471,7 +472,7 @@ In addition, it supports the following custom containers:

- `rfl::Binary`: Used to express numbers in binary format.
- `rfl::Box`: Similar to `std::unique_ptr`, but (almost) guaranteed to never be null.
- `rfl::Bytestring`: An alias for `std::basic_string<std::byte>`. Supported by BSON, CBOR, flexbuffers and msgpack.
- `rfl::Bytestring`: An alias for `std::basic_string<std::byte>`. Supported by BSON, CBOR, flexbuffers, msgpack and UBJSON.
- `rfl::Generic`: A catch-all type that can represent (almost) anything.
- `rfl::Hex`: Used to express numbers in hex format.
- `rfl::Literal`: An explicitly enumerated string.
Expand Down Expand Up @@ -569,6 +570,7 @@ set(REFLECTCPP_CBOR ON) # Optional
set(REFLECTCPP_FLEXBUFFERS ON) # Optional
set(REFLECTCPP_MSGPACK ON) # Optional
set(REFLECTCPP_TOML ON) # Optional
set(REFLECTCPP_UBJSON ON) # Optional
set(REFLECTCPP_XML ON) # Optional
set(REFLECTCPP_YAML ON) # Optional

Expand Down Expand Up @@ -619,7 +621,7 @@ To run the tests, do the following:
To compile the tests with serialization formats other than JSON, do the following:

```bash
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
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
cmake --build build -j 4 # gcc, clang
cmake --build build --config Release -j 4 # MSVC
```
Expand All @@ -633,6 +635,7 @@ To run the tests, do the following:
./build/tests/msgpack/reflect-cpp-msgpack-tests
./build/tests/json/reflect-cpp-json-tests
./build/tests/toml/reflect-cpp-toml-tests
./build/tests/toml/reflect-cpp-ubjson-tests
./build/tests/xml/reflect-cpp-xml-tests
./build/tests/yaml/reflect-cpp-yaml-tests
```
Expand Down
25 changes: 25 additions & 0 deletions benchmarks/all/canada_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <rfl/json.hpp>
#include <rfl/msgpack.hpp>
#include <rfl/toml.hpp>
#include <rfl/ubjson.hpp>
#include <rfl/yaml.hpp>
#include <type_traits>
#include <vector>
Expand Down Expand Up @@ -164,6 +165,30 @@ static void BM_canada_read_reflect_cpp_toml(benchmark::State &state) {
}
BENCHMARK(BM_canada_read_reflect_cpp_toml);

static void BM_canada_read_reflect_cpp_ubjson(benchmark::State &state) {
const auto data = rfl::ubjson::write(load_data());
for (auto _ : state) {
const auto res = rfl::ubjson::read<FeatureCollection>(data);
if (!res) {
std::cout << res.error()->what() << std::endl;
}
}
}
BENCHMARK(BM_canada_read_reflect_cpp_ubjson);

static void BM_canada_read_reflect_cpp_ubjson_without_field_names(
benchmark::State &state) {
const auto data = rfl::ubjson::write<rfl::NoFieldNames>(load_data());
for (auto _ : state) {
const auto res =
rfl::ubjson::read<FeatureCollection, rfl::NoFieldNames>(data);
if (!res) {
std::cout << res.error()->what() << std::endl;
}
}
}
BENCHMARK(BM_canada_read_reflect_cpp_ubjson_without_field_names);

static void BM_canada_read_reflect_cpp_yaml(benchmark::State &state) {
const auto data = rfl::yaml::write(load_data());
for (auto _ : state) {
Expand Down
24 changes: 24 additions & 0 deletions benchmarks/all/canada_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <rfl/json.hpp>
#include <rfl/msgpack.hpp>
#include <rfl/toml.hpp>
#include <rfl/ubjson.hpp>
#include <rfl/yaml.hpp>
#include <type_traits>
#include <vector>
Expand Down Expand Up @@ -149,6 +150,29 @@ static void BM_canada_write_reflect_cpp_toml(benchmark::State &state) {
}
BENCHMARK(BM_canada_write_reflect_cpp_toml);

static void BM_canada_write_reflect_cpp_ubjson(benchmark::State &state) {
const auto data = load_data();
for (auto _ : state) {
const auto output = rfl::ubjson::write(data);
if (output.size() == 0) {
std::cout << "No output" << std::endl;
}
}
}
BENCHMARK(BM_canada_write_reflect_cpp_ubjson);

static void BM_canada_write_reflect_cpp_ubjson_without_field_names(
benchmark::State &state) {
const auto data = load_data();
for (auto _ : state) {
const auto output = rfl::ubjson::write<rfl::NoFieldNames>(data);
if (output.size() == 0) {
std::cout << "No output" << std::endl;
}
}
}
BENCHMARK(BM_canada_write_reflect_cpp_ubjson_without_field_names);

static void BM_canada_write_reflect_cpp_yaml(benchmark::State &state) {
const auto data = load_data();
for (auto _ : state) {
Expand Down
24 changes: 24 additions & 0 deletions benchmarks/all/licenses_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <rfl/json.hpp>
#include <rfl/msgpack.hpp>
#include <rfl/toml.hpp>
#include <rfl/ubjson.hpp>
#include <rfl/xml.hpp>
#include <rfl/yaml.hpp>
#include <type_traits>
Expand Down Expand Up @@ -187,6 +188,29 @@ static void BM_licenses_read_reflect_cpp_toml(benchmark::State &state) {
}
BENCHMARK(BM_licenses_read_reflect_cpp_toml);

static void BM_licenses_read_reflect_cpp_ubjson(benchmark::State &state) {
const auto data = rfl::ubjson::write(load_data());
for (auto _ : state) {
const auto res = rfl::ubjson::read<Licenses>(data);
if (!res) {
std::cout << res.error()->what() << std::endl;
}
}
}
BENCHMARK(BM_licenses_read_reflect_cpp_ubjson);

static void BM_licenses_read_reflect_cpp_ubjson_without_field_names(
benchmark::State &state) {
const auto data = rfl::ubjson::write<rfl::NoFieldNames>(load_data());
for (auto _ : state) {
const auto res = rfl::ubjson::read<Licenses, rfl::NoFieldNames>(data);
if (!res) {
std::cout << res.error()->what() << std::endl;
}
}
}
BENCHMARK(BM_licenses_read_reflect_cpp_ubjson_without_field_names);

static void BM_licenses_read_reflect_cpp_yaml(benchmark::State &state) {
const auto data = rfl::yaml::write(load_data());
for (auto _ : state) {
Expand Down
24 changes: 24 additions & 0 deletions benchmarks/all/licenses_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <rfl/json.hpp>
#include <rfl/msgpack.hpp>
#include <rfl/toml.hpp>
#include <rfl/ubjson.hpp>
#include <rfl/xml.hpp>
#include <rfl/yaml.hpp>
#include <type_traits>
Expand Down Expand Up @@ -176,6 +177,29 @@ static void BM_licenses_write_reflect_cpp_toml(benchmark::State &state) {
}
BENCHMARK(BM_licenses_write_reflect_cpp_toml);

static void BM_licenses_write_reflect_cpp_ubjson(benchmark::State &state) {
const auto data = load_data();
for (auto _ : state) {
const auto output = rfl::ubjson::write(data);
if (output.size() == 0) {
std::cout << "No output" << std::endl;
}
}
}
BENCHMARK(BM_licenses_write_reflect_cpp_ubjson);

static void BM_licenses_write_reflect_cpp_ubjson_without_field_names(
benchmark::State &state) {
const auto data = load_data();
for (auto _ : state) {
const auto output = rfl::ubjson::write<rfl::NoFieldNames>(data);
if (output.size() == 0) {
std::cout << "No output" << std::endl;
}
}
}
BENCHMARK(BM_licenses_write_reflect_cpp_ubjson_without_field_names);

static void BM_licenses_write_reflect_cpp_xml(benchmark::State &state) {
const auto data = load_data();
for (auto _ : state) {
Expand Down
25 changes: 24 additions & 1 deletion benchmarks/all/person_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
#include <rfl/json.hpp>
#include <rfl/msgpack.hpp>
#include <rfl/toml.hpp>
#include <rfl/ubjson.hpp>
#include <rfl/xml.hpp>
#include <rfl/yaml.hpp>
#include <type_traits>
#include <vector>

namespace person_read {

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -156,6 +156,29 @@ static void BM_person_read_reflect_cpp_toml(benchmark::State &state) {
}
BENCHMARK(BM_person_read_reflect_cpp_toml);

static void BM_person_read_reflect_cpp_ubjson(benchmark::State &state) {
const auto data = rfl::ubjson::write(load_data());
for (auto _ : state) {
const auto res = rfl::ubjson::read<Person>(data);
if (!res) {
std::cout << res.error()->what() << std::endl;
}
}
}
BENCHMARK(BM_person_read_reflect_cpp_ubjson);

static void BM_person_read_reflect_cpp_ubjson_without_field_names(
benchmark::State &state) {
const auto data = rfl::ubjson::write<rfl::NoFieldNames>(load_data());
for (auto _ : state) {
const auto res = rfl::ubjson::read<Person, rfl::NoFieldNames>(data);
if (!res) {
std::cout << res.error()->what() << std::endl;
}
}
}
BENCHMARK(BM_person_read_reflect_cpp_ubjson_without_field_names);

static void BM_person_read_reflect_cpp_xml(benchmark::State &state) {
const auto data = rfl::xml::write(load_data());
for (auto _ : state) {
Expand Down
24 changes: 24 additions & 0 deletions benchmarks/all/person_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <rfl/json.hpp>
#include <rfl/msgpack.hpp>
#include <rfl/toml.hpp>
#include <rfl/ubjson.hpp>
#include <rfl/xml.hpp>
#include <rfl/yaml.hpp>
#include <type_traits>
Expand Down Expand Up @@ -156,6 +157,29 @@ static void BM_person_write_reflect_cpp_toml(benchmark::State &state) {
}
BENCHMARK(BM_person_write_reflect_cpp_toml);

static void BM_person_write_reflect_cpp_ubjson(benchmark::State &state) {
const auto data = load_data();
for (auto _ : state) {
const auto output = rfl::ubjson::write(data);
if (output.size() == 0) {
std::cout << "No output" << std::endl;
}
}
}
BENCHMARK(BM_person_write_reflect_cpp_ubjson);

static void BM_person_write_reflect_cpp_ubjson_without_field_names(
benchmark::State &state) {
const auto data = load_data();
for (auto _ : state) {
const auto output = rfl::ubjson::write<rfl::NoFieldNames>(data);
if (output.size() == 0) {
std::cout << "No output" << std::endl;
}
}
}
BENCHMARK(BM_person_write_reflect_cpp_ubjson_without_field_names);

static void BM_person_write_reflect_cpp_xml(benchmark::State &state) {
const auto data = load_data();
for (auto _ : state) {
Expand Down
6 changes: 4 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@

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

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

6.8) [YAML](https://github.com/getml/reflect-cpp/blob/main/docs/yaml.md)
6.8) [XML](https://github.com/getml/reflect-cpp/blob/main/docs/xml.md)

6.9) [YAML](https://github.com/getml/reflect-cpp/blob/main/docs/yaml.md)

## 7) Advanced topics

Expand Down
2 changes: 1 addition & 1 deletion docs/cbor.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct Person {
};
```

A `Person` can be turned into a bytes vector like this:
A `Person` can be serialized to a bytes vector like this:

```cpp
const auto person = Person{...};
Expand Down
Loading
Loading