Ladybird: Add a helper to deserialize a JSON string to an NSDictionary

The steps here are a tad verbose, and will be needed several times in
the Inspector window.
This commit is contained in:
Timothy Flynn 2023-09-13 11:57:54 -04:00 committed by Andrew Kaster
parent 507dea5fdd
commit 33b006f157
Notes: sideshowbarker 2024-07-17 22:09:47 +09:00
2 changed files with 19 additions and 0 deletions

View File

@ -19,6 +19,8 @@ namespace Ladybird {
String ns_string_to_string(NSString*);
NSString* string_to_ns_string(StringView);
NSDictionary* deserialize_json_to_dictionary(StringView);
Gfx::IntRect ns_rect_to_gfx_rect(NSRect);
NSRect gfx_rect_to_ns_rect(Gfx::IntRect);

View File

@ -20,6 +20,23 @@ NSString* string_to_ns_string(StringView string)
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
NSDictionary* deserialize_json_to_dictionary(StringView json)
{
auto* ns_json = string_to_ns_string(json);
auto* json_data = [ns_json dataUsingEncoding:NSUTF8StringEncoding];
NSError* error = nil;
NSDictionary* dictionary = [NSJSONSerialization JSONObjectWithData:json_data
options:0
error:&error];
if (!dictionary) {
NSLog(@"Error deserializing DOM tree: %@", error);
}
return dictionary;
}
Gfx::IntRect ns_rect_to_gfx_rect(NSRect rect)
{
return {