json lib
Dependents: grove_stream_jpa_sd2 grove_stream_jpa_sd2 grove_stream_jpa_sd2-2 grove_stream_jpa_sd2-3 ... more
Json.cpp
- Committer:
- mercurywaters
- Date:
- 2016-05-13
- Revision:
- 0:7f4a18b3fd82
- Child:
- 3:fab591fca1e7
File content as of revision 0:7f4a18b3fd82:
#include "Json.h" #include "debug.h" Json::Json ( const char * jsonString, size_t length ) : source(jsonString), sourceLength(length) { jsmn_parser parser; 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() { delete [] tokens; } 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 ++ ) { jsmntok_t t = tokens [ i ]; 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 ) ) { retVal = i; break; } } } } return retVal; } int Json::findChildIndexOf ( const int &parentIndex, const int &startingAt ) const { int retVal = -1; if ( parentIndex != -1 && parentIndex < tokenCount ) { jsmntype_t type = tokens [ parentIndex ].type; if ( ( type == JSMN_KEY ) || ( type == JSMN_OBJECT ) || ( type == JSMN_ARRAY ) ) { int i = startingAt + 1; if ( startingAt < 0 ) { i = 0; } for( ; i < tokenCount; i ++ ) { if ( tokens [ i ].parent == parentIndex ) { retVal = i; break; } } } } return retVal; } 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 ); } 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 }