json.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #pragma once
  2. #include <string>
  3. #include <map>
  4. #include <vector>
  5. #include <assert.h>
  6. namespace json
  7. {
  8. enum class Type
  9. {
  10. intValue,
  11. floatValue,
  12. boolValue,
  13. stringValue,
  14. arrayValue,
  15. objectValue,
  16. nullValue
  17. };
  18. class Value
  19. {
  20. public:
  21. Type type;
  22. union ValueHolder
  23. {
  24. int intValue;
  25. float floatValue;
  26. bool boolValue;
  27. std::string* stringValue;
  28. std::vector<Value>* arrayValue;
  29. std::map<std::string, Value>* objectValue;
  30. } value;
  31. static Value null;
  32. Value();
  33. Value(Type type);
  34. Value(int value);
  35. Value(float value);
  36. Value(bool value);
  37. Value(const std::string &value);
  38. Value(const char* value);
  39. Value(const Value& other);
  40. virtual ~Value();
  41. void operator = (const Value& other);
  42. inline operator int() const { return asInt(); }
  43. inline operator float() const { return asFloat(); }
  44. inline operator bool() const { return asBool(); }
  45. inline operator const std::string&() const { return asString(); }
  46. inline int asInt() const { assert(type == Type::intValue); return value.intValue; }
  47. inline float asFloat() const { assert(type == Type::floatValue || type == Type::intValue); return type == Type::floatValue ? value.floatValue : value.intValue; }
  48. inline bool asBool() const { assert(type == Type::boolValue); return value.boolValue; }
  49. inline const std::string& asString() const { assert(type == Type::stringValue); return *value.stringValue; }
  50. inline bool isNull() const { return type == Type::nullValue; }
  51. inline bool isString() const { return type == Type::stringValue; }
  52. inline bool isInt() const { return type == Type::intValue; }
  53. inline bool isBool() const { return type == Type::boolValue; }
  54. inline bool isFloat() const { return type == Type::floatValue; }
  55. inline bool isObject() const { return type == Type::objectValue; }
  56. inline bool isArray() const { return type == Type::arrayValue; }
  57. inline bool isMember(const std::string &name) const { assert(type == Type::objectValue); return value.objectValue->find(name) != value.objectValue->end(); }
  58. //array/object
  59. virtual size_t size() const;
  60. //array
  61. virtual void push_back(const Value& value);
  62. virtual void erase(size_t index);
  63. virtual Value& operator [] (size_t index);
  64. virtual Value& operator [] (int index);
  65. virtual Value& operator [] (size_t index) const;
  66. virtual Value& operator [] (int index) const;
  67. virtual Value& operator [] (const std::string &key);
  68. virtual Value& operator [] (const char* key);
  69. virtual Value& operator [] (const std::string &key) const;
  70. virtual Value& operator [] (const char* key) const;
  71. virtual bool operator == (const std::string &other) { return asString() == other; }
  72. virtual bool operator == (const int other) { return asInt() == other; }
  73. virtual bool operator == (const float other) { return asFloat() == other; }
  74. std::ostream& prettyPrint(std::ostream& stream, json::Value& printConfig = null, int level = 0) const;
  75. class Iterator;
  76. Iterator begin() const;
  77. Iterator end() const;
  78. };
  79. class Value::Iterator
  80. {
  81. private:
  82. Type type;
  83. std::map<std::string, Value>::iterator objectIterator;
  84. std::vector<Value>::iterator arrayIterator;
  85. public:
  86. Iterator(const std::map<std::string, Value>::iterator& objectIterator);
  87. Iterator(const std::vector<Value>::iterator& arrayIterator);
  88. void operator ++();
  89. void operator ++(int);
  90. bool operator != (const Iterator &other);
  91. Value operator*();
  92. std::string key();
  93. Value& value();
  94. };
  95. Value readJson(const std::string &data);
  96. Value readJson(std::istream &stream);
  97. std::ostream &operator << (std::ostream &stream, const Value& value); //serializes json data
  98. }