| 40 | }; |
| 41 | |
| 42 | class TBuf: TNonCopyable { |
| 43 | public: |
| 44 | TBuf(EHtmlEscapeMode mode = HEM_DONT_ESCAPE_HTML, IOutputStream* stream = nullptr); |
| 45 | |
| 46 | TValueContext WriteString(const TStringBuf& s, EHtmlEscapeMode hem); |
| 47 | TValueContext WriteString(const TStringBuf& s); |
| 48 | TValueContext WriteInt(int i); |
| 49 | TValueContext WriteLongLong(long long i); |
| 50 | TValueContext WriteULongLong(unsigned long long i); |
| 51 | TValueContext WriteFloat(float f, EFloatToStringMode mode = PREC_NDIGITS, int ndigits = 6); |
| 52 | TValueContext WriteDouble(double f, EFloatToStringMode mode = PREC_NDIGITS, int ndigits = 10); |
| 53 | TValueContext WriteBool(bool b); |
| 54 | TValueContext WriteNull(); |
| 55 | TValueContext WriteJsonValue(const NJson::NOrderedJson::TJsonValue* value, bool sortKeys = false, EFloatToStringMode mode = PREC_NDIGITS, int ndigits = 10); |
| 56 | |
| 57 | TValueContext BeginList(); |
| 58 | TBuf& EndList(); |
| 59 | |
| 60 | TPairContext BeginObject(); |
| 61 | TAfterColonContext WriteKey(const TStringBuf& key, EHtmlEscapeMode hem); |
| 62 | TAfterColonContext WriteKey(const TStringBuf& key); |
| 63 | TAfterColonContext UnsafeWriteKey(const TStringBuf& key); |
| 64 | bool KeyExpected() const { |
| 65 | return Stack.back() == JE_OBJECT; |
| 66 | } |
| 67 | |
| 68 | //! deprecated, do not use in new code |
| 69 | TAfterColonContext CompatWriteKeyWithoutQuotes(const TStringBuf& key); |
| 70 | |
| 71 | TBuf& EndObject(); |
| 72 | |
| 73 | /*** Indent the resulting JSON with spaces. |
| 74 | * By default (spaces==0) no formatting is done. */ |
| 75 | TBuf& SetIndentSpaces(int spaces) { |
| 76 | IndentSpaces = spaces; |
| 77 | return *this; |
| 78 | } |
| 79 | |
| 80 | /*** NaN and Inf are not valid json values, |
| 81 | * so if WriteNanAsString is set, writer would write string |
| 82 | * intead of throwing exception (default case) */ |
| 83 | TBuf& SetWriteNanAsString(bool writeNanAsString = true) { |
| 84 | WriteNanAsString = writeNanAsString; |
| 85 | return *this; |
| 86 | } |
| 87 | |
| 88 | /*** Return the string formed in the internal TStringStream. |
| 89 | * You may only call it if the `stream' parameter was NULL |
| 90 | * at construction time. */ |
| 91 | const TString& Str() const; |
| 92 | |
| 93 | /*** Dump and forget the string constructed so far. |
| 94 | * You may only call it if the `stream' parameter was NULL |
| 95 | * at construction time. */ |
| 96 | void FlushTo(IOutputStream* stream); |
| 97 | |
| 98 | /*** Write a literal string that represents a JSON value |
| 99 | * (string, number, object, array, bool, or null). |