json_writer.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #pragma once
  2. // Deprecated. Use library/cpp/json/writer in new code.
  3. #include "json_value.h"
  4. #include <library/cpp/json/writer/json.h>
  5. #include <util/stream/output.h>
  6. #include <util/generic/hash.h>
  7. #include <util/generic/maybe.h>
  8. #include <util/generic/strbuf.h>
  9. namespace NJson {
  10. struct TJsonWriterConfig {
  11. constexpr static ui32 DefaultDoubleNDigits = 10;
  12. constexpr static ui32 DefaultFloatNDigits = 6;
  13. constexpr static EFloatToStringMode DefaultFloatToStringMode = PREC_NDIGITS;
  14. inline TJsonWriterConfig& SetUnbuffered(bool v) noexcept {
  15. Unbuffered = v;
  16. return *this;
  17. }
  18. inline TJsonWriterConfig& SetValidateUtf8(bool v) noexcept {
  19. ValidateUtf8 = v;
  20. return *this;
  21. }
  22. inline TJsonWriterConfig& SetFormatOutput(bool v) noexcept {
  23. FormatOutput = v;
  24. return *this;
  25. }
  26. ui32 DoubleNDigits = DefaultDoubleNDigits;
  27. ui32 FloatNDigits = DefaultFloatNDigits;
  28. EFloatToStringMode FloatToStringMode = DefaultFloatToStringMode;
  29. bool FormatOutput = false;
  30. bool SortKeys = false;
  31. bool ValidateUtf8 = true;
  32. bool DontEscapeStrings = false;
  33. bool Unbuffered = false;
  34. bool WriteNanAsString = false; // NaN and Inf are not valid json values, so if WriteNanAsString is set, writer would write string intead of throwing exception (default case)
  35. };
  36. class TJsonWriter {
  37. IOutputStream* Out;
  38. NJsonWriter::TBuf Buf;
  39. const ui32 DoubleNDigits;
  40. const ui32 FloatNDigits;
  41. const EFloatToStringMode FloatToStringMode;
  42. const bool SortKeys;
  43. const bool ValidateUtf8;
  44. const bool DontEscapeStrings;
  45. const bool DontFlushInDestructor;
  46. public:
  47. TJsonWriter(IOutputStream* out, bool formatOutput, bool sortkeys = false, bool validateUtf8 = true);
  48. TJsonWriter(IOutputStream* out, const TJsonWriterConfig& config, bool DontFlushInDestructor = false);
  49. ~TJsonWriter();
  50. void Flush();
  51. void OpenMap();
  52. void OpenMap(const TStringBuf& key) {
  53. Buf.WriteKey(key);
  54. OpenMap();
  55. }
  56. void CloseMap();
  57. void OpenArray();
  58. void OpenArray(const TStringBuf& key) {
  59. Buf.WriteKey(key);
  60. OpenArray();
  61. }
  62. void CloseArray();
  63. void WriteNull();
  64. void Write(const TStringBuf& value);
  65. void Write(float value);
  66. void Write(double value);
  67. void Write(bool value);
  68. void Write(const TJsonValue* value);
  69. void Write(const TJsonValue& value);
  70. // must use all variations of integer types since long
  71. // and long long are different types but with same size
  72. void Write(long long value);
  73. void Write(unsigned long long value);
  74. void Write(long value) {
  75. Write((long long)value);
  76. }
  77. void Write(unsigned long value) {
  78. Write((unsigned long long)value);
  79. }
  80. void Write(int value) {
  81. Write((long long)value);
  82. }
  83. void Write(unsigned int value) {
  84. Write((unsigned long long)value);
  85. }
  86. void Write(short value) {
  87. Write((long long)value);
  88. }
  89. void Write(unsigned short value) {
  90. Write((unsigned long long)value);
  91. }
  92. void Write(const unsigned char* value) {
  93. Write((const char*)value);
  94. }
  95. void Write(const char* value) {
  96. Write(TStringBuf(value));
  97. }
  98. void Write(const TString& value) {
  99. Write(TStringBuf(value));
  100. }
  101. void Write(const std::string& value) {
  102. Write(TStringBuf(value));
  103. }
  104. // write raw json without checks
  105. void UnsafeWrite(const TStringBuf& value) {
  106. Buf.UnsafeWriteValue(value);
  107. }
  108. template <typename T>
  109. void Write(const TStringBuf& key, const T& value) {
  110. Buf.WriteKey(key);
  111. Write(value);
  112. }
  113. // write raw json without checks
  114. void UnsafeWrite(const TStringBuf& key, const TStringBuf& value) {
  115. Buf.WriteKey(key);
  116. UnsafeWrite(value);
  117. }
  118. void WriteNull(const TStringBuf& key) {
  119. Buf.WriteKey(key);
  120. WriteNull();
  121. }
  122. template <typename T>
  123. void WriteOptional(const TStringBuf& key, const TMaybe<T>& value) {
  124. if (value) {
  125. Write(key, *value);
  126. }
  127. }
  128. void WriteOptional(const TStringBuf&, const TNothing&) {
  129. // nothing to do
  130. }
  131. void WriteKey(const TStringBuf key) {
  132. Buf.WriteKey(key);
  133. }
  134. void WriteKey(const unsigned char* key) {
  135. WriteKey((const char*)key);
  136. }
  137. void WriteKey(const char* key) {
  138. WriteKey(TStringBuf{key});
  139. }
  140. void WriteKey(const TString& key) {
  141. WriteKey(TStringBuf{key});
  142. }
  143. void WriteKey(const std::string& key) {
  144. WriteKey(TStringBuf{key});
  145. }
  146. NJsonWriter::TBufState State() const {
  147. return Buf.State();
  148. }
  149. void Reset(const NJsonWriter::TBufState& from) {
  150. return Buf.Reset(from);
  151. }
  152. void Reset(NJsonWriter::TBufState&& from) {
  153. return Buf.Reset(std::move(from));
  154. }
  155. };
  156. void WriteJson(IOutputStream*, const TJsonValue*, bool formatOutput = false, bool sortkeys = false, bool validateUtf8 = true);
  157. TString WriteJson(const TJsonValue*, bool formatOutput = true, bool sortkeys = false, bool validateUtf8 = false);
  158. TString WriteJson(const TJsonValue&, bool formatOutput = true, bool sortkeys = false, bool validateUtf8 = false);
  159. void WriteJson(IOutputStream*, const TJsonValue*, const TJsonWriterConfig& config);
  160. }