JSON library based on JSMN lib

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.

Revision:
3:fab591fca1e7
Parent:
0:7f4a18b3fd82
Child:
4:ae34010d87e5
--- 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
+// }
+