Night Crue / JSON Featured

Dependents:   ATT_WNCInterface_Info WNCInterface_HTTP_example NerfUS-Coord Mbed_Prototype_copy_4_INNO_day_15_6_2017 ... more

C++ JSON wrapper over JSMN lib (https://github.com/zserge/jsmn).

This C++ Class is a set of common tools/procedures as a C++ wrapper over JSMN JSON parser library. It is intended to provide the boiler-plate code, with intentions to reduce code clutter, in more of C++ fashion.

In contrast to original library, Json is intended to work strictly with valid JSON structures. Non-standard JSON structures should result in an error.

This class works explicitly on the indices returned by underlying JSMN library. In the scope of this class, its function parameters, return types, and documentation, the term 'index' will always mean the index of JSMN tokens, parsed by the Json constructor, unless and until explicitly mentioned otherwise.

include/Json.h

Committer:
mercurywaters
Date:
2016-05-13
Revision:
0:7f4a18b3fd82
Child:
2:d33e3dbe6d07

File content as of revision 0:7f4a18b3fd82:

#ifndef __JSON_LIB_CLASS_H_
#define __JSON_LIB_CLASS_H_

#include "jsmn.h"
#include <stdlib.h>
#include <string.h>

class Json {
private:
    const char * source;
    const size_t sourceLength;
    jsmntok_t * tokens;
    int tokenCount;

public:
    Json ( const char * jsonString, size_t length );
    Json ( const Json & other ) : source (NULL), sourceLength (0) {};
    virtual ~Json ();

    int findKeyIndexIn ( const char * key, const int &parentIndex ) const;
    int findChildIndexOf ( const int &parentIndex, const int &startingAt ) const;
    bool matches ( const int & tokenIndex, const char * value ) const;
    
    inline jsmntype_t type ( const int tokenIndex ) const { return tokens [ tokenIndex ].type; };
    inline int parent ( const int tokenIndex ) const { return tokens [ tokenIndex ].parent; };
    inline int childCount ( const int tokenIndex ) const { return tokens [ tokenIndex ].childCount; };
    
    inline int tokenLength ( const int tokenIndex ) const { return tokens [ tokenIndex ].end - tokens [ tokenIndex ].start; };
    inline const char * tokenAddress ( const int tokenIndex ) const { return source + tokens [ tokenIndex ].start; };
    inline int tokenIntegerValue ( const int tokenIndex ) const {
        if ( type ( tokenIndex ) == JSMN_PRIMITIVE ) {
            int len = tokenLength ( tokenIndex );
            char * tok = new char [ len + 1 ];
            strncpy ( tok, tokenAddress( tokenIndex ), len );
            tok [ len ] = 0;
            int retVal = atoi(tok);
            delete [] tok;
            return retVal;
        }
        return -1;
    };
    inline float tokenNumberValue ( const int tokenIndex ) const {
        if ( type ( tokenIndex ) == JSMN_PRIMITIVE ) {
            int len = tokenLength ( tokenIndex );
            char * tok = new char [ len + 1 ];
            strncpy ( tok, tokenAddress( tokenIndex ), len );
            tok [ len ] = 0;
            float retVal = atof(tok);
            delete [] tok;
            return retVal;
        }
        return -1;
    };
    inline bool tokenBooleanValue ( const int tokenIndex ) const {
        if ( type ( tokenIndex ) == JSMN_PRIMITIVE ) {
            return matches ( tokenIndex, "true" );
        }
        return false;
    };
    
    void print () const;
};

#endif