Skip to content

Commit 057a028

Browse files
authored
Fix RawNumber write (#240)
1 parent 095bf27 commit 057a028

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

tests/UnitTest/JsonSortByKeyTest.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
#include "JsonHandler.h"
66

7-
// TODO: Adjust test case when numeric values are properly supported in SortJsonByKey
8-
97
namespace JsonSortByKey
108
{
119
class JsonSortByKeyTest : public ::testing::Test
@@ -43,13 +41,13 @@ namespace JsonSortByKey
4341
auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 4);
4442

4543
ASSERT_TRUE(result.success);
46-
ASSERT_EQ(result.response, "{\n \"a\": \"30\",\n \"b\": \"valueB\"\n}");
44+
ASSERT_EQ(result.response, "{\n \"a\": 30,\n \"b\": \"valueB\"\n}");
4745

4846
std::string inputJson2 = R"({ "name": "My name?", "age": 27, "is_student": false })";
4947
auto result2 = m_jsonHandler.SortJsonByKey(inputJson2, {}, {}, ' ', 2);
5048

5149
ASSERT_TRUE(result2.success);
52-
ASSERT_EQ(result2.response, "{\n \"age\": \"27\",\n \"is_student\": false,\n \"name\": \"My name?\"\n}");
50+
ASSERT_EQ(result2.response, "{\n \"age\": 27,\n \"is_student\": false,\n \"name\": \"My name?\"\n}");
5351
}
5452

5553
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_NaN_Value_Success)
@@ -58,7 +56,7 @@ namespace JsonSortByKey
5856
auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 4);
5957

6058
ASSERT_TRUE(result.success);
61-
ASSERT_EQ(result.response, "{\n \"Hello\": \"World\",\n \"Nan\": \"NaN\"\n}");
59+
ASSERT_EQ(result.response, "{\n \"Hello\": \"World\",\n \"Nan\": NaN\n}");
6260
}
6361

6462
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_ArrayValue_Success)
@@ -67,7 +65,7 @@ namespace JsonSortByKey
6765
auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, LF::kFormatSingleLineArray, ' ', 2);
6866

6967
ASSERT_TRUE(result.success);
70-
ASSERT_EQ(result.response, "{\n \"Inf\": [\"-Infinity\", \"Infinity\", \"-Infinity\", \"Infinity\"],\n \"text\": [\"Hello\", \"World\"]\n}");
68+
ASSERT_EQ(result.response, "{\n \"Inf\": [-Infinity, Infinity, -Infinity, Infinity],\n \"text\": [\"Hello\", \"World\"]\n}");
7169
}
7270

7371
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_EmptyObject)
@@ -85,7 +83,7 @@ namespace JsonSortByKey
8583
auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 2);
8684

8785
ASSERT_TRUE(result.success);
88-
ASSERT_EQ(result.response, "{\n \"a\": {\n \"c\": \"3\"\n },\n \"z\": {\n \"a\": \"1\",\n \"b\": \"2\"\n }\n}");
86+
ASSERT_EQ(result.response, "{\n \"a\": {\n \"c\": 3\n },\n \"z\": {\n \"a\": 1,\n \"b\": 2\n }\n}");
8987
}
9088

9189
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_SpecialCharacters)
@@ -94,7 +92,7 @@ namespace JsonSortByKey
9492
auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 2);
9593

9694
ASSERT_TRUE(result.success);
97-
ASSERT_EQ(result.response, "{\n \"!\": \"1\",\n \"#\": \"2\",\n \"A\": \"3\"\n}");
95+
ASSERT_EQ(result.response, "{\n \"!\": 1,\n \"#\": 2,\n \"A\": 3\n}");
9896
}
9997

10098
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_UnicodeKeys)
@@ -103,7 +101,7 @@ namespace JsonSortByKey
103101
auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 2);
104102

105103
ASSERT_TRUE(result.success);
106-
ASSERT_EQ(result.response, "{\n \"α\": \"1\",\n \"β\": \"2\",\n \"γ\": \"3\"\n}");
104+
ASSERT_EQ(result.response, "{\n \"α\": 1,\n \"β\": 2,\n \"γ\": 3\n}");
107105
}
108106

109107
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_NullValue)
@@ -123,7 +121,7 @@ namespace JsonSortByKey
123121
auto result = JsonHandler {options}.SortJsonByKey(inputJson, {}, {}, ' ', 2);
124122

125123
ASSERT_TRUE(result.success);
126-
ASSERT_EQ(result.response, "{\n \"a\": \"1\",\n \"b\": \"2\"\n}");
124+
ASSERT_EQ(result.response, "{\n \"a\": 1,\n \"b\": 2\n}");
127125
}
128126

129127
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_IgnoreTrailingComma)
@@ -135,4 +133,17 @@ namespace JsonSortByKey
135133

136134
ASSERT_FALSE(result.success);
137135
}
136+
137+
TEST_F(JsonSortByKeyTest, TestSortJsonByKey_NumberPrecision_Success)
138+
{
139+
// Verify numbers survive the sort-by-key roundtrip without gaining quotes.
140+
// Note: SortJsonByKey re-parses via FormatJson (which uses kParseFullPrecisionFlag),
141+
// so very high precision may be normalized (e.g. 0.00000000000001 -> 1e-14).
142+
// But they must NOT become strings (e.g. "3.14" or "12345678901234567890").
143+
std::string inputJson = R"({"z": "last", "pi": 3.141592653589793, "tiny": 1e-14, "tiny2": 0.00000000000001, "big": 12345678901234567890})";
144+
auto result = m_jsonHandler.SortJsonByKey(inputJson, {}, {}, ' ', 2);
145+
146+
ASSERT_TRUE(result.success);
147+
ASSERT_EQ(result.response, "{\n \"big\": 12345678901234567890,\n \"pi\": 3.141592653589793,\n \"tiny\": 1e-14,\n \"tiny2\": 1e-14,\n \"z\": \"last\"\n}");
148+
}
138149
} // namespace JsonSortByKey

0 commit comments

Comments
 (0)