1+ ![ logo] ( logo.png )
2+
13# ** mysvac-jsonlib**
24
35A simple and efficient C++20 JSON parsing library.
@@ -7,3 +9,119 @@ A simple and efficient C++20 JSON parsing library.
79- Excellent performance.
810- Easy to use.
911- Support streaming parsing and CRUD.
12+
13+ > -> ** [ Performance Comparison] ( https://github.com/Mysvac/cpp-jsonlib?tab=readme-ov-file#lib_compare ) **
14+
15+ ## Install
16+
17+ The library only contains one module interface file, which can be downloaded and used directly.
18+
19+ The author recommends using vcpkg:
20+
21+ ``` shell
22+ vcpkg install mysvac-jsonlib;
23+ ```
24+
25+ CMakeLists.txt config:
26+
27+ ``` cmake
28+ find_package(mysvac-jsonlib CONFIG REQUIRED)
29+ ...
30+ target_link_mysvac_jsonlib(main PRIVATE)
31+ ```
32+
33+ > ` target_link_mysvac_jsonlib ` includes ` target_link_library ` and ` target_sources ` , the latter is used to add module compilation targets.
34+
35+ cpp files:
36+
37+ ``` cpp
38+ import mysvac.json;
39+ using namespace mysvac ;
40+ ```
41+
42+
43+ ## Example
44+
45+ ### 1. init and get_ref
46+
47+
48+ ``` cpp
49+ // create by initialize_list
50+ Json val = Json::Obj{
51+ { "key1", 42 },
52+ { "key2", "value2" },
53+ { "key3", true },
54+ { "arr", Json::Arr{ { 2, 3.14, nullptr } } },
55+ { "obj", Json::Obj{ { "nested_k", "nested_v" } } }
56+ };
57+
58+ // use is_xxx() or type() to determine type
59+ std::cout << val.is_arr(); << std::endl; // 0 (false)
60+ json::Type type = val[" key3" ].type(); // json::Type::eBol
61+
62+ // use xxx() to get ref
63+ double & key1 = val[" key1" ].num(); // get reference
64+ key1 = 55.0 ;
65+
66+ // operators== can be used for all types, noexcept
67+ std::cout << val[" arr" ][3 ] == " hello" << std::endl; // 1
68+
69+ // auxiliary function, such as size, insert, contains
70+ val[" arr" ].push_back( " hello" );
71+
72+ val[2 ]; // throw runtime_error, because val isn't array
73+
74+ val.reset(); // clear date and become `Nul` type
75+ ```
76+
77+ There are six functions for getting internal ref: ` arr() ` ` obj() ` ` num() ` ` str() ` ` bol() ` ` nul() ` , and six functions ` is_xxx() ` for determining internal types.
78+
79+ ### 2. serialize and deserialize
80+
81+ ``` cpp
82+ // top-level can be any JSON type
83+ std::string json_val_str = R"( false )" ;
84+ // deserialize by `parse` function, return a option<>
85+ std::optional<Json> = Json::parse(json_val_str);
86+
87+ std::string json_str = R"( [ 1, false, null, { "Hello": "World" } ] )" ;
88+ Json val = Json::parse(json_str).value_or(nullptr );
89+ // `parse` can also input `istream`
90+
91+ // serialize by dump or write
92+ std::string text = val.dump(); // return string
93+ // write to ostream or string back
94+ val.write( std::cout );
95+
96+ // serialize_pretty by dumpf or writef
97+ auto pretty = val.dumpf(); // default indentation is 2 spaces
98+ std::string buffer;
99+ val.writef( buffer, 4 ); // set indentation as 4 spaces
100+ ```
101+
102+ ### 3. to and move API
103+
104+ ``` cpp
105+ std::string json_str = R"( [ 1, false, null, { "Hello": "World" } ] )" ;
106+ Json val = Json::parse(json_str).value_or(nullptr );
107+
108+ // use `to<>` to copy inner data and cast type
109+ int t1 = val[0 ].to<int >(); // 1
110+
111+ // use to_if or to_or cast data safely
112+ std::optional<int > t2 = val[2 ].to_if<int >(); // std::nullopt
113+ double t3 = val[3 ].to_or<double >( 3.14 ); // 3.14
114+
115+ // to `move<>` to move inner data and cast type
116+ std::string world = val[3 ][" Hello" ].move<std::string>();
117+ // ↑ Prioritize using `move` over `copy`
118+
119+ // or use `move_if` `move_or` safely
120+ ```
121+
122+ ## README
123+
124+ For more guidelines, please refer to the [ readme] ( https://github.com/Mysvac/cpp-jsonlib?tab=readme-ov-file#ENGLISH ) .
125+
126+ ---
127+
0 commit comments