Squashed 'third_party/jsont/' content from commit 1536152d7

Change-Id: I51a80190772b74ca0d45fd3fadc130e872b57cc0
git-subtree-dir: third_party/jsont
git-subtree-split: 1536152d7c1926448d42e4a691acd9a15940b20c
diff --git a/example1.c b/example1.c
new file mode 100644
index 0000000..c36559c
--- /dev/null
+++ b/example1.c
@@ -0,0 +1,76 @@
+//
+// This is a simple example of running the tokenizer, outputting information
+// to stdout about what tokens we get and their values.
+//
+#include <jsont.h>
+#include <stdio.h>
+#include <string.h>
+
+static const char* _tok_name(jsont_tok_t tok);
+
+int main(int argc, const char** argv) {
+  // Create a new reusable tokenizer
+  jsont_ctx_t* S = jsont_create(0);
+
+  // Sample input
+  const char* inbuf = "{\"Ape\":123,\"Bro\":[400192,\"51\",true, false, null,"
+                      " -67,\r\n\t 6.123]}";
+
+  // Reset the parser with a pointer to our sample input
+  jsont_reset(S, (const uint8_t*)inbuf, strlen(inbuf));
+
+  // Read each token
+  jsont_tok_t tok;
+  printf("Token        | Value\n"
+         "-------------|----------------------------------------\n");
+  while ( (tok = jsont_next(S)) != JSONT_END && tok != JSONT_ERR) {
+    printf("%-12s |", _tok_name(tok));
+
+    // If the token has a value, also print its value
+    if (tok == JSONT_STRING || tok == JSONT_FIELD_NAME) {
+      const uint8_t* bytes = 0;
+      size_t len = jsont_data_value(S, &bytes);
+      if (len != 0)
+        printf(" '%.*s'", (int)len, (const char*)bytes);
+    } else if (tok == JSONT_NUMBER_INT) {
+      printf(" %lld", jsont_int_value(S));
+    } else if (tok == JSONT_NUMBER_FLOAT) {
+      printf(" %f", jsont_float_value(S));
+    }
+    
+    printf("\n");
+  }
+
+  // If we got an error, print some useful information and exit with 1
+  if (tok == JSONT_ERR) {
+    fprintf(stderr, "Error: %s ('%c' at offset %lu)\n",
+            jsont_error_info(S),
+            (char)jsont_current_byte(S),
+            (unsigned long)jsont_current_offset(S));
+    return 1;
+  }
+
+  // Destroy our reusable tokenizer and exit
+  jsont_destroy(S);
+  return 0;
+}
+
+// Utility to get a printable name for a token
+static const char* _tok_name(jsont_tok_t tok) {
+  switch (tok) {
+    case JSONT_END:           return "END";
+    case JSONT_ERR:           return "ERR";
+    case JSONT_OBJECT_START:  return "OBJECT_START";
+    case JSONT_OBJECT_END:    return "OBJECT_END";
+    case JSONT_ARRAY_START:   return "ARRAY_START";
+    case JSONT_ARRAY_END:     return "ARRAY_END";
+    case JSONT_TRUE:          return "TRUE";
+    case JSONT_FALSE:         return "FALSE";
+    case JSONT_NULL:          return "NULL";
+    case JSONT_NUMBER_INT:    return "NUMBER_INT";
+    case JSONT_NUMBER_FLOAT:  return "NUMBER_FLOAT";
+    case JSONT_STRING:        return "STRING";
+    case JSONT_FIELD_NAME:    return "FIELD_NAME";
+    default:                  return "?";
+  }
+}