Coder Social home page Coder Social logo

json_decoder's Introduction

JSON Decoder

JSON Decoder aims to:

  1. Be highly optimized for constrained embedded systems
  2. Work on both PIC18 and AVR

Example Usage

#define TEST_JSON "{\"main\":{\"key\" : 10,\"foo\":\"bar\"}, \"alt\":2}"

...

jsonNode_t *root, *objmain;
char foo[10], str[50];
int alt;

memcpy(str, TEST_JSON, sizeof(TEST_JSON));

JSON_DECODER_fromString(str)

JSON_DECODER_getRoot(&root);
JSON_DECODER_getObject(root, "main", &objmain);

JSON_DECODER_getNumber(root, "alt", &alt)
JSON_DECODER_getString(objmain, "foo", strlen(foo), foo)

API

jsonDecoderStatus_t JSON_DECODER_fromString(char *str)

Description

Parses a JSON string into a C representation. It supports string, number and object values.

Params
Name Type Description
str IN A string representation of a JSON object. After parsing, the string will have been altered.
Return
Name Description
JSON_DECODER_OK Decoding was successful
JSON_DECODER_BAD_FORMAT The input is invalid.

JSON_DECODER_getRoot(jsonNode_t **pNode)

Description

The outermost object in a JSON is called the root. This function must not be called before JSON_DECODER_fromString.

Params
Name Type Description
pNode OUT Pointer to the root. When the function is called this parameter does not need to point to a valid jsonNode_t address.
Return
Name Description
JSON_DECODER_OK pNode now points to the root

jsonDecoderStatus_t JSON_DECODER_getObject(jsonNode_t *current, char *key, jsonNode_t **pNode)

Description

Finds a JSON object by key in another JOSN object. This function must not be called before JSON_DECODER_fromString.

Params
Name Type Description
current IN The JSON object to search into
key IN The key of the object to find
pNode OUT Pointer to the object, if found. When the function is called this parameter does not need to point to a valid jsonNode_t address.
Return
Name Description
JSON_DECODER_OK Object was found and pNode now points to in
JSON_DECODER_KEY_NOT_FOUND The specified key does not exist

jsonDecoderStatus_t JSON_DECODER_getString(jsonNode_t *current, char *key, uint8_t size, char *pVal)

Description

Finds a string by key in a JOSN object. This function must not be called before JSON_DECODER_fromString.

Params
Name Type Description
current IN The JSON object to search into
key IN The key of the string to find
size IN Maximum size of the string. Should not be bigger than the length of the pVal buffer
pVal OUT Pointer to the string, if found
Return
Name Description
JSON_DECODER_OK String was found and pVal now points to in
JSON_DECODER_KEY_NOT_FOUND The specified key does not exist

jsonDecoderStatus_t JSON_DECODER_getNumber(jsonNode_t *current, char *key, int *pVal)

Description

Finds a number by key in a JSON object. This function must not be called before JSON_DECODER_fromString.

Params
Name Type Description
current IN The JSON object to search into
key IN The key of the string to find
pVal OUT Pointer to the number, if found
Return
Name Description
JSON_DECODER_OK Number was found and pVal now points to in
JSON_DECODER_KEY_NOT_FOUND The specified key does not exist

json_decoder's People

Contributors

alexmchp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

json_decoder's Issues

Doesn't work with PIC18F27K40

Hi I have tried to use your library with PIC18F27K40. But it doesn't work. The library worked in Proteus simulation with PIC18LF46K80. But same code doesn't work with PIC18F27K40 hardware. it toggle six times. that means JSON_DECODER_fromString() doesn't return JSON_DECODER_OK. Please Help.

Here is my code:
#include "mcc_generated_files/mcc.h"
#include "json_decoder.h"
#include<stdlib.h>
#include<string.h>
#define LED "{"blink":1,"setHigh":2,"setLow":0}"

/*
Main application
*/
jsonNode_t *root;
int blink, setHigh, setLow;
char str[64];
jsonDecoderStatus_t ret;

void toggle_times(int n) {
int i;
for (i = 0; i < n; i++) {
LED_RED_Toggle();
LED_GREEN_Toggle();
LED_BLUE_Toggle();
__delay_ms(1000);
}
}

void set_all_low() {

LED_RED_SetLow();
LED_GREEN_SetLow();
LED_BLUE_SetLow();

}

void set_all_high() {

LED_RED_SetHigh();
LED_GREEN_SetHigh();
LED_BLUE_SetHigh();

}

void main(void) {
// Initialize the device
SYSTEM_Initialize();
memcpy(str, LED, sizeof (LED));
ret = JSON_DECODER_fromString(str);
if (JSON_DECODER_OK != ret) {
toggle_times(6);
set_all_high();
}

JSON_DECODER_getRoot(&root);
JSON_DECODER_getNumber(root, "setHigh", &setHigh);

JSON_DECODER_getNumber(root, "blink", &blink);

JSON_DECODER_getNumber(root, "setLow", &setLow);

switch (setHigh) {
    case 0:
        LED_RED_SetHigh();
        break;
    case 1:
        LED_GREEN_SetHigh();
        break;
    case 2:
        LED_BLUE_SetHigh();
        break;
}
switch (setLow) {
    case 0:
        LED_RED_SetLow();
        break;
    case 1:
        LED_GREEN_SetLow();
        break;
    case 2:
        LED_BLUE_SetLow();
        break;
}



while (1) {
    switch (blink) {
        case 0:
            LED_RED_Toggle();
            break;
        case 1:
            LED_GREEN_Toggle();
            break;
        case 2:
            LED_BLUE_Toggle();
            break;
    }
    __delay_ms(1000);
    // Add your application code
}

}
/**
End of File
*/_

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.