json lib
Dependents: grove_stream_jpa_sd2 grove_stream_jpa_sd2 grove_stream_jpa_sd2-2 grove_stream_jpa_sd2-3 ... more
Diff: Json.cpp
- Revision:
- 3:fab591fca1e7
- Parent:
- 0:7f4a18b3fd82
- Child:
- 4:ae34010d87e5
diff -r d33e3dbe6d07 -r fab591fca1e7 Json.cpp --- a/Json.cpp Tue May 17 05:29:03 2016 +0000 +++ b/Json.cpp Thu Jun 02 06:08:24 2016 +0000 @@ -1,18 +1,24 @@ #include "Json.h" -#include "debug.h" Json::Json ( const char * jsonString, size_t length ) - : source(jsonString), sourceLength(length) + : source ( jsonString ), sourceLength ( length ) { jsmn_parser parser; - int count = 100;//jsmn_parse ( &parser, jsonString, length, NULL, 16384 ); + int count = 100; //jsmn_parse ( &parser, jsonString, length, NULL, 16384 ); tokens = new jsmntok_t [ count ]; - + jsmn_init ( &parser ); tokenCount = jsmn_parse ( &parser, jsonString, length, tokens, count ); } -Json::~Json() +Json::Json ( const Json & other ) + : source ( NULL ), sourceLength ( 0 ) +{ + tokenCount = 0; + tokens = NULL; +} + +Json::~Json () { delete [] tokens; } @@ -20,52 +26,62 @@ int Json::findKeyIndexIn ( const char * key, const int &parentIndex ) const { int retVal = -1; - - if ( parentIndex != -1 && parentIndex < tokenCount ) { - - for ( int i = parentIndex + 1; i < tokenCount; i ++ ) { - + + if ( parentIndex != -1 && parentIndex < tokenCount ) + { + + for ( int i = parentIndex + 1; i < tokenCount; i++ ) + { + jsmntok_t t = tokens [ i ]; - - if ( t.end >= tokens [ parentIndex ].end ) { + + if ( t.end >= tokens [ parentIndex ].end ) + { break; } - - if ( ( t.type == JSMN_KEY ) && ( t.parent == parentIndex ) ) { - int keyLength = t.end - t.start; - if ( ( strlen ( key ) == keyLength ) && ( strncmp ( source + t.start, key, keyLength ) == 0 ) ) { + + if ( ( t.type == JSMN_KEY ) && ( t.parent == parentIndex ) ) + { + size_t keyLength = (size_t) ( t.end - t.start ); + if ( ( strlen ( key ) == keyLength ) && ( strncmp ( source + t.start, key, keyLength ) == 0 ) ) + { retVal = i; break; } } } } - + return retVal; } int Json::findChildIndexOf ( const int &parentIndex, const int &startingAt ) const { int retVal = -1; - - if ( parentIndex != -1 && parentIndex < tokenCount ) { - + + if ( parentIndex != -1 && parentIndex < tokenCount ) + { + jsmntype_t type = tokens [ parentIndex ].type; - if ( ( type == JSMN_KEY ) || ( type == JSMN_OBJECT ) || ( type == JSMN_ARRAY ) ) { + if ( ( type == JSMN_KEY ) || ( type == JSMN_OBJECT ) || ( type == JSMN_ARRAY ) ) + { int i = startingAt + 1; - if ( startingAt < 0 ) { + if ( startingAt < 0 ) + { i = 0; } - - for( ; i < tokenCount; i ++ ) { - if ( tokens [ i ].parent == parentIndex ) { + + for ( ; i < tokenCount; i++ ) + { + if ( tokens [ i ].parent == parentIndex ) + { retVal = i; break; } } } } - + return retVal; } @@ -75,21 +91,61 @@ return ( strncmp ( source + token.start, value, ( token.end - token.start ) ) == 0 ); } -void Json::print () const +int Json::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; +} + +float Json::tokenNumberValue ( const int tokenIndex ) const { - #ifdef SOFTWARE_DEBUG - const char * TYPES [] = { - "UNDEFINED", - "OBJECT ", - "ARRAY ", - "STRING ", - "PRIMITIVE", - "KEY " - }; - - for ( int i = 0; i < tokenCount; i ++ ) { - debug ( "Index: %3d, Type:%d(%s), Indices: (%3d to %3d), ParentIndex: %3d, ChildCount: %3d Data: %.*s", i, tokens [ i ].type, TYPES [ tokens [ i ].type ], tokens [ i ].start, tokens [ i ].end, tokens [ i ].parent, tokens [ i ].childCount, tokens [ i ].end - tokens [ i ].start, source + tokens [ i ].start ); - } - debug ( "" ); - #endif + 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 Json::tokenBooleanValue ( const int tokenIndex ) const +{ + if ( type ( tokenIndex ) == JSMN_PRIMITIVE ) + { + return matches ( tokenIndex, "true" ); + } + return false; } + +// void Json::print () const +// { +// #ifdef SOFTWARE_DEBUG +// const char * TYPES [] = { +// "UNDEFINED", +// "OBJECT ", +// "ARRAY ", +// "STRING ", +// "PRIMITIVE", +// "KEY " +// }; +// +// for ( int i = 0; i < tokenCount; i ++ ) { +// debug ( "Index: %3d, Type:%d(%s), Indices: (%3d to %3d), ParentIndex: %3d, ChildCount: %3d Data: %.*s", i, tokens [ i ].type, TYPES [ tokens [ i ].type ], tokens [ i ].start, tokens [ i ].end, tokens [ i ].parent, tokens [ i ].childCount, tokens [ i ].end - tokens [ i ].start, source + tokens [ i ].start ); +// } +// debug ( "" ); +// #endif +// } +