Skip to content

Commit fe2f200

Browse files
committed
Adjust code structure
1 parent 220083a commit fe2f200

13 files changed

+1228
-2591
lines changed

Makefile

+18-16
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,24 @@
66

77
PACKAGE_NAME = ljson
88

9-
major_ver = 1
10-
minor_ver = 3
11-
patch_ver = 3
12-
staticlib = lib$(PACKAGE_NAME).a
13-
sharedlib = lib$(PACKAGE_NAME).so $(major_ver) $(minor_ver) $(patch_ver)
9+
staticlib = libljson.a
10+
sharedlib = libljson.so 2 0 0
1411
testedbin = ljson
12+
testednum = jnum_test
1513

16-
ldoublelib = libldouble.a
17-
ldoublebin = ldouble
18-
19-
INSTALL_HEADERS = json.h
20-
FMUL ?= 0
14+
INSTALL_HEADERS = json.h jnum.h
2115
DTOA ?= 0
2216
RBIT ?= 11
2317
TCMP ?= 2
2418

25-
CPFLAGS += -DUSE_FLOAT_MUL_CONVERT=$(FMUL)
19+
libsrcs := json.c jnum.c
20+
ifeq ($(DTOA),2)
21+
libsrcs += grisu2.c
22+
endif
23+
ifeq ($(DTOA),3)
24+
libsrcs += dragonbox.c
25+
endif
26+
2627
CPFLAGS += -DJSON_DTOA_ALGORITHM=$(DTOA) # 0:ldouble 1:sprintf 2:grisu2 3:dragonbox
2728
CPFLAGS += -DLSHIFT_RESERVED_BIT=$(RBIT) # 2 <= RBIT <= 11
2829
CPFLAGS += -DAPPROX_TAIL_CMP_VAL=$(TCMP) # 0 <= TCMP <= 4
@@ -32,14 +33,15 @@ all:
3233
@echo "Build $(PACKAGE_NAME) Done!"
3334

3435
INC_MAKES := app
35-
object_byte_size=2304
36+
object_byte_size=10240
3637
include inc.makes
37-
$(eval $(call add-liba-build,$(staticlib),json.c))
38-
$(eval $(call add-libso-build,$(sharedlib),json.c))
38+
39+
$(eval $(call add-liba-build,$(staticlib),$(libsrcs)))
40+
$(eval $(call add-libso-build,$(sharedlib),$(libsrcs)))
3941
$(eval $(call add-bin-build,$(testedbin),json_test.c,-L $(OBJ_PREFIX) $(call set_links,ljson),,$(OBJ_PREFIX)/$(staticlib)))
4042

41-
$(eval $(call add-liba-build,$(ldoublelib),ldouble.c))
42-
$(eval $(call add-bin-build,$(ldoublebin),ldouble_test.c,-L $(OBJ_PREFIX) $(call set_links,ldouble),,$(OBJ_PREFIX)/$(ldoublelib)))
43+
numsrcs := json.c jnum.c grisu2.c dragonbox.c jnum_test.c
44+
$(eval $(call add-bin-build,$(testednum),$(numsrcs)))
4345

4446
all: $(BIN_TARGETS) $(LIB_TARGETS)
4547

README_zh-cn.md

+48-76
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ make O=<编译输出目录> CROSS_COMPILE=<交叉编译器前缀> && make O=<编
8282

8383
## json对象结构
8484

