Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example 풀리퀘스트 #133

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mple/myjson.o
jsmn.o
libjsmn.a
myexample
*.o
89 changes: 48 additions & 41 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,41 +1,48 @@
# You can put your build options here
-include config.mk

all: libjsmn.a

libjsmn.a: jsmn.o
$(AR) rc $@ $^

%.o: %.c jsmn.h
$(CC) -c $(CFLAGS) $< -o $@

test: test_default test_strict test_links test_strict_links
test_default: test/tests.c
$(CC) $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@
test_strict: test/tests.c
$(CC) -DJSMN_STRICT=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@
test_links: test/tests.c
$(CC) -DJSMN_PARENT_LINKS=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@
test_strict_links: test/tests.c
$(CC) -DJSMN_STRICT=1 -DJSMN_PARENT_LINKS=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@

jsmn_test.o: jsmn_test.c libjsmn.a

simple_example: example/simple.o libjsmn.a
$(CC) $(LDFLAGS) $^ -o $@

jsondump: example/jsondump.o libjsmn.a
$(CC) $(LDFLAGS) $^ -o $@

clean:
rm -f *.o example/*.o
rm -f *.a *.so
rm -f simple_example
rm -f jsondump

.PHONY: all clean test

# You can put your build options here
-include config.mk

all: libjsmn.a


libjsmn.a: jsmn.o
$(AR) rc $@ $^

%.o: %.c jsmn.h
$(CC) -c $(CFLAGS) $< -o $@ # define하려면 여기를 수정해야 함

test: test_default test_strict test_links test_strict_links
test_default: test/tests.c
$(CC) $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@
test_strict: test/tests.c
$(CC) -DJSMN_STRICT=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@
test_links: test/tests.c
$(CC) -DJSMN_PARENT_LINKS=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@
test_strict_links: test/tests.c
$(CC) -DJSMN_STRICT=1 -DJSMN_PARENT_LINKS=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@

jsmn_test.o: jsmn_test.c libjsmn.a

myexample: example/myjson.o libjsmn.a
$(CC) $(LDFLAGS) $^ -o $@

jsondump: example/jsondump.o libjsmn.a
$(CC) $(LDFLAGS) $^ -o $@

filesimple: mysource/filesimple.o libjsmn.a
$(CC) $(LDFLAGS) $^ -o $@

OSS_21000127: example/OSS_21000127.o libjsmn.a
$(CC) $(LDFLAGS) $^ -o $@

clean:
rm -f *.o example/*.o
rm -f *.a *.so
rm -f myexample
rm -f jsondump
rm -f OSS_21000127

.PHONY: all clean test
Binary file added OSS_21000127
Binary file not shown.
28 changes: 28 additions & 0 deletions cars.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"cars" : [
{
"model" : "Sonata",
"maker" : "Hyundai",
"year" : 2016,
"gas" : "gasoline"
},
{
"model" : "Avante",
"maker" : "Hyundai",
"year" : 2015,
"gas" : "diesel"
},
{
"model" : "K5",
"maker" : "Kia",
"year" : 2014,
"gas" : "lpg"
},
{
"model" : "BMW5",
"maker" : "BMW",
"year" : 2013,
"gas" : "gasoline"
}
]
}
32 changes: 32 additions & 0 deletions data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "jsmn",
"keywords": "json",
"description": "Minimalistic JSON parser/tokenizer in C. It can be easily integrated into resource-limited or embedded projects",
"repository":
{
"type": "git",
"url": "https://github.com/zserge/jsmn.git"
},
"frameworks": "*",
"platforms": "*",
"examples": [
"example/*.c"
],
"exclude": "test"
}
{
"name": "jsmn2",
"keywords": "json",
"description": "Minimalistic JSON parser/tokenizer in C. It can be easily integrated into resource-limited or embedded projects",
"repository":
{
"type": "gitsdfsdf",
"url": "https://github.com/zserge/jsmn.git"
},
"frameworks": "*",
"platforms": "*",
"examples": [
"example/*.c"
],
"exclude": "test"
}
27 changes: 27 additions & 0 deletions data2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "hello!",
"data": {
"name" : "jspiner",
"age" : 8,
"birth" : 1996
},
"friends":[
"john",
"smith",
"sam"
],
"books":[
{
"name": "book1",
"price":10000
},
{
"name":"book2",
"price":15000
},
{
"name":"book3",
"price":7000
}
]
}
14 changes: 14 additions & 0 deletions data3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"company": "농심",
"name": "신라면",
"price": 676,
"count": 4
},
{
"company": "팔도",
"name": "비빔면",
"price": 596,
"count": 5
}
]
17 changes: 17 additions & 0 deletions data4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"Ramen":
[
{
"company": "농심",
"name": "신라면",
"price": 676,
"count": 4
},
{
"company": "팔도",
"name": "비빔면",
"price": 596,
"count": 5
}
]
}
162 changes: 162 additions & 0 deletions example/OSS_21000127.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../jsmn.h"

/*
* A small example of jsmn parsing when JSON structure is known and number of
* tokens is predictable.
*/

static char * readJSONFile(){ // 파일 읽어들이는 함수
char oneline[255];
char * c = (char *)malloc(6); // 먼저 메모리 할당
int count=0;
// char c[1000];
FILE *fp = fopen("/home/u21000127/jsmn/cars.json","r"); // data.json 읽기 모드로 열기
while(fgets(oneline,255,fp)!= NULL){ // 읽어들이는 내용 없을 떄까지
count += strlen(oneline); // 한번 읽어들이는 문자 개수
c = (char *)realloc(c,count+1); // 누적된 문자개수에 맞는 메모리 할당
strcat(c,oneline);
}
if(feof(fp)!=0)
puts("복사 완료");
fclose(fp);

return c;
}

