来自
从零开始的 JSON 库教程
从零开始教授如何写一个符合标准的 C 语言 JSON 库
作者 Milo Yip
https://zhuanlan.zhihu.com/json-tutorial
根据第二课教程 自己重新编写 做了一点修改 加深学习印象
这里写的有点随便 怎么方便怎么来
浮点的判断也是使用的函数 判断范围比json的要宽松些 仅作为练手练习使用
1 #ifndef LEPTJSON_H 2 #define LEPTJSON_H 3 #include <string> 4 5 6 7 8 enum DefParseResult{ 9 DEF_PARSE_OK = 0, 10 DEF_PARSE_EXPECT_VALUE, 11 DEF_PARSE_INVALID_VALUE, 12 DEF_PARSE_ROOT_NOT_SINGULAR 13 }; 14 15 enum DefType { 16 DEF_NONE = 0, 17 DEF_NULL, 18 DEF_FALSE, 19 DEF_TRUE, 20 DEF_NUMBER, 21 DEF_STRING, 22 DEF_ARRAY, 23 DEF_OBJECT 24 }; 25 26 struct MyJsonStruct { 27 double d; 28 std::string jsonStr; 29 size_t index; 30 DefType type; 31 }; 32 33 34 DefParseResult DefParse(MyJsonStruct& jstruct); 35 MyJsonStruct InitJsonStruct(char* str); 36 DefType GetJsonStructType( MyJsonStruct& jstruct); 37 double GetJsonStructNumber( MyJsonStruct& jstruct); 38 39 #endif // LEPTJSON_H