85-
使用 long long 类型支持,编译时需要设置 json.h 中的 `JSON_LONG_LONG_SUPPORT` 值为 1
86-
8785
```c
8886
struct json_list {
8987
struct json_list *next;
@@ -98,10 +96,8 @@ typedef enum {
9896
JSON_BOOL,
9997
JSON_INT,
10098
JSON_HEX,
101-
#if JSON_LONG_LONG_SUPPORT
10299
JSON_LINT,
103100
JSON_LHEX,
104-
#endif
105101
JSON_DOUBLE,
106102
JSON_STRING,
107103
JSON_ARRAY,
@@ -119,12 +115,10 @@ typedef struct {
119115

120116
typedef union {
121117
bool vbool;
122-
int vint;
123-
unsigned int vhex;
124-
#if JSON_LONG_LONG_SUPPORT
125-
long long int vlint;
126-
unsigned long long int vlhex;
127-
#endif
118+
int32_t vint;
119+
uint32_t vhex;
120+
int64_t vlint;
121+
uint64_t vlhex;
128122
double vdbl;
129123
} json_number_t; // json数字对象值
130124

@@ -187,24 +181,20 @@ json_object *json_create_item_array(json_type_t type, void *values, int count);
187181

188182
static inline json_object *json_create_null(void);
189183
static inline json_object *json_create_bool(bool value);
190-
static inline json_object *json_create_int(int value);
191-
static inline json_object *json_create_hex(unsigned int value);
192-
#if JSON_LONG_LONG_SUPPORT
193-
static inline json_object *json_create_lint(long long int value);
194-
static inline json_object *json_create_lhex(unsigned long long int value);
195-
#endif
184+
static inline json_object *json_create_int(int32_t value);
185+
static inline json_object *json_create_hex(uint32_t value);
186+
static inline json_object *json_create_lint(int64_t value);
187+
static inline json_object *json_create_lhex(uint64_t value);
196188
static inline json_object *json_create_double(double value);
197189
static inline json_object *json_create_string(json_string_t *value);
198190
static inline json_object *json_create_array(void);
199191
static inline json_object *json_create_object(void);
200192

201193
static inline json_object *json_create_bool_array(bool *values, int count);
202-
static inline json_object *json_create_int_array(int *values, int count);
203-
static inline json_object *json_create_hex_array(unsigned int *values, int count);
204-
#if JSON_LONG_LONG_SUPPORT
205-
static inline json_object *json_create_lint_array(long long int *values, int count);
206-
static inline json_object *json_create_lhex_array(unsigned long long int *values, int count);
207-
#endif
194+
static inline json_object *json_create_int_array(int32_t *values, int count);
195+
static inline json_object *json_create_hex_array(uint32_t *values, int count);
196+
static inline json_object *json_create_lint_array(int64_t *values, int count);
197+
static inline json_object *json_create_lhex_array(uint64_t *values, int count);
208198
static inline json_object *json_create_double_array(double *values, int count);
209199
static inline json_object *json_create_string_array(json_string_t *values, int count);
210200
```
@@ -234,21 +224,17 @@ int json_get_number_value(json_object *json, json_type_t type, void *value);
234224
int json_set_number_value(json_object *json, json_type_t type, void *value);
235225

236226
static inline bool json_get_bool_value(json_object *json);
237-
static inline int json_get_int_value(json_object *json);
238-
static inline unsigned int json_get_hex_value(json_object *json);
239-
#if JSON_LONG_LONG_SUPPORT
240-
static inline long long int json_get_lint_value(json_object *json);
241-
static inline unsigned long long int json_get_lhex_value(json_object *json);
242-
#endif
227+
static inline int32_t json_get_int_value(json_object *json);
228+
static inline uint32_t json_get_hex_value(json_object *json);
229+
static inline int64_t json_get_lint_value(json_object *json);
230+
static inline uint64_t json_get_lhex_value(json_object *json);
243231
static inline double json_get_double_value(json_object *json);
244232

