Giter Club home page Giter Club logo

json-parser's Introduction

中文版说明

Json Parser in Standard C (C99)

This json parser was created for the project of C++ Workflow.

Build tests

$ make

Run tests

Parse and print json document:

$ ./parse_json < xxx.json

Test parsing speed:

$ time ./test_speed <repeat times> < xxx.json

Main Interfaces

JSON value related

/* Parse JSON text and create a JSON value. Returns NULL on parsing
   failures (Invalid JSON, nesting too deep, memory allocation failure).
   @text: JSON text string */
json_value_t *json_value_parse(const char *text);

/* Destroy the JSON value
   @val: JSON value. Typically created by the parsing function. */
void json_value_destroy(json_value_t *val);

/* Get JSON value's type
     Return values:
     JSON_VALUE_STRING: string
     JSON_VALUE_NUMBER: number
     JSON_VALUE_OBJECT: JSON object
     JSON_VALUE_ARRAY: JSON array
     JSON_VALUE_TRUE: true
     JSON_VALUE_FALSE: false
     JSON_VALUE_NULL: null
   @val: JSON value */
int json_value_type(const json_value_t *val);

/* Obtain the JSON string. The function returns the string or
   returns NULL if the type of @val is not JSON_VALUE_STRING.
   @val: JSON value */
const char *json_value_string(const json_value_t *val);

/* Obtain the JSON number. The function returns the number or
   returns NAN if the type of @val is not JSON_VALUE_NUMBER.
   @val: JSON value */
double json_value_number(const json_value_t *val);

/* Obtain JSON object. The function returns the JSON object or
   returns NULL if the type of @val is not JSON_VALUE_OBJECT.
   @val: JSON value
   Note: The returned pointer to JSON object is not const.
         You may extend the object using buiding functions. */
json_object_t *json_value_object(const json_value_t *val);

/* Obtain JSON arary. The function returns the JSON array or
   returns NULL if the type of @val is not JSON_VALUE_ARRAY.
   @val: JSON value
   Note: The returned pointer to the JSON array is not const and
         can been extended by using building functions. */
json_array_t *json_value_array(const json_value_t *val);

JSON object related

/* Get the size of the JSON object.
   @obj: JSON object */
int json_object_size(const json_object_t *obj);

/* Find the JSON value under the key @name. Returns NULL if @name
   can not be found. The time complexity of this function is
   O(log(n)), where n is the size of the JSON object.
   @name: The key to find
   @obj: JSON object
   Note: The returned pointer to JSON value is const. */
const json_value_t *json_object_find(const char *name, const json_object_t *obj);

/* Traversing the JSON object forward or backward
   @name: Temporary (const char *) pointer for each key
   @val: Temporary (const json_value_t *) pointer for each JSON value
   @obj: JSON object
   NOTE: These are not functions, but macros of looping. */
json_object_for_each(name, val, obj)
json_object_for_each_prev(name, val, obj)

JSON array related

/* Get the size of the JSON array.
   @arr:JSON array */
int json_array_size(const json_array_t *arr);

/* Traversing the JSON array forward or backward
   @val: Temporary (const json_value_t *) pointer for each JSON value
   @arr: JSON array
   NOTE: These are not functions, but macros of looping. */
json_array_for_each(val, arr)
json_array_for_each_prev(val, arr)

Building JSON

All the following functions return NULL on failure of allocating memory.

/* Create a JSON value.
   @type: JSON value's type.
   Note: Variable argument. Example:
     v_str = json_value_create(JSON_VALUE_STRING, "hello");
     v_num = json_value_create(JSON_VALUE_NUMBER, 3.14);
     v_int = json_value_create(JSON_VALUE_NUMBER, (double)100);
     v_obj = json_value_create(JSON_VALUE_OBJECT);
     v_arr = json_value_create(JSON_VALUE_ARRAY);
     v_true = json_value_create(JSON_VALUE_TRUE);
   The returned value is not const and has to be destroyed. */
json_value_t *json_value_create(int type, ...);

