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:
- 5:dd98cf00ed9b
- Parent:
- 4:ae34010d87e5
- Child:
- 8:43e1e35bb7ec
diff -r ae34010d87e5 -r dd98cf00ed9b Json.cpp --- a/Json.cpp Tue Aug 02 20:21:04 2016 +0000 +++ b/Json.cpp Mon Aug 15 22:50:26 2016 +0000 @@ -1,18 +1,39 @@ +/* Json.cpp */ +/* Original Author: Faheem Inayat + * Created by "Night Crue" Team @ TechShop San Jose, CA + * + * MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + #include "Json.h" -Json::Json ( const char * jsonString, size_t length ) - : source ( jsonString ), sourceLength ( length ) +Json::Json ( const char * jsonString, size_t length, unsigned int maxTokens ) + : maxTokenCount ( maxTokens ), source ( jsonString ), sourceLength ( length ) { jsmn_parser parser; - int count = 100; //jsmn_parse ( &parser, jsonString, length, NULL, 16384 ); - tokens = new jsmntok_t [ count ]; + tokens = new jsmntok_t [ maxTokenCount ]; jsmn_init ( &parser ); - tokenCount = jsmn_parse ( &parser, jsonString, length, tokens, count ); + tokenCount = jsmn_parse ( &parser, jsonString, length, tokens, maxTokenCount ); } -Json::Json ( const Json & other ) - : source ( NULL ), sourceLength ( 0 ) +Json::Json ( const Json & ) + : maxTokenCount ( 0 ), source ( NULL ), sourceLength ( 0 ) { tokenCount = 0; tokens = NULL; @@ -54,12 +75,10 @@ { int retVal = -1; - if ( parentIndex != -1 && parentIndex < tokenCount ) + if ( isValidToken ( parentIndex ) ) { - for ( int i = parentIndex + 1; i < tokenCount; i++ ) { - jsmntok_t t = tokens [ i ]; if ( t.end >= tokens [ parentIndex ].end ) @@ -86,7 +105,7 @@ { int retVal = -1; - if ( parentIndex != -1 && parentIndex < tokenCount ) + if ( isValidToken ( parentIndex ) ) { jsmntype_t type = tokens [ parentIndex ].type; @@ -98,7 +117,7 @@ i = 0; } - for ( ; i < tokenCount; i++ ) + for ( i += parentIndex; i < tokenCount; i++ ) { if ( tokens [ i ].parent == parentIndex ) { @@ -114,59 +133,64 @@ bool Json::matches ( const int & tokenIndex, const char * value ) const { - jsmntok_t token = tokens [ tokenIndex ]; - return ( strncmp ( source + token.start, value, ( token.end - token.start ) ) == 0 ); + bool retVal = false; + + if ( isValidToken ( tokenIndex ) ) + { + jsmntok_t token = tokens [ tokenIndex ]; + retVal = ( strncmp ( source + token.start, value, ( token.end - token.start ) ) == 0 ); + } + + return retVal; } -int Json::tokenIntegerValue ( const int tokenIndex ) const +int Json::tokenIntegerValue ( const int tokenIndex, int &returnValue ) 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 -{ + int retVal = -1; + 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 ); + returnValue = atoi ( tok ); delete [] tok; - return retVal; + retVal = 0; } - return -1; + return retVal; } +int Json::tokenNumberValue ( const int tokenIndex, float &returnValue ) const +{ + int retVal = -1; + + if ( type ( tokenIndex ) == JSMN_PRIMITIVE ) + { + int len = tokenLength ( tokenIndex ); + char * tok = new char [ len + 1 ]; + strncpy ( tok, tokenAddress ( tokenIndex ), len ); + tok [ len ] = 0; + returnValue = atof ( tok ); + delete [] tok; + retVal = 0; + } + + return retVal; +} -// 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 -// } +int Json::tokenBooleanValue ( const int tokenIndex, bool &returnValue ) const +{ + int retVal = -1; + + if ( type ( tokenIndex ) == JSMN_PRIMITIVE ) + { + returnValue = matches ( tokenIndex, "true" ); + retVal = 0; + } + + return retVal; +} char * Json::unescape ( char * jsonString ) {