Przeglądaj źródła

AK: Make JsonParser correctly parse unsigned values larger than u32

Prior to this, it'd try to stuff them into an i64, which could fail and
give us nothing.
Even though this is an extension we've made to JSON, the parser should
be able to correctly round-trip from whatever our serialiser has
generated.
Ali Mohammad Pur 4 lat temu
rodzic
commit
5d170810db
2 zmienionych plików z 15 dodań i 2 usunięć
  1. 6 2
      AK/JsonParser.cpp
  2. 9 0
      Tests/AK/TestJSON.cpp

+ 6 - 2
AK/JsonParser.cpp

@@ -263,9 +263,13 @@ Optional<JsonValue> JsonParser::parse_number()
         value = JsonValue((double)whole + ((double)fraction / divider));
         value = JsonValue((double)whole + ((double)fraction / divider));
     } else {
     } else {
 #endif
 #endif
-        auto to_unsigned_result = number_string.to_uint();
+        auto to_unsigned_result = number_string.to_uint<u64>();
         if (to_unsigned_result.has_value()) {
         if (to_unsigned_result.has_value()) {
-            value = JsonValue(to_unsigned_result.value());
+            auto number = *to_unsigned_result;
+            if (number <= NumericLimits<u32>::max())
+                value = JsonValue((u32)number);
+            else
+                value = JsonValue(number);
         } else {
         } else {
             auto number = number_string.to_int<i64>();
             auto number = number_string.to_int<i64>();
             if (!number.has_value())
             if (!number.has_value())

+ 9 - 0
Tests/AK/TestJSON.cpp

@@ -105,3 +105,12 @@ TEST_CASE(json_duplicate_keys)
     json.set("test", "baz");
     json.set("test", "baz");
     EXPECT_EQ(json.to_string(), "{\"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);
+}