JSON.cpp 2.6 KB

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