Skip to content

Commit 956fc48

Browse files
committed
json02_answear
1 parent 22c051f commit 956fc48

File tree

22 files changed

+505
-359
lines changed

22 files changed

+505
-359
lines changed

tutorial01/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
1111
endif()
1212

1313
# 添加库(假设 leptjson 是 C 源代码)
14-
add_library(leptjson STATIC leptjson.cpp) # 修改5:显式声明为静态库
14+
add_library(leptjson STATIC leptjson.cpp ../tutorial02/leptjson.cpp ../tutorial01_answer/leptjson.h ../tutorial02/test.cpp ../tutorial01_answer/test.cpp) # 修改5:显式声明为静态库
1515

1616
# 添加测试可执行文件(注意:源文件改为 .cpp 后缀)
1717
add_executable(leptjson_test test.cpp) # 修改6:C++ 源文件

tutorial01/cmake-build-debug/.cmake/api/v1/reply/codemodel-v2-fe0ce344029c80c9654c.json renamed to tutorial01/cmake-build-debug/.cmake/api/v1/reply/codemodel-v2-ddaabd7d3c7bf294442e.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
{
4242
"directoryIndex" : 0,
4343
"id" : "leptjson::@6890427a1f51a3e7e1df",
44-
"jsonFile" : "target-leptjson-Debug-25b4af56ad05331ef260.json",
44+
"jsonFile" : "target-leptjson-Debug-6921ffe562341ecd4357.json",
4545
"name" : "leptjson",
4646
"projectIndex" : 0
4747
},

tutorial01/cmake-build-debug/.cmake/api/v1/reply/index-2025-04-22T07-54-45-0842.json renamed to tutorial01/cmake-build-debug/.cmake/api/v1/reply/index-2025-05-19T12-18-04-0181.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"objects" :
2727
[
2828
{
29-
"jsonFile" : "codemodel-v2-fe0ce344029c80c9654c.json",
29+
"jsonFile" : "codemodel-v2-ddaabd7d3c7bf294442e.json",
3030
"kind" : "codemodel",
3131
"version" :
3232
{
@@ -86,7 +86,7 @@
8686
},
8787
"codemodel-v2" :
8888
{
89-
"jsonFile" : "codemodel-v2-fe0ce344029c80c9654c.json",
89+
"jsonFile" : "codemodel-v2-ddaabd7d3c7bf294442e.json",
9090
"kind" : "codemodel",
9191
"version" :
9292
{
Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@
6868
},
6969
"sourceIndexes" :
7070
[
71-
0
71+
0,
72+
1,
73+
4
7274
]
7375
}
7476
],
@@ -86,7 +88,17 @@
8688
"name" : "Source Files",
8789
"sourceIndexes" :
8890
[
89-
0
91+
0,
92+
1,
93+
3,
94+
4
95+
]
96+
},
97+
{
98+
"name" : "Header Files",
99+
"sourceIndexes" :
100+
[
101+
2
90102
]
91103
}
92104
],
@@ -97,6 +109,28 @@
97109
"compileGroupIndex" : 0,
98110
"path" : "leptjson.cpp",
99111
"sourceGroupIndex" : 0
112+
},
113+
{
114+
"backtrace" : 1,
115+
"compileGroupIndex" : 0,
116+
"path" : "D:/clion/json/json-tutorial/tutorial02/leptjson.cpp",
117+
"sourceGroupIndex" : 0
118+
},
119+
{
120+
"backtrace" : 1,
121+
"path" : "D:/clion/json/json-tutorial/tutorial01_answer/leptjson.h",
122+
"sourceGroupIndex" : 1
123+
},
124+
{
125+
"backtrace" : 1,
126+
"path" : "D:/clion/json/json-tutorial/tutorial02/test.c",
127+
"sourceGroupIndex" : 0
128+
},
129+
{
130+
"backtrace" : 1,
131+
"compileGroupIndex" : 0,
132+
"path" : "D:/clion/json/json-tutorial/tutorial01_answer/test.cpp",
133+
"sourceGroupIndex" : 0
100134
}
101135
],
102136
"type" : "STATIC_LIBRARY"
-1 KB
Binary file not shown.
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# ninja log v5
2-
1 1657 7670225253244936 CMakeFiles/leptjson.dir/leptjson.cpp.obj 2bedf8c3071cc91e
3-
125 1679 7670225253254912 CMakeFiles/leptjson_test.dir/test.cpp.obj e7623431d8599d6
4-
1661 2164 7670225258384677 libleptjson.a 58ee4819385b7ba4
2+
4 986 7685873633884002 CMakeFiles/leptjson.dir/leptjson.cpp.obj 2bedf8c3071cc91e
53
6 1016 7670231665803424 CMakeFiles/leptjson_test.dir/test.cpp.obj e7623431d8599d6
6-
7 931 7670236230211863 CMakeFiles/leptjson.dir/leptjson.cpp.obj 2bedf8c3071cc91e
7-
932 1166 7670236232566803 libleptjson.a 58ee4819385b7ba4
8-
1166 1452 7670236235379284 leptjson_test.exe 97e7f204705cd6c2
4+
987 1322 7685873637264964 libleptjson.a 58ee4819385b7ba4
5+
1322 2056 7685873644561010 leptjson_test.exe 97e7f204705cd6c2
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Start testing: May 04 20:24 �й���׼ʱ��
1+
Start testing: May 19 20:18 �й���׼ʱ��
22
----------------------------------------------------------
3-
End testing: May 04 20:24 �й���׼ʱ��
3+
End testing: May 19 20:18 �й���׼ʱ��

