azakharov93

JSON.CPP for BOOLEAN VALUE

Sep 19th, 2021 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. namespace {
  2.  
  3. struct NodeContainerPrinter {
  4.     std::ostream& out;
  5.  
  6.     void operator()(std::nullptr_t /* value */) const {
  7.         out << "null"s;
  8.     }
  9.     void operator()(bool value) const {
  10.         out << (value ? "true"s : "false"s);
  11.     }
  12.     void operator()(int value) const {...}
  13.     void operator()(double value) const {...}
  14.     void operator()(const std::string& value) const {...}
  15.     void operator()(const Dict& map) const {...}
  16.     void operator()(const Array& array) const {...}
  17. };
  18.  
  19. /* Load methods */
  20.  
  21. Node LoadNode(std::istream& input);
  22.  
  23. std::string LoadLetters(std::istream& input) {
  24.     std::string result;
  25.     while (std::isalpha(input.peek()))
  26.         result.push_back(static_cast<char>(input.get()));
  27.  
  28.     return result;
  29. }
  30.  
  31. Node LoadBool(std::istream& input) {
  32.     std::string value = LoadLetters(input);
  33.     if (value == "true"s)
  34.         return Node{true};
  35.     if (value == "false"s)
  36.         return Node{false};
  37.  
  38.     throw ParsingError(R"(Incorrect format for Boolean Node parsing. "true" or "false" expected)"s);
  39. }
  40.  
  41. ...
  42.  
  43. Node LoadNode(std::istream& input) {
  44.     char symbol;
  45.     if (!(input >> symbol))
  46.         throw ParsingError("Incorrect format for Node parsing. Unexpected EOF"s);
  47.  
  48.     if (symbol == 'n') {
  49.         input.putback(symbol);
  50.         return LoadNull(input);
  51.     } else if (symbol == 't' || symbol == 'f') {
  52.         input.putback(symbol);
  53.         return LoadBool(input);
  54.     } else if (symbol == '[') {
  55.         return LoadArray(input);
  56.     } else if (symbol == '{') {
  57.         return LoadDict(input);
  58.     } else if (symbol == '"') {
  59.         return LoadString(input);
  60.     } else {
  61.         input.putback(symbol);
  62.         return LoadNumber(input);
  63.     }
  64. }
  65.  
  66. }  // namespace
  67.  
  68. /* Constructors */
  69.  
  70. Node::Node(bool value) : data_(value) {}
  71. Node::Node(std::nullptr_t /* value*/) : Node() {}
  72. Node::Node(int value) : data_(value) {}
  73. Node::Node(double value) : data_(value) {}
  74. Node::Node(std::string value) : data_(std::move(value)) {}
  75. Node::Node(Dict map) : data_(std::move(map)) {}
  76. Node::Node(Array array) : data_(std::move(array)) {}
  77.  
  78. /* Is-like methods */
  79.  
  80. bool Node::IsBool() const {
  81.     return std::holds_alternative<bool>(data_);
  82. }
  83.  
  84. ...
  85.  
  86. /* As-like methods */
  87.  
  88. const NodeContainer& Node::AsPureNodeContainer() const {
  89.     return data_;
  90. }
  91.  
  92. bool Node::AsBool() const {
  93.     if (auto* value = std::get_if<bool>(&data_))
  94.         return *value;
  95.     throw std::logic_error("Impossible to parse node as Boolean"s);
  96. }
  97.  
  98. /* Operators */
  99.  
  100. bool operator==(const Node& left, const Node& right) {
  101.     return left.data_ == right.data_;
  102. }
  103.  
  104. bool operator!=(const Node& left, const Node& right) {
  105.     return !(left == right);
  106. }
Add Comment
Please, Sign In to add comment