1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-14 08:07:36 +00:00
serenity/Ladybird/AppKit/Utilities/Conversions.mm
Timothy Flynn 33b006f157 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.
2023-09-18 11:11:23 -06:00

89 lines
2.2 KiB
Text

/*
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#import <Utilities/Conversions.h>
namespace Ladybird {
String ns_string_to_string(NSString* string)
{
auto const* utf8 = [string UTF8String];
return MUST(String::from_utf8({ utf8, strlen(utf8) }));
}
NSString* string_to_ns_string(StringView string)
{
auto* data = [NSData dataWithBytes:string.characters_without_null_termination() length:string.length()];
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 {
static_cast<int>(rect.origin.x),
static_cast<int>(rect.origin.y),
static_cast<int>(rect.size.width),
static_cast<int>(rect.size.height),
};
}
NSRect gfx_rect_to_ns_rect(Gfx::IntRect rect)
{
return NSMakeRect(
static_cast<CGFloat>(rect.x()),
static_cast<CGFloat>(rect.y()),
static_cast<CGFloat>(rect.width()),
static_cast<CGFloat>(rect.height()));
}
Gfx::IntSize ns_size_to_gfx_size(NSSize size)
{
return {
static_cast<int>(size.width),
static_cast<int>(size.height),
};
}
NSSize gfx_size_to_ns_size(Gfx::IntSize size)
{
return NSMakeSize(
static_cast<CGFloat>(size.width()),
static_cast<CGFloat>(size.height()));
}
Gfx::IntPoint ns_point_to_gfx_point(NSPoint point)
{
return {
static_cast<int>(point.x),
static_cast<int>(point.y),
};
}
NSPoint gfx_point_to_ns_point(Gfx::IntPoint point)
{
return NSMakePoint(
static_cast<CGFloat>(point.x()),
static_cast<CGFloat>(point.y()));
}
}