Commits

del6597  committed b6a8f4f

Added a method for jsmn_estimate_tokens(const char *json);

  • Participants
  • Parent commits 09ad3d4

Comments (0)

Files changed (2)

 #include <stdlib.h>
+#include <string.h>
 
 #include "jsmn.h"
 
 }
 
 /**
+ * Estimate the number of JSON tokens in the string given.
+ * It should return a number greater than or equal to the actual amount of items.
+ * If the JSON string is malformed an incorrect number will be returned.
+ * Of course if the malformed string is going to be parsed, then parsing
+ * will fail anyway.
+ */
+int jsmn_count_tokens(const char *json) {
+    int c = 1;
+    int i;
+    for(i=0;i<strlen(json);i++) {
+        if(json[i] == ':' ||
+                json[i] == ',' ||
+                json[i] == '[' ||
+                json[i] == '{') {
+            c++;
+        }
+    }
+    return c;
+}
+
+/**
  * Fills next available token with JSON primitive.
  */
 static jsmnerr_t jsmn_parse_primitive(jsmn_parser *parser, const char *js,
 void jsmn_init(jsmn_parser *parser);
 
 /**
+ * Estimate the number of JSON tokens in the string given.
+ * It should return a number greater than or equal to the actual amount of items.
+ * If the JSON string is malformed an incorrect number will be returned.
+ * Of course if the malformed string is going to be parsed, then parsing
+ * will fail anyway.
+ */
+int jsmn_estimate_tokens(const char *json);
+
+/**
  * Run JSON parser. It parses a JSON data string into and array of tokens, each describing
  * a single JSON object.
  */