- Notifications
You must be signed in to change notification settings - Fork289
Very low footprint DOM-style JSON parser written in portable ANSI C
License
json-parser/json-parser
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Very low footprint DOM-style JSON parser written in portable C89 (sometimes referred to as ANSI C).
- BSD licensed with no dependencies (i.e. just drop
json.c
andjson.h
into your project) - Never recurses or allocates more memory than it needs to represent the parsed JSON
- Very simple API with operator sugar for C++
Want to serialize? Check outjson-builder!
There is now a makefile which will produce a libjsonparser static and dynamic library. However, thisisnot required to build json-parser, and the source files (json.c
andjson.h
) should be happyin any build system you already have in place.
json_value*json_parse (constjson_char*json,size_tlength);json_value*json_parse_ex (json_settings*settings,constjson_char*json,size_tlength,char*error);voidjson_value_free (json_value*);
Thetype
field ofjson_value
is one of:
json_object
(seeu.object.length
,u.object.values[x].name
,u.object.values[x].value
)json_array
(seeu.array.length
,u.array.values
)json_integer
(seeu.integer
)json_double
(seeu.dbl
)json_string
(seeu.string.ptr
,u.string.length
)json_boolean
(seeu.boolean
)json_null
Unless otherwise specified, compile definitions must be provided both when compilingjson.c
and when compiling any of your own source files that includejson.h
.
Stores the source location (line and column number) inside eachjson_value
.
This is useful for application-level error reporting.
By default,json_int_t
is defined aslong
under C89 andint_fast64_t
otherwise. For MSVC it is defined as__int64
regardless of language standard support.
Optionally, you may definejson_int_t
to be your own preferred type name for integer types parsed from JSON documents. It must be a signed integer type, there is no support for unsigned types. If you specify a raw primitive type withoutsigned
orunsigned
(and not a typdef),JSON_INT_MAX
will be calculated for you. Otherwise, you must provide your own definition ofJSON_INT_MAX
as the highest positive integer value that can be represented byjson_int_t
.
Example usage:
-Djson_int_t=short
"-Djson_int_t=signed char" -DJSON_INT_MAX=127
"-Djson_int_t=long long"
-Djson_int_t=__int128
settings |=json_enable_comments;
Enables C-style// line
and/* block */
comments.
size_tvalue_extra
The amount of space (if any) to allocate at the end of eachjson_value
, inorder to give the application space to add metadata.
void* (*mem_alloc) (size_t,intzero,void*user_data);void (*mem_free) (void*,void*user_data);
Custom allocator routines. If NULL, the defaultmalloc
andfree
will be used.
Theuser_data
pointer will be forwarded fromjson_settings
to allow applicationcontext to be passed.
UTF-8 byte order marks are now skipped if present
Allows cross-compilation by honoring --host if given (@wkz)
Maximum size for error buffer is now exposed in header (@LB--)
GCC warning for
static
afterconst
fixed (@batrick)Optional support for C-style line and block comments added (@Jin-W-FS)
name_length
field added to object valuesIt is now possible to retrieve the source line/column number of a parsed
json_value
whenJSON_TRACK_SOURCE
is enabledThe application may now extend
json_value
using thevalue_extra
settingUn-ambiguate pow call in the case of C++ overloaded pow (@fcartegnie)
Fix null pointer de-reference when a non-existing array is closed and no root value is present
About
Very low footprint DOM-style JSON parser written in portable ANSI C