### CMake Configuration for C++ Project with Protobuf
Source: https://github.com/qicosmos/iguana/blob/master/tool/CMakeLists.txt
This snippet sets up a C++ project using CMake. It specifies the C++ standard, finds the Protobuf library, includes necessary directories, and defines an executable target linking against Protobuf and pthread. Ensure Protobuf is installed and findable by CMake.
```cmake
project(proto_to_struct)
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 20)
find_package(Protobuf REQUIRED)
include_directories(${PROTOBUF_INCLUDE_DIR})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
set(SOURCE_FILE proto_to_struct.cpp)
add_executable(proto_to_struct ${SOURCE_FILE})
target_link_libraries(proto_to_struct protobuf::libprotobuf protobuf::libprotoc pthread)
```
--------------------------------
### XML Serialization and Deserialization with Iguana
Source: https://github.com/qicosmos/iguana/blob/master/README.md
Illustrates how to serialize C++ structures into XML strings and deserialize XML strings back into C++ structures using the iguana library. This example defines a library structure containing books and demonstrates the process. Ensure YLT_REFL is used for structure reflection.
```c++
struct book_t {
std::string author;
float price;
};
YLT_REFL(book_t, author, price);
struct library_t {
std::string name;
int id;
std::vector book;
};
YLT_REFL(library_t, name, id, book);
// serialization the structure to the string
library_t library = {"Pro Lib", 110, {{"tom", 1.8}, {"jack", 2.1}}};
std::string ss;
iguana::to_xml(library, ss);
std::cout << ss << "\n";
// deserialization the structure from the string
std::string str = R"(
Pro Lib
110
tom1.8
jack2.1
)";
library_t lib;
iguana::from_xml(lib, str);
```
--------------------------------
### YAML Serialization and Deserialization with Iguana
Source: https://github.com/qicosmos/iguana/blob/master/README.md
Shows how to serialize C++ structures to YAML strings and deserialize YAML strings back into C++ structures using the iguana library. This example includes handling enums, optional types, and plain data types. The YLT_REFL macro is necessary for reflection.
```c++
enum class enum_status {
start,
stop,
};
struct plain_type_t {
bool isok;
enum_status status;
char c;
std::optional hasprice;
std::optional num;
std::optional price;
};
YLT_REFL(plain_type_t, isok, status, c, hasprice, num, price);
// deserialization the structure from the string
std::string str = R"(
isok: false
status: 1
c: a
hasprice: true
num:
price: 20
)";
plain_type_t p;
iguana::from_yaml(p, str);
// serialization the structure to the string
std::string ss;
iguana::to_yaml(ss, p);
std::cout << ss << "\n";
```
--------------------------------
### Parse JSON String to Iguana JValue
Source: https://github.com/qicosmos/iguana/blob/master/README.md
Demonstrates parsing a JSON string representing a boolean value into an iguana::jvalue object. It shows how to retrieve the boolean value from the jvalue using get() with and without error handling, and includes assertions for validation.
```c++
std::string_view str = R"(false)";
iguana::jvalue val;
iguana::parse(val, str.begin(), str.end());
std::error_code ec;
auto b = val.get(ec);
CHECK(!ec);
CHECK(!b);
// or
b = val.get(); // this interface maybe throw exception
CHECK(!b);
```
--------------------------------
### Generate Standard Struct Pack Headers with protoc
Source: https://github.com/qicosmos/iguana/blob/master/tool/README.md
Generates C++ struct pack header files from a .proto definition using the protoc compiler and a custom plugin. The output is written to a specified directory. This is the default mode of operation.
```shell
protoc --plugin=protoc-gen-custom=./build/proto_to_struct data.proto --custom_out=:./protos
```
--------------------------------
### Compile proto_to_struct Tool
Source: https://github.com/qicosmos/iguana/blob/master/tool/README.md
Compiles the proto_to_struct tool using CMake and Make. Requires libprotobuf and libprotoc version 3.21.0. This process creates the necessary executable for generating struct pack headers.
```shell
mkdir build
cd build
cmake .. && make
```
--------------------------------
### Generated C++ Struct Pack Headers (Standard)
Source: https://github.com/qicosmos/iguana/blob/master/tool/README.md
C++ header file generated from a .proto file using the proto_to_struct tool in its standard mode. It defines C++ structs corresponding to the Protocol Buffers messages, with reflection support via YLT_REFL.
```cpp
#pragma once
#include
enum class Color {
Red = 0,
Green = 1,
Blue = 2,
};
struct Vec3 {
float x;
float y;
float z;
};
YLT_REFL(Vec3, x, y, z);
struct Weapon {
std::string name;
int32_t damage;
};
YLT_REFL(Weapon, name, damage);
struct Monster {
Vec3 pos;
int32_t mana;
int32_t hp;
std::string name;
std::string inventory;
Color color;
std::vectorweapons;
Weapon equipped;
std::vectorpath;
};
YLT_REFL(Monster, pos, mana, hp, name, inventory, color, weapons, equipped, path);
struct Monsters {
std::vectormonsters;
};
YLT_REFL(Monsters, monsters);
struct person {
int32_t id;
std::string name;
int32_t age;
double salary;
};
YLT_REFL(person, id, name, age, salary);
struct persons {
std::vectorperson_list;
};
YLT_REFL(persons, person_list);
struct bench_int32 {
int32_t a;
int32_t b;
int32_t c;
int32_t d;
};
YLT_REFL(bench_int32, a, b, c, d);
```
--------------------------------
### Generated C++ Struct Pack Headers (Optional)
Source: https://github.com/qicosmos/iguana/blob/master/tool/README.md
C++ header file generated from a .proto file using the proto_to_struct tool in `add_optional` mode. It defines C++ structs where string fields and complex types are wrapped in `std::optional` for nullable support.
```cpp
#pragma once
#include
enum class Color {
Red = 0,
Green = 1,
Blue = 2,
};
struct Vec3 {
float x;
float y;
float z;
};
YLT_REFL(Vec3, x, y, z);
struct Weapon {
std::optional name;
int32_t damage;
};
YLT_REFL(Weapon, name, damage);
struct Monster {
std::optional pos;
int32_t mana;
int32_t hp;
std::optional name;
std::optional inventory;
Color color;
std::optional> weapons;
std::optional equipped;
std::optional> path;
};
YLT_REFL(Monster, pos, mana, hp, name, inventory, color, weapons, equipped, path);
struct Monsters {
std::optional> monsters;
};
YLT_REFL(Monsters, monsters);
struct person {
int32_t id;
std::optional name;
int32_t age;
double salary;
};
YLT_REFL(person, id, name, age, salary);
struct persons {
std::optional> person_list;
};
YLT_REFL(persons, person_list);
struct bench_int32 {
int32_t a;
int32_t b;
int32_t c;
int32_t d;
};
YLT_REFL(bench_int32, a, b, c, d);
```
--------------------------------
### Generate C++ structs with std::optional using protoc and iguana
Source: https://github.com/qicosmos/iguana/blob/master/tool/README.md
This command generates C++ files from a protobuf definition (`data.proto`). It utilizes the `protoc-gen-custom` plugin with both `add_optional` and `enable_inherit` options, outputting to the `./protos` directory. The generated C++ code includes `std::optional` for fields that might be absent, while still conforming to the `struct pb` standard.
```shell
protoc --plugin=protoc-gen-custom=./build/proto_to_struct data.proto --custom_out=add_optional+enable_inherit:./protos
```
--------------------------------
### Generate Optional Struct Pack Headers with protoc
Source: https://github.com/qicosmos/iguana/blob/master/tool/README.md
Generates C++ struct pack header files from a .proto definition using the proto_to_struct tool with the `add_optional` flag. This mode wraps string types and class types in `std::optional`.
```shell
protoc --plugin=protoc-gen-custom=./build/proto_to_struct data.proto --custom_out=add_optional:./protos
```
--------------------------------
### Define Protocol Buffers Messages (data.proto)
Source: https://github.com/qicosmos/iguana/blob/master/tool/README.md
Defines various data structures (Vec3, Weapon, Monster, person, etc.) using Protocol Buffers syntax (proto3). These definitions are used as input for the proto_to_struct tool.
```proto
syntax = "proto3";
package mygame;
option optimize_for = SPEED;
option cc_enable_arenas = true;
message Vec3 {
float x = 1;
float y = 2;
float z = 3;
}
message Weapon {
string name = 1;
int32 damage = 2;
}
message Monster {
Vec3 pos = 1;
int32 mana = 2;
int32 hp = 3;
string name = 4;
bytes inventory = 5;
enum Color {
Red = 0;
Green = 1;
Blue = 2;
}
Color color = 6;
repeated Weapon weapons = 7;
Weapon equipped = 8;
repeated Vec3 path = 9;
}
message Monsters {
repeated Monster monsters = 1;
}
message person {
int32 id = 1;
string name = 2;
int32 age = 3;
double salary = 4;
}
message persons {
repeated person person_list = 1;
}
message bench_int32 {
int32 a = 1;
int32 b = 2;
int32 c = 3;
int32 d = 4;
}
```
--------------------------------
### Protobuf Serialization and Deserialization with Iguana
Source: https://github.com/qicosmos/iguana/blob/master/README.md
Demonstrates how to use the iguana library for serializing C++ structures to and deserializing from Protocol Buffers (protobuf) format. This requires defining the structure and its metadata using YLT_REFL, and then using `iguana::to_pb` and `iguana::from_pb`.
```c++
struct person {
int id;
std::string name;
int age;
bool operator==(person const& rhs) const {
return id == rhs.id && name == rhs.name && age == rhs.age;
}
};
#if __cplusplus < 202002L
YLT_REFL(person, id, name, age) //define meta data
#endif
void test() {
person p{1, "tom", 20};
std::string pb;
iguana::to_pb(p, pb);
person p1;
iguana::from_pb(p1, pb);
CHECK(p == p1);
}
```
--------------------------------
### Generate C++ structs without std::optional using protoc and iguana
Source: https://github.com/qicosmos/iguana/blob/master/tool/README.md
This command generates C++ files from a protobuf definition (`data.proto`). It uses the `protoc-gen-custom` plugin with the `enable_inherit` option, directing the output to the `./protos` directory. The generated C++ code adheres to the `struct pb` standard and does not use `std::optional`.
```shell
protoc --plugin=protoc-gen-custom=./build/proto_to_struct data.proto --custom_out=enable_inherit:./protos
```
--------------------------------
### Define Test Targets (CMake)
Source: https://github.com/qicosmos/iguana/blob/master/CMakeLists.txt
This snippet defines multiple test targets that can be executed. Each 'add_test' command registers a named test with a corresponding executable command. These tests cover various functionalities including basic tests, JSON, XML, YAML parsing, exception handling ('nothrow'), utilities, Protocol Buffers ('pb'), and C++20 features.
```cmake
add_test(NAME test_some COMMAND test_some)
add_test(NAME test_ut COMMAND test_ut)
add_test(NAME test_json_files COMMAND test_json_files)
add_test(NAME test_xml COMMAND test_xml)
add_test(NAME test_yaml COMMAND test_yaml)
add_test(NAME test_nothrow COMMAND test_nothrow)
add_test(NAME test_util COMMAND test_util)
add_test(NAME test_xml_nothrow COMMAND test_xml_nothrow)
add_test(NAME test_pb COMMAND test_pb)
add_test(NAME test_cpp20 COMMAND test_cpp20)
```
--------------------------------
### C++ struct definitions with std::optional
Source: https://github.com/qicosmos/iguana/blob/master/tool/README.md
This C++ code defines several structs (Vec3, Weapon, Monster, Monsters, person, persons, bench_int32) using the iguana library. These structs incorporate `std::optional` for fields that can be absent, allowing for more flexible data representation. The code is generated to conform to the `struct pb` standard.
```cpp
#pragma once
#include
enum class Color {
Red = 0,
Green = 1,
Blue = 2,
};
struct Vec3 : public iguana::base_impl {
Vec3() = default;
Vec3(float a, float b, float c) : x(a), y(b), z(c) {}
float x;
float y;
float z;
};
YLT_REFL(Vec3, x, y, z);
struct Weapon : public iguana::base_impl {
Weapon() = default;
Weapon(std::optional a, int32_t b) : name(std::move(a)), damage(b) {}
std::optional name;
int32_t damage;
};
YLT_REFL(Weapon, name, damage);
struct Monster : public iguana::base_impl {
Monster() = default;
Monster(std::optional a, int32_t b, int32_t c, std::optional d, std::optional e, Color f, std::optional> g, std::optional h, std::optional> i) : pos(a), mana(b), hp(c), name(std::move(d)), inventory(std::move(e)), color(f), weapons(std::move(g)), equipped(h), path(std::move(i)) {}
std::optional pos;
int32_t mana;
int32_t hp;
std::optional name;
std::optional inventory;
Color color;
std::optional> weapons;
std::optional equipped;
std::optional> path;
};
YLT_REFL(Monster, pos, mana, hp, name, inventory, color, weapons, equipped, path);
struct Monsters : public iguana::base_impl {
Monsters() = default;
Monsters(std::optional> a) : monsters(std::move(a)) {}
std::optional> monsters;
};
YLT_REFL(Monsters, monsters);
struct person : public iguana::base_impl {
person() = default;
person(int32_t a, std::optional b, int32_t c, double d) : id(a), name(std::move(b)), age(c), salary(d) {}
int32_t id;
std::optional name;
int32_t age;
double salary;
};
YLT_REFL(person, id, name, age, salary);
struct persons : public iguana::base_impl {
persons() = default;
persons(std::optional> a) : person_list(std::move(a)) {}
std::optional> person_list;
};
YLT_REFL(persons, person_list);
struct bench_int32 : public iguana::base_impl {
bench_int32() = default;
bench_int32(int32_t a, int32_t b, int32_t c, int32_t d) : a(a), b(b), c(c), d(d) {}
int32_t a;
int32_t b;
int32_t c;
int32_t d;
};
YLT_REFL(bench_int32, a, b, c, d);
```
--------------------------------
### C++ struct definitions without std::optional
Source: https://github.com/qicosmos/iguana/blob/master/tool/README.md
This C++ code defines several structs (Vec3, Weapon, Monster, Monsters, person, persons, bench_int32) that are designed to work with the iguana library. These structs do not use `std::optional` and are generated to conform to the `struct pb` standard, suitable for serialization and deserialization without optional fields.
```cpp
#pragma once
#include
enum class Color {
Red = 0,
Green = 1,
Blue = 2,
};
struct Vec3 : public iguana::base_impl {
Vec3() = default;
Vec3(float a, float b, float c) : x(a), y(b), z(c) {}
float x;
float y;
float z;
};
YLT_REFL(Vec3, x, y, z);
struct Weapon : public iguana::base_impl {
Weapon() = default;
Weapon(std::string a, int32_t b) : name(std::move(a)), damage(b) {}
std::string name;
int32_t damage;
};
YLT_REFL(Weapon, name, damage);
struct Monster : public iguana::base_impl {
Monster() = default;
Monster(Vec3 a, int32_t b, int32_t c, std::string d, std::string e, Color f, std::vector g, Weapon h, std::vector i) : pos(a), mana(b), hp(c), name(std::move(d)), inventory(std::move(e)), color(f), weapons(std::move(g)), equipped(h), path(std::move(i)) {}
Vec3 pos;
int32_t mana;
int32_t hp;
std::string name;
std::string inventory;
Color color;
std::vector weapons;
Weapon equipped;
std::vector path;
};
YLT_REFL(Monster, pos, mana, hp, name, inventory, color, weapons, equipped, path);
struct Monsters : public iguana::base_impl {
Monsters() = default;
Monsters(std::vector a) : monsters(std::move(a)) {}
std::vector monsters;
};
YLT_REFL(Monsters, monsters);
struct person : public iguana::base_impl {
person() = default;
person(int32_t a, std::string b, int32_t c, double d) : id(a), name(std::move(b)), age(c), salary(d) {}
int32_t id;
std::string name;
int32_t age;
double salary;
};
YLT_REFL(person, id, name, age, salary);
struct persons : public iguana::base_impl {
persons() = default;
persons(std::vector a) : person_list(std::move(a)) {}
std::vector person_list;
};
YLT_REFL(persons, person_list);
struct bench_int32 : public iguana::base_impl {
bench_int32() = default;
bench_int32(int32_t a, int32_t b, int32_t c, int32_t d) : a(a), b(b), c(c), d(d) {}
int32_t a;
int32_t b;
int32_t c;
int32_t d;
};
YLT_REFL(bench_int32, a, b, c, d);
```
--------------------------------
### Handling Unicode Paths in C++ with std::filesystem
Source: https://github.com/qicosmos/iguana/blob/master/README.md
Explains how to handle unicode string paths when working with file operations in C++. It demonstrates using `std::filesystem::u8path` from C++17 to correctly interpret unicode paths, preventing issues with strange characters when Iguana parses UTF-8 encoded files.
```c++
//the p.path is a unicode string path
std::ifstream in(std::filesystem::u8path(p.path)); //std::filesystem::u8path help us
//now you can operate the file
```
--------------------------------
### JSON Serialization and Deserialization with Iguana
Source: https://github.com/qicosmos/iguana/blob/master/README.md
Demonstrates how to serialize C++ structures to JSON strings and deserialize JSON strings back into C++ structures using the iguana library. This includes handling nested structures, vectors, maps, and unordered maps. Ensure the YLT_REFL macro is used to reflect the structure's members.
```c++
struct one_t
{
int id;
};
YLT_REFL(one_t, id);
struct two
{
std::string name;
one_t one;
int age;
};
YLT_REFL(two, name, one, age);
struct composit_t
{
int a;
std::vector b;
int c;
std::map d;
std::unordered_map e;
double f;
std::list g;
};
YLT_REFL(composit_t, a, b, c, d, e, f, g);
one_t one = { 2 };
composit_t composit = { 1,{ "tom", "jack" }, 3,{ { 2,3 } },{ { 5,6 } }, 5.3,{ one } };
iguana::string_stream ss;
iguana::to_json(composit, ss);
std::cout << ss.str() << std::endl;
std::string str_comp = R"({\"a\":1, \"b\":[\"tom\", \"jack\"], \"c\":3, \"d\":{\"2\":3,\"5\":6},\"e\":{\"3\":4},\"f\":5.3,\"g\":[{\"id\":1},{\"id\":2}])";
composit_t comp;
iguana::from_json(comp, str_comp);
```
--------------------------------
### Serialize C++ Structs to Protocol Buffers - iguana::to_pb
Source: https://context7.com/qicosmos/iguana/llms.txt
Serializes C++ structs into Protocol Buffer binary format. Requires 'iguana/pb_writer.hpp'. Supports deserialization using iguana::from_pb. The input is a C++ struct, and the output is a binary string.
```cpp
#include
struct person {
int id;
std::string name;
int age;
bool operator==(const person& rhs) const {
return id == rhs.id && name == rhs.name && age == rhs.age;
}
};
YLT_REFL(person, id, name, age);
int main() {
person p{1, "tom", 20};
std::string pb_binary;
iguana::to_pb(p, pb_binary);
std::cout << "Serialized size: " << pb_binary.size() << " bytes" << std::endl;
// Deserialize
person p1;
iguana::from_pb(p1, pb_binary);
if (p == p1) {
std::cout << "Deserialization successful!" << std::endl;
std::cout << "ID: " << p1.id << ", Name: " << p1.name
<< ", Age: " << p1.age << std::endl;
}
return 0;
}
```
--------------------------------
### C++ Structs with Generated YLT_REFL Macros
Source: https://github.com/qicosmos/iguana/blob/master/README.md
This C++ code demonstrates the result of processing the initial struct definitions with the `automatic_macro_generator.py` script. The `YLT_REFL` macros are now appended to each struct, enabling iguana's reflection features.
```c++
struct person {
std::string name;
int age;
};
YLT_REFL(person, name, age);
char *iguana = NULL;
struct composit_t { int a; std::vector b; int c; std::map d; std::unordered_map e; double f;};
YLT_REFL(composit_t, a, b, c, d, e, f);
struct composit_t2
{
int a;
std::vector b;
int iguana;
std::map example_test;
std::unordered_map random_name__;
double __f__number__complex;
};
YLT_REFL(composit_t2, a, b, iguana, example_test, random_name__, __f__number__complex);
```
--------------------------------
### Custom JSON Serialization for User-Defined Types - to_json_impl/from_json_impl
Source: https://context7.com/qicosmos/iguana/llms.txt
Implements custom JSON serialization and deserialization for user-defined types by providing `to_json_impl` and `from_json_impl` functions. Requires 'iguana/json_reader.hpp' and 'iguana/json_writer.hpp'. Enables treating types as arrays or other custom JSON structures.
```cpp
#include
#include
namespace my_space {
struct my_struct {
int x, y, z;
bool operator==(const my_struct& o) const {
return x == o.x && y == o.y && z == o.z;
}
};
void ylt_custom_reflect(my_struct*) {}
// Custom serialization: treat as array [x, y, z]
template
inline void to_json_impl(Stream& s, const my_struct& t) {
iguana::to_json(*(int(*)[3])&t, s);
}
template
inline void from_json_impl(my_struct& value, It&& it, It&& end) {
iguana::from_json(*(int(*)[3])&value, it, end);
}
}
struct nest {
std::string name;
my_space::my_struct value;
bool operator==(const nest& o) const {
return name == o.name && value == o.value;
}
};
YLT_REFL(nest, name, value);
int main() {
// Serialize as array
my_space::my_struct v{7, 8, 9};
std::string json;
iguana::to_json(v, json);
std::cout << json << std::endl;
// Output: [7,8,9]
// Works in nested structures
nest n{"Hi", {1, 2, 3}};
std::string json2;
iguana::to_json(n, json2);
std::cout << json2 << std::endl;
// Output: {"name":"Hi","value":[1,2,3]}
// Deserialize
nest n2;
iguana::from_json(n2, json2);
if (n == n2) {
std::cout << "Custom serialization works!" << std::endl;
}
return 0;
}
```
--------------------------------
### Serialize C++ Structs to XML with Iguana
Source: https://context7.com/qicosmos/iguana/llms.txt
Serialize C++ structures to XML format using `iguana::to_xml`. This function supports both minified and pretty-printed output by utilizing a template parameter. Ensure `xml_writer.hpp` is included.
```cpp
#include
#include
struct book_t {
std::string author;
float price;
};
YLT_REFL(book_t, author, price);
struct library_t {
std::string name;
int id;
std::vector book;
};
YLT_REFL(library_t, name, id, book);
int main() {
library_t lib{"Pro Lib", 110, {{"tom", 1.8f}, {"jack", 2.1f}}};
// Minified output
std::string xml;
iguana::to_xml(lib, xml);
std::cout << "Minified:\n" << xml << std::endl;
// Pretty printed output
std::string xml_pretty;
iguana::to_xml(lib, xml_pretty);
std::cout << "Pretty:\n" << xml_pretty << std::endl;
return 0;
}
```
--------------------------------
### Enable Coverage Testing Flags (CMake)
Source: https://github.com/qicosmos/iguana/blob/master/CMakeLists.txt
This snippet enables unit test coverage by setting specific C++ compiler flags. It differentiates between GCC and other compilers, applying '-fprofile-arcs -ftest-coverage --coverage' for GCC and '-fprofile-instr-generate -fcoverage-mapping' for others. The coverage is enabled only if the 'COVERAGE_TEST' option is set to ON.
```cmake
option(COVERAGE_TEST "Build with unit test coverage" OFF)
if(COVERAGE_TEST)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage --coverage")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
endif()
endif()
```
--------------------------------
### Generate YLT_REFL Macros for C++ Structs
Source: https://github.com/qicosmos/iguana/blob/master/README.md
This script automatically generates YLT_REFL macros for C++ structs, enabling compile-time reflection. It takes a C++ source file as input and can either modify it in place or output to a new file. The script supports Python 2.7 and 3.x.
```bash
python automatic_macro_generator.py -h
```
```bash
python automatic_macro_generator.py -i test_macro_generator.cpp
```
```bash
python automatic_macro_generator.py -i test_macro_generator.cpp -o have_macro.cpp
```
--------------------------------
### Serialize and deserialize complex JSON with nested structures
Source: https://context7.com/qicosmos/iguana/llms.txt
Demonstrates serialization and deserialization of complex C++ structures including nested objects, vectors, maps, and unordered maps using Iguana. Requires reflection metadata for all involved types.
```cpp
#include
#include
#include
#include
]]>
nest cdata node]]>
this is a cdata node]]>
)";
node_t node;
iguana::from_xml(node, xml);
std::cout << "Title: " << node.title << std::endl;
std::cout << "Description CDATA: " << node.description.cdata.value() << std::endl;
std::cout << "Root CDATA: " << node.cdata.value() << std::endl;
// Serialize with CDATA preserved
std::string output;
iguana::to_xml(node, output);
std::cout << output << std::endl;
return 0;
}
```
--------------------------------
### Serialize C++ Struct to YAML with Iguana
Source: https://github.com/qicosmos/iguana/blob/master/README.md
Serializes a C++ struct instance ('person') into a YAML string using iguana::to_yaml. The output is directed to an iguana::string_stream. It also demonstrates deserializing a YAML string back into a 'person' struct using iguana::from_yaml. The struct's metadata must be defined for these operations.
```c++
// serialization the structure to the string
person p = {"admin", 20};
iguan
```
--------------------------------
### Handle XML Attributes with iguana::xml_attr_t
Source: https://context7.com/qicosmos/iguana/llms.txt
Manage XML attributes alongside element content using the `iguana::xml_attr_t` wrapper. This allows you to access both attributes and the inner value of an XML node during deserialization and serialization. Requires `xml_reader.hpp` and `xml_writer.hpp`.
```cpp
#include
#include
struct book_t {
std::string title;
std::string author;
};
YLT_REFL(book_t, title, author);
struct library {
iguana::xml_attr_t book;
};
YLT_REFL(library, book);
int main() {
std::string xml = R"(
Harry Potter and the Philosopher's Stone
J.K. Rowling
)";
iguana::xml_attr_t lib;
iguana::from_xml(lib, xml);
// Access attributes
std::cout << "Library: " << lib.attr()["name"] << std::endl;
std::cout << "Book ID: " << lib.value().book.attr()["id"] << std::endl;
std::cout << "Language: " << lib.value().book.attr()["language"] << std::endl;
// Access content
std::cout << "Title: " << lib.value().book.value().title << std::endl;
std::cout << "Author: " << lib.value().book.value().author << std::endl;
// Serialize back
std::string output;
iguana::to_xml(lib, output);
std::cout << output << std::endl;
return 0;
}
```
--------------------------------
### Serialize C++ struct to JSON using iguana::to_json
Source: https://context7.com/qicosmos/iguana/llms.txt
Serializes a C++ struct to JSON format, outputting to a string or stream. Requires the struct to have reflection metadata defined using YLT_REFL. Supports both string and stream outputs.
```cpp
#include
#include
#include
struct person {
std::string name;
int age;
};
YLT_REFL(person, name, age);
int main() {
person p{"tom", 28};
// Method 1: Using string_stream
iguana::string_stream ss;
iguana::to_json(p, ss);
std::cout << ss.str() << std::endl;
// Output: {"name":"tom","age":28}
// Method 2: Using std::string
std::string json_str;
iguana::to_json(p, json_str);
std::cout << json_str << std::endl;
return 0;
}
```