Alexander Graf | 190c882 | 2015-01-22 15:01:37 +0100 | [diff] [blame] | 1 | /* |
Markus Armbruster | 17b74b9 | 2016-05-04 18:49:17 +0200 | [diff] [blame] | 2 | * A simple JSON writer |
Alexander Graf | 190c882 | 2015-01-22 15:01:37 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright Alexander Graf |
| 5 | * |
| 6 | * Authors: |
Greg Kurz | 559782c | 2015-02-07 11:25:50 +0100 | [diff] [blame] | 7 | * Alexander Graf <agraf@suse.de> |
Alexander Graf | 190c882 | 2015-01-22 15:01:37 +0100 | [diff] [blame] | 8 | * |
| 9 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. |
| 10 | * See the COPYING.LIB file in the top-level directory. |
| 11 | * |
| 12 | */ |
| 13 | |
Markus Armbruster | 17b74b9 | 2016-05-04 18:49:17 +0200 | [diff] [blame] | 14 | /* |
| 15 | * Type QJSON lets you build JSON text. Its interface mirrors (a |
| 16 | * subset of) abstract JSON syntax. |
| 17 | * |
| 18 | * It does *not* detect incorrect use. It happily produces invalid |
| 19 | * JSON then. This is what migration wants. |
| 20 | * |
| 21 | * QAPI output visitors also produce JSON text. However, they do |
| 22 | * assert their preconditions and invariants, and therefore abort on |
| 23 | * incorrect use. |
| 24 | */ |
| 25 | |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 26 | #include "qemu/osdep.h" |
Markus Armbruster | 17b74b9 | 2016-05-04 18:49:17 +0200 | [diff] [blame] | 27 | #include "qapi/qmp/qstring.h" |
Juan Quintela | 05b98c2 | 2017-04-20 13:10:28 +0200 | [diff] [blame] | 28 | #include "qjson.h" |
Alexander Graf | 190c882 | 2015-01-22 15:01:37 +0100 | [diff] [blame] | 29 | |
| 30 | struct QJSON { |
Alexander Graf | 190c882 | 2015-01-22 15:01:37 +0100 | [diff] [blame] | 31 | QString *str; |
| 32 | bool omit_comma; |
| 33 | }; |
| 34 | |
| 35 | static void json_emit_element(QJSON *json, const char *name) |
| 36 | { |
| 37 | /* Check whether we need to print a , before an element */ |
| 38 | if (json->omit_comma) { |
| 39 | json->omit_comma = false; |
| 40 | } else { |
| 41 | qstring_append(json->str, ", "); |
| 42 | } |
| 43 | |
| 44 | if (name) { |
| 45 | qstring_append(json->str, "\""); |
| 46 | qstring_append(json->str, name); |
| 47 | qstring_append(json->str, "\" : "); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | void json_start_object(QJSON *json, const char *name) |
| 52 | { |
| 53 | json_emit_element(json, name); |
| 54 | qstring_append(json->str, "{ "); |
| 55 | json->omit_comma = true; |
| 56 | } |
| 57 | |
| 58 | void json_end_object(QJSON *json) |
| 59 | { |
| 60 | qstring_append(json->str, " }"); |
| 61 | json->omit_comma = false; |
| 62 | } |
| 63 | |
| 64 | void json_start_array(QJSON *json, const char *name) |
| 65 | { |
| 66 | json_emit_element(json, name); |
| 67 | qstring_append(json->str, "[ "); |
| 68 | json->omit_comma = true; |
| 69 | } |
| 70 | |
| 71 | void json_end_array(QJSON *json) |
| 72 | { |
| 73 | qstring_append(json->str, " ]"); |
| 74 | json->omit_comma = false; |
| 75 | } |
| 76 | |
| 77 | void json_prop_int(QJSON *json, const char *name, int64_t val) |
| 78 | { |
| 79 | json_emit_element(json, name); |
| 80 | qstring_append_int(json->str, val); |
| 81 | } |
| 82 | |
| 83 | void json_prop_str(QJSON *json, const char *name, const char *str) |
| 84 | { |
| 85 | json_emit_element(json, name); |
| 86 | qstring_append_chr(json->str, '"'); |
| 87 | qstring_append(json->str, str); |
| 88 | qstring_append_chr(json->str, '"'); |
| 89 | } |
| 90 | |
| 91 | const char *qjson_get_str(QJSON *json) |
| 92 | { |
| 93 | return qstring_get_str(json->str); |
| 94 | } |
| 95 | |
| 96 | QJSON *qjson_new(void) |
| 97 | { |
Markus Armbruster | b72fe9e | 2016-05-04 18:49:18 +0200 | [diff] [blame] | 98 | QJSON *json = g_new0(QJSON, 1); |
| 99 | |
| 100 | json->str = qstring_from_str("{ "); |
| 101 | json->omit_comma = true; |
Alexander Graf | 190c882 | 2015-01-22 15:01:37 +0100 | [diff] [blame] | 102 | return json; |
| 103 | } |
| 104 | |
| 105 | void qjson_finish(QJSON *json) |
| 106 | { |
| 107 | json_end_object(json); |
| 108 | } |
| 109 | |
Markus Armbruster | b72fe9e | 2016-05-04 18:49:18 +0200 | [diff] [blame] | 110 | void qjson_destroy(QJSON *json) |
Alexander Graf | 190c882 | 2015-01-22 15:01:37 +0100 | [diff] [blame] | 111 | { |
Marc-André Lureau | df37dd6 | 2016-07-15 10:41:03 +0200 | [diff] [blame] | 112 | QDECREF(json->str); |
Markus Armbruster | b72fe9e | 2016-05-04 18:49:18 +0200 | [diff] [blame] | 113 | g_free(json); |
Alexander Graf | 190c882 | 2015-01-22 15:01:37 +0100 | [diff] [blame] | 114 | } |