Introduction to the use of libjson-c

I. Introduction

        libjson-c is an open source library of json implemented by C language. Provides all interfaces for manipulating the json structure, which meets the needs of manipulating json data in the C language development environment. This article mainly introduces the use of some library functions. This article uses libjson-c-0.13 version. After this version, it needs to be compiled with cmake, which is troublesome and not recommended.

2. Introduction to library functions

1. struct json_object* json_object_new_object(void)

        Creates a new object with a reference count of 1. The returned json_object has the sole control over the object (responsible for the release of the final memory space). When using json_object_object_add or json_object_array_put_idx, control of the object is transferred to the object or array where it was added. If you need to add the object to other multiple objects or arrays, you need to call json_object_get (reference count plus 1, which will be mentioned later).

2. struct json_object* json_object_get(struct json_object *jso)

        Increment the reference count of the json_object by 1. When json_object needs to be referenced by multiple objects, each time a reference object is added, this interface needs to be called to increase the reference count for the json_object.

3. int json_object_put(struct json_object *jso)

        Decrease the reference count of json_object by 1, if the reference count is 0 at this time, release the json_object.

        PS : If json_object is referenced by two objects at the same time, but the reference count of json_object is 1, after calling json_object_put to release the first object, an error will be reported when releasing the second object, because after the first object is released, json_object The reference count is 0 and will be released, causing repeated releases later.

4. struct json_object* json_object_new_int(int32_t i)

        Creates a new empty json_object of type json_type_int. In fact, the object uses 64-bit space to store values, so it is recommended to use the json_object_new_int64 function uniformly.

5. struct json_object* json_object_new_string(const char *s)

        Creates a new empty json_object of type json_type_string. In the function, a copy of the string is created through strdup, and the memory is managed by json_object.

6. struct json_object* json_object_new_boolean(json_bool b)

        Creates a new empty json_object of type json_type_boolean.

7. struct json_object* json_object_new_array(void)

        Creates a new json_object of type json_type_array.

8. struct json_object* json_tokener_parse(const char *str)

        Parses the string and returns a non-NULL json_object if a valid JSON value is found. String doesn't need to be a JSON object or array; it can also be a string, number, or boolean.

9. struct json_object_iterator json_object_iter_begin(struct json_object* obj)

        Find the first object contained in a json_object and save it in a json_object_iterator.

10. struct json_object_iterator json_object_iter_end(const struct json_object* obj)

        Find the last object contained in a json_object and save it in a json_object_iterator.

11. void json_object_iter_next(struct json_object_iterator* iter)

        iter points to the next json_object_iterator structure.

12. const char* json_object_iter_peek_name(const struct json_object_iterator* iter)

        Get the name of the corresponding json_object from json_object_iterator.

13. struct json_object* json_object_iter_peek_value(const struct json_object_iterator* iter)

        Get the value of the corresponding json_object from json_object_iterator.

The case is as follows:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <json-c/json_object.h>
#include <json-c/json_tokener.h>
#include <json-c/json_object_iterator.h>


int main(int argc, char *argv[])
{
    struct json_object_iterator it;
    struct json_object_iterator itEnd;
    struct json_object* obj;

    obj = json_tokener_parse("{'first':'george', 'age':100}");
    it = json_object_iter_begin(obj);
    itEnd = json_object_iter_end(obj);

    while (!json_object_iter_equal(&it, &itEnd)) 
    {
        printf("name = %s, value = %s\n", \
            json_object_iter_peek_name(&it), \
            json_object_to_json_string(json_object_iter_peek_value(&it)));
            
        json_object_iter_next(&it);
    }


    return 0;
}


# ./test1
name = first, value = "george"
name = age, value = 100

14. int json_c_set_serialization_double_format(const char *double_format, int global_or_thread)

        global_or_thread can be JSON_C_OPTION_GLOBAL or JSON_C_OPTION_THREAD, and the format of double_format is "%.x", such as "%.5f", which means to intercept 5 digits after the decimal point.

15. struct json_object* json_object_from_file(const char *filename)

        Read the entire content of the given file and convert it to a json_object using json_tokener_parse().

16. int json_object_to_file(const char *filename, struct json_object *obj)

        Equivalent to: json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN).

PS : json_object output to files can have different formats, as follows:

JSON_C_TO_STRING_PLAIN : No spaces and newlines, so the content is all on one line and the content sticks together.

JSON_C_TO_STRING_SPACED : The content is separated by spaces, but the content is all on one line.

JSON_C_TO_STRING_PRETTY : There are spaces, newlines and indents, which are more beautiful.

3. Summary

        This article summarizes the usage of some commonly used functions of the libjson-c library, introduces the creation of int, bool, string, array, object objects, and how to traverse a json_object, and finally summarizes the format of the json_object output to the file.

Guess you like

Origin blog.csdn.net/to_be_better_wen/article/details/129740897