json lib

Dependents:   grove_stream_jpa_sd2 grove_stream_jpa_sd2 grove_stream_jpa_sd2-2 grove_stream_jpa_sd2-3 ... more

Json.h

Committer:
faheem_chaudhary
Date:
2016-08-02
Revision:
4:ae34010d87e5
Parent:
3:fab591fca1e7
Child:
5:dd98cf00ed9b

File content as of revision 4:ae34010d87e5:

#ifndef __JSON_LIB_CLASS_H_
#define __JSON_LIB_CLASS_H_

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

/**
 * JSON wrapper over JSMN lib
 */

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

    public:
        /** The only constructor allowed.  As JSON object will create/allocate
         *  memory for it's working, in favor of small memory fottprints, it
         *  is not allowed to be passed-by-value.  So there is no copy or 
         *  default constructor
         *  @param jsonString - char string containing JSON data
         *  @param length - length of the jsonString
         */
        Json ( const char * jsonString, size_t length );
        virtual ~Json ();

        int findKeyIndex ( const char * key, const int &startingAt ) const;
        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 bool isValidJson () const;
        inline jsmntype_t type ( const int tokenIndex ) const;
        inline int parent ( const int tokenIndex ) const;
        inline int childCount ( const int tokenIndex ) const;
        inline int tokenLength ( const int tokenIndex ) const;
        inline const char * tokenAddress ( const int tokenIndex ) const;

        int tokenIntegerValue ( const int tokenIndex ) const;
        float tokenNumberValue ( const int tokenIndex ) const;

        inline bool tokenBooleanValue ( const int tokenIndex ) const;

        // void print () const;
        
        static char * unescape ( char * jsonString );
};

inline bool Json::isValidJson () const
{
    return ( tokenCount >= 1 );
}

inline jsmntype_t Json::type ( const int tokenIndex ) const
{
    return tokens [ tokenIndex ].type;
}

inline int Json::parent ( const int tokenIndex ) const
{
    return tokens [ tokenIndex ].parent;
}

inline int Json::childCount ( const int tokenIndex ) const
{
    return tokens [ tokenIndex ].childCount;
}

inline int Json::tokenLength ( const int tokenIndex ) const
{
    return tokens [ tokenIndex ].end - tokens [ tokenIndex ].start;
}

inline const char * Json::tokenAddress ( const int tokenIndex ) const
{
    return source + tokens [ tokenIndex ].start;
}

inline bool Json::tokenBooleanValue ( const int tokenIndex ) const
{
    if ( type ( tokenIndex ) == JSMN_PRIMITIVE )
    {
        return matches ( tokenIndex, "true" );
    }
    return false;
}

#endif