Coverage Report

Created: 2025-08-28 06:26

/src/serenity/Userland/Libraries/LibWeb/Infra/JSON.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#include <AK/String.h>
8
#include <LibJS/Runtime/AbstractOperations.h>
9
#include <LibJS/Runtime/Completion.h>
10
#include <LibJS/Runtime/Value.h>
11
#include <LibTextCodec/Decoder.h>
12
#include <LibWeb/Infra/JSON.h>
13
#include <LibWeb/WebIDL/ExceptionOr.h>
14
15
namespace Web::Infra {
16
17
// https://infra.spec.whatwg.org/#parse-a-json-string-to-a-javascript-value
18
WebIDL::ExceptionOr<JS::Value> parse_json_string_to_javascript_value(JS::Realm& realm, StringView string)
19
0
{
20
0
    auto& vm = realm.vm();
21
22
    // 1. Return ? Call(%JSON.parse%, undefined, « string »).
23
0
    return TRY(JS::call(vm, *realm.intrinsics().json_parse_function(), JS::js_undefined(), JS::PrimitiveString::create(vm, string)));
24
0
}
25
26
// https://infra.spec.whatwg.org/#parse-json-bytes-to-a-javascript-value
27
WebIDL::ExceptionOr<JS::Value> parse_json_bytes_to_javascript_value(JS::Realm& realm, ReadonlyBytes bytes)
28
0
{
29
0
    auto& vm = realm.vm();
30
31
    // 1. Let string be the result of running UTF-8 decode on bytes.
32
0
    TextCodec::UTF8Decoder decoder;
33
0
    auto string = TRY_OR_THROW_OOM(vm, decoder.to_utf8(bytes));
34
35
    // 2. Return the result of parsing a JSON string to an Infra value given string.
36
0
    return parse_json_string_to_javascript_value(realm, string);
37
0
}
38
39
// https://infra.spec.whatwg.org/#serialize-a-javascript-value-to-a-json-string
40
WebIDL::ExceptionOr<String> serialize_javascript_value_to_json_string(JS::VM& vm, JS::Value value)
41
0
{
42
0
    auto& realm = *vm.current_realm();
43
44
    // 1. Let result be ? Call(%JSON.stringify%, undefined, « value »).
45
0
    auto result = TRY(JS::call(vm, *realm.intrinsics().json_stringify_function(), JS::js_undefined(), value));
46
47
    // 2. If result is undefined, then throw a TypeError.
48
0
    if (result.is_undefined())
49
0
        return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Result of stringifying value must not be undefined"sv };
50
51
    // 3. Assert: result is a string.
52
0
    VERIFY(result.is_string());
53
54
    // 4. Return result.
55
0
    return result.as_string().utf8_string();
56
0
}
57
58
// https://infra.spec.whatwg.org/#serialize-a-javascript-value-to-json-bytes
59
WebIDL::ExceptionOr<ByteBuffer> serialize_javascript_value_to_json_bytes(JS::VM& vm, JS::Value value)
60
0
{
61
    // 1. Let string be the result of serializing a JavaScript value to a JSON string given value.
62
0
    auto string = TRY(serialize_javascript_value_to_json_string(vm, value));
63
64
    // 2. Return the result of running UTF-8 encode on string.
65
    // NOTE: LibJS strings are stored as UTF-8.
66
0
    return TRY_OR_THROW_OOM(vm, ByteBuffer::copy(string.bytes()));
67
0
}
68
69
}