JsonObjectSerializer.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/JsonArraySerializer.h>
  28. #include <AK/JsonValue.h>
  29. namespace AK {
  30. template<typename Builder>
  31. class JsonObjectSerializer {
  32. public:
  33. explicit JsonObjectSerializer(Builder& builder)
  34. : m_builder(builder)
  35. {
  36. m_builder.append('{');
  37. }
  38. JsonObjectSerializer(const JsonObjectSerializer&) = delete;
  39. JsonObjectSerializer(JsonObjectSerializer&&) = delete;
  40. ~JsonObjectSerializer()
  41. {
  42. if (!m_finished)
  43. finish();
  44. }
  45. void add(const StringView& key, const JsonValue& value)
  46. {
  47. begin_item(key);
  48. value.serialize(m_builder);
  49. }
  50. void add(const StringView& key, const StringView& value)
  51. {
  52. begin_item(key);
  53. m_builder.append('"');
  54. m_builder.append_escaped_for_json(value);
  55. m_builder.append('"');
  56. }
  57. void add(const StringView& key, const String& value)
  58. {
  59. begin_item(key);
  60. m_builder.append('"');
  61. m_builder.append_escaped_for_json(value);
  62. m_builder.append('"');
  63. }
  64. void add(const StringView& key, const char* value)
  65. {
  66. begin_item(key);
  67. m_builder.append('"');
  68. m_builder.append_escaped_for_json(value);
  69. m_builder.append('"');
  70. }
  71. void add(const StringView& key, bool value)
  72. {
  73. begin_item(key);
  74. m_builder.append(value ? "true" : "false");
  75. }
  76. void add(const StringView& key, int value)
  77. {
  78. begin_item(key);
  79. m_builder.appendff("{}", value);
  80. }
  81. void add(const StringView& key, unsigned value)
  82. {
  83. begin_item(key);
  84. m_builder.appendff("{}", value);
  85. }
  86. void add(const StringView& key, long value)
  87. {
  88. begin_item(key);
  89. m_builder.appendff("{}", value);
  90. }
  91. void add(const StringView& key, long unsigned value)
  92. {
  93. begin_item(key);
  94. m_builder.appendff("{}", value);
  95. }
  96. void add(const StringView& key, long long value)
  97. {
  98. begin_item(key);
  99. m_builder.appendff("{}", value);
  100. }
  101. void add(const StringView& key, long long unsigned value)
  102. {
  103. begin_item(key);
  104. m_builder.appendff("{}", value);
  105. }
  106. void add(const StringView& key, double value)
  107. {
  108. begin_item(key);
  109. m_builder.appendff("{}", value);
  110. }
  111. JsonArraySerializer<Builder> add_array(const StringView& key)
  112. {
  113. begin_item(key);
  114. return JsonArraySerializer(m_builder);
  115. }
  116. JsonObjectSerializer<Builder> add_object(const StringView& key)
  117. {
  118. begin_item(key);
  119. return JsonObjectSerializer(m_builder);
  120. }
  121. void finish()
  122. {
  123. VERIFY(!m_finished);
  124. m_finished = true;
  125. m_builder.append('}');
  126. }
  127. private:
  128. void begin_item(const StringView& key)
  129. {
  130. if (!m_empty)
  131. m_builder.append(',');
  132. m_empty = false;
  133. m_builder.append('"');
  134. m_builder.append_escaped_for_json(key);
  135. m_builder.append("\":");
  136. }
  137. Builder& m_builder;
  138. bool m_empty { true };
  139. bool m_finished { false };
  140. };
  141. template<typename Builder>
  142. JsonObjectSerializer<Builder> JsonArraySerializer<Builder>::add_object()
  143. {
  144. begin_item();
  145. return JsonObjectSerializer(m_builder);
  146. }
  147. }
  148. using AK::JsonObjectSerializer;