int makecarlist(const char *json, jsmntok_t *t, int tokcount, car_t * list[]){
int i,count=1;
char * value1[4];
char * value2[4];
char * value3[4];
char * value4[4];
for(i=0;i<4;i++){
value1[i] = (char *)malloc(10);
value2[i] = (char *)malloc(10);
value3[i] = (char *)malloc(10);
value4[i] = (char *)malloc(10);

}
for(i=0;i<tokcount;i++){
if(t[i].size >= 1 && t[i].type == JSMN_OBJECT && t[i].parent != -1){ // 제품 정보담은 객체 추출
list[count] = (car_t *)malloc(sizeof(car_t));
int size1 = t[i+2].end-t[i+2].start;
value1[count-1] =(char *)realloc(value1[count-1],size1+1);
strcat(list[count]->model,strncpy(value1[count-1],json+t[i+2].start,size1));
int size2 = t[i+4].end-t[i+4].start;
value2[count-1] =(char *)realloc(value2[count-1],size2+1);
strcat(list[count]->maker,strncpy(value2[count-1],json+t[i+4].start,size2));
int size3 = t[i+8].end-t[i+8].start;
value3[count-1] =(char *) realloc(value3[count-1],size3+1);
strcat(list[count]->gastype,strncpy(value3[count-1],json+t[i+8].start,size3));
int size4 = t[i+6].end-t[i+6].start;
value4[count-1] = (char *)realloc(value4[count-1],size4+1);
list[count]->year = atoi(strncpy(value4[count-1],json+t[i+6].start,size4));
printf("%s\t%s\t%s \n", list[count]->model,list[count]->maker,list[count]->gastype);
count++; // list배열 번호 증가
}
}
// free(value1);
// free(value2);
// free(value3);
// free(value4);
return count-1;
}

void printcarlist(car_t * list[], int carcount){
printf("출력 예\n");
int i;
printf("번호 모델명\t제조사\t\t제조년도\t연료타입\n");
for(i=0;i<carcount;i++){
printf("%-3d %-10s\t%-10s\t%-10d\t%-10s\n",i+1,list[i+1]->model,list[i+1]->maker,list[i+1]->year,list[i+1]->gastype );
}

}

// static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
// if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
// strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
// return 0;
// }
// return -1;
// }

int main() {
char *JSON_STRING = readJSONFile();
int i;
int r;
jsmn_parser p;
jsmntok_t t[128]; /* We expect no more than 128 tokens */

jsmn_init(&p);
r = jsmn_parse(&p, JSON_STRING, strlen(JSON_STRING), t, sizeof(t)/sizeof(t[0]));
car_t * carlist[5];
// for(i=0;i<5;i++){
// carlist[i] = (car_t *)malloc(sizeof(car_t));
// }
// carlist[1] = malloc(1000);
// strcat(carlist[1]->model,"ddd"); // 왜 이건 안됨?
// printf("%s\n",carlist[1]->model );
//
// car_t list[5];
// strcat(list[1].model,"fff"); // 이건 되는데
// printf("%s\n",list[1].model );


int num = makecarlist(JSON_STRING,t,r,carlist);
printcarlist(carlist,num);
// free(carlist[1]);
// free(carlist[2]);
// free(carlist[3]);
// free(carlist[4]);



// if (r < 0) {
// printf("Failed to parse JSON: %d\n", r);
// return 1;
// }
//
// /* Assume the top-level element is an object */
// if (r < 1 || t[0].type != JSMN_OBJECT) {
// printf("Object expected\n");
// return 1;
// }
//
// /* Loop over all keys of the root object */
// for (i = 1; i < r; i++) {
// if (jsoneq(JSON_STRING, &t[i], "user") == 0) {
// /* We may use strndup() to fetch string value */
// printf("- User: %.*s\n", t[i+1].end-t[i+1].start,
// JSON_STRING + t[i+1].start);
// i++;
// } else if (jsoneq(JSON_STRING, &t[i], "admin") == 0) {
// /* We may additionally check if the value is either "true" or "false" */
// printf("- Admin: %.*s\n", t[i+1].end-t[i+1].start,
// JSON_STRING + t[i+1].start);
// i++;
// } else if (jsoneq(JSON_STRING, &t[i], "uid") == 0) {
// /* We may want to do strtol() here to get numeric value */
// printf("- UID: %.*s\n", t[i+1].end-t[i+1].start,
// JSON_STRING + t[i+1].start);
// i++;
// } else if (jsoneq(JSON_STRING, &t[i], "groups") == 0) {
// int j;
// printf("- Groups:\n");
// if (t[i+1].type != JSMN_ARRAY) {
// continue; /* We expect groups to be an array of strings */
// }
// for (j = 0; j < t[i+1].size; j++) {
// jsmntok_t *g = &t[i+j+2];
// printf(" * %.*s\n", g->end - g->start, JSON_STRING + g->start);
// }
// i += t[i+1].size + 1;
// } else {
// printf("Unexpected key: %.*s\n", t[i].end-t[i].start,
// JSON_STRING + t[i].start);
// }
// }
// return EXIT_SUCCESS;
}
Loading