Here is the detailed blog post of how I built it(with screenshots).
- no memory leaks
- no need to free memory: smart pointers are used
- pretty printing
- clone the repository inside a folder
- include header files:
serializer.h print.h - printer.cpp and serializer.cpp should be compiled and linked with your binary
- all entities are defined in SJSON namespace
- create a new Serializer object with JSON string
std::string json_string{"{\"bar\": 10 }"}
SJSON::Serializer foo(json_string);
SJSON::Json_entity_shared_ptr foo_ptr = foo.serialize();Json_entity_shared_ptrhasget_type()method. It returnsSJSON::Data_type- definition of
SJSON::Data_type
enum class Data_type { OBJECT, BOOLEAN, NUMBER, ARRAY, TNULL, STRING };- cast the object to its respective type based on the
Date\_typereturned byget_type()
| Data_type | to be casted to |
|---|---|
| OBJECT | Json_obj_shared_ptr |
| BOOLEAN | Json_bool_shared_ptr |
| NUMBER | Json_number_shared_ptr |
| ARRAY | Json_arr_shared_ptr |
| TNULL | Json_null_shared_ptr |
| STRING | Json_shared_shared_ptr |
- if the
Data_typeis ARRAY,get_value()returnsstd::vector<Json_entity_shared_ptr> - if the
Data_typeis Object,get_value()returnsstd::map<std::string, Json_entity_shared_ptr>
- Use
SJSON::print_json(Json_entity_shared_ptr)to pretty print JSON.
checkout Makefile to replicate tests