All Projects → rafagafe → json-maker

rafagafe / json-maker

Licence: MIT license
C library used to code JSON objects in null-terminated strings

Programming Languages

c
50402 projects - #5 most used programming language
CMake
9771 projects

Projects that are alternatives of or similar to json-maker

rnk
rnk is a RTOS targeting ARM architecture.
Stars: ✭ 22 (-60%)
Mutual labels:  microcontroller, embeded
LameUI
A very lame UI library for embedded systems.
Stars: ✭ 35 (-36.36%)
Mutual labels:  microcontroller
Embedded UKF Library
A compact Unscented Kalman Filter (UKF) library for Teensy4/Arduino system (or any real time embedded system in general)
Stars: ✭ 31 (-43.64%)
Mutual labels:  microcontroller
xForth
Experimental Forth cross compiler for tiny devices
Stars: ✭ 53 (-3.64%)
Mutual labels:  microcontroller
pumbaa
Python on Simba.
Stars: ✭ 61 (+10.91%)
Mutual labels:  microcontroller
circuitpython
CircuitPython - a Python implementation for teaching coding with microcontrollers
Stars: ✭ 3,097 (+5530.91%)
Mutual labels:  microcontroller
ewok-kernel
A secure and high performances microkernel for building secure MCU-based IoTs
Stars: ✭ 69 (+25.45%)
Mutual labels:  microcontroller
chirp8-avr
CHIP-8 implementation in Rust targeting AVR microcontrollers
Stars: ✭ 40 (-27.27%)
Mutual labels:  microcontroller
StratifyOS
A Powerful embedded RTOS for ARM Cortex M microcontrollers
Stars: ✭ 94 (+70.91%)
Mutual labels:  microcontroller
uDevkit-SDK
Embedded systems SDK for Uniswarm boards and others (dsPIC33, dsPIC30, PIC24 and PIC32 compatible)
Stars: ✭ 14 (-74.55%)
Mutual labels:  microcontroller
DigiOS
Mini OS emulator for Digispark (an Attiny85 based microcontroller).
Stars: ✭ 46 (-16.36%)
Mutual labels:  microcontroller
lwjson
Lightweight JSON parser for embedded systems
Stars: ✭ 66 (+20%)
Mutual labels:  microcontroller
nrf52832-pac
Peripheral Access Crate for the nRF52832 microcontroller
Stars: ✭ 21 (-61.82%)
Mutual labels:  microcontroller
open-electronics
📚 💻 Great Resources for Electronics Enthusiasts
Stars: ✭ 347 (+530.91%)
Mutual labels:  microcontroller
smartsilo
Hardware-integrated system composed by a desktop app and a Node.js server able to control an Arduino and manipulate the temperature of grains within storage silos
Stars: ✭ 33 (-40%)
Mutual labels:  microcontroller
microflo-example-arduino
Quick start for MicroFlo on Arduino - clone and go!
Stars: ✭ 15 (-72.73%)
Mutual labels:  microcontroller
risc8
Mostly AVR compatible FPGA soft-core
Stars: ✭ 19 (-65.45%)
Mutual labels:  microcontroller
HIGH-TO-LOW
in this repository you will find codes in C and their equivalence in MIPS Assembly
Stars: ✭ 20 (-63.64%)
Mutual labels:  microcontroller
mdepx
MDEPX — A BSD-style RTOS
Stars: ✭ 17 (-69.09%)
Mutual labels:  microcontroller
air-snare
Play drums in the air.
Stars: ✭ 32 (-41.82%)
Mutual labels:  microcontroller

JSON Maker

Build Status

JSON Maker is a C library used to code JSON objects in null-terminated strings.

Surely the most effective method to create simple JSON objects is to use sprintf. But when you need to reuse code, nest objects or include arrays you can fall into the formatted-strings hell.

  • Backslash escapes are automatically added. Only in the fields of type string.
  • By means of compilation options, the use of print can be avoided. This is very useful in embedded systems with memory constraint.