245233
static inline int json_set_bool_value(json_object *json, bool value);
246-
static inline int json_set_int_value(json_object *json, int value);
247-
static inline int json_set_hex_value(json_object *json, unsigned int value);
248-
#if JSON_LONG_LONG_SUPPORT
249-
static inline int json_set_lint_value(json_object *json, long long int value);
250-
static inline int json_set_lhex_value(json_object *json, unsigned long long int value);
251-
#endif
234+
static inline int json_set_int_value(json_object *json, int32_t value);
235+
static inline int json_set_hex_value(json_object *json, uint32_t value);
236+
static inline int json_set_lint_value(json_object *json, int64_t value);
237+
static inline int json_set_lhex_value(json_object *json, uint64_t value);
252238
static inline int json_set_double_value(json_object *json, double value);
253239
```
254240
@@ -326,25 +312,21 @@ json_object *json_add_new_item_to_object(json_object *object, json_type_t type,
326312

327313
static inline json_object *json_add_null_to_array(json_object *array);
328314
static inline json_object *json_add_bool_to_array(json_object *array, bool value);
329-
static inline json_object *json_add_int_to_array(json_object *array, int value);
330-
static inline json_object *json_add_hex_to_array(json_object *array, unsigned int value);
331-
#if JSON_LONG_LONG_SUPPORT
332-
static inline json_object *json_add_lint_to_array(json_object *array, long long int value);
333-
static inline json_object *json_add_lhex_to_array(json_object *array, unsigned long long int value);
334-
#endif
315+
static inline json_object *json_add_int_to_array(json_object *array, int32_t value);
316+
static inline json_object *json_add_hex_to_array(json_object *array, uint32_t value);
317+
static inline json_object *json_add_lint_to_array(json_object *array, int64_t value);
318+
static inline json_object *json_add_lhex_to_array(json_object *array, uint64_t value);
335319
static inline json_object *json_add_double_to_array(json_object *array, double value);
336320
static inline json_object *json_add_string_to_array(json_object *array, json_string_t *value);
337321
static inline json_object *json_add_array_to_array(json_object *array);
338322
static inline json_object *json_add_object_to_array(json_object *array);
339323

340324
static inline json_object *json_add_null_to_object(json_object *object, json_string_t *jkey);
341325
static inline json_object *json_add_bool_to_object(json_object *object, json_string_t *jkey, bool value);
342-
static inline json_object *json_add_int_to_object(json_object *object, json_string_t *jkey, int value);
343-
static inline json_object *json_add_hex_to_object(json_object *object, json_string_t *jkey, unsigned int value);
344-
#if JSON_LONG_LONG_SUPPORT
345-
static inline json_object *json_add_lint_to_object(json_object *object, json_string_t *jkey, long long int value);
346-
static inline json_object *json_add_lhex_to_object(json_object *object, json_string_t *jkey, unsigned long long int value);
347-
#endif
326+
static inline json_object *json_add_int_to_object(json_object *object, json_string_t *jkey, int32_t value);
327+
static inline json_object *json_add_hex_to_object(json_object *object, json_string_t *jkey, uint32_t value);
328+
static inline json_object *json_add_lint_to_object(json_object *object, json_string_t *jkey, int64_t value);
329+
static inline json_object *json_add_lhex_to_object(json_object *object, json_string_t *jkey, uint64_t value);
348330
static inline json_object *json_add_double_to_object(json_object *object, json_string_t *jkey, double value);
349331
static inline json_object *json_add_string_to_object(json_object *object, json_string_t *jkey, json_string_t *value);
350332
static inline json_object *json_add_array_to_object(json_object *object, json_string_t *jkey);
@@ -426,24 +408,20 @@ json_object *pjson_create_item_array(json_type_t item_type, void *values, int co
426408
427409
static inline json_object *pjson_create_null(json_mem_t *mem);
428410
static inline json_object *pjson_create_bool(bool value, json_mem_t *mem);
429-
static inline json_object *pjson_create_int(int value, json_mem_t *mem);
430-
static inline json_object *pjson_create_hex(unsigned int value, json_mem_t *mem);
431-
#if JSON_LONG_LONG_SUPPORT
432-
static inline json_object *pjson_create_lint(long long int value, json_mem_t *mem);
433-
static inline json_object *pjson_create_lhex(unsigned long long int value, json_mem_t *mem);
434-
#endif
411+
static inline json_object *pjson_create_int(int32_t value, json_mem_t *mem);
412+
static inline json_object *pjson_create_hex(uint32_t value, json_mem_t *mem);
413+
static inline json_object *pjson_create_lint(int64_t value, json_mem_t *mem);
414+
static inline json_object *pjson_create_lhex(uint64_t value, json_mem_t *mem);
435415
static inline json_object *pjson_create_double(double value, json_mem_t *mem);
436416
static inline json_object *pjson_create_string(json_string_t *value, json_mem_t *mem);
437417
static inline json_object *pjson_create_array(json_mem_t *mem);
438418
static inline json_object *pjson_create_object(json_mem_t *mem);
439419
440420
static inline json_object *pjson_create_bool_array(bool *values, int count, json_mem_t *mem);
441-
static inline json_object *pjson_create_int_array(int *values, int count, json_mem_t *mem);
442-
static inline json_object *pjson_create_hex_array(unsigned int *values, int count, json_mem_t *mem);
443-
#if JSON_LONG_LONG_SUPPORT
444-
static inline json_object *pjson_create_lint_array(long long int *values, int count, json_mem_t *mem);
445-
static inline json_object *pjson_create_lhex_array(unsigned long long int *values, int count, json_mem_t *mem);
446-
#endif
421+
static inline json_object *pjson_create_int_array(int32_t *values, int count, json_mem_t *mem);
422+
static inline json_object *pjson_create_hex_array(uint32_t *values, int count, json_mem_t *mem);
423+
static inline json_object *pjson_create_lint_array(int64_t *values, int count, json_mem_t *mem);
424+
static inline json_object *pjson_create_lhex_array(uint64_t *values, int count, json_mem_t *mem);
447425
static inline json_object *pjson_create_double_array(double *values, int count, json_mem_t *mem);
448426
static inline json_object *pjson_create_string_array(json_string_t *values, int count, json_mem_t *mem);
449427
```
@@ -485,25 +463,21 @@ json_object *pjson_add_new_item_to_object(json_object *object, json_type_t type,
485463
486464
static inline json_object *pjson_add_null_to_array(json_object *array, json_mem_t *mem);
487465
static inline json_object *pjson_add_bool_to_array(json_object *array, bool value, json_mem_t *mem);
488-
static inline json_object *pjson_add_int_to_array(json_object *array, int value, json_mem_t *mem);
489-
static inline json_object *pjson_add_hex_to_array(json_object *array, unsigned int value, json_mem_t *mem);
490-
#if JSON_LONG_LONG_SUPPORT
491-
static inline json_object *pjson_add_lint_to_array(json_object *array, long long int value, json_mem_t *mem);
492-
static inline json_object *pjson_add_lhex_to_array(json_object *array, unsigned long long int value, json_mem_t *mem);
493-
#endif
466+
static inline json_object *pjson_add_int_to_array(json_object *array, int32_t value, json_mem_t *mem);
467+
static inline json_object *pjson_add_hex_to_array(json_object *array, uint32_t value, json_mem_t *mem);
468+
static inline json_object *pjson_add_lint_to_array(json_object *array, int64_t value, json_mem_t *mem);
469+
static inline json_object *pjson_add_lhex_to_array(json_object *array, uint64_t value, json_mem_t *mem);
494470
static inline json_object *pjson_add_double_to_array(json_object *array, double value, json_mem_t *mem);
495471
static inline json_object *pjson_add_string_to_array(json_object *array, json_string_t *value, json_mem_t *mem);
496472
static inline json_object *pjson_add_array_to_array(json_object *array, json_mem_t *mem);
497473
static inline json_object *pjson_add_object_to_array(json_object *array, json_mem_t *mem);
498474
499475
static inline json_object *pjson_add_null_to_object(json_object *object, json_string_t *jkey, json_mem_t *mem);
500476
static inline json_object *pjson_add_bool_to_object(json_object *object, json_string_t *jkey, bool value, json_mem_t *mem);
501-
static inline json_object *pjson_add_int_to_object(json_object *object, json_string_t *jkey, int value, json_mem_t *mem);
502-
static inline json_object *pjson_add_hex_to_object(json_object *object, json_string_t *jkey, unsigned int value, json_mem_t *mem);
503-
#if JSON_LONG_LONG_SUPPORT
504-
static inline json_object *pjson_add_lint_to_object(json_object *object, json_string_t *jkey, long long int value, json_mem_t *mem);
505-
static inline json_object *pjson_add_lhex_to_object(json_object *object, json_string_t *jkey, unsigned long long int value, json_mem_t *mem);
506-
#endif
477+
static inline json_object *pjson_add_int_to_object(json_object *object, json_string_t *jkey, int32_t value, json_mem_t *mem);
478+
static inline json_object *pjson_add_hex_to_object(json_object *object, json_string_t *jkey, uint32_t value, json_mem_t *mem);
479+
static inline json_object *pjson_add_lint_to_object(json_object *object, json_string_t *jkey, int64_t value, json_mem_t *mem);
480+
static inline json_object *pjson_add_lhex_to_object(json_object *object, json_string_t *jkey, uint64_t value, json_mem_t *mem);
507481
static inline json_object *pjson_add_double_to_object(json_object *object, json_string_t *jkey, double value, json_mem_t *mem);
508482
static inline json_object *pjson_add_string_to_object(json_object *object, json_string_t *jkey, json_string_t *value, json_mem_t *mem);
509483
static inline json_object *pjson_add_array_to_object(json_object *object, json_string_t *jkey, json_mem_t *mem);
@@ -606,12 +580,10 @@ static inline json_sax_print_hd json_sax_fprint_unformat_start(int item_total, c
606580
int json_sax_print_value(json_sax_print_hd handle, json_type_t type, json_string_t *jkey, const void *value);
607581
static inline int json_sax_print_null(json_sax_print_hd handle, json_string_t *jkey);
608582
static inline int json_sax_print_bool(json_sax_print_hd handle, json_string_t *jkey, bool value);
609-
static inline int json_sax_print_int(json_sax_print_hd handle, json_string_t *jkey, int value);
610-
static inline int json_sax_print_hex(json_sax_print_hd handle, json_string_t *jkey, unsigned int value);
611-
#if JSON_LONG_LONG_SUPPORT
612-
static inline int json_sax_print_lint(json_sax_print_hd handle, json_string_t *jkey, long long int value);
613-
static inline int json_sax_print_lhex(json_sax_print_hd handle, json_string_t *jkey, unsigned long long int value);
614-
#endif
583+
static inline int json_sax_print_int(json_sax_print_hd handle, json_string_t *jkey, int32_t value);
584+
static inline int json_sax_print_hex(json_sax_print_hd handle, json_string_t *jkey, uint32_t value);
585+
static inline int json_sax_print_lint(json_sax_print_hd handle, json_string_t *jkey, int64_t value);
586+
static inline int json_sax_print_lhex(json_sax_print_hd handle, json_string_t *jkey, uint64_t value);
615587
static inline int json_sax_print_double(json_sax_print_hd handle, json_string_t *jkey, double value);
616588
static inline int json_sax_print_string(json_sax_print_hd handle, json_string_t *jkey, json_string_t *value);
617589
static inline int json_sax_print_array(json_sax_print_hd handle, json_string_t *jkey, json_sax_cmd_t value);

0 commit comments

Comments
 (0)