diff options
author | myltsev <myltsev@yandex-team.ru> | 2022-02-10 16:46:03 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:03 +0300 |
commit | fc361854fd6ee8d747229b090f0b8018e260d1fb (patch) | |
tree | 1a2c5ffcf89eb53ecd79dbc9bc0a195c27404d0c /library/cpp/json/writer/json.h | |
parent | 9166d66c30c23c9e85a7c88185a068987148d23f (diff) | |
download | ydb-fc361854fd6ee8d747229b090f0b8018e260d1fb.tar.gz |
Restoring authorship annotation for <myltsev@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/json/writer/json.h')
-rw-r--r-- | library/cpp/json/writer/json.h | 330 |
1 files changed, 165 insertions, 165 deletions
diff --git a/library/cpp/json/writer/json.h b/library/cpp/json/writer/json.h index f8e0783574..0aae2531b9 100644 --- a/library/cpp/json/writer/json.h +++ b/library/cpp/json/writer/json.h @@ -1,38 +1,38 @@ -#pragma once - -#include <util/generic/noncopyable.h> -#include <util/generic/ptr.h> +#pragma once + +#include <util/generic/noncopyable.h> +#include <util/generic/ptr.h> #include <util/generic/string.h> -#include <util/generic/vector.h> -#include <util/generic/yexception.h> -#include <util/stream/str.h> +#include <util/generic/vector.h> +#include <util/generic/yexception.h> +#include <util/stream/str.h> #include <util/string/cast.h> - -namespace NJson { - class TJsonValue; -} - -namespace NJsonWriter { + +namespace NJson { + class TJsonValue; +} + +namespace NJsonWriter { enum EJsonEntity : ui8 { - JE_OUTER_SPACE = 1, - JE_LIST, - JE_OBJECT, - JE_PAIR, - }; - - enum EHtmlEscapeMode { - HEM_ESCAPE_HTML = 1, // Use HTML escaping: < > & \/ - HEM_DONT_ESCAPE_HTML, // Use JSON escaping: \u003C \u003E \u0026 \/ - HEM_RELAXED, // Use JSON escaping: \u003C \u003E \u0026 / - HEM_UNSAFE, // Turn escaping off: < > & / - }; - - class TError: public yexception {}; - - class TValueContext; - class TPairContext; - class TAfterColonContext; - + JE_OUTER_SPACE = 1, + JE_LIST, + JE_OBJECT, + JE_PAIR, + }; + + enum EHtmlEscapeMode { + HEM_ESCAPE_HTML = 1, // Use HTML escaping: < > & \/ + HEM_DONT_ESCAPE_HTML, // Use JSON escaping: \u003C \u003E \u0026 \/ + HEM_RELAXED, // Use JSON escaping: \u003C \u003E \u0026 / + HEM_UNSAFE, // Turn escaping off: < > & / + }; + + class TError: public yexception {}; + + class TValueContext; + class TPairContext; + class TAfterColonContext; + struct TBufState { bool NeedComma; bool NeedNewline; @@ -40,102 +40,102 @@ namespace NJsonWriter { }; class TBuf : TNonCopyable { - public: + public: TBuf(EHtmlEscapeMode mode = HEM_DONT_ESCAPE_HTML, IOutputStream* stream = nullptr); - + TValueContext WriteString(const TStringBuf& s, EHtmlEscapeMode hem); TValueContext WriteString(const TStringBuf& s); - TValueContext WriteInt(int i); + TValueContext WriteInt(int i); TValueContext WriteLongLong(long long i); TValueContext WriteULongLong(unsigned long long i); TValueContext WriteFloat(float f, EFloatToStringMode mode = PREC_NDIGITS, int ndigits = 6); TValueContext WriteDouble(double f, EFloatToStringMode mode = PREC_NDIGITS, int ndigits = 10); - TValueContext WriteBool(bool b); - TValueContext WriteNull(); + TValueContext WriteBool(bool b); + TValueContext WriteNull(); TValueContext WriteJsonValue(const NJson::TJsonValue* value, bool sortKeys = false, EFloatToStringMode mode = PREC_NDIGITS, int ndigits = 10); - - TValueContext BeginList(); - TBuf& EndList(); - - TPairContext BeginObject(); + + TValueContext BeginList(); + TBuf& EndList(); + + TPairContext BeginObject(); TAfterColonContext WriteKey(const TStringBuf& key, EHtmlEscapeMode hem); TAfterColonContext WriteKey(const TStringBuf& key); TAfterColonContext UnsafeWriteKey(const TStringBuf& key); bool KeyExpected() const { return Stack.back() == JE_OBJECT; } - - //! deprecated, do not use in new code + + //! deprecated, do not use in new code TAfterColonContext CompatWriteKeyWithoutQuotes(const TStringBuf& key); - - TBuf& EndObject(); - - /*** Indent the resulting JSON with spaces. - * By default (spaces==0) no formatting is done. */ - TBuf& SetIndentSpaces(int spaces) { - IndentSpaces = spaces; - return *this; - } - + + TBuf& EndObject(); + + /*** Indent the resulting JSON with spaces. + * By default (spaces==0) no formatting is done. */ + TBuf& SetIndentSpaces(int spaces) { + IndentSpaces = spaces; + return *this; + } + /*** NaN and Inf are not valid json values, * so if WriteNanAsString is set, writer would write string * intead of throwing exception (default case) */ - TBuf& SetWriteNanAsString(bool writeNanAsString = true) { + TBuf& SetWriteNanAsString(bool writeNanAsString = true) { WriteNanAsString = writeNanAsString; - return *this; + return *this; } - /*** Return the string formed in the internal TStringStream. - * You may only call it if the `stream' parameter was NULL - * at construction time. */ + /*** Return the string formed in the internal TStringStream. + * You may only call it if the `stream' parameter was NULL + * at construction time. */ const TString& Str() const; - - /*** Dump and forget the string constructed so far. - * You may only call it if the `stream' parameter was NULL - * at construction time. */ + + /*** Dump and forget the string constructed so far. + * You may only call it if the `stream' parameter was NULL + * at construction time. */ void FlushTo(IOutputStream* stream); - - /*** Write a literal string that represents a JSON value - * (string, number, object, array, bool, or null). - * - * Example: - * j.UnsafeWriteValue("[1, 2, 3, \"o'clock\", 4, \"o'clock rock\"]"); - * - * As in all of the Unsafe* functions, no escaping is done. */ + + /*** Write a literal string that represents a JSON value + * (string, number, object, array, bool, or null). + * + * Example: + * j.UnsafeWriteValue("[1, 2, 3, \"o'clock\", 4, \"o'clock rock\"]"); + * + * As in all of the Unsafe* functions, no escaping is done. */ void UnsafeWriteValue(const TStringBuf& s); - void UnsafeWriteValue(const char* s, size_t len); - - /*** When in the context of an object, write a literal string - * that represents a key:value pair (or several pairs). - * - * Example: - * j.BeginObject(); - * j.UnsafeWritePair("\"adam\": \"male\", \"eve\": \"female\""); - * j.EndObject(); - * - * As in all of the Unsafe* functions, no escaping is done. */ + void UnsafeWriteValue(const char* s, size_t len); + + /*** When in the context of an object, write a literal string + * that represents a key:value pair (or several pairs). + * + * Example: + * j.BeginObject(); + * j.UnsafeWritePair("\"adam\": \"male\", \"eve\": \"female\""); + * j.EndObject(); + * + * As in all of the Unsafe* functions, no escaping is done. */ TPairContext UnsafeWritePair(const TStringBuf& s); - - /*** Copy the supplied string directly into the output stream. */ + + /*** Copy the supplied string directly into the output stream. */ void UnsafeWriteRawBytes(const TStringBuf& s); - void UnsafeWriteRawBytes(const char* c, size_t len); - + void UnsafeWriteRawBytes(const char* c, size_t len); + TBufState State() const; void Reset(const TBufState& from); void Reset(TBufState&& from); - private: - void BeginValue(); - void EndValue(); - void BeginKey(); - void RawWriteChar(char c); + private: + void BeginValue(); + void EndValue(); + void BeginKey(); + void RawWriteChar(char c); bool EscapedWriteChar(const char* b, const char* c, EHtmlEscapeMode hem); void WriteBareString(const TStringBuf s, EHtmlEscapeMode hem); - void WriteComma(); - void PrintIndentation(bool closing); + void WriteComma(); + void PrintIndentation(bool closing); void PrintWhitespaces(size_t count, bool prependWithNewLine); - void WriteHexEscape(unsigned char c); - + void WriteHexEscape(unsigned char c); + void StackPush(EJsonEntity e); void StackPop(); void CheckAndPop(EJsonEntity e); @@ -146,23 +146,23 @@ namespace NJsonWriter { private: IOutputStream* Stream; - THolder<TStringStream> StringStream; + THolder<TStringStream> StringStream; typedef TVector<const TString*> TKeys; TKeys Keys; TVector<EJsonEntity> Stack; - bool NeedComma; + bool NeedComma; bool NeedNewline; - const EHtmlEscapeMode EscapeMode; - int IndentSpaces; + const EHtmlEscapeMode EscapeMode; + int IndentSpaces; bool WriteNanAsString; - }; - - // Please don't try to instantiate the classes declared below this point. - + }; + + // Please don't try to instantiate the classes declared below this point. + template <typename TOutContext> class TValueWriter { - public: + public: TOutContext WriteNull(); TOutContext WriteString(const TStringBuf&); TOutContext WriteString(const TStringBuf& s, EHtmlEscapeMode hem); @@ -177,82 +177,82 @@ namespace NJsonWriter { TOutContext WriteJsonValue(const NJson::TJsonValue* value, bool sortKeys = false); TOutContext UnsafeWriteValue(const TStringBuf&); - TValueContext BeginList(); - TPairContext BeginObject(); - - protected: - TValueWriter(TBuf& buf) - : Buf(buf) + TValueContext BeginList(); + TPairContext BeginObject(); + + protected: + TValueWriter(TBuf& buf) + : Buf(buf) { } - friend class TBuf; - - protected: - TBuf& Buf; - }; - - class TValueContext: public TValueWriter<TValueContext> { - public: - TBuf& EndList() { - return Buf.EndList(); - } + friend class TBuf; + + protected: + TBuf& Buf; + }; + + class TValueContext: public TValueWriter<TValueContext> { + public: + TBuf& EndList() { + return Buf.EndList(); + } TString Str() const { return Buf.Str(); } - private: - TValueContext(TBuf& buf) - : TValueWriter<TValueContext>(buf) + private: + TValueContext(TBuf& buf) + : TValueWriter<TValueContext>(buf) { } - friend class TBuf; - friend class TValueWriter<TValueContext>; - }; - - class TAfterColonContext: public TValueWriter<TPairContext> { - private: + friend class TBuf; + friend class TValueWriter<TValueContext>; + }; + + class TAfterColonContext: public TValueWriter<TPairContext> { + private: TAfterColonContext(TBuf& iBuf) : TValueWriter<TPairContext>(iBuf) { } - friend class TBuf; - friend class TPairContext; - }; - - class TPairContext { - public: + friend class TBuf; + friend class TPairContext; + }; + + class TPairContext { + public: TAfterColonContext WriteKey(const TStringBuf& s, EHtmlEscapeMode hem) { - return Buf.WriteKey(s, hem); - } + return Buf.WriteKey(s, hem); + } TAfterColonContext WriteKey(const TStringBuf& s) { - return Buf.WriteKey(s); - } + return Buf.WriteKey(s); + } TAfterColonContext UnsafeWriteKey(const TStringBuf& s) { return Buf.UnsafeWriteKey(s); } TAfterColonContext CompatWriteKeyWithoutQuotes(const TStringBuf& s) { - return Buf.CompatWriteKeyWithoutQuotes(s); - } + return Buf.CompatWriteKeyWithoutQuotes(s); + } TPairContext UnsafeWritePair(const TStringBuf& s) { - return Buf.UnsafeWritePair(s); - } - TBuf& EndObject() { - return Buf.EndObject(); - } - - private: - TPairContext(TBuf& buf) - : Buf(buf) + return Buf.UnsafeWritePair(s); + } + TBuf& EndObject() { + return Buf.EndObject(); + } + + private: + TPairContext(TBuf& buf) + : Buf(buf) { } - - friend class TBuf; - friend class TValueWriter<TPairContext>; - - private: - TBuf& Buf; - }; - + + friend class TBuf; + friend class TValueWriter<TPairContext>; + + private: + TBuf& Buf; + }; + #define JSON_VALUE_WRITER_WRAP(function, params, args) \ template <typename TOutContext> \ TOutContext TValueWriter<TOutContext>::function params { \ @@ -277,13 +277,13 @@ namespace NJsonWriter { template <typename TOutContext> TValueContext TValueWriter<TOutContext>::BeginList() { - return Buf.BeginList(); - } - + return Buf.BeginList(); + } + template <typename TOutContext> TPairContext TValueWriter<TOutContext>::BeginObject() { - return Buf.BeginObject(); - } + return Buf.BeginObject(); + } TString WrapJsonToCallback(const TBuf& buf, TStringBuf callback); -} +} |