tutorial01/cmake-build-debug/build.ninja

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ build CMakeFiles/leptjson.dir/leptjson.cpp.obj: CXX_COMPILER__leptjson_Debug D$:
5555
OBJECT_DIR = CMakeFiles\leptjson.dir
5656
OBJECT_FILE_DIR = CMakeFiles\leptjson.dir
5757

58+
build CMakeFiles/leptjson.dir/D_/clion/json/json-tutorial/tutorial02/leptjson.cpp.obj: CXX_COMPILER__leptjson_Debug D$:/clion/json/json-tutorial/tutorial02/leptjson.cpp || cmake_object_order_depends_target_leptjson
59+
DEP_FILE = CMakeFiles\leptjson.dir\D_\clion\json\json-tutorial\tutorial02\leptjson.cpp.obj.d
60+
FLAGS = -Wall -Wextra -pedantic -g -fdiagnostics-color=always -Wno-gnu-anonymous-struct -Wno-c99-extensions -std=gnu++11
61+
OBJECT_DIR = CMakeFiles\leptjson.dir
62+
OBJECT_FILE_DIR = CMakeFiles\leptjson.dir\D_\clion\json\json-tutorial\tutorial02
63+
64+
build CMakeFiles/leptjson.dir/D_/clion/json/json-tutorial/tutorial01_answer/test.cpp.obj: CXX_COMPILER__leptjson_Debug D$:/clion/json/json-tutorial/tutorial01_answer/test.cpp || cmake_object_order_depends_target_leptjson
65+
DEP_FILE = CMakeFiles\leptjson.dir\D_\clion\json\json-tutorial\tutorial01_answer\test.cpp.obj.d
66+
FLAGS = -Wall -Wextra -pedantic -g -fdiagnostics-color=always -Wno-gnu-anonymous-struct -Wno-c99-extensions -std=gnu++11
67+
OBJECT_DIR = CMakeFiles\leptjson.dir
68+
OBJECT_FILE_DIR = CMakeFiles\leptjson.dir\D_\clion\json\json-tutorial\tutorial01_answer
69+
5870

5971
# =============================================================================
6072
# Link build statements for STATIC_LIBRARY target leptjson
@@ -63,7 +75,7 @@ build CMakeFiles/leptjson.dir/leptjson.cpp.obj: CXX_COMPILER__leptjson_Debug D$:
6375
#############################################
6476
# Link the static library libleptjson.a
6577

66-
build libleptjson.a: CXX_STATIC_LIBRARY_LINKER__leptjson_Debug CMakeFiles/leptjson.dir/leptjson.cpp.obj
78+
build libleptjson.a: CXX_STATIC_LIBRARY_LINKER__leptjson_Debug CMakeFiles/leptjson.dir/leptjson.cpp.obj CMakeFiles/leptjson.dir/D_/clion/json/json-tutorial/tutorial02/leptjson.cpp.obj CMakeFiles/leptjson.dir/D_/clion/json/json-tutorial/tutorial01_answer/test.cpp.obj
6779
LANGUAGE_COMPILE_FLAGS = -Wall -Wextra -pedantic -g
6880
OBJECT_DIR = CMakeFiles\leptjson.dir
6981
POST_BUILD = cd .
0 Bytes
Binary file not shown.

tutorial01/leptjson.cpp

Lines changed: 72 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,98 @@
11
#include "leptjson.h"
22
#include <cassert> /* assert() */
3-
#include <stdexcept> /* NULL */
4-
#include <string>
3+
#include <cctype> /* NULL */
4+
#include <bitset>
55

66
namespace lept{
7+
struct Context{
8+
const char* json;
9+
};
710

8-
// 解析上下文
9-
class Context{
10-
public:
11-
explicit Context(const std::string& json):json_(json),pos_(0){}
11+
#define EXPECT(c,ch) do {assert(*c.json==(ch));c.json++;}while(0)
1212

13-
//获取当前字符
14-
char current()const{
15-
return pos_<json_.size()?json_[pos_]:'\0';
16-
}
13+
static void parse_whitespace(Context& c){
14+
const char* p = c.json;
15+
while(*p ==' '||*p=='\t'||*p=='\n'||*p=='\r')
16+
++p;
17+
c.json=p;
18+
}
1719

18-
//移动到下一个字符
19-
void advance(){
20-
if(pos_<json_.size())++pos_;
20+
static ParseResult parse_true(Context& c, Value& v){
21+
EXPECT(c,'t');
22+
if(c.json[0]!='r'||c.json[1]!='u'||c.json[2]!='e'){
23+
return ParseResult::PARSE_INVALID_VALUE;
2124
}
25+
c.json+=3;
26+
v.type=Type::TRUE;
27+
return ParseResult::PARSE_OK;
28+
}
2229

23-
//验证当前字符是否符合预期
24-
void expect(char ch){
25-
if(current()!=ch){
26-
throw std::runtime_error("unexpected character");
27-
}
28-
advance();
30+
static ParseResult parse_false(Context& c, Value& v){
31+
EXPECT(c, 'f');
32+
if(c.json[0]!='a'||c.json[1]!='l'||c.json[2]!='s'||c.json[3]!='e'){
33+
return ParseResult::PARSE_INVALID_VALUE;
2934
}
35+
c.json+=4;
36+
v.type=Type::FALSE;
37+
return ParseResult::PARSE_OK;
38+
}
3039

31-
//跳过空白字符
32-
void skip_whitespace(){
33-
while(current()!=' '||current()!='\t'||current()!='\n'||current()!='\r'){
34-
advance();
35-
}
40+
static ParseResult parse_null(Context& c, Value& v){
41+
EXPECT(c,'n');
42+
if(c.json[0]!='u'||c.json[1]!='l'||c.json[2]!='l'){
43+
return ParseResult::PARSE_INVALID_VALUE;
3644
}
45+
c.json+=3;
46+
v.type=Type::NULL_;
47+
return ParseResult::PARSE_OK;
48+
}
3749

38-
//查看当前解析位置之后的字符
39-
char peek(size_t offset=0)const{
40-
size_t index = pos_ + offset;
41-
return index<json_.size()?json_[index]:'\0';
50+
static ParseResult parse_value(Context& c, Value& v){
51+
switch (*c.json) {
52+
case 't': return parse_true(c,v);
53+
case 'f': return parse_false(c,v);
54+
case 'n': return parse_null(c,v);
55+
case '\0': return ParseResult::PARSE_EXPECT_VALUE;
56+
default: return ParseResult::PARSE_INVALID_VALUE;
4257
}
43-
private:
44-
std::string json_; //JSON字符串
45-
size_t pos_;//当前解析位置
58+
}
4659

47-
};
4860

49-
//解析null值
50-
int parse_null(Context& c,Value* v){
51-
c.expect('n');
52-
if(c.peek(0)!='u'||c.peek(1)!='l'||c.peek(2)!='l'){
53-
return ParseError::ParseInvalidValue;
54-
}
55-
c.advance();c.advance();c.advance();
56-
v->type=Type::Null;
57-
return ParseError::ParseOk;
61+
static int lept_parse_number(lept_context* c, lept_value* v) {
62+
char* end;
63+
/* \TODO validate number */
64+
v->n = strtod(c->json, &end);
65+
if (c->json == end)
66+
return LEPT_PARSE_INVALID_VALUE;
67+
c->json = end;
68+
v->type = LEPT_NUMBER;
69+
return LEPT_PARSE_OK;
70+
}
71+
static ParseResult parse_number(Context& c,Value& v){
72+
std::bitset<8> binary()
5873
}
5974

60-
//解析值
61-
int parse_value(Context& c,Value *v){
62-
switch(c.current()){
63-
case 'n':return parse_null(c,v);//如果当前字符为'n',尝试解析为null
64-
case '\0':return ParseError::ParseExpectValue;
65-
default: return ParseError::ParseInvalidValue;
75+
ParseResult parse(Value& v, const std::string& json){
76+
Context c;
77+
c.json=json.c_str();
78+
v.type = Type::NULL_;
79+
parse_whitespace(c);
80+
auto result = parse_value(c,v);
81+
if(result==ParseResult::PARSE_OK){
82+
parse_whitespace(c);
83+
if(*c.json!='\0'){
84+
result = ParseResult::PARSE_ROOT_NOT_SINGULAR;
85+
}
6686
}
87+
return result;
6788
}
6889

69-
//主解析函数
70-
int type_parse(Value* v,const std::string& json){
71-
assert(v!= nullptr);
72-
Context c(json);
73-
v->type=Type::Null;
74-
c.skip_whitespace();
75-
return parse_value(c,v);
90+
Type get_type(const Value& v){
91+
return v.type;
7692
}
77-
78-
//获取类型
79-
Type get_type(const Value* v){
80-
assert(v!= nullptr);
81-
return v->type;
93+
double get_number(const Value& v){
94+
return v.n;
8295
}
8396
}
8497

8598

86-
87-
88-

0 commit comments

Comments
 (0)