GithubHelp home page GithubHelp logo

maycut51 / tiny-json Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rafagafe/tiny-json

0.0 0.0 0.0 86 KB

The tiny-json is a versatile and easy to use json parser in C suitable for embedded systems. It is fast, robust and portable.

License: MIT License

C 98.92% Makefile 1.08%

tiny-json's Introduction

tiny-json

Build Status GitHub contributors

tiny-json is a versatile and easy to use json parser written in C and suitable for embedded systems. It is fast, robust and portable.

It is not only a tokenizer. You can access json data in string format or get primitive values directly as C type variables without any loss of performance.

You can access the JSON fields one on one or get their values by their names. This helps you to save a lot of source code lines and development time.

  • It does not use recursivity.
  • It does not use dynamic memory. The memory you use can be reserved statically.
  • There is no limit for nested levels in arrays or json objects.
  • The JSON property number limit is determined by the size of a buffer that can be statically reserved.

If you need to create JSON strings please visit: https://github.com/rafagafe/json-maker

Philosophy

When parsing a JSON text string a tree is created by linking json_t structures. Navigating or querying this tree is very easy using the provided API.

To maintain reduced memory usage and fast processing the strings are not copied. When you request the value of a JSON element, a reference to the original JSON string is returned.

To facilitate the processing of the data the returned strings are null-terminated. This is achieved by setting the null character to JSON control characters such as commas, brackets, braces, and quotation marks.

API

The tiny-json API provides two types. jsonType_t is an enumeration for all possible JSON field types. json_t is a structure containing internal data which you don't need to know.

typedef enum {
    JSON_OBJ, JSON_ARRAY, JSON_TEXT, JSON_BOOLEAN,
    JSON_INTEGER, JSON_REAL, JSON_NULL
} jsonType_t;

To parse a JSON string use json_create(). We pass it an array of json_t for it to allocate JSON fields. If the JSON string is bad formated or has more fields than the array this function returns a null pointer.

enum { MAX_FIELDS = 4 };
json_t pool[ MAX_FIELDS ];

char str[] = "{ \"name\": \"peter\", \"age\": 32 }";	

json_t const* parent = json_create( str, pool, MAX_FIELDS );
if ( parent == NULL ) return EXIT_FAILURE;

To get a field by its name we use json_getProperty(). If the field does not exist the function returns a null pointer. To get the type of a field we use json_getType().

json_t const* namefield = json_getProperty( parent, "name" );
if ( namefield == NULL ) return EXIT_FAILURE;
if ( json_getType( namefield ) != JSON_TEXT ) return EXIT_FAILURE;

To get the value of a field in string format we use json_getValue(). It always returns a valid null-teminated string.

char const* namevalue = json_getValue( namefield );
printf( "%s%s%s", "Name: '", namevalue, "'.\n" );

For primitive fields we can use a specific function to get the fields value directly as a C type, f.i. json_getInteger() or we can use json_getValue() to get its value in text format.

json_t const* agefield = json_getProperty( parent, "age" );
if ( agefield == NULL ) return EXIT_FAILURE;
if ( json_getType( agefield ) != JSON_INTEGER ) return EXIT_FAILURE;

int64_t agevalue = json_getInteger( agefield );
printf( "%s%lld%s", "Age: '", agevalue, "'.\n" );

char const* agetxt = json_getValue( agefield );
printf( "%s%s%s", "Age: '", agetxt, "'.\n" );

For an example how to use nested JSON objects and arrays please see example-01.c.

tiny-json's People

Contributors

rafagafe avatar namazso avatar alamyliu avatar jurgen178 avatar peterssharp avatar ibortolazzi avatar themiron avatar void256 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.