]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/lib/json.qc
Add some useful lerp lib functions made use of in the polytrails branch and in mods
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / json.qc
index 555c4b10cc75a6877eb3450bedcde8a29aab87cb..b477fe15e59124924662e4c463030ff7016ca94e 100644 (file)
@@ -32,6 +32,7 @@ string _json_ns;
 // Current keys
 int _json_keys;
 
+ERASEABLE
 bool _json_parse_object() {
     JSON_BEGIN();
     if (STRING_ITERATOR_GET(_json) != '{') JSON_FAIL("expected '{'");
@@ -40,6 +41,7 @@ bool _json_parse_object() {
     JSON_END();
 }
 
+    ERASEABLE
     bool _json_parse_members() {
         JSON_BEGIN();
         for (;;) {
@@ -53,6 +55,7 @@ bool _json_parse_object() {
         JSON_END();
     }
 
+        ERASEABLE
         bool _json_parse_pair() {
             JSON_BEGIN();
             if (!_json_parse_string(false)) JSON_FAIL("expected string");
@@ -66,6 +69,7 @@ bool _json_parse_object() {
             JSON_END();
         }
 
+ERASEABLE
 bool _json_parse_array() {
     JSON_BEGIN();
     if (STRING_ITERATOR_GET(_json) != '[') JSON_FAIL("expected '['");
@@ -78,7 +82,7 @@ bool _json_parse_array() {
         int it = bufstr_add(_json_buffer, key, 0);
         bool ret = false; WITH(string, _json_ns, key, ret = _json_parse_value());
         if (!ret) {
-            bufstr_set(_json_buffer, it, string_null);
+            bufstr_free(_json_buffer, it);
             if (required) JSON_FAIL("expected value"); else break;
         }
         bufstr_set(_json_buffer, len, ftos(n + 1));
@@ -93,6 +97,7 @@ bool _json_parse_array() {
     JSON_END();
 }
 
+ERASEABLE
 bool _json_parse_value() {
     JSON_BEGIN();
     if (!(_json_parse_string(true)
@@ -105,6 +110,7 @@ bool _json_parse_value() {
     JSON_END();
 }
 
+    ERASEABLE
     bool _json_parse_true() {
         JSON_BEGIN();
         if (!(STRING_ITERATOR_GET(_json) == 't'
@@ -116,6 +122,7 @@ bool _json_parse_value() {
         JSON_END();
     }
 
+    ERASEABLE
     bool _json_parse_false() {
         JSON_BEGIN();
         if (!(STRING_ITERATOR_GET(_json) == 'f'
@@ -128,6 +135,7 @@ bool _json_parse_value() {
         JSON_END();
     }
 
+    ERASEABLE
     bool _json_parse_null() {
         JSON_BEGIN();
         if (!(STRING_ITERATOR_GET(_json) == 'n'
@@ -139,6 +147,7 @@ bool _json_parse_value() {
         JSON_END();
     }
 
+ERASEABLE
 bool _json_parse_string(bool add) {
     JSON_BEGIN();
     if (STRING_ITERATOR_GET(_json) != '"') JSON_FAIL("expected opening '\"'");
@@ -157,6 +166,7 @@ bool _json_parse_string(bool add) {
                 case 'n': esc = "\n"; break;
                 case 't': esc = "\t"; break;
                 case 'u': esc = "\\u"; break; // TODO
+                case '/': esc = "/"; break;
             }
             s = strcat(s, esc);
         } else {
@@ -169,12 +179,14 @@ bool _json_parse_string(bool add) {
     JSON_END();
 }
 
+ERASEABLE
 bool _json_parse_number() {
     JSON_BEGIN();
     if (!(_json_parse_float() || _json_parse_int())) JSON_FAIL("expected number");
     JSON_END();
 }
 
+    ERASEABLE
     bool _json_parse_float() {
         JSON_BEGIN();
         string s = "";
@@ -196,6 +208,7 @@ bool _json_parse_number() {
         JSON_END();
     }
 
+    ERASEABLE
     bool _json_parse_int() {
         JSON_BEGIN();
         string s = "";
@@ -213,6 +226,7 @@ bool _json_parse_number() {
         JSON_END();
     }
 
+ERASEABLE
 int json_parse(string in, bool() func) {
     string trimmed = "";
     LABEL(trim) {
@@ -258,6 +272,7 @@ int json_parse(string in, bool() func) {
     return _json_buffer;
 }
 
+ERASEABLE
 string json_get(int buf, string key)
 {
     for (int i = 1, n = buf_getsize(buf); i < n; i += 2) {
@@ -266,11 +281,20 @@ string json_get(int buf, string key)
     return string_null;
 }
 
+ERASEABLE
 void json_del(int buf)
 {
     buf_del(buf);
 }
 
+ERASEABLE
+void json_dump(int buf)
+{
+    for (int i = 0, n = buf_getsize(buf); i < n; ++i) {
+        print(bufstr_get(buf, i), "\n");
+    }
+}
+
 #undef JSON_BEGIN
 #undef JSON_FAIL
 #undef JSON_END
@@ -287,8 +311,6 @@ TEST(json, Parse)
     print(s, "\n");
     int buf = json_parse(s, _json_parse_object);
     EXPECT_NE(-1, buf);
-    for (int i = 0, n = buf_getsize(buf); i < n; ++i) {
-        print(bufstr_get(buf, i), "\n");
-    }
+    json_dump(buf);
     SUCCEED();
 }