JSONObject.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. explicit JSONObject(GlobalObject&);
  13. virtual void initialize(GlobalObject&) override;
  14. virtual ~JSONObject() override;
  15. // The base implementation of stringify is exposed because it is used by
  16. // test-js to communicate between the JS tests and the C++ test runner.
  17. static ThrowCompletionOr<String> stringify_impl(GlobalObject&, Value value, Value replacer, Value space);
  18. static Value parse_json_value(GlobalObject&, const JsonValue&);
  19. private:
  20. struct StringifyState {
  21. FunctionObject* replacer_function { nullptr };
  22. HashTable<Object*> seen_objects;
  23. String indent { String::empty() };
  24. String gap;
  25. Optional<Vector<String>> property_list;
  26. };
  27. // Stringify helpers
  28. static ThrowCompletionOr<String> serialize_json_property(GlobalObject&, StringifyState&, const PropertyKey& key, Object* holder);
  29. static ThrowCompletionOr<String> serialize_json_object(GlobalObject&, StringifyState&, Object&);
  30. static ThrowCompletionOr<String> serialize_json_array(GlobalObject&, StringifyState&, Object&);
  31. static String quote_json_string(String);
  32. // Parse helpers
  33. static Object* parse_json_object(GlobalObject&, const JsonObject&);
  34. static Array* parse_json_array(GlobalObject&, const JsonArray&);
  35. static ThrowCompletionOr<Value> internalize_json_property(GlobalObject&, Object* holder, PropertyKey const& name, FunctionObject& reviver);
  36. JS_DECLARE_NATIVE_FUNCTION(stringify);
  37. JS_DECLARE_NATIVE_FUNCTION(parse);
  38. };
  39. }