json.h 2.95 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_TORQUE_LS_JSON_H_
#define V8_TORQUE_LS_JSON_H_

#include <map>
9
#include <memory>
10 11 12
#include <string>
#include <vector>

13
#include "src/base/logging.h"
14 15 16 17 18 19 20 21 22 23 24 25

namespace v8 {
namespace internal {
namespace torque {
namespace ls {

struct JsonValue;

using JsonObject = std::map<std::string, JsonValue>;
using JsonArray = std::vector<JsonValue>;

struct JsonValue {
26
 public:
27 28
  enum { OBJECT, ARRAY, STRING, NUMBER, BOOL, IS_NULL } tag;

29 30 31 32 33 34 35 36
  // JsonValues can only be moved, not copied.
  JsonValue() V8_NOEXCEPT = default;
  constexpr JsonValue(const JsonValue& other) = delete;
  JsonValue& operator=(const JsonValue& other) = delete;

  JsonValue(JsonValue&& other) V8_NOEXCEPT = default;
  JsonValue& operator=(JsonValue&& other) V8_NOEXCEPT = default;

37 38 39 40 41 42 43 44 45 46
  static JsonValue From(double number) {
    JsonValue result;
    result.tag = JsonValue::NUMBER;
    result.number_ = number;
    return result;
  }

  static JsonValue From(JsonObject object) {
    JsonValue result;
    result.tag = JsonValue::OBJECT;
47
    result.object_ = std::make_unique<JsonObject>(std::move(object));
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    return result;
  }

  static JsonValue From(bool b) {
    JsonValue result;
    result.tag = JsonValue::BOOL;
    result.flag_ = b;
    return result;
  }

  static JsonValue From(const std::string& string) {
    JsonValue result;
    result.tag = JsonValue::STRING;
    result.string_ = string;
    return result;
  }

  static JsonValue From(JsonArray array) {
    JsonValue result;
    result.tag = JsonValue::ARRAY;
68
    result.array_ = std::make_unique<JsonArray>(std::move(array));
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    return result;
  }

  static JsonValue JsonNull() {
    JsonValue result;
    result.tag = JsonValue::IS_NULL;
    return result;
  }

  bool IsNumber() const { return tag == NUMBER; }
  double ToNumber() const {
    CHECK(IsNumber());
    return number_;
  }

  bool IsBool() const { return tag == BOOL; }
  bool ToBool() const {
    CHECK(IsBool());
    return flag_;
  }

  bool IsString() const { return tag == STRING; }
  const std::string& ToString() const {
    CHECK(IsString());
    return string_;
  }

  bool IsObject() const { return object_ && tag == OBJECT; }
  const JsonObject& ToObject() const {
    CHECK(IsObject());
    return *object_;
  }
  JsonObject& ToObject() {
    CHECK(IsObject());
    return *object_;
  }

  bool IsArray() const { return array_ && tag == ARRAY; }
  const JsonArray& ToArray() const {
    CHECK(IsArray());
    return *array_;
  }
  JsonArray& ToArray() {
    CHECK(IsArray());
    return *array_;
  }

 private:
  double number_ = 0;
  bool flag_ = false;
  std::string string_;
  std::unique_ptr<JsonObject> object_;
  std::unique_ptr<JsonArray> array_;
};
123

124 125 126 127 128 129 130 131
std::string SerializeToString(const JsonValue& value);

}  // namespace ls
}  // namespace torque
}  // namespace internal
}  // namespace v8

#endif  // V8_TORQUE_LS_JSON_H_