JSONObject.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Runtime/Object.h>
  8. namespace JS {
  9. class JSONObject final : public Object {
  10. JS_OBJECT(JSONObject, Object);
  11. public:
  12. virtual void initialize(Realm&) override;
  13. virtual ~JSONObject() override = default;
  14. // The base implementation of stringify is exposed because it is used by
  15. // test-js to communicate between the JS tests and the C++ test runner.
  16. static ThrowCompletionOr<Optional<DeprecatedString>> stringify_impl(VM&, Value value, Value replacer, Value space);
  17. static Value parse_json_value(VM&, JsonValue const&);
  18. private:
  19. explicit JSONObject(Realm&);
  20. struct StringifyState {
  21. GCPtr<FunctionObject> replacer_function;
  22. HashTable<GCPtr<Object>> seen_objects;
  23. DeprecatedString indent { DeprecatedString::empty() };
  24. DeprecatedString gap;
  25. Optional<Vector<DeprecatedString>> property_list;
  26. };
  27. // Stringify helpers
  28. static ThrowCompletionOr<Optional<DeprecatedString>> serialize_json_property(VM&, StringifyState&, PropertyKey const& key, Object* holder);
  29. static ThrowCompletionOr<DeprecatedString> serialize_json_object(VM&, StringifyState&, Object&);
  30. static ThrowCompletionOr<DeprecatedString> serialize_json_array(VM&, StringifyState&, Object&);
  31. static DeprecatedString quote_json_string(DeprecatedString);
  32. // Parse helpers
  33. static Object* parse_json_object(VM&, JsonObject const&);
  34. static Array* parse_json_array(VM&, JsonArray const&);
  35. static ThrowCompletionOr<Value> internalize_json_property(VM&, Object* holder, PropertyKey const& name, FunctionObject& reviver);
  36. JS_DECLARE_NATIVE_FUNCTION(stringify);
  37. JS_DECLARE_NATIVE_FUNCTION(parse);
  38. };
  39. }