json.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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(__int64 value);
  36. Value(float value);
  37. Value(bool value);
  38. Value(const std::string &value);
  39. Value(const char* value);
  40. Value(const Value& other);
  41. virtual ~Value();
  42. void operator = (const Value& other);
  43. inline operator int() const { return asInt(); }
  44. inline operator float() const { return asFloat(); }
  45. inline operator bool() const { return asBool(); }
  46. inline operator const std::string&() const { return asString(); }
  47. inline int asInt() const { assert(type == Type::intValue); return value.intValue; }
  48. inline float asFloat() const { assert(type == Type::floatValue || type == Type::intValue); return type == Type::floatValue ? value.floatValue : value.intValue; }
  49. inline bool asBool() const { assert(type == Type::boolValue); return value.boolValue; }
  50. inline const std::string& asString() const { assert(type == Type::stringValue); return *value.stringValue; }
  51. inline bool isNull() const { return type == Type::nullValue; }
  52. inline bool isString() const { return type == Type::stringValue; }
  53. inline bool isInt() const { return type == Type::intValue; }
  54. inline bool isBool() const { return type == Type::boolValue; }
  55. inline bool isFloat() const { return type == Type::floatValue; }
  56. inline bool isObject() const { return type == Type::objectValue; }
  57. inline bool isArray() const { return type == Type::arrayValue; }
  58. inline bool isMember(const std::string &name) const { if (type != Type::objectValue) return false; return value.objectValue->find(name) != value.objectValue->end(); }
  59. //array/object
  60. virtual size_t size() const;
  61. //array
  62. virtual void push_back(const Value& value);
  63. virtual void erase(size_t index);
  64. virtual Value& operator [] (size_t index);
  65. virtual Value& operator [] (int index);
  66. virtual Value& operator [] (size_t index) const;
  67. virtual Value& operator [] (int index) const;
  68. virtual Value& operator [] (const std::string &key);
  69. virtual Value& operator [] (const char* key);
  70. virtual Value& operator [] (const std::string &key) const;
  71. virtual Value& operator [] (const char* key) const;
  72. virtual const Value& get(const char* key, const Value& default) const;
  73. virtual bool operator == (const std::string &other) { return asString() == other; }
  74. virtual bool operator == (const int other) { return asInt() == other; }
  75. virtual bool operator == (const float other) { return asFloat() == other; }
  76. std::ostream& prettyPrint(std::ostream& stream, json::Value& printConfig = null, int level = 0) const;
  77. class Iterator;
  78. private:
  79. public:
  80. Iterator begin() const;
  81. Iterator end() const;
  82. };
  83. class Value::Iterator
  84. {
  85. private:
  86. Type type;
  87. std::map<std::string, Value>::iterator objectIterator;
  88. std::vector<Value>::iterator arrayIterator;
  89. public:
  90. Iterator(const std::map<std::string, Value>::iterator& objectIterator);
  91. Iterator(const std::vector<Value>::iterator& arrayIterator);
  92. void operator ++();
  93. void operator ++(int);
  94. bool operator != (const Iterator &other);
  95. Value operator*();
  96. std::string key();
  97. Value& value();
  98. };
  99. Value readJson(const std::string &data);
  100. Value readJson(std::istream &stream);
  101. std::ostream &operator << (std::ostream &stream, const Value& value); //serializes json data
  102. std::string &operator << (std::string &stream, const Value& value); //serializes json data
  103. }