C++11 header-only library for deserializing binary data created by the obj2buf JavaScript library.
Compatible with obj2buf JavaScript library v1.0.0+
- *Header-only// 2. C++ side #include "obj2buf.hpp"
void process_user_data(const std::vector<uint8_t>& buffer, const nlohmann::json& schema_json) { std::cout << "Using obj2buf C++ bindings v" << obj2buf::version() << std::endl;
obj2buf::Schema schema(schema_json["root_type"]);
nlohmann::json user = schema.deserialize(buffer);
uint32_t id = user["id"];
std::string name = user["name"];
uint32_t age = user["age"];
std::cout << "Processing user: " << name
<< " (ID: " << id << ", Age: " << age << ")" << std::endl;
}buf.hpp` file, no compilation required
- C++11 Compatible: Works with modern C++ standards
- nlohmann/json Integration: Uses familiar JSON objects for deserialized data
- Complete Type Support: All obj2buf types supported (primitives, strings, arrays, maps, etc.)
- Exception Safety: Proper error handling with descriptive messages
- Version Information: Built-in version checking for compatibility
- nlohmann/json: For JSON object representation
- C++11 or later: Modern C++ standard
vcpkg install nlohmann-jsonconan install nlohmann_json/3.11.2@Download nlohmann/json from https://github.com/nlohmann/json
#include "obj2buf.hpp"
#include <iostream>
#include <vector>
int main() {
// Schema definition (exported from JavaScript schema.to_json())
nlohmann::json schema_def = {
{"type", "Schema"},
{"root_type", {
{"type", "MapType"},
{"field_pairs", {
{"id", {{"type", "UInt32"}}},
{"name", {{"type", "VarStringType"}, {"max_length", 100}}},
{"age", {{"type", "UInt8"}}},
{"is_active", {{"type", "BooleanType"}}}
}}
}}
};
// Create schema from the root_type
obj2buf::Schema schema(schema_def["root_type"]);
// Your binary data (from JavaScript obj2buf.serialize())
std::vector<uint8_t> buffer = {/* ... binary data ... */};
// Deserialize
nlohmann::json result = schema.deserialize(buffer);
// Access data
std::cout << "ID: " << result["id"] << std::endl;
std::cout << "Name: " << result["name"] << std::endl;
std::cout << "Age: " << result["age"] << std::endl;
std::cout << "Active: " << result["is_active"] << std::endl;
return 0;
}// If you have raw buffer data
const uint8_t* raw_buffer = /* ... */;
size_t buffer_size = /* ... */;
nlohmann::json result = schema.deserialize(raw_buffer, buffer_size);// In JavaScript:
// const schema_json = schema.to_json();
// // Send schema_json to C++
// In C++:
nlohmann::json schema_json = /* received from JavaScript */;
obj2buf::Schema schema(schema_json);Check the version of the C++ bindings:
#include "obj2buf.hpp"
#include <iostream>
int main() {
std::cout << "obj2buf C++ bindings version: " << obj2buf::version() << std::endl;
std::cout << "Major: " << obj2buf::version_major() << std::endl;
std::cout << "Minor: " << obj2buf::version_minor() << std::endl;
std::cout << "Patch: " << obj2buf::version_patch() << std::endl;
return 0;
}- C++ Bindings v1.0.0: Compatible with obj2buf JavaScript v1.0.0+
- Breaking changes: Will be indicated by major version bumps
- New features: Will be indicated by minor version bumps
- Bug fixes: Will be indicated by patch version bumps
UInt8,UInt16,UInt32- Unsigned integersInt8,Int16,Int32- Signed integersFloat32,Float64- IEEE 754 floating pointBooleanType- Boolean valuesChar- Single characters
UInt(byte_length)- Generic unsigned integer (1, 2, or 4 bytes)Int(byte_length)- Generic signed integer (1, 2, or 4 bytes)Float(byte_length)- Generic floating point (4 or 8 bytes)
FixedStringType(length)- Fixed-length stringsVarStringType(max_length)- Variable-length strings
ArrayType(element_type, length?)- Arrays (fixed or variable length)TupleType(element_types...)- Fixed-structure tuplesMapType(field_pairs)- Objects with named fieldsEnumType(options)- String enumerationsOptionalType(base_type)- Nullable values
All parsing errors throw obj2buf::parser_error:
try {
nlohmann::json result = schema.deserialize(buffer);
} catch (const obj2buf::parser_error& e) {
std::cerr << "Parse error: " << e.what() << std::endl;
}- Zero-copy: Strings are constructed directly from buffer data
- Minimal allocations: Efficient memory usage
- Little-endian: Matches JavaScript buffer format
- Header-only: No linking overhead
| obj2buf Type | C++ JSON Type | Notes |
|---|---|---|
| UInt8/16/32 | uint32_t |
Promoted to 32-bit for JSON compatibility |
| Int8/16/32 | int32_t |
Promoted to 32-bit for JSON compatibility |
| UInt(1/2/4) | uint32_t |
Generic unsigned integer types |
| Int(1/2/4) | int32_t |
Generic signed integer types with sign extension |
| Float32/64 | double |
Float32 promoted to double |
| Float(4/8) | double |
Generic floating point types |
| BooleanType | bool |
|
| String types | std::string |
UTF-8 encoded |
| Arrays | json::array |
|
| Maps | json::object |
|
| Enums | std::string |
Enum value as string |
| Optional | null or value |
null for absent values |
// Complete example showing JavaScript → C++ workflow
// 1. JavaScript side (Node.js/Browser)
/*
const { Schema, types } = require('obj2buf');
const user_type = new types.MapType([
['id', new types.UInt32()],
['name', new types.VarStringType(50)],
['age', new types.UInt8()]
]);
const schema = new Schema(user_type);
const user_data = { id: 1234, name: 'Alice', age: 30 };
// Serialize data
const buffer = schema.serialize(user_data);
// Export schema for C++
const schema_json = schema.to_json();
// Send both buffer and schema_json to C++
*/
// 2. C++ side
#include "obj2buf.hpp"
void process_user_data(const std::vector<uint8_t>& buffer,
const nlohmann::json& schema_json) {
obj2buf::Schema schema(schema_json["root_type"]);
nlohmann::json user = schema.deserialize(buffer);
uint32_t id = user["id"];
std::string name = user["name"];
uint8_t age = user["age"];
std::cout << "Processing user: " << name
<< " (ID: " << id << ", Age: " << age << ")" << std::endl;
}The easiest way to get started is using the provided run.sh script:
cd bindings/cpp
./run.shThis script will:
- Download nlohmann/json locally (single header)
- Compile the example with the correct flags
- Run the example program
No system installation required!
No special build configuration required. Just include the header:
#include "obj2buf.hpp"Make sure nlohmann/json is available in your include path.
find_package(nlohmann_json REQUIRED)
target_link_libraries(your_target nlohmann_json::nlohmann_json)Same as obj2buf JavaScript library (ISC License).