JSON.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #pragma once
  2. #include "Text.h"
  3. #include "Array.h"
  4. namespace Framework
  5. {
  6. namespace JSON
  7. {
  8. enum JSONType
  9. {
  10. NULL_,
  11. BOOLEAN,
  12. NUMBER,
  13. STRING,
  14. ARRAY,
  15. OBJECT
  16. };
  17. class JSONArray;
  18. class JSONObject;
  19. class JSONValue
  20. {
  21. private:
  22. JSONType type;
  23. protected:
  24. JSONValue( JSONType type );
  25. public:
  26. JSONValue();
  27. JSONType getType() const;
  28. virtual Text toString() const;
  29. };
  30. class JSONBool : public JSONValue
  31. {
  32. private:
  33. bool b;
  34. public:
  35. JSONBool( bool b );
  36. bool getBool() const;
  37. Text toString() const override;
  38. };
  39. class JSONNumber : public JSONValue
  40. {
  41. private:
  42. double number;
  43. public:
  44. JSONNumber( double num );
  45. double getNumber() const;
  46. Text toString() const override;
  47. };
  48. class JSONString : public JSONValue
  49. {
  50. private:
  51. Text string;
  52. public:
  53. JSONString( Text string );
  54. Text getString() const;
  55. Text toString() const override;
  56. };
  57. class JSONArray : public JSONValue
  58. {
  59. private:
  60. Array< JSONValue > *array;
  61. public:
  62. JSONArray();
  63. JSONArray( Text string );
  64. JSONArray( const JSONArray &arr );
  65. ~JSONArray();
  66. JSONArray &operator=( const JSONArray &arr );
  67. void addValue( JSONValue value );
  68. JSONValue getValue( int i ) const;
  69. int getLength() const;
  70. Text toString() const override;
  71. };
  72. class JSONObject : public JSONValue
  73. {
  74. private:
  75. Array< Text > *fields;
  76. Array< JSONValue > *values;
  77. public:
  78. JSONObject();
  79. JSONObject( Text string );
  80. JSONObject( const JSONObject &obj );
  81. ~JSONObject();
  82. JSONObject &operator=( const JSONObject &obj );
  83. bool addValue( Text field, JSONValue value );
  84. bool removeValue( Text field );
  85. bool hasValue( Text field );
  86. JSONValue getValue( Text field );
  87. Iterator< Text > getFields();
  88. Iterator< JSONValue > getValues();
  89. Text toString() const override;
  90. };
  91. namespace Parser
  92. {
  93. int findObjectEndInArray( const char *str );
  94. Text removeWhitespace( const char *str );
  95. JSONValue getValue( const char *str );
  96. int findFieldEndInObject( const char *str );
  97. int findValueEndInObject( const char *str );
  98. };
  99. }
  100. }