JSON.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. int ref;
  24. protected:
  25. __declspec( dllexport ) JSONValue( JSONType type );
  26. public:
  27. __declspec( dllexport ) JSONValue();
  28. __declspec( dllexport ) virtual ~JSONValue();
  29. __declspec( dllexport ) JSONType getType() const;
  30. __declspec( dllexport ) virtual Text toString() const;
  31. __declspec( dllexport ) JSONValue *getThis();
  32. __declspec( dllexport ) JSONValue *release();
  33. };
  34. class JSONBool : public JSONValue
  35. {
  36. private:
  37. bool b;
  38. public:
  39. __declspec( dllexport ) JSONBool( bool b );
  40. __declspec( dllexport ) bool getBool() const;
  41. __declspec( dllexport ) Text toString() const override;
  42. };
  43. class JSONNumber : public JSONValue
  44. {
  45. private:
  46. double number;
  47. public:
  48. __declspec( dllexport ) JSONNumber( double num );
  49. __declspec( dllexport ) double getNumber() const;
  50. __declspec( dllexport ) Text toString() const override;
  51. };
  52. class JSONString : public JSONValue
  53. {
  54. private:
  55. Text string;
  56. public:
  57. __declspec( dllexport ) JSONString( Text string );
  58. __declspec( dllexport ) Text getString() const;
  59. __declspec( dllexport ) Text toString() const override;
  60. };
  61. class JSONArray : public JSONValue
  62. {
  63. private:
  64. RCArray< JSONValue > *array;
  65. public:
  66. __declspec( dllexport ) JSONArray();
  67. __declspec( dllexport ) JSONArray( Text string );
  68. __declspec( dllexport ) JSONArray( const JSONArray &arr );
  69. __declspec( dllexport ) ~JSONArray();
  70. __declspec( dllexport ) JSONArray &operator=( const JSONArray &arr );
  71. __declspec( dllexport ) void addValue( JSONValue *value );
  72. __declspec( dllexport ) JSONValue *getValue( int i ) const;
  73. __declspec( dllexport ) int getLength() const;
  74. __declspec( dllexport ) Text toString() const override;
  75. };
  76. class JSONObject : public JSONValue
  77. {
  78. private:
  79. Array< Text > *fields;
  80. RCArray< JSONValue > *values;
  81. public:
  82. __declspec( dllexport ) JSONObject();
  83. __declspec( dllexport ) JSONObject( Text string );
  84. __declspec( dllexport ) JSONObject( const JSONObject &obj );
  85. __declspec( dllexport ) ~JSONObject();
  86. __declspec( dllexport ) JSONObject &operator=( const JSONObject &obj );
  87. __declspec( dllexport ) bool addValue( Text field, JSONValue *value );
  88. __declspec( dllexport ) bool removeValue( Text field );
  89. __declspec( dllexport ) bool hasValue( Text field );
  90. __declspec( dllexport ) JSONValue *getValue( Text field );
  91. __declspec( dllexport ) Iterator< Text > getFields();
  92. __declspec( dllexport ) Iterator< JSONValue* > getValues();
  93. __declspec( dllexport ) int getFieldCount() const;
  94. __declspec( dllexport ) Text toString() const override;
  95. };
  96. __declspec( dllexport ) JSONValue *loadJSONFromFile( Text path );
  97. namespace Parser
  98. {
  99. __declspec( dllexport ) int findObjectEndInArray( const char *str );
  100. __declspec( dllexport ) Text removeWhitespace( const char *str );
  101. __declspec( dllexport ) JSONValue *getValue( const char *str );
  102. __declspec( dllexport ) int findFieldEndInObject( const char *str );
  103. __declspec( dllexport ) int findValueEndInObject( const char *str );
  104. };
  105. }
  106. }