Code Monkey home page Code Monkey logo

emjson's Introduction

emJSON - JSON for embedded systems

emJSON is a fast and memory efficient JSON library for embedded systems, written in C99

There are two libraries in this project. You may use either one, depending on your favor:

  • json.h: Low-level JSON library that does not use dynamic memory allocation (e.g. malloc().)
  • emJSON.h : Easy-to-use JSON library that is built open json.h functions and makes use of dynamic memory allocation.

Features

  • JSON Encoding.
  • JSON Decoding.
  • Easy to use.
  • Fast and efficient hash map algorithm.
  • Minimized use of memory and memory fragmentation by using only a single buffer.
  • No use of malloc() (json.h only)
  • No extra library dependencies. (Only C standard libraries used.)
  • Only need to include a single file (emJSON.h or json.h)

Simple Examples

json.h:

char str_input[] = "{\"sensor1\":0.045600,\"message\":\"JSON Is Cool\",\"sensor2\":142}";

char buffer[256];
json_t test = json_init(buffer, 256, 4);  // 4 is table size, this MUST be power of 2.
json_parse(&test, str_input);

float sensor1 = json_get_float(&test, "sensor1"); // 0.0456
int   sensor2 = json_get_int(&test, "sensor2");   // 142
char *message = json_get_str(&test, "message");   // "JSON Is Cool"

json_insert_int(&test, "intInput", 999); // insert

char str[80];
json_strcpy(str, &test);
printf("%s\n", str);
// {"sensor1":0.045600,"intInput":999,"message":"JSON Is Cool","sensor2":142}

emJSON.h:

char str_input[] = "{\"sensor1\":0.045600,\"message\":\"JSON Is Cool\",\"sensor2\":142}";

// The difference to json.h is emJSON does not require declaring buffer. 
json_t test = emJSON_init();  // 8 is table size
emJSON_parse(&test, str_input);

float sensor1 = emJSON_get_float(&test, "sensor1"); // 0.0456
int   sensor2 = emJSON_get_int(&test, "sensor2");   // 142
char *message = emJSON_get_str(&test, "message");   // "JSON Is Cool"

emJSON_insert_int(&test, "intInput", 999); // insert

char *str = emJSON_string(&test);
printf("%s\n", str);
// {"sensor1":0.045600,"intInput":999,"message":"JSON Is Cool","sensor2":142}

free(str);
// emJSON.h objects needs to be freed after used instead.
emJSON_free(&test);

Supported Devices

  • Arduino and AVR Microcontrollers (tested using Arduino Uno)
  • 32-bit ARM Cortex-M devices
    • NUCLEO-F4xx series (tested in mbed platform)
    • Other Cortex-M MCUs should work too.
  • x86 and x86-64 based general purpose computers (such as Windows/Linux/Unix computers.)

How To Use

All you need is the header and source files in emJSON folder. You may also want to see documentations in doc folder before you get started. For testing and using existing sample codes, please look at examples folder.

emjson's People

Contributors

kbumsik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

bomei

emjson's Issues

Development progress

🚧 In progress.

Adding object type.

  • adding it in json.h : Done in PR #3
  • adding it in emJSON.h
  • adding parser for object type : Done in PR #5
  • handling buffer inside/outside of parent buffer

Adding null type (needed for adding object type)

  • adding it in json.h : Done in PR #3, #5
  • Serialize : Done in PR #5
  • adding it in emJSON.h
  • adding parser for null type
  • Proper handling

Others

  • Delete content size
  • Optimize
  • Automatic adjusting size
  • Compile option for architecture not supporting unaligned access (such as ARM by default)

Crashing input example (null pointer dereference), and other issues

I came across your library today, and after some poking around I discovered an input that causes the parser to deference a null pointer: {"":{. Complete example program:

#include "emJSON.h"
int main(void)
{
    char buf[] = "{\"\":{";
    json_t test = emJSON_init();
    json_parse(&test, buf);
}

After fixing, fuzzing may reveal additional crashing inputs. Other issues I noticed:

  • Compilation requires -DDEBUG due to an incorrectly-placed #endif
  • Unaligned memory accesses (to observe, compile and run with UBSan, -fsanitize=undefined)
  • Invalid signed shifts (to observe, compile and run with UBSan, -fsanitize=undefined)
  • Pointer arithmetic on void *, a GNU extension and not valid C99 (to observe, compile with -pedantic -std=c99)
  • Variadic macros that depend on a GNU extension, not valid in C99 (to observe, compile with -pedantic -std=c99)

I only point out the last two since the README says "written in C99."

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.