2
0

qjson.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * A simple JSON writer
  3. *
  4. * Copyright Alexander Graf
  5. *
  6. * Authors:
  7. * Alexander Graf <agraf@suse.de>
  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. /*
  14. * Type QJSON lets you build JSON text. Its interface mirrors (a
  15. * subset of) abstract JSON syntax.
  16. *
  17. * It does *not* detect incorrect use. It happily produces invalid
  18. * JSON then. This is what migration wants.
  19. *
  20. * QAPI output visitors also produce JSON text. However, they do
  21. * assert their preconditions and invariants, and therefore abort on
  22. * incorrect use.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qapi/qmp/qstring.h"
  26. #include "qjson.h"
  27. struct QJSON {
  28. QString *str;
  29. bool omit_comma;
  30. };
  31. static void json_emit_element(QJSON *json, const char *name)
  32. {
  33. /* Check whether we need to print a , before an element */
  34. if (json->omit_comma) {
  35. json->omit_comma = false;
  36. } else {
  37. qstring_append(json->str, ", ");
  38. }
  39. if (name) {
  40. qstring_append(json->str, "\"");
  41. qstring_append(json->str, name);
  42. qstring_append(json->str, "\" : ");
  43. }
  44. }
  45. void json_start_object(QJSON *json, const char *name)
  46. {
  47. json_emit_element(json, name);
  48. qstring_append(json->str, "{ ");
  49. json->omit_comma = true;
  50. }
  51. void json_end_object(QJSON *json)
  52. {
  53. qstring_append(json->str, " }");
  54. json->omit_comma = false;
  55. }
  56. void json_start_array(QJSON *json, const char *name)
  57. {
  58. json_emit_element(json, name);
  59. qstring_append(json->str, "[ ");
  60. json->omit_comma = true;
  61. }
  62. void json_end_array(QJSON *json)
  63. {
  64. qstring_append(json->str, " ]");
  65. json->omit_comma = false;
  66. }
  67. void json_prop_int(QJSON *json, const char *name, int64_t val)
  68. {
  69. json_emit_element(json, name);
  70. qstring_append_int(json->str, val);
  71. }
  72. void json_prop_str(QJSON *json, const char *name, const char *str)
  73. {
  74. json_emit_element(json, name);
  75. qstring_append_chr(json->str, '"');
  76. qstring_append(json->str, str);
  77. qstring_append_chr(json->str, '"');
  78. }
  79. const char *qjson_get_str(QJSON *json)
  80. {
  81. return qstring_get_str(json->str);
  82. }
  83. QJSON *qjson_new(void)
  84. {
  85. QJSON *json = g_new0(QJSON, 1);
  86. json->str = qstring_from_str("{ ");
  87. json->omit_comma = true;
  88. return json;
  89. }
  90. void qjson_finish(QJSON *json)
  91. {
  92. json_end_object(json);
  93. }
  94. void qjson_destroy(QJSON *json)
  95. {
  96. qobject_unref(json->str);
  97. g_free(json);
  98. }