json_cast.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #pragma once
  2. #include "CoreMinimal.h"
  3. #include "rapidjson/rapidjson.h" // rapidjson's DOM-style API
  4. #include "rapidjson/document.h" // rapidjson's DOM-style API
  5. #include "rapidjson/prettywriter.h" // for stringify JSON
  6. #include <rapidjson/error/en.h> // 包含 GetParseError_En 函数
  7. #include "MatureJsonEnums.h" // 包含 GetParseError_En 函数
  8. #include <iostream>
  9. #include <fstream>
  10. #include <sstream>
  11. #include <string>
  12. namespace mature {
  13. template<typename >
  14. void print_type();
  15. template<typename T>
  16. class JsonChar {
  17. public:
  18. };
  19. template<>
  20. class JsonChar <char> : public rapidjson::UTF8<char> {
  21. public:
  22. };
  23. template<>
  24. class JsonChar <wchar_t> : public rapidjson::UTF16LE<wchar_t> {
  25. public:
  26. };
  27. template<>
  28. class JsonChar <char16_t> : public rapidjson::UTF16LE<char16_t> {
  29. public:
  30. };
  31. template<>
  32. class JsonChar <unsigned> : public rapidjson::UTF32LE<unsigned> {
  33. public:
  34. };
  35. typedef rapidjson::GenericStringRef<TCHAR> StringRefType;
  36. typedef rapidjson::GenericDocument<JsonChar<TCHAR> > Document;
  37. typedef rapidjson::GenericStringBuffer<JsonChar<TCHAR> > StringBuffer;
  38. typedef rapidjson::Writer<StringBuffer, JsonChar<TCHAR>, JsonChar<TCHAR>> Writer;
  39. typedef rapidjson::GenericValue<JsonChar<TCHAR> > Value;
  40. typedef rapidjson::GenericArray<false, Value > Array;
  41. typedef rapidjson::GenericArray<true, Value > ConstArray;
  42. typedef rapidjson::GenericObject<false, Value > Object;
  43. typedef rapidjson::GenericObject<true, Value > ConstObject;
  44. typedef rapidjson::GenericMemberIterator<false, JsonChar<TCHAR>, Document::AllocatorType> MemberIterator;
  45. typedef rapidjson::GenericMemberIterator<true, JsonChar<TCHAR>, Document::AllocatorType> ConstMemberIterator;
  46. //using Allocator = Document::AllocatorType;
  47. typedef Document::AllocatorType Allocator;
  48. using Type = rapidjson::Type;
  49. //using Array = Value::Array;
  50. //using ConstArray = Value::ConstArray;
  51. //using Object = Value::Object;
  52. //using ConstObject = Value::ConstObject;
  53. template<class T>
  54. T GetNumber(const Value& ValueRef) {
  55. if (mature::Type::kNumberType!=ValueRef.GetType())return T(0);
  56. if (ValueRef.IsDouble())return T(ValueRef.GetDouble());
  57. else if (ValueRef.IsInt64())return T(ValueRef.GetInt64());
  58. else if (ValueRef.IsUint64())return T(ValueRef.GetUint64());
  59. else if (ValueRef.IsInt())return T(ValueRef.GetInt());
  60. else if (ValueRef.IsUint())return T(ValueRef.GetUint());
  61. return T(0);
  62. }
  63. template<class T>
  64. bool GetNumberBool(const Value& ValueRef, T& value) {
  65. if (mature::Type::kNumberType != ValueRef.GetType())return false;
  66. if (ValueRef.IsDouble())value = T(ValueRef.GetDouble());
  67. else if (ValueRef.IsInt64())value = T(ValueRef.GetInt64());
  68. else if (ValueRef.IsUint64())value = T(ValueRef.GetUint64());
  69. else if (ValueRef.IsInt())value = T(ValueRef.GetInt());
  70. else if (ValueRef.IsUint())value = T(ValueRef.GetUint());
  71. else return false;
  72. return false;
  73. }
  74. template<class T>
  75. T GetNumberArray(const unsigned int idx,const Array& ArrayRef) {
  76. if (ArrayRef.Size() < idx)return T();
  77. return GetNumber<T>(ArrayRef[idx]);
  78. }
  79. FString GetString(const Value& ValueRef);
  80. bool GetStringBool(const Value& ValueRef, FString& value);
  81. FString GetStringArray(const unsigned int idx, const Array& ArrayRef);
  82. }