If you need a JSON parser please visit: https://github.com/rafagafe/tiny-json

Philosophy

To form JSON objects in strings of characters are invoked sequences of functions that concatenate sub-strings. Each substring includes a field of the JSON object, key-value. To add fields of the type object or array you need to invoke two functions, one to open and another to close.

struct weather {
    int temp;
    int hum;
};

/** Convert a weather structure in a JSON string.
  * @param dest Destination memory block.
  * @param src Source structure.
  * @return The length of the null-terminated string in dest. */
int weather_to_json( char* dest, struct weather const* src ) {
    char* p = dest;                       // p always points to the null character
    p = json_objOpen( p, NULL );          // --> {\0
    p = json_int( p, "temp", src->temp ); // --> {"temp":22,\0
    p = json_int( p, "hum", src->hum );   // --> {"temp":22,"hum":45,\0
    p = json_objClose( p );               // --> {"temp":22,"hum":45},\0
    p = json_end( p );                    // --> {"temp":22,"hum":45}\0
    return p - dest;       
}
    

The complexity of these sequences of concatenations is kept in O(n) thanks to the fluent interface of JSON Maker.

It is very easy to extend the library by creating methods to convert C structures into JSON fields of object type. As with the arrays.

struct weather {
    int temp;
    int hum;
};

/* Add a time object property in a JSON string.
  "name":{"temp":-5,"hum":48}, */
char* json_weather( char* dest, char const* name, struct weather const* weather ) {
    // dest always points to the null character
    dest = json_objOpen( dest, name );              // --> "name":{\0
    dest = json_int( dest, "temp", weather->temp ); // --> "name":{"temp":22,\0
    dest = json_int( dest, "hum", weather->hum );   // --> "name":{"temp":22,"hum":45,\0
    dest = json_objClose( dest );                   // --> "name":{"temp":22,"hum":45},\0
    return dest;
}

struct time {
    int hour;
    int minute;
};

/* Add a time object property in a JSON string.
  "name":{"hour":18,"minute":32}, */
char* json_time( char* dest, char const* name, struct time const* time ) {
    dest = json_objOpen( dest, name );
    dest = json_int( dest, "hour",   time->hour   );
    dest = json_int( dest, "minute", time->minute );
    dest = json_objClose( dest );
    return dest;
}

struct measure {
    struct weather weather;
    struct time time;
};

/** Convert a weather structure in a JSON string.
  * {"weather":{"temp":-5,"hum":48},"time":{"hour":18,"minute":32}}
  * @param dest Destination memory block.
  * @param src Source structure.
  * @return The length of the null-terminated string in dest. */
int measure_to_json( char* dest, struct measure const* measure ) {
    char* p = json_objOpen( dest, NULL );
    p = json_weather( p, "weather", &measure->weather );
    p = json_time( p, "time", &measure->time );
    p = json_objClose( p );
    p = json_end( p );
    return p - dest;
}

To see more nested JSON objects and arrays please read example.c.

#Building and Testing

JSON Maker is built as a static library. JSON Maker relies on CMake and CTest for building and testing (see https://cmake.org/ for more information).

The library is found under {project_root}/build/lib/static, the executables under {project_root}/build/bin.

Two configurations are supported, Debug and Release. You can use option CMAKE_BUILD_TYPE to change from the default (Debug). The following examples assume Debug builds, except for install which uses Release.

Create the build folder

mkdir build
cd build

##Building the static library

cmake --configure ..
cmake --build .

##Installing the static library if you are not super-user or don't want to use sudo you can install the library in your home's opt folder.

cmake --configure -DCMAKE_BUILD_TYPE=Release..
cmake --build .

As sudo/root (will use default /usr/local/ on Unix)

cmake --install .

As a regular user

cmake --install . --prefix $HOME/opt

##Building the sample application

cmake --configure -DBUILD_SAMPLES=ON ..
cmake --build .

##Runing the tests

cmake --configure ..
cmake --build .
cd tests
ctest .
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].