Benoit Blanchon 11 лет назад
Родитель
Сommit
649f292ea7
3 измененных файлов с 41 добавлено и 26 удалено
  1. 9 0
      JsonParser/JsonToken.cpp
  2. 31 25
      JsonParser/JsonValue.cpp
  3. 1 1
      JsonParser/JsonValue.h

+ 9 - 0
JsonParser/JsonToken.cpp

@@ -1,3 +1,7 @@
+/*
+* Arduino JSON library
+* Benoit Blanchon 2014 - MIT License
+*/
 
 #include "JsonToken.h"
 
@@ -5,14 +9,19 @@ using namespace ArduinoJson::Parser;
 
 JsonToken JsonToken::nextSibling() const
 {
+    // start with current token
     jsmntok_t* t = token;
+
+    // count the number of token to skip
     int yetToVisit = 1;
     
+    // skip all nested tokens
     while (yetToVisit)
     {
         yetToVisit += t->size - 1;
         t++;
     }
 
+    // build a JsonToken at the new location
     return JsonToken(json, t);
 }

+ 31 - 25
JsonParser/JsonValue.cpp

@@ -11,6 +11,8 @@
 
 using namespace ArduinoJson::Parser;
 
+// Convert the JsonValue to a bool.
+// Returns false if the JsonValue is not a primitve.
 JsonValue::operator bool()
 {
     if (!isPrimitive()) return 0;
@@ -30,24 +32,50 @@ JsonValue::operator bool()
     return strtol(text, 0, 0) != 0;
 }
 
+// Convert the JsonValue to a floating point value.
+// Returns false if the JsonValue is not a number.
 JsonValue::operator double()
 {
     return isPrimitive() ? strtod(getText(), 0) : 0;
 }
 
+// Convert the JsonValue to a floating point value.
+// Returns false if the JsonValue is not a number.
 JsonValue::operator long()
 {
     return isPrimitive() ? strtol(getText(), 0, 0) : 0;
 }
 
+// Convert the JsonValue to a string.
+// Returns 0 if the JsonValue is not a string.
 JsonValue::operator char*()
 {
     return isString() || isPrimitive() ? getText() : 0;
 }
 
-/*
-* Returns the token for the value associated with the specified key
-*/
+// Get the nested value at the specified index.            
+// Returns an invalid JsonValue if the current value is not an array.
+JsonValue JsonValue::operator[](int index)
+{
+    // sanity check
+    if (index < 0 || !isArray() || index >= childrenCount())
+        return null();
+
+    // skip first token, it's the whole object
+    JsonToken runningToken = firstChild();
+
+    // skip all tokens before the specified index
+    for (int i = 0; i < index; i++)
+    {
+        // move forward: current + nested tokens
+        runningToken = runningToken.nextSibling();
+    }
+
+    return runningToken;
+}
+
+// Get the nested value matching the specified index.            
+// Returns an invalid JsonValue if the current value is not an object.
 JsonValue JsonValue::operator[](const char* desiredKey)
 {
     // sanity check
@@ -80,25 +108,3 @@ JsonValue JsonValue::operator[](const char* desiredKey)
     // nothing found, return NULL
     return null();
 }
-
-/*
-* Returns the token for the value at the specified index
-*/
-JsonValue JsonValue::operator[](int index)
-{
-    // sanity check
-    if (index < 0 || !isArray() || index >= childrenCount())
-        return null();
-
-    // skip first token, it's the whole object
-    JsonToken runningToken = firstChild();
-
-    // skip all tokens before the specified index
-    for (int i = 0; i < index; i++)
-    {
-        // move forward: current + nested tokens
-        runningToken = runningToken.nextSibling();
-    }
-
-    return runningToken;
-}

+ 1 - 1
JsonParser/JsonValue.h

@@ -45,7 +45,7 @@ namespace ArduinoJson
             }
 
             // Convert the JsonValue to a bool.
-            // Returns false if the JsonValue is not a boolean.
+            // Returns false if the JsonValue is not a primitve.
             operator bool();
 
             // Convert the JsonValue to a floating point value.