diff options
| author | robot-piglet <[email protected]> | 2025-09-18 10:59:35 +0300 |
|---|---|---|
| committer | robot-piglet <[email protected]> | 2025-09-18 11:28:07 +0300 |
| commit | d2d6681538770d0c2725615373a6c2e07adf653e (patch) | |
| tree | 7e2940f3e26b33acc858f98f8e3191495bdd7170 /library/cpp/json/ordered_maps | |
| parent | 5e07e59ba03f57c1d960a3ad55cad8b2c00bbe76 (diff) | |
Intermediate changes
commit_hash:a2c65251912228db1f8c195afd59840d04daf7ff
Diffstat (limited to 'library/cpp/json/ordered_maps')
4 files changed, 16 insertions, 10 deletions
diff --git a/library/cpp/json/ordered_maps/json_ordered.cpp b/library/cpp/json/ordered_maps/json_ordered.cpp index 960064a458f..438d4571a21 100644 --- a/library/cpp/json/ordered_maps/json_ordered.cpp +++ b/library/cpp/json/ordered_maps/json_ordered.cpp @@ -57,15 +57,17 @@ namespace NJsonOrderedWriter { Y_ASSERT(!Stack.empty()); const EJsonEntity current = StackTop(); Stack.pop_back(); + bool needMinusLevel = Stack.empty() + ? false : StackTop() == EJsonEntity::JE_OBJECT; switch (current) { case JE_OUTER_SPACE: ythrow TError() << "JSON writer: stack empty"; case JE_LIST: - PrintIndentation(true); + PrintIndentation(true, needMinusLevel); RawWriteChar(']'); break; case JE_OBJECT: - PrintIndentation(true); + PrintIndentation(true, needMinusLevel); RawWriteChar('}'); break; case JE_PAIR: @@ -83,10 +85,11 @@ namespace NJsonOrderedWriter { StackPop(); } - void TBuf::PrintIndentation(bool closing) { + void TBuf::PrintIndentation(bool closing, bool sublevel) { if (!IndentSpaces) return; - const int indentation = IndentSpaces * (Stack.size() - 1); + int substruct = Min<int>(int(sublevel) + 1, Stack.size()); + const int indentation = IndentSpaces * (Stack.size() - substruct); if (!indentation && !closing) return; @@ -144,18 +147,18 @@ namespace NJsonOrderedWriter { } TValueContext TBuf::BeginList() { - NeedNewline = true; BeginValue(); RawWriteChar('['); + NeedNewline = true; StackPush(JE_LIST); NeedComma = false; return TValueContext(*this); } TPairContext TBuf::BeginObject() { - NeedNewline = true; BeginValue(); RawWriteChar('{'); + NeedNewline = true; StackPush(JE_OBJECT); NeedComma = false; return TPairContext(*this); @@ -178,6 +181,9 @@ namespace NJsonOrderedWriter { BeginKey(); WriteBareString(s, hem); RawWriteChar(':'); + if (IndentSpaces) { + RawWriteChar(' '); + } return TAfterColonContext(*this); } diff --git a/library/cpp/json/ordered_maps/json_ordered.h b/library/cpp/json/ordered_maps/json_ordered.h index 193858fa51b..3bdbb5a2200 100644 --- a/library/cpp/json/ordered_maps/json_ordered.h +++ b/library/cpp/json/ordered_maps/json_ordered.h @@ -132,7 +132,7 @@ namespace NJsonOrderedWriter { bool EscapedWriteChar(const char* b, const char* c, EHtmlEscapeMode hem); void WriteBareString(const TStringBuf s, EHtmlEscapeMode hem); void WriteComma(); - void PrintIndentation(bool closing); + void PrintIndentation(bool closing, bool sublevel = false); void PrintWhitespaces(size_t count, bool prependWithNewLine); void WriteHexEscape(unsigned char c); diff --git a/library/cpp/json/ordered_maps/ut/json_ordered_ut.cpp b/library/cpp/json/ordered_maps/ut/json_ordered_ut.cpp index 2607814de3c..93d81485660 100644 --- a/library/cpp/json/ordered_maps/ut/json_ordered_ut.cpp +++ b/library/cpp/json/ordered_maps/ut/json_ordered_ut.cpp @@ -118,8 +118,8 @@ Y_UNIT_TEST_SUITE(JsonWriter) { " 1,\n" " \"hello\",\n" " {\n" - " \"abc\":3,\n" - " \"def\":4\n" + " \"abc\": 3,\n" + " \"def\": 4\n" " }\n" "]"; UNIT_ASSERT_STRINGS_EQUAL(exp, w.Str()); diff --git a/library/cpp/json/ordered_maps/ut/json_writer_ordered_ut.cpp b/library/cpp/json/ordered_maps/ut/json_writer_ordered_ut.cpp index adf2e6f6230..1e6213668fc 100644 --- a/library/cpp/json/ordered_maps/ut/json_writer_ordered_ut.cpp +++ b/library/cpp/json/ordered_maps/ut/json_writer_ordered_ut.cpp @@ -56,7 +56,7 @@ Y_UNIT_TEST_SUITE(TJsonWriterTest) { } Y_UNIT_TEST(FormatOutput) { - TString expected = "{\n \"key1\":null,\n \"key2\":\n {\n \"subkey1\":\n [\n 1,\n {\n \"subsubkey\":\"test2\"\n },\n null,\n true\n ],\n \"subkey2\":\"test\"\n }\n}"; + TString expected = "{\n \"key1\": null,\n \"key2\": {\n \"subkey1\": [\n 1,\n {\n \"subsubkey\": \"test2\"\n },\n null,\n true\n ],\n \"subkey2\": \"test\"\n }\n}"; TJsonValue v; v["key1"] = JSON_NULL; v["key2"]["subkey1"].AppendValue(1); |
