diff --git a/AK/JsonParser.cpp b/AK/JsonParser.cpp index 3beda694dd9..4d497cb1b50 100644 --- a/AK/JsonParser.cpp +++ b/AK/JsonParser.cpp @@ -263,9 +263,13 @@ Optional JsonParser::parse_number() value = JsonValue((double)whole + ((double)fraction / divider)); } else { #endif - auto to_unsigned_result = number_string.to_uint(); + auto to_unsigned_result = number_string.to_uint(); if (to_unsigned_result.has_value()) { - value = JsonValue(to_unsigned_result.value()); + auto number = *to_unsigned_result; + if (number <= NumericLimits::max()) + value = JsonValue((u32)number); + else + value = JsonValue(number); } else { auto number = number_string.to_int(); if (!number.has_value()) diff --git a/Tests/AK/TestJSON.cpp b/Tests/AK/TestJSON.cpp index 15f4e9ecade..86d4dce2cee 100644 --- a/Tests/AK/TestJSON.cpp +++ b/Tests/AK/TestJSON.cpp @@ -105,3 +105,12 @@ TEST_CASE(json_duplicate_keys) json.set("test", "baz"); EXPECT_EQ(json.to_string(), "{\"test\":\"baz\"}"); } + +TEST_CASE(json_u64_roundtrip) +{ + auto big_value = 0xffffffffffffffffull; + auto json = JsonValue(big_value).to_string(); + auto value = JsonValue::from_string(json); + EXPECT_EQ_FORCE(value.has_value(), true); + EXPECT_EQ(value->as_u64(), big_value); +}