Skip to content

Commit a462ffc

Browse files
committed
Add jsonparser
1 parent a6ba919 commit a462ffc

7 files changed

Lines changed: 109 additions & 0 deletions

File tree

‎c/Json/CMakeLists.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CMAKE_MINIMUM_REQUIRED(VERSION 2.9)
2+
PROJECT(JSONPARSER)
3+
INCLUDE_DIRECTORIES(./include)
4+
AUX_SOURCE_DIRECTORY(./src DIR_SRCS)
5+
ADD_EXECUTABLE(parser ${DIR_SRCS})

‎c/Json/include/jsonparser.h‎

Whitespace-only changes.

‎c/Json/src/CMakeLists.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CMAKE_MINIMUM_REQUIRED(VERSION 2.9)
2+
PROJECT(TEST)
3+
INCLUDE_DIRECTORIES(../include)
4+
AUX_SOURCE_DIRECTORY(./ DIR_SRCS)
5+
ADD_EXECUTABLE(test ${DIR_SRCS})

‎c/Json/test/CMakeLists.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CMAKE_MINIMUM_REQUIRED(VERSION 2.9)
2+
PROJECT(JSONPARSER)
3+
INCLUDE_DIRECTORIES(../include)
4+
AUX_SOURCE_DIRECTORY(./ DIR_SRCS)
5+
ADD_EXECUTABLE(parser ${DIR_SRCS})

‎c/Json/test/jsonparser.c‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
int jumpChar(const char *string, int *index, char Char) {
4+
while (string[*index] == ' ') {
5+
index++;
6+
}
7+
return *index;
8+
}
9+
int jumpNULL(const char *string, int *index) {
10+
while (string[*index] == ' ' || string[*index] == '\n') {
11+
12+
index++;
13+
}
14+
return *index;
15+
}
16+
int indexCharInString(const char *string, int length, const char target) {
17+
int i;
18+
for (i = 0; i < length; i++) {
19+
if (string[i] == target) {
20+
{ return i; }
21+
}
22+
}
23+
return -1;
24+
}
25+
int sliceString(const char *string, int index, int length) { return 0; }
26+
27+
int indexStringInString(const char *string, int length, const char *target) {
28+
int index = 0;
29+
index = indexCharInString(string + index, length - strlen(target), target[0]);
30+
while (memcmp(string + index, target, strlen(target)) != 0) {
31+
if (index > length) {
32+
return -1;
33+
} else {
34+
index++;
35+
index =
36+
indexCharInString(string + index, length - strlen(target), target[0]);
37+
}
38+
}
39+
return index;
40+
}
41+
42+
int main() {
43+
char json[] = "{\"int1\":1,\"str1\":\"2\",\"double1\":3.14,\"bool1\":true,"
44+
"\"char1\":48,\"null1\":null,\"array1\":[\"string\",1,null,"
45+
"true,false,3.14],\"json1\":{\"id\":0,\"status\":200}}";
46+
struct json1 {
47+
int int1;
48+
char *str1;
49+
double double1;
50+
_Bool bool1;
51+
char char1;
52+
void *null1;
53+
void **array1;
54+
struct json1 *next;
55+
};
56+
int strindex = 0;
57+
jumpNULL(json, &strindex);
58+
if (json[strindex] != '{') {
59+
printf("json parse failed");
60+
} else {
61+
strindex++;
62+
jumpNULL(json, &strindex);
63+
if (json[strindex] != '"') {
64+
{ jumpNULL(json, &strindex); }
65+
}
66+
if (json[strindex] != '"') {
67+
printf("json parse failed");
68+
} else {
69+
}
70+
}
71+
}

‎c/Json/test/test.json‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"int1": 1,
3+
"str1": "2",
4+
"double1": 3.14,
5+
"bool1": true,
6+
"char1": 48,
7+
"null1": null,
8+
"array1": [
9+
"string",
10+
1,
11+
null,
12+
true,
13+
false,
14+
3.14
15+
],
16+
"json1": {
17+
"id": 0,
18+
"status": 200
19+
}
20+
}
21+
//读取文件 读到字符串解析到字符串 读到除字符串外空格跳过 读到除字符串外{}解析到子json 读到除字符串外[]解析到子json
22+
char json[] ="{\"int1\":1,\"str1\":\"2\",\"double1\":3.14,\"bool1\":true,\"char1\":48,\"null1\":null,\"array1\":[\"string\",1,null,true,false,3.14],\"json1\":{\"id\":0,\"status\":200}}";

‎c/Json/test/test2.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"int1":1,"str1":"2","double1":3.14,"bool1":true,"char1":48,"null1":null,"array1":["string",1,null,true,false,3.14],"json1":{"id":0,"status":200}}

0 commit comments

Comments
 (0)