Coder Social home page Coder Social logo

hotakus / c-json Goto Github PK

View Code? Open in Web Editor NEW

This project forked from davegamble/cjson

1.0 0.0 0.0 2.52 MB

cJSON 的重构版本,将 cJSON 源码使用下划线风格(snake_case)重构;The refactored version of cJSON, refactoring the cJSON source code using the underline style (snake_case).

License: MIT License

Shell 0.10% Ruby 14.92% Python 1.26% C 80.13% Makefile 1.55% CMake 2.04%

c-json's Introduction

c-json

该 fork 将 cJSON 项目使用下划线风格(snake_case)进行重构,
若你喜欢下划线命名风格,那你可以直接使用这个 fork 项目,使用 git 拉取:

git clone --depth=1 https://github.com/Hotakus/c-json.git

该项目适用人群为:
喜欢下划线命名风格,并且项目为新建的项目(因为该项目是大量的 API 改动)。
功能与 cJSON 完全相同。


如何使用 (How to use)

很简单,用法和 cJSON 完全相同,但你需要知道该项目改动了什么。
以下是更改说明。

类型

该项目将类型(type)重构为 xxx_t 的形式:
例如,将类型 cJSON 重构为 cjson_t
又如,将类型 cJSON_Hooks 重构为 cjson_hooks_t
以此类推。

函数

该项目将函数名小写化,并在单词间增加 _
例如,将函数 cJSON_CreateObject() 重构为 cjson_create_object()
又如,将函数 cJSON_Delete() 重构为 cjson_delete()
以此类推。

预定义(#define)

该项目将 预定义 内容重构为纯大写、下划线形式。
例如,将定义 cJSON_Object 重构为 CJSON_OBJECT
又如,将定义 cJSON_StringISConst 重构为 CJSON_STRING_IS_CONST
以此类推。

例程

以下是一段读取 JSON 数据并解析的例程:

#include <stdio.h>
#include "cjson.h"

const char *json_data = "{"
                        "   \"name\" : \"Hotakus\", "
                        "   \"age\" : 114514, "
                        "   \"gender\" : \"helicopter\""
                        "}"; 

int main() {
    // 解析 json_data, 创建 cjson 根对象
    cjson_t *json_body = cjson_parse(json_data);
    if(json_body == NULL) {
        printf("Error: cannot parse JSON data\n");
        return -1;
    }
    
    // 根据 key 获取 value
    cjson_t *name = cjson_get_object_item(json_body, "name");
    cjson_t *age = cjson_get_object_item(json_body, "age");
    cjson_t *gender = cjson_get_object_item(json_body, "gender");
    
    // 输出解析结果
    printf("name  : %s\n", cjson_get_string_value(name));
    printf("age   : %f\n", cjson_get_number_value(age));
    // printf("age   : %d\n", age->valueint);
    printf("gender: %s\n", cjson_get_string_value(gender));
    
    // 用完释放
    cjson_delete(json_body);
    
    return 0;
}

输出:

name  : Hotakus
age   : 114514.000000
gender: helicopter

TODO

  • 进一步封装 API,支持旧项目

c-json's People

Contributors

alanscut avatar anton-sergeev avatar bigbrainafk avatar casperinous avatar davegamble avatar deborag avatar dongwenhuang avatar ffontaine avatar fpnuseis avatar fsmaxb avatar gatzka avatar gitmensch avatar hotakus avatar hqsz avatar kbranigan avatar miaoerduo avatar mjerris avatar pawelwms avatar randy408 avatar sanbeam avatar saynb avatar schmidtw avatar sdrsdr avatar simon-p-r avatar square789 avatar superh-0630 avatar timgates42 avatar vemakereporter avatar yangfl avatar zhindes avatar

Stargazers

 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.