| 
									
										
										
										
											2022-09-25 19:19:24 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2023-03-04 21:46:04 +00:00
										 |  |  |  * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-09-25 19:19:24 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-04 21:46:04 +00:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-25 19:19:24 +01:00
										 |  |  | #include <LibJS/Runtime/AbstractOperations.h>
 | 
					
						
							| 
									
										
										
										
											2023-01-07 12:14:54 -05:00
										 |  |  | #include <LibJS/Runtime/Completion.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-25 19:19:24 +01:00
										 |  |  | #include <LibJS/Runtime/Value.h>
 | 
					
						
							| 
									
										
										
										
											2022-10-04 18:04:10 +01:00
										 |  |  | #include <LibTextCodec/Decoder.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-25 19:19:24 +01:00
										 |  |  | #include <LibWeb/Infra/JSON.h>
 | 
					
						
							|  |  |  | #include <LibWeb/WebIDL/ExceptionOr.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::Infra { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://infra.spec.whatwg.org/#parse-a-json-string-to-a-javascript-value
 | 
					
						
							| 
									
										
										
										
											2023-02-28 17:45:49 +00:00
										 |  |  | WebIDL::ExceptionOr<JS::Value> parse_json_string_to_javascript_value(JS::Realm& realm, StringView string) | 
					
						
							| 
									
										
										
										
											2022-09-25 19:19:24 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-02-28 17:45:49 +00:00
										 |  |  |     auto& vm = realm.vm(); | 
					
						
							| 
									
										
										
										
											2022-09-25 19:19:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 1. Return ? Call(%JSON.parse%, undefined, « string »).
 | 
					
						
							| 
									
										
										
										
											2023-04-13 00:47:15 +02:00
										 |  |  |     return TRY(JS::call(vm, *realm.intrinsics().json_parse_function(), JS::js_undefined(), MUST_OR_THROW_OOM(JS::PrimitiveString::create(vm, string)))); | 
					
						
							| 
									
										
										
										
											2022-09-25 19:19:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://infra.spec.whatwg.org/#parse-json-bytes-to-a-javascript-value
 | 
					
						
							| 
									
										
										
										
											2023-02-28 17:45:49 +00:00
										 |  |  | WebIDL::ExceptionOr<JS::Value> parse_json_bytes_to_javascript_value(JS::Realm& realm, ReadonlyBytes bytes) | 
					
						
							| 
									
										
										
										
											2022-09-25 19:19:24 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-02-28 17:45:49 +00:00
										 |  |  |     auto& vm = realm.vm(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-04 18:04:10 +01:00
										 |  |  |     // 1. Let string be the result of running UTF-8 decode on bytes.
 | 
					
						
							|  |  |  |     TextCodec::UTF8Decoder decoder; | 
					
						
							| 
									
										
										
										
											2023-02-17 20:15:10 +00:00
										 |  |  |     auto string = TRY_OR_THROW_OOM(vm, decoder.to_utf8(bytes)); | 
					
						
							| 
									
										
										
										
											2022-09-25 19:19:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 2. Return the result of parsing a JSON string to an Infra value given string.
 | 
					
						
							| 
									
										
										
										
											2023-02-28 17:45:49 +00:00
										 |  |  |     return parse_json_string_to_javascript_value(realm, string); | 
					
						
							| 
									
										
										
										
											2022-09-25 19:19:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://infra.spec.whatwg.org/#serialize-a-javascript-value-to-a-json-string
 | 
					
						
							| 
									
										
										
										
											2023-03-04 21:46:04 +00:00
										 |  |  | WebIDL::ExceptionOr<String> serialize_javascript_value_to_json_string(JS::VM& vm, JS::Value value) | 
					
						
							| 
									
										
										
										
											2022-09-25 19:19:24 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     auto& realm = *vm.current_realm(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 1. Let result be ? Call(%JSON.stringify%, undefined, « value »).
 | 
					
						
							| 
									
										
										
										
											2023-04-13 00:47:15 +02:00
										 |  |  |     auto result = TRY(JS::call(vm, *realm.intrinsics().json_stringify_function(), JS::js_undefined(), value)); | 
					
						
							| 
									
										
										
										
											2022-09-25 19:19:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 2. If result is undefined, then throw a TypeError.
 | 
					
						
							|  |  |  |     if (result.is_undefined()) | 
					
						
							|  |  |  |         return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Result of stringifying value must not be undefined"sv }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 3. Assert: result is a string.
 | 
					
						
							|  |  |  |     VERIFY(result.is_string()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 4. Return result.
 | 
					
						
							| 
									
										
										
										
											2023-03-04 21:46:04 +00:00
										 |  |  |     return TRY(result.as_string().utf8_string()); | 
					
						
							| 
									
										
										
										
											2022-09-25 19:19:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://infra.spec.whatwg.org/#serialize-a-javascript-value-to-json-bytes
 | 
					
						
							|  |  |  | WebIDL::ExceptionOr<ByteBuffer> serialize_javascript_value_to_json_bytes(JS::VM& vm, JS::Value value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // 1. Let string be the result of serializing a JavaScript value to a JSON string given value.
 | 
					
						
							|  |  |  |     auto string = TRY(serialize_javascript_value_to_json_string(vm, value)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-04 18:04:10 +01:00
										 |  |  |     // 2. Return the result of running UTF-8 encode on string.
 | 
					
						
							|  |  |  |     // NOTE: LibJS strings are stored as UTF-8.
 | 
					
						
							| 
									
										
										
										
											2023-01-07 12:14:54 -05:00
										 |  |  |     return TRY_OR_THROW_OOM(vm, ByteBuffer::copy(string.bytes())); | 
					
						
							| 
									
										
										
										
											2022-09-25 19:19:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |