This example demonstrates how to use Fory's high-performance serialization for C++ objects.
- CMake 3.16 or higher (for CMake build)
- Bazel 8+ (for Bazel build)
- C++17 compatible compiler (GCC 7+, Clang 5+, MSVC 2017+)
The easiest way to use Fory is with CMake's FetchContent module:
include(FetchContent)
FetchContent_Declare(
fory
GIT_REPOSITORY https://github.com/apache/fory.git
GIT_TAG main
SOURCE_SUBDIR cpp
)
FetchContent_MakeAvailable(fory)
target_link_libraries(your_app PRIVATE fory::serialization)cd examples/cpp/hello_world
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build . --parallel
./hello_worldOr use the provided script:
./run.shFrom the repository root:
bazel build //examples/cpp/hello_world:hello_world
bazel run //examples/cpp/hello_world:hello_worldOr use the provided script:
./run_bazel.shFor your own project using Fory as a dependency:
- Copy
MODULE.bazel.exampletoMODULE.bazelin your project root - Copy
BUILD.standalonetoBUILDin your source directory - Adjust the git commit or use
local_path_overridefor local development
# In your MODULE.bazel
bazel_dep(name = "fory", version = "0.17.0")
git_override(
module_name = "fory",
remote = "https://github.com/apache/fory.git",
commit = "main", # Use specific commit for reproducibility
)
# In your BUILD file
cc_binary(
name = "my_app",
srcs = ["main.cc"],
deps = ["@fory//cpp/fory/serialization:fory_serialization"],
)This example demonstrates:
- Primitive types: Serializing integers, floats, and basic types
- Strings: High-performance string serialization
- Collections: Vectors and maps
- Custom structs: Using
FORY_STRUCTmacro to register struct fields - Nested structs: Structs containing other structs
- Enums: Enum serialization
Use the FORY_STRUCT macro to register struct fields for serialization:
struct Point {
int32_t x;
int32_t y;
FORY_STRUCT(Point, x, y);
};auto fory = fory::serialization::Fory::builder()
.xlang(true) // Enable cross-language serialization
.track_ref(false) // Disable reference tracking
.build();
// Register struct types with unique IDs
fory.register_struct<Point>(1);// Serialize
Point point{10, 20};
auto bytes_result = fory.serialize(point);
if (bytes_result.ok()) {
auto bytes = bytes_result.value();
// Use bytes...
}
// Deserialize
auto result = fory.deserialize<Point>(bytes.data(), bytes.size());
if (result.ok()) {
Point deserialized = result.value();
}# Link to serialization library
target_link_libraries(your_app PRIVATE fory::serialization)#include "fory/serialization/fory.h"