11cb0ef41Sopenharmony_ci#ifndef SRC_JSON_UTILS_H_
21cb0ef41Sopenharmony_ci#define SRC_JSON_UTILS_H_
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci#include <iomanip>
71cb0ef41Sopenharmony_ci#include <limits>
81cb0ef41Sopenharmony_ci#include <ostream>
91cb0ef41Sopenharmony_ci#include <string>
101cb0ef41Sopenharmony_ci#include <string_view>
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cinamespace node {
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconstexpr bool NeedsJsonEscape(std::string_view str) {
151cb0ef41Sopenharmony_ci  for (const char c : str) {
161cb0ef41Sopenharmony_ci    if (c == '\\' || c == '"' || c < 0x20) return true;
171cb0ef41Sopenharmony_ci  }
181cb0ef41Sopenharmony_ci  return false;
191cb0ef41Sopenharmony_ci}
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_cistd::string EscapeJsonChars(std::string_view str);
221cb0ef41Sopenharmony_cistd::string Reindent(const std::string& str, int indentation);
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci// JSON compiler definitions.
251cb0ef41Sopenharmony_ciclass JSONWriter {
261cb0ef41Sopenharmony_ci public:
271cb0ef41Sopenharmony_ci  JSONWriter(std::ostream& out, bool compact)
281cb0ef41Sopenharmony_ci    : out_(out), compact_(compact) {}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci private:
311cb0ef41Sopenharmony_ci  inline void indent() { indent_ += 2; }
321cb0ef41Sopenharmony_ci  inline void deindent() { indent_ -= 2; }
331cb0ef41Sopenharmony_ci  inline void advance() {
341cb0ef41Sopenharmony_ci    if (compact_) return;
351cb0ef41Sopenharmony_ci    for (int i = 0; i < indent_; i++) out_ << ' ';
361cb0ef41Sopenharmony_ci  }
371cb0ef41Sopenharmony_ci  inline void write_one_space() {
381cb0ef41Sopenharmony_ci    if (compact_) return;
391cb0ef41Sopenharmony_ci    out_ << ' ';
401cb0ef41Sopenharmony_ci  }
411cb0ef41Sopenharmony_ci  inline void write_new_line() {
421cb0ef41Sopenharmony_ci    if (compact_) return;
431cb0ef41Sopenharmony_ci    out_ << '\n';
441cb0ef41Sopenharmony_ci  }
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci public:
471cb0ef41Sopenharmony_ci  inline void json_start() {
481cb0ef41Sopenharmony_ci    if (state_ == kAfterValue) out_ << ',';
491cb0ef41Sopenharmony_ci    write_new_line();
501cb0ef41Sopenharmony_ci    advance();
511cb0ef41Sopenharmony_ci    out_ << '{';
521cb0ef41Sopenharmony_ci    indent();
531cb0ef41Sopenharmony_ci    state_ = kObjectStart;
541cb0ef41Sopenharmony_ci  }
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  inline void json_end() {
571cb0ef41Sopenharmony_ci    write_new_line();
581cb0ef41Sopenharmony_ci    deindent();
591cb0ef41Sopenharmony_ci    advance();
601cb0ef41Sopenharmony_ci    out_ << '}';
611cb0ef41Sopenharmony_ci    state_ = kAfterValue;
621cb0ef41Sopenharmony_ci  }
631cb0ef41Sopenharmony_ci  template <typename T>
641cb0ef41Sopenharmony_ci  inline void json_objectstart(T key) {
651cb0ef41Sopenharmony_ci    if (state_ == kAfterValue) out_ << ',';
661cb0ef41Sopenharmony_ci    write_new_line();
671cb0ef41Sopenharmony_ci    advance();
681cb0ef41Sopenharmony_ci    write_string(key);
691cb0ef41Sopenharmony_ci    out_ << ':';
701cb0ef41Sopenharmony_ci    write_one_space();
711cb0ef41Sopenharmony_ci    out_ << '{';
721cb0ef41Sopenharmony_ci    indent();
731cb0ef41Sopenharmony_ci    state_ = kObjectStart;
741cb0ef41Sopenharmony_ci  }
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci  template <typename T>
771cb0ef41Sopenharmony_ci  inline void json_arraystart(T key) {
781cb0ef41Sopenharmony_ci    if (state_ == kAfterValue) out_ << ',';
791cb0ef41Sopenharmony_ci    write_new_line();
801cb0ef41Sopenharmony_ci    advance();
811cb0ef41Sopenharmony_ci    write_string(key);
821cb0ef41Sopenharmony_ci    out_ << ':';
831cb0ef41Sopenharmony_ci    write_one_space();
841cb0ef41Sopenharmony_ci    out_ << '[';
851cb0ef41Sopenharmony_ci    indent();
861cb0ef41Sopenharmony_ci    state_ = kObjectStart;
871cb0ef41Sopenharmony_ci  }
881cb0ef41Sopenharmony_ci  inline void json_objectend() {
891cb0ef41Sopenharmony_ci    write_new_line();
901cb0ef41Sopenharmony_ci    deindent();
911cb0ef41Sopenharmony_ci    advance();
921cb0ef41Sopenharmony_ci    out_ << '}';
931cb0ef41Sopenharmony_ci    if (indent_ == 0) {
941cb0ef41Sopenharmony_ci      // Top-level object is complete, so end the line.
951cb0ef41Sopenharmony_ci      out_ << '\n';
961cb0ef41Sopenharmony_ci    }
971cb0ef41Sopenharmony_ci    state_ = kAfterValue;
981cb0ef41Sopenharmony_ci  }
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci  inline void json_arrayend() {
1011cb0ef41Sopenharmony_ci    write_new_line();
1021cb0ef41Sopenharmony_ci    deindent();
1031cb0ef41Sopenharmony_ci    advance();
1041cb0ef41Sopenharmony_ci    out_ << ']';
1051cb0ef41Sopenharmony_ci    state_ = kAfterValue;
1061cb0ef41Sopenharmony_ci  }
1071cb0ef41Sopenharmony_ci  template <typename T, typename U>
1081cb0ef41Sopenharmony_ci  inline void json_keyvalue(const T& key, const U& value) {
1091cb0ef41Sopenharmony_ci    if (state_ == kAfterValue) out_ << ',';
1101cb0ef41Sopenharmony_ci    write_new_line();
1111cb0ef41Sopenharmony_ci    advance();
1121cb0ef41Sopenharmony_ci    write_string(key);
1131cb0ef41Sopenharmony_ci    out_ << ':';
1141cb0ef41Sopenharmony_ci    write_one_space();
1151cb0ef41Sopenharmony_ci    write_value(value);
1161cb0ef41Sopenharmony_ci    state_ = kAfterValue;
1171cb0ef41Sopenharmony_ci  }
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci  template <typename U>
1201cb0ef41Sopenharmony_ci  inline void json_element(const U& value) {
1211cb0ef41Sopenharmony_ci    if (state_ == kAfterValue) out_ << ',';
1221cb0ef41Sopenharmony_ci    write_new_line();
1231cb0ef41Sopenharmony_ci    advance();
1241cb0ef41Sopenharmony_ci    write_value(value);
1251cb0ef41Sopenharmony_ci    state_ = kAfterValue;
1261cb0ef41Sopenharmony_ci  }
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci  struct Null {};  // Usable as a JSON value.
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci  struct ForeignJSON {
1311cb0ef41Sopenharmony_ci    std::string as_string;
1321cb0ef41Sopenharmony_ci  };
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci private:
1351cb0ef41Sopenharmony_ci  template <typename T,
1361cb0ef41Sopenharmony_ci            typename test_for_number = typename std::
1371cb0ef41Sopenharmony_ci                enable_if<std::numeric_limits<T>::is_specialized, bool>::type>
1381cb0ef41Sopenharmony_ci  inline void write_value(T number) {
1391cb0ef41Sopenharmony_ci    if constexpr (std::is_same<T, bool>::value)
1401cb0ef41Sopenharmony_ci      out_ << (number ? "true" : "false");
1411cb0ef41Sopenharmony_ci    else
1421cb0ef41Sopenharmony_ci      out_ << number;
1431cb0ef41Sopenharmony_ci  }
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci  inline void write_value(Null null) { out_ << "null"; }
1461cb0ef41Sopenharmony_ci  inline void write_value(std::string_view str) { write_string(str); }
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci  inline void write_value(const ForeignJSON& json) {
1491cb0ef41Sopenharmony_ci    out_ << Reindent(json.as_string, indent_);
1501cb0ef41Sopenharmony_ci  }
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci  inline void write_string(std::string_view str) {
1531cb0ef41Sopenharmony_ci    out_ << '"';
1541cb0ef41Sopenharmony_ci    if (NeedsJsonEscape(str))  // only create temporary std::string if necessary
1551cb0ef41Sopenharmony_ci      out_ << EscapeJsonChars(str);
1561cb0ef41Sopenharmony_ci    else
1571cb0ef41Sopenharmony_ci      out_ << str;
1581cb0ef41Sopenharmony_ci    out_ << '"';
1591cb0ef41Sopenharmony_ci  }
1601cb0ef41Sopenharmony_ci
1611cb0ef41Sopenharmony_ci  enum JSONState { kObjectStart, kAfterValue };
1621cb0ef41Sopenharmony_ci  std::ostream& out_;
1631cb0ef41Sopenharmony_ci  bool compact_;
1641cb0ef41Sopenharmony_ci  int indent_ = 0;
1651cb0ef41Sopenharmony_ci  int state_ = kObjectStart;
1661cb0ef41Sopenharmony_ci};
1671cb0ef41Sopenharmony_ci
1681cb0ef41Sopenharmony_ci}  // namespace node
1691cb0ef41Sopenharmony_ci
1701cb0ef41Sopenharmony_ci#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ci#endif  // SRC_JSON_UTILS_H_
173