/* Extend the JSON object. Returns the value that's added.
   @obj: JSON object
   @name: New member's name
   @type: New member's value type or zero which means another JSON value
   Note: Variable argument. Example:
     v_num = json_object_append(obj, "pi", JSON_VALUE_NUMBER, 3.14);
     v_obj = json_object_append(obj, "user", JSON_VALUE_OBJECT);
     v_from_doc = json_object_append(obj, "doc", 0, json_value_parse("{\"data\" : [1, 2, 3]}")); */
const json_value_t *json_object_append(json_object_t *obj, const char *name,
                                       int type, ...);

/* Remove a JSON value from a JSON object and return the value.
   @val: JSON value to be removed.
   @obj: JSON object
   Note: The returned value is not const and need to be destroyed,
         or appended to another JSON object or JSON array. */
json_value_t *json_object_remove(const json_value_t *val,
                                 json_object_t *obj);

/* Extend the JSON array. Return the value that's added.
   @arr: JSON array
   @type: New member's value type or zero which means another JSON value
   Note: Variable argument. Example:
     v_str = json_array_append(arr, JSON_VALUE_STRING, "hello");
     equal to:
     v_str = json_array_append(arr, 0, json_value_create(JSON_VALUE_STRING, "hello")); */
const json_value_t *json_array_append(json_array_t *arr, int type, ...);

/* Remove a JSON value from a JSON array and return the value.
   @val: JSON value to be removed
   @arr: JSON array
   Note: The returned value is not const and need to be destroyed,
         or appended to another JSON object or JSON array. */
json_value_t *json_array_remove(const json_value_t *val,
                                json_object_t *arr);

A beautiful C++ wrapper

https://github.com/wfrest/Json

json-parser's People

Contributors

barenboim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

json-parser's Issues

About c version

The title is Json Parser in ANSI-C (C89), but I add the flag -std=c89 in makefile, it can't compile.

image

image

The usefulness of free() in __move_json_value

Hello, I want to know the usefulness of free() in __move_json_value , especially when type is object or array,
I wonder if there is a memory bug here (I have such doubts, I haven't read the code in too much detail).

json_value_t to string

i can call json_value_parse converts const char*(json format) to json_value_t, then get param by Traversing the JSON object. My question is that if i call json_value_create(JSON_VALUE_OBJECT) return a json_value_t variable val,call json_object_append or json_array_append to build json format data,Finally, I can't find the interface obtain a string from val, like cjson cJSON_Print function,for example
char *json_data = cJSON_Print(obj);

heap-buffer-overflow at json_value_parse

CC=gcc -fsanitize=address make
test.c:
char* jstr = "{ "";
json_value_t *val = json_value_parse(jstr);

use json_value_parse to parse input "{ "" will cause a heap-buffer-overflow error.

==2352670==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000109953 at pc 0x55900a20cc38 bp 0x7ffd112d2dc0 sp 0x7ffd112d2db0
READ of size 1 at 0x602000109953 thread T0
#0 0x55900a20cc37 in json_value_parse /opt1/software/json-parser/json_parser.c:685
#1 0x55900a213a4f in main /opt1/software/json-parser/test.c:184

取某一个value 怎么用呢?

json_value_t *json_value_parse(const char *doc);
这里的返回 json_value_t 不是整个json吗?
const char *json_value_string(const json_value_t *val);
再把整个json_value_t 作为参数 去取value 吗?

JSON value can be cast to it's data type directly.

A JSON value can be cast directly to it's data:

int main()
{
    json_value_t *val = json_value_parse("...");

    // The following castings are legal if 'val' is the corresponding type.
    const char *str = *(const char **)val;          // equal to: str = json_value_string(val);
    double num = *(double *)val;                    // equal to: num = json_value_number(val);
    json_object_t *obj = (json_object_t *)val;      // equal to: obj = json_value_object(val);
    json_array_t *arr = (json_array_t *)val;        // equal to: arr = json_value_array(val);
}

Because the JSON value shares the same address with it's data, and this was designed on purpose and you may always make use of this feature. In json_parser.c, the JSON value structure is defined as:

struct __json_value
{
	union
	{
		char *string;
		double number;
		json_object_t object;
		json_array_t array;
	} value;
	int type;
};

The JSON value's data are always the first fields, so the addresses of them are identical to the value. You can cast reversely as well by casting an object or an array to a JSON value.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.