mirror of
https://github.com/sigmasternchen/libargo
synced 2025-03-15 21:28:54 +00:00
added example for null value to marshaller demo
This commit is contained in:
parent
ac78c7856c
commit
091132e27c
5 changed files with 71 additions and 3 deletions
10
Makefile
10
Makefile
|
@ -37,6 +37,15 @@ obj/%.o: src/%.c obj
|
||||||
obj:
|
obj:
|
||||||
@mkdir -p obj
|
@mkdir -p obj
|
||||||
|
|
||||||
|
marshaller-demo: marshaller/gen/demo.tab.c marshaller/demo/demo.c marshaller/marshaller.c $(A_LIB_NAME)
|
||||||
|
$(CC) $(CFLAGS) -Imarshaller/demo/ -Isrc/ -Imarshaller/ -o $@ $^
|
||||||
|
|
||||||
|
marshaller/gen/demo.tab.c: marshaller/demo/demo.h marshaller/marshaller-gen
|
||||||
|
./marshaller/marshaller-gen -o $@ $<
|
||||||
|
|
||||||
|
marshaller/marshaller-gen:
|
||||||
|
$(MAKE) -C marshaller/ marshaller-gen
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@echo "Cleaning up..."
|
@echo "Cleaning up..."
|
||||||
@rm -f obj/*.o
|
@rm -f obj/*.o
|
||||||
|
@ -45,3 +54,4 @@ clean:
|
||||||
@rm -f $(BIN_NAME)
|
@rm -f $(BIN_NAME)
|
||||||
@rm -f $(A_LIB_NAME)
|
@rm -f $(A_LIB_NAME)
|
||||||
@rm -f $(SO_LIB_NAME)
|
@rm -f $(SO_LIB_NAME)
|
||||||
|
$(MAKE) -C marshaller/ clean
|
||||||
|
|
|
@ -3,9 +3,9 @@ YACC = bison
|
||||||
YFLAGS = -y -d
|
YFLAGS = -y -d
|
||||||
CC = gcc
|
CC = gcc
|
||||||
|
|
||||||
BIN_NAME = marshaller
|
BIN_NAME = marshaller-gen
|
||||||
|
|
||||||
marshaller: src/codegen.c gen/lex.yy.c gen/y.tab.c
|
$(BIN_NAME): src/codegen.c gen/lex.yy.c gen/y.tab.c
|
||||||
$(CC) -Isrc/ -o $@ $^
|
$(CC) -Isrc/ -o $@ $^
|
||||||
|
|
||||||
gen/y.tab.c gen/y.tab.h: src/parser.y
|
gen/y.tab.c gen/y.tab.h: src/parser.y
|
||||||
|
@ -19,4 +19,3 @@ gen/lex.yy.c: src/scanner.l gen/y.tab.h
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f gen/* $(BIN_NAME)
|
rm -f gen/* $(BIN_NAME)
|
||||||
|
|
||||||
|
|
40
marshaller/demo/demo.c
Normal file
40
marshaller/demo/demo.c
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <json.h>
|
||||||
|
#include <marshaller.h>
|
||||||
|
|
||||||
|
#include "demo.h"
|
||||||
|
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
const char* json = "{\
|
||||||
|
\"name\": \"How To Write A JSON Marshaller In C\",\
|
||||||
|
\"content\": \"TODO\",\
|
||||||
|
\"views\": null,\
|
||||||
|
\"user\": {\
|
||||||
|
\"uid\": 1000,\
|
||||||
|
\"username\": \"overflowerror\",\
|
||||||
|
\"email\": \"overflow@persei.net\"\
|
||||||
|
}\
|
||||||
|
}";
|
||||||
|
|
||||||
|
post_t* post = json_unmarshall(post_t, json);
|
||||||
|
|
||||||
|
printf("Name: %s\n", post->name);
|
||||||
|
printf("Content: %s\n", post->content);
|
||||||
|
printf("Views: %s (%ld)\n", post->views == NULL ? "null" : "", post->views == NULL ? 0 : *post->views);
|
||||||
|
printf("User.Uid: %lld\n", post->user.uid);
|
||||||
|
printf("User.Username: %s\n", post->user.username);
|
||||||
|
printf("User.Email: %s\n", post->user.email);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
post->content = "Just do it.";
|
||||||
|
|
||||||
|
char* newJson = json_marshall(post_t, post);
|
||||||
|
printf("%s\n", newJson);
|
||||||
|
|
||||||
|
free(newJson);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
18
marshaller/demo/demo.h
Normal file
18
marshaller/demo/demo.h
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
#ifndef DEMO_H
|
||||||
|
#define DEMO_H
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
long long uid;
|
||||||
|
const char* username;
|
||||||
|
const char* email;
|
||||||
|
} user_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const char* name;
|
||||||
|
const char* content;
|
||||||
|
long* views;
|
||||||
|
user_t user;
|
||||||
|
} post_t;
|
||||||
|
|
||||||
|
#endif
|
|
@ -1,6 +1,7 @@
|
||||||
#ifndef JSON_H
|
#ifndef JSON_H
|
||||||
#define JSON_H
|
#define JSON_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
|
|
Loading…
Reference in a new issue