Giter Site home page Giter Site logo

jsong's Introduction

jsong

A simple json library for c language

compile

make

usage

#include "libjson.h"
#include <stdlib.h>
#include <stdio.h>
int main()
{
    /* create a empty json OBJECT */
    JSONObject* json_obj = JSON_OBJECT_PTR();
    /* create a empty json ARRAY */
    JSONArray* json_arr = JSON_ARRAY_PTR();
    /* create a json NUMBER */
    JSONNumber* json_num =  JSON_NUMBER_PTR(2022);
    JSONNumber* json_max = JSON_NUMBER_PTR(2147483647);
    /* create a json STRING */
    JSONString* json_str = JSON_STRING_PTR("");

    /* add a key-value in json OBJECT */
    json_obj->add(json_obj, "key0", json_num);
    json_obj->add_str(json_obj, "key1", "this is a `string` message");
    json_obj->add_num(json_obj, "key2", 1993);
    json_obj->add_true(json_obj, "key3");
    json_obj->add_false(json_obj, "key4");
    json_obj->add_null(json_obj, "key5");
    /* set a key-value in json OBJECT */
    json_obj->set(json_obj, "key0", json_max);
    json_obj->set_num(json_obj, "key1", 2022);
    json_obj->set_str(json_obj, "key2", "this is a `set` test");
    json_obj->set_null(json_obj, "key3");
    json_obj->set_true(json_obj, "key4");
    json_obj->set_false(json_obj, "key5");
    /* get a key-value in json OBJECT */
    json_obj->get(json_obj, "key2", json_str);
    int num = json_obj->get_num(json_obj, "key1");
    printf("key1: %d\n", num);
    char* chars = json_obj->get_str(json_obj, "key2");
    printf("key2: %s\n", chars);
    free(chars);
    /* delete a key-value in json OBJECT */
    json_obj->del(json_obj, "key0");

    /* add a value in tail end of json ARRAY */
    json_arr->add(json_arr, -1, json_num);
    json_arr->add_str(json_arr, -1, "this is second `string` message");
    json_arr->add_num(json_arr, -1, 1993);
    json_arr->add_true(json_arr, -1);
    json_arr->add_false(json_arr, -1);
    json_arr->add_null(json_arr, -1);
    /* set a value in json ARRAY */
    json_arr->set(json_arr, 0, json_max);
    json_arr->set_num(json_arr, 1, 2022);
    json_arr->set_str(json_arr, 2, "this is a `set` test");
    json_arr->set_null(json_arr, 3);
    json_arr->set_true(json_arr, -2);
    json_arr->set_false(json_arr, -1);
    /* get a value in json ARRAY */
    json_arr->get(json_arr, 1, json_num);
    num = json_arr->get_num(json_arr, 1);
    chars = json_arr->get_str(json_arr, 2);
    free(chars);
    /* delete a value in json ARRAY */
    json_arr->del(json_arr, -1);

    /* stringify a json */
    char* str;
    int len;
    JSON_STRINGIFY(json_obj, &str, &len);
    printf("%s\n", str);
    free(str);

    /* parse json string to a json object */
    str =
    "{"
        "\"object\":"
            "{"
                "\"\":\"\""
            "},"
        "\"array\":"
            "[],"
        "\"false\":false,"
        "\"string\":\"this is a string\","
        "\"true\":true,"
        "\"null\":null,"
        "\"number\":2022"
    "}";
    printf("%s\n", str);
    if(JSON_PARSE(str, json_obj))
    {
        printf("json parse failed\n");
    }
    JSON_STRINGIFY(json_obj, &str, &len);
    printf("%s\n", str);

    /* free json obejct */
    FREE_JSON(json_obj);
    FREE_JSON(json_arr);
    FREE_JSON(json_num);
    FREE_JSON(json_str);
    FREE_JSON(json_max);

    return 0;
}

jsong's People

Contributors

shaobosong avatar

Watchers

 avatar

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.