mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 12:27:34 +00:00
AK: Make JSON parser return ErrorOr<JsonValue> (instead of Optional)
Also add slightly richer parse errors now that we can include a string literal with returned errors. This will allow us to use TRY() when working with JSON data.
This commit is contained in:
parent
304c03f457
commit
587f9af960
54 changed files with 172 additions and 228 deletions
|
@ -58,11 +58,10 @@ int main(int argc, char** argv)
|
|||
if (!file->open(Core::OpenMode::ReadOnly))
|
||||
return 1;
|
||||
|
||||
auto json = JsonValue::from_string(file->read_all());
|
||||
VERIFY(json.has_value());
|
||||
VERIFY(json.value().is_object());
|
||||
auto json = JsonValue::from_string(file->read_all()).release_value_but_fixme_should_propagate_errors();
|
||||
VERIFY(json.is_object());
|
||||
|
||||
auto& properties = json.value().as_object();
|
||||
auto& properties = json.as_object();
|
||||
|
||||
StringBuilder builder;
|
||||
SourceGenerator generator { builder };
|
||||
|
|
|
@ -36,9 +36,8 @@ int main(int argc, char** argv)
|
|||
if (!file->open(Core::OpenMode::ReadOnly))
|
||||
return 1;
|
||||
|
||||
auto json = JsonValue::from_string(file->read_all());
|
||||
VERIFY(json.has_value());
|
||||
VERIFY(json.value().is_object());
|
||||
auto json = JsonValue::from_string(file->read_all()).release_value_but_fixme_should_propagate_errors();
|
||||
VERIFY(json.is_object());
|
||||
|
||||
StringBuilder builder;
|
||||
SourceGenerator generator { builder };
|
||||
|
@ -60,7 +59,7 @@ enum class PropertyID {
|
|||
Vector<String> shorthand_property_ids;
|
||||
Vector<String> longhand_property_ids;
|
||||
|
||||
json.value().as_object().for_each_member([&](auto& name, auto& value) {
|
||||
json.as_object().for_each_member([&](auto& name, auto& value) {
|
||||
VERIFY(value.is_object());
|
||||
if (value.as_object().has("longhands"))
|
||||
shorthand_property_ids.append(name);
|
||||
|
|
|
@ -36,9 +36,8 @@ int main(int argc, char** argv)
|
|||
if (!file->open(Core::OpenMode::ReadOnly))
|
||||
return 1;
|
||||
|
||||
auto json = JsonValue::from_string(file->read_all());
|
||||
VERIFY(json.has_value());
|
||||
VERIFY(json.value().is_array());
|
||||
auto json = JsonValue::from_string(file->read_all()).release_value_but_fixme_should_propagate_errors();
|
||||
VERIFY(json.is_array());
|
||||
|
||||
StringBuilder builder;
|
||||
SourceGenerator generator { builder };
|
||||
|
@ -53,7 +52,7 @@ ValueID value_id_from_string(StringView string)
|
|||
{
|
||||
)~~~");
|
||||
|
||||
json.value().as_array().for_each([&](auto& name) {
|
||||
json.as_array().for_each([&](auto& name) {
|
||||
auto member_generator = generator.fork();
|
||||
member_generator.set("name", name.to_string());
|
||||
member_generator.set("name:titlecase", title_casify(name.to_string()));
|
||||
|
@ -71,7 +70,7 @@ const char* string_from_value_id(ValueID value_id) {
|
|||
switch (value_id) {
|
||||
)~~~");
|
||||
|
||||
json.value().as_array().for_each([&](auto& name) {
|
||||
json.as_array().for_each([&](auto& name) {
|
||||
auto member_generator = generator.fork();
|
||||
member_generator.set("name", name.to_string());
|
||||
member_generator.set("name:titlecase", title_casify(name.to_string()));
|
||||
|
|
|
@ -36,9 +36,8 @@ int main(int argc, char** argv)
|
|||
if (!file->open(Core::OpenMode::ReadOnly))
|
||||
return 1;
|
||||
|
||||
auto json = JsonValue::from_string(file->read_all());
|
||||
VERIFY(json.has_value());
|
||||
VERIFY(json.value().is_array());
|
||||
auto json = JsonValue::from_string(file->read_all()).release_value_but_fixme_should_propagate_errors();
|
||||
VERIFY(json.is_array());
|
||||
|
||||
StringBuilder builder;
|
||||
SourceGenerator generator { builder };
|
||||
|
@ -54,7 +53,7 @@ enum class ValueID {
|
|||
Invalid,
|
||||
)~~~");
|
||||
|
||||
json.value().as_array().for_each([&](auto& name) {
|
||||
json.as_array().for_each([&](auto& name) {
|
||||
auto member_generator = generator.fork();
|
||||
member_generator.set("name:titlecase", title_casify(name.to_string()));
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue