{"id":23459577,"url":"https://github.com/d-led/picojson_serializer","last_synced_at":"2025-04-14T04:15:06.709Z","repository":{"id":10871829,"uuid":"13158812","full_name":"d-led/picojson_serializer","owner":"d-led","description":"a simple header-only json serialization solution for c++ based on picojson","archived":false,"fork":false,"pushed_at":"2016-12-25T14:24:11.000Z","size":690,"stargazers_count":12,"open_issues_count":0,"forks_count":4,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-14T04:15:00.874Z","etag":null,"topics":["cplusplus","header-only","lightweight","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/d-led.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}},"created_at":"2013-09-27T19:05:58.000Z","updated_at":"2023-01-05T21:17:55.000Z","dependencies_parsed_at":"2022-09-03T00:00:48.826Z","dependency_job_id":null,"html_url":"https://github.com/d-led/picojson_serializer","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/d-led%2Fpicojson_serializer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/d-led%2Fpicojson_serializer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/d-led%2Fpicojson_serializer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/d-led%2Fpicojson_serializer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/d-led","download_url":"https://codeload.github.com/d-led/picojson_serializer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248819408,"owners_count":21166477,"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":["cplusplus","header-only","lightweight","serialization"],"created_at":"2024-12-24T06:15:43.940Z","updated_at":"2025-04-14T04:15:06.690Z","avatar_url":"https://github.com/d-led.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"picojson serializer\n===================\n\nThis is a lightweight header-only solution for serializing objects to and from json using the header-only library [picojson](https://github.com/kazuho/picojson).\n\n[![Build Status](https://travis-ci.org/d-led/picojson_serializer.png?branch=master)](https://travis-ci.org/d-led/picojson_serializer) [![Coverage Status](https://coveralls.io/repos/d-led/picojson_serializer/badge.png?branch=master)](https://coveralls.io/r/d-led/picojson_serializer?branch=master) [![Coverity Status](https://scan.coverity.com/projects/3010/badge.svg)](https://scan.coverity.com/projects/3010)\n\nCurrent release: [v0.10](https://github.com/d-led/picojson_serializer/releases/tag/v0.10)\n\na more feature-rich alternative: [cereal](https://github.com/USCiLab/cereal)\n\nusage\n=====\n\nbuilding\n--------\n\nMake sure `picojson.h` can be found, include `picojson_serialization.h`, no extra build steps necessary.\n\n[Premake5](https://premake.github.io/) is included and can be used to generate build files in the Build folder. To build and run the tests, do:\n\n    [path_to]/premake5 gmake\n    make -C Build\n\ndeclaring objects as serializable\n---------------------------------\n\nThe API is similar to [hiberlite](https://github.com/paulftw/hiberlite)'s or [Boost.Serialization](http://www.boost.org/doc/libs/1_54_0/libs/serialization/doc/tutorial.html#serializablemembers)'s:\n\n````cpp\n/// A JSON serializable class\nstruct Point {\n    double x, y, z;\n\n    friend class picojson::convert::access;\n    template\u003cclass Archive\u003e\n    void json(Archive \u0026 ar)\n    {\n        ar \u0026 picojson::convert::member(\"x\", x);\n        ar \u0026 picojson::convert::member(\"y\", y);\n        ar \u0026 picojson::convert::member(\"z\", z);\n    }\n};\n\n/// Composed serialization is possible\nstruct NamedPoint {\n    std::string name;\n    Point point;\n\n    friend class picojson::convert::access;\n    template\u003cclass Archive\u003e\n    void json(Archive \u0026 ar)\n    {\n        ar \u0026 picojson::convert::member(\"name\", name);\n        ar \u0026 picojson::convert::member(\"point\", point);\n    }\n};\n````\n\n\nserializing\n-----------\n\n````cpp\nNamedPoint np = { \"test point\" , { 1 , 2 , 3 } };\npicojson::value npv = picojson::convert::to_value(np);\nstd::string nps = picojson::convert::to_string(np);\n````\n\nresulting in\n````js\n{\"name\":\"test point\",\"point\":{\"x\":1,\"y\":2,\"z\":3}}\n````\n\ndeserializing\n-------------\n\n````cpp\nNamedPoint np;\nstd::string json=...\npicojson::convert::from_string(json,np);\n````\n\nCurrently, if deserialization fails for a member, that member is not modified.\n\nnon-intrusive serialization\n---------------------------\n\nA class that cannot be extended, but the internals are accessible (following to the [Boost.Serialization](http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc/serialization.html#free) api):\n\n````cpp\nstruct Untouchable {\n    int value;\n};\n````\n\nWith a free function defined in the `::picojson::convert` namespace,\n\n````cpp\nnamespace picojson {\n    namespace convert {\n\n        template \u003cclass Archive\u003e\n        void json(Archive \u0026ar, Untouchable \u0026u) {\n            ar \u0026 picojson::convert::member(\"value\", u.value);\n        }\n\n    }\n}\n````\nserialization is again possible:\n\n````cpp\nUntouchable example = { 42 };\nstd::string example_string( picojson::convert::to_string(example) );\n\nUntouchable example_deserialized = { 0 };\npicojson::convert::from_string( example_string, example_deserialized );\nCHECK( example.value == example_deserialized.value );\n````\n\nserializing containers\n----------------------\n\nTo enable `std::vector` serialization, use the header `picojson_vector_serializer.h` and likewise for the other supported container types.\n\n### List of supported standard library containers ###\n\n- vector\n- map\n- multimap\n- set\n- multiset\n\nserializing const data\n----------------------\n\nTo serialize `const` data types (including the keys of `std::map`, `std::multimap`, `std::set`, and `std::multiset`), the `json()` member must be overloaded for `const` objects. The normal `void(Archive\u0026)` function template as explained above works on non-`const` objects (`Point` and `NamedPoint` in the above example). If a `const` object is to be serialized, an additional `const` version of the `json()` function must be defined. E.g.\n\n````cpp\nstruct NamedPoint {\n    // in addition to the non-const version\n    template\u003cclass Archive\u003e\n    void json(Archive \u0026 ar) const\n    {\n        ar \u0026 picojson::convert::member(\"name\", name);\n        ar \u0026 picojson::convert::member(\"point\", point);\n    }\n};\n````\n\nFree function version can also be overloaded\n\n````cpp\ntemplate \u003cclass Archive\u003e\nvoid json(Archive \u0026ar, Point const \u0026p) {\n    ar \u0026 ...\n}\n````\n\ninitializing the object upon serialization\n------------------------------------------\n\nas a convenience, the function `picojson::convert::init_from_string` can be used instead of `from_string`\nto default-initialize the object before deserialization.\n\n\nimplementing custom value converters\n------------------------------------\n\nThe serialization can be easily customized for types that are not default-convertible. Example class:\n\n````cpp\nstruct Example {\n\tenum Status {\n\t\tNONE = 0,\n\t\tSOME,\n\t\tSOME_OTHER\n\t};\n\n\tStatus status;\n\n\tfriend class picojson::convert::access;\n\ttemplate\u003cclass Archive\u003e\n\tvoid json(Archive \u0026 ar)\n\t{\n\t\tar \u0026 picojson::convert::member(\"status\", status);\n\t}\n};\n````\n\nAttempting to serialize instances of `Example` should lead to compile error:\n\n````cpp\nExample e = { Example::NONE };\npicojson::value ev = picojson::convert::to_value(e);\n````\n\nHowever, you can specialize the value converter for the enum, i.e.:\n\n````cpp\nnamespace picojson {\nnamespace convert {\n\n\ttemplate\u003c\u003e struct value_converter\u003cExample::Status\u003e {\n\t\tstatic value to_value(Example::Status v) {\n\t\t\treturn value(static_cast\u003cdouble\u003e(v));\n\t\t}\n\n\t\tstatic void from_value(value const\u0026 ov, Example::Status\u0026 v) {\n\t\t\tif ( ov.is\u003cdouble\u003e() ) v = Example::Status(static_cast\u003cint\u003e(ov.get\u003cdouble\u003e()));\n\t\t}\n\t};\n}\n}\n````\n\nand the test passes:\n\n````cpp\nExample e = { Example::SOME };\npicojson::value ev = picojson::convert::to_value(e);\nCHECK(has\u003cdouble\u003e(ev, \"status\", static_cast\u003cint\u003e(Example::SOME)));\n````\n\n\nmapping between unrelated types\n-------------------------------\n\nIf you have serializable types that may be unrelated, such as a logic component and a data transfer object, you can 'project' the data from one object to another simply by mapping the values through the serialization like so:\n\n````cpp\nClass1 c1=...;\nClass2 c2=picojson::project::from(c1).onto\u003cClass2\u003e();\n````\n\nlicense\n=======\n\n- Copyright 2013, Dmitry Ledentsov\n- Copyright 2014, project contributors\n- [MIT License](http://www.opensource.org/licenses/mit-license.php)\n\n\ndependencies\n============\n- [picojson](https://github.com/kazuho/picojson) as the main dependency\n- [msinttypes](https://code.google.com/p/msinttypes/) for in64 in MSVC\n- [premake](industriousone.com/premake) for generating build files\n\nDependencies retain their respective licenses\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fd-led%2Fpicojson_serializer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fd-led%2Fpicojson_serializer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fd-led%2Fpicojson_serializer/lists"}