{"id":13521246,"url":"https://github.com/RedSkittleFox/serialize","last_synced_at":"2025-03-31T20:30:58.406Z","repository":{"id":218273377,"uuid":"737821025","full_name":"RedSkittleFox/serialize","owner":"RedSkittleFox","description":"C++ 23 serialization library","archived":false,"fork":false,"pushed_at":"2024-01-25T00:07:32.000Z","size":46,"stargazers_count":14,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-21T17:54:28.969Z","etag":null,"topics":["cpp","cpp23","reflections","serialization"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RedSkittleFox.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-01T16:13:32.000Z","updated_at":"2025-03-16T15:36:09.000Z","dependencies_parsed_at":"2024-01-25T01:28:00.173Z","dependency_job_id":"65617bb0-f959-4930-a78b-268a7eb3b81a","html_url":"https://github.com/RedSkittleFox/serialize","commit_stats":null,"previous_names":["redskittlefox/serialize"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RedSkittleFox%2Fserialize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RedSkittleFox%2Fserialize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RedSkittleFox%2Fserialize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RedSkittleFox%2Fserialize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RedSkittleFox","download_url":"https://codeload.github.com/RedSkittleFox/serialize/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246535799,"owners_count":20793325,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","cpp23","reflections","serialization"],"created_at":"2024-08-01T06:00:31.457Z","updated_at":"2025-03-31T20:30:58.128Z","avatar_url":"https://github.com/RedSkittleFox.png","language":"C++","readme":"[![CMake MSVC Windows Build and Test](https://github.com/RedSkittleFox/serialize/actions/workflows/cmake-msvc-windows.yml/badge.svg)](https://github.com/RedSkittleFox/serialize/actions/workflows/cmake-msvc-windows.yml)\n[![CMake CLANG Windows Build and Test](https://github.com/RedSkittleFox/serialize/actions/workflows/cmake-clang-windows.yml/badge.svg)](https://github.com/RedSkittleFox/serialize/actions/workflows/cmake-clang-windows.yml)\n\n# serialize\nis a C++ 23 serialization library. It provides a simple to use interfaces, supports containers, trivial types and aggregates[1] out of the box and provides a simple interface to create custom serialization methods.\n\n[1] [Aggregate](https://en.cppreference.com/w/cpp/language/aggregate_initialization) types are supported via [fox::reflexpr](https://github.com/RedSkittleFox/reflexpr/tree/main) reflections library.\n\n# Examples\n\n## Builtin serializers\nLibrary uses `operator|` to chain together objects to serialize and deserialize. \nBuiltin supported types are:\n* Trivailly copyable types\n* [range-to](https://en.cppreference.com/w/cpp/ranges/to) compatible types\n* [tuple-like](https://en.cppreference.com/w/cpp/utility/tuple/tuple-like) types\n* `std::optional`\n\n```cpp\n#include \u003cfox/serialize.hpp\u003e\n\nnamespace sr = fox::serialize;\n\nstd::string a = \"Capybara\";\nint b = 123;\n\nsr::bit_writer writer;\nwriter | a | b;\n\nsr::bit_reader reader(std::from_range, writer.data());\nstd::string a_out;\nint b_out;\nreader | a_out | b_out;\n```\n\n## Aggregates\nAggregate types are supported through [fox::reflexpr](https://github.com/RedSkittleFox/reflexpr/tree/main) reflections library if available. Library can be disabled by setting `FOX_SERIALIZE_INCLUDE_REFLEXPR` CMAKE flag to OFF.\n\n```cpp\n#include \u003cfox/serialize.hpp\u003e\n\nnamespace sr = fox::serialize;\n\nstruct aggregate\n{\n\tstd::string a;\n\tint b;\n};\n\naggregate a{ \"Fox, 123 };\n\nsr::bit_writer writer;\nwriter | a;\n\nsr::bit_reader reader(std::from_range, writer.data());\naggregate a_out;\nreader | a_out;\n```\n\n## Custom serialization methods\nIt's possible to provide custom serialization method for a given type.\n\n### [Custom `serialize_trait\u003cT\u003e`](sample/sample_custom_0.cpp)\n```cpp\n#include \u003cfox/serialize.hpp\u003e\n\ntemplate\u003c\u003e\nstruct fox::serialize::serialize_traits\u003cstd::unique_ptr\u003cint\u003e\u003e\n{\n\tstatic void serialize(fox::serialize::bit_writer\u0026 writer, const std::unique_ptr\u003cint\u003e\u0026 ptr)\n\t{\n\t\twriter | (static_cast\u003cbool\u003e(ptr));\n\t\tif (ptr)\n\t\t\twriter | (*ptr);\n\t}\n\n\tstatic void deserialize(fox::serialize::bit_reader\u0026 reader, std::unique_ptr\u003cint\u003e\u0026 ptr)\n\t{\n\t\tbool b = fox::serialize::deserialize\u003cbool\u003e(reader);\n\t\tif(b)\n\t\t{\n\t\t\tptr = std::make_unique\u003cint\u003e(fox::serialize::deserialize\u003cint\u003e(reader));\n\t\t}\n\t\telse\n\t\t{\n\t\t\tptr.reset();\n\t\t}\n\t}\n};\n\nvoid sample_custom_0()\n{\n\tfox::serialize::bit_writer writer;\n\n\tauto a = std::make_unique\u003cint\u003e(2);\n\twriter | a;\n\n\tfox::serialize::bit_reader reader(std::from_range, writer.data());\n\n\tstd::unique_ptr\u003cint\u003e b;\n\treader | b;\n}\n```\n\n### [Member functions](sample/sample_custom_1.cpp)\n```cpp\n#include \u003cfox/serialize.hpp\u003e\n\nclass custom_type_1\n{\n\tfriend void sample_custom_1();\n\n\tstd::string v0_;\n\tint v1_;\n\npublic:\n\tcustom_type_1() = default;\n\n\tcustom_type_1(const std::string\u0026 v0, int v1)\n\t\t: v0_(v0), v1_(v1) {}\n\n\tvoid serialize(fox::serialize::bit_writer\u0026 writer) const\n\t{\n\t\twriter | v0_ | v1_;\n\t}\n\n\tvoid deserialize(fox::serialize::bit_reader\u0026 reader)\n\t{\n\t\treader | v0_ | v1_;\n\t}\n};\n\nvoid sample_custom_1()\n{\n\tfox::serialize::bit_writer writer;\n\n\tcustom_type_1 a = custom_type_1(\"Fox\", 1);\n\twriter | a;\n\n\tfox::serialize::bit_reader reader(std::from_range, writer.data());\n\n\tcustom_type_1 b;\n\treader | b;\n}\n```\n\n### [Static member methods](sample/sample_custom_2.cpp)\n```cpp\n#include \u003cfox/serialize.hpp\u003e\n\nclass custom_type_2\n{\n\tfriend void sample_custom_2();\n\n\tstd::string v0_;\n\tint v1_;\n\npublic:\n\tcustom_type_2() = default;\n\n\tcustom_type_2(const std::string\u0026 v0, int v1)\n\t\t: v0_(v0), v1_(v1) {}\n\n\tstatic void serialize(fox::serialize::bit_writer\u0026 writer, const custom_type_2\u0026 obj) \n\t{\n\t\twriter | obj.v0_ | obj.v1_;\n\t}\n\n\tstatic void deserialize(fox::serialize::bit_reader\u0026 reader, custom_type_2\u0026 obj)\n\t{\n\t\treader | obj.v0_ | obj.v1_;\n\t}\n};\n\nvoid sample_custom_2()\n{\n\tfox::serialize::bit_writer writer;\n\n\tcustom_type_2 a = custom_type_2(\"Fox\", 1);\n\twriter | a;\n\n\tfox::serialize::bit_reader reader(std::from_range, writer.data());\n\n\tcustom_type_2 b;\n\treader | b;\n}\n```\n### [Constructor accepting bit_reader](sample/sample_custom_3.cpp)\n```cpp\n#include \u003cfox/serialize.hpp\u003e\n\n#include \u003ciostream\u003e\n#include \u003cformat\u003e\n#include \u003cstring\u003e\n\nclass custom_type_3\n{\n\tfriend void sample_custom_3();\n\n\tstd::string v0_;\n\tint v1_;\n\npublic:\n\tcustom_type_3() = default;\n\n\tcustom_type_3(fox::serialize::from_bit_reader_t, fox::serialize::bit_reader\u0026 reader)\n\t{\n\t\treader | v0_ | v1_;\n\t}\n\n\tcustom_type_3(const std::string\u0026 v0, int v1)\n\t\t: v0_(v0), v1_(v1) {}\n\n\tstatic void serialize(fox::serialize::bit_writer\u0026 writer, const custom_type_3\u0026 obj)\n\t{\n\t\twriter | obj.v0_ | obj.v1_;\n\t}\n\n};\n\nvoid sample_custom_3()\n{\n\tfox::serialize::bit_writer writer;\n\n\tcustom_type_3 a = custom_type_3(\"Fox\", 1);\n\twriter | a;\n\n\tfox::serialize::bit_reader reader(std::from_range, writer.data());\n\n\tcustom_type_3 b;\n\treader | b;\n}\n```\n### [Custom member trait](sample/sample_custom_4.cpp)\n```cpp\n#include \u003cfox/serialize.hpp\u003e\n\nclass custom_type_4\n{\n\tfriend void sample_custom_4();\n\n\tstd::string v0_;\n\tint v1_;\n\npublic:\n\tcustom_type_4() = default;\n\n\tcustom_type_4(const std::string\u0026 v0, int v1)\n\t\t: v0_(v0), v1_(v1) {}\n\n\t// This can be a hand written trait like in a sample samples/sample_custom_0.cpp\n\tusing serialize_trait = fox::serialize::serialize_from_members\u003ccustom_type_4, \u0026custom_type_4::v0_, \u0026custom_type_4::v1_\u003e;\n};\n\nvoid sample_custom_4()\n{\n\tfox::serialize::bit_writer writer;\n\n\tcustom_type_4 a = custom_type_4(\"Fox\", 1);\n\twriter | a;\n\n\tfox::serialize::bit_reader reader(std::from_range, writer.data());\n\n\tcustom_type_4 b;\n\treader | b;\n}\n```\n### Member variable list\n```cpp\nstruct custom_struct\n{\n\tstd::string a;\n\tint b;\n};\n\ntemplate\u003c\u003e\nstruct fox::serialize::serialize_traits\u003ccustom_struct\u003e : \n\tfox::serialize::serialize_from_members\u003ccustom_struct, \u0026custom_struct::a, \u0026custom_struct::b\u003e\n{\n};\n```\n\n# Supported compilers\nThis is a C++ 23 library. All compilers that support `std::ranges::to` and `std::format` should support this library. Following compilers are known to work:\n* Microsoft Visual C++ 2022 (msvc) / Build Tools 19.38.33134 (and possibly later)\n* Microsoft Visual C++ 2022 (clang) 16.0.5 (and possibly later)\n\n# Integration\n[`serialize.hpp`](include/fox/serialize.hpp) is single header library. It requires C++ 23. If you want to enable aggreage serialization, install (single header library) [`fox::reflexpr`](https://github.com/RedSkittleFox/reflexpr) and define `FOX_SERIALIZE_HAS_REFLEXPR` macro before including `fox/serialize.hpp`.\n\n## CMake\n```cmake\ninclude(FetchContent)\n\n# One could disable fox::reflexpr by setting FOX_SERIALIZE_INCLUDE_REFLEXPR to OFF\n# set(FOX_SERIALIZE_INCLUDE_REFLEXPR OFF)\n\nFetchContent_Declare(fox_serialize GIT_REPOSITORY https://github.com/RedSkittleFox/serialize.git)\nFetchContent_MakeAvailable(fox_serialize)\ntarget_link_libraries(foo PRIVATE fox::serialize)\n```\n\n# License\nThis library is licensed under the [MIT License](LICENSE).","funding_links":[],"categories":["序列化"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRedSkittleFox%2Fserialize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FRedSkittleFox%2Fserialize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRedSkittleFox%2Fserialize/lists"}