Advertisement
azakharov93

full_json.h

Sep 19th, 2021
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <iostream>
  4. #include <map>
  5. #include <string>
  6. #include <variant>
  7. #include <vector>
  8.  
  9. namespace json {
  10.  
  11. class Node;
  12.  
  13. using Dict = std::map<std::string, Node>;
  14. using Array = std::vector<Node>;
  15. using NodeContainer = std::variant<std::nullptr_t, bool, int, double, std::string, Dict, Array>;
  16.  
  17. // All methods should throw this error while JSON parsing
  18. class ParsingError : public std::runtime_error {
  19. public:
  20.     using runtime_error::runtime_error;
  21. };
  22.  
  23. class Node {
  24. public:  // Constructors
  25.     Node() = default;
  26.  
  27.     Node(std::nullptr_t /* value*/);
  28.     Node(bool value);
  29.     Node(int value);
  30.     Node(double value);
  31.     Node(std::string value);
  32.     Node(Dict map);
  33.     Node(Array array);
  34.  
  35. public:  // Methods
  36.     bool IsNull() const;
  37.     bool IsBool() const;
  38.     bool IsInt() const;
  39.     bool IsDouble() const;
  40.     bool IsPureDouble() const;
  41.     bool IsString() const;
  42.     bool IsArray() const;
  43.     bool IsMap() const;
  44.  
  45.     const NodeContainer& AsPureNodeContainer() const;
  46.     bool AsBool() const;
  47.     int AsInt() const;
  48.     double AsDouble() const;
  49.     const std::string& AsString() const;
  50.     const Array& AsArray() const;
  51.     const Dict& AsMap() const;
  52.  
  53. public:  // Operators
  54.     friend bool operator==(const Node& left, const Node& right);
  55.     friend bool operator!=(const Node& left, const Node& right);
  56.  
  57. private:  // Fields
  58.     NodeContainer data_;
  59. };
  60.  
  61. class Document {
  62. public:  // Constructor
  63.     explicit Document(Node root);
  64.  
  65. public:  // Methods
  66.     [[nodiscard]] const Node& GetRoot() const;
  67.  
  68. public:  // Operators
  69.     friend bool operator==(const Document& left, const Document& right);
  70.     friend bool operator!=(const Document& left, const Document& right);
  71.  
  72. private:
  73.     Node root_;
  74. };
  75.  
  76. Document Load(std::istream& input);
  77.  
  78. void Print(const Document& doc, std::ostream& output);
  79.  
  80. }  // namespace json
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement