Thanks for json-tutorial provided by miloyip. This is a small C++ project using for parsing/generating json/string format file.
-
Use standard C++14 grammer and STL without any other libraries.
-
Use CMake to complie codes and generate executable file automatically.
-
Distinct functions are encapsulated in different classes respectively, which provides the only interface (i.e. Json class) for user.
-
Download/Gitclone this source project
git clone [email protected]:Zhirui-Zhang/JsonParser_zzr.git
-
Create build directory and enter
mkdir build && cd build
-
Set CMake compilation mode (Debug/Release)
before this step, maybe you should config CMake enviroment at first
sudo apt-get install cmake
cmake -dcmake_build_type=debug ..
-
Makefile & Run myJson project
make ./myJson
-
Final test result is showed as :
-
We can also use valgrind tool to check if memory leaks
likewise, config valgrind environment before
sudo apt-get install valgrind
valgrind --leak-check=full ./myJson
and the result will be showed as :
-
root directory : store all attachments
-
src directory : store declaration and definition for different classes, including :
-
JsonEnum.h : define
JSON_TYPE
andPARSE_TYPE
enum struct -
Json.h / Json.cpp : define smart pointer member
m_jv
to JsonValue and all member functions -
JsonValue.h / JsonValue.cpp : define
JSON_TYPE
asm_type
member andunion
struct for Json info, etc -
JsonParser.h / JsonParser.cpp : define all member functions using for parsing input string to json
-
JsonStringify.h / JsonStringify.cpp : define all member functions using for generating string from existed json
-
-
JsonTest.cpp : test the whole project and verify parsing/generating functions especially
-
CMakeLists.txt : create auto compilation
-
README.md : introduction to this project
-
Json & JsonValue classes :
-
To reduce compilation dependency between files and avoid memory leak problem, the only member in Json is a smart pointer
m_jv
to JsonValue class, which will destroy and free memory automatically. -
In JsonValue class, we use
m_type
member to indicate type for current json. Besides, we useunion
struct to store json info because a json has only one type among 7 types, which are :null
,true
,false
,number
,string
,array
andobject
-
As for member functions, we use parse/stringify function to connect Parser/Generator class, vector container for array type and map container for object type, also define some common APIs such as
size()
,clear()
,insert()
,erase()
etc.
-
-
Parser class :
-
Provides various member functions to handle different situations, the overall
parse()
function callsparse_value()
to parse specificJSON_TYPE
, returnPARST_TYPE
to indicate parsing result. -
parse_literal()
deals withnull
,true
andfalse
type,parse_number()
deals withnumber
type, follows the rule as
-
parse_string()
deals withstring
type by callingparse_string_raw()
, which only supports UTF-8 characters. Be careful for\uXXXX
hexadecimal format, we useparse_hex4()
to parse it andparse_encode_utf8()
to decode this string. When\uXXXX\uYYYY
surrogate pair occurs, following functioncodepoint = 0x10000 + (H − 0xD800) × 0x400 + (L − 0xDC00)
to transfer it, if the input string is invalid, i.e.
(unsigned char)ch < 0x20
, return PARSE_INVALID_STRING_CHAR. -
parse_array()
deals witharray
type, constructvector<JsonValue>
tmp to store array info, setm_jv
as tmp when meets]
finally. -
parse_object()
deals withobejct
type, constructstring
tmpKey andmap<string, JsonValue>
tmpMap to store [key, val] pair, setm_jv
as tmpMap when meets}
finally.
-
-
Generator class :
- Provides
stringify_value()
to stringify an existed json to string, pass the whole value inm_res
string member. According to input parameterjv.type()
to stringify differentJSON_TYPE
.
- Provides
-
Use C++17 new characteristic such as
std::variant
struct would be better thanunion
struct, since thector
anddtor
inunion
will be complicated and make mistakes easily. -
Use
map<string, JsonValue>
asJSON_OBJECT
container cannot keep the original sequence same as input string. Things will be better when usingvector<pair<string, JsonValue>>
struct instead, but time complexity will increase fromO(logn)
toO(n)
infind
andremove
operation. Therefore, we should consider different containers betweenQuery-Oriented
task andStorage-Oriented
task.