MatureJsonValue.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2024 Tracer Interactive, LLC. All Rights Reserved.
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "json_cast.hpp"
  5. #include "MatureJsonEnums.h"
  6. #include "MatureJsonValue.generated.h"
  7. typedef struct FMatureJsonList FMatureJsonList;
  8. typedef struct FMatureJsonObject FMatureJsonObject;
  9. USTRUCT(BlueprintType, meta = (DisplayName = "Mature JSON|Value"))
  10. struct MATUREJSON_API FMatureJsonValue
  11. {
  12. GENERATED_USTRUCT_BODY()
  13. public:
  14. FMatureJsonValue(/*const TSharedPtr<mature::Document>& Doc*/);
  15. FMatureJsonValue(const TSharedPtr<mature::Document>& Doc, mature::Value&);
  16. FMatureJsonValue(const FMatureJsonValue& Doc);
  17. public:
  18. FMatureJsonValue& SetValue( );
  19. FMatureJsonValue& SetValue(const bool Value );
  20. FMatureJsonValue& SetValue(const float Value );
  21. FMatureJsonValue& SetValue(const double Value );
  22. FMatureJsonValue& SetValue(const int8 Value );
  23. FMatureJsonValue& SetValue(const uint8 Value );
  24. FMatureJsonValue& SetValue(const int16 Value );
  25. FMatureJsonValue& SetValue(const uint16 Value );
  26. FMatureJsonValue& SetValue(const int32 Value );
  27. FMatureJsonValue& SetValue(const uint32 Value );
  28. FMatureJsonValue& SetValue(const int64 Value );
  29. FMatureJsonValue& SetValue(const uint64 Value );
  30. FMatureJsonValue& SetValue(const TCHAR* Value);
  31. FMatureJsonValue& SetValue(const FString& Value );
  32. FMatureJsonValue& SetValue(const FDateTime& Value );
  33. FMatureJsonValue& SetValue(const FGuid& Value );
  34. FMatureJsonValue& SetValue(const FColor& Value );
  35. FMatureJsonValue& SetValue(const FTransform& Value);
  36. FMatureJsonValue& SetValue(const FVector& Value);
  37. FMatureJsonValue& SetValue(const FRotator& Value);
  38. FMatureJsonValue& SetValue(const FLinearColor& Value);
  39. FMatureJsonValue& SetValue(const FMatureJsonValue& Value);
  40. FMatureJsonValue& SetValue(const FMatureJsonObject& Value);
  41. FMatureJsonValue& SetValue(const FMatureJsonList& Value);
  42. EMatureJsonTypeNumber NumberType()const;
  43. // Get the JSON type of this value.
  44. EMatureJsonType GetType() const;
  45. int Size() const ;
  46. bool IsEmpty() const;
  47. void Clear();
  48. // Convert this value to a boolean.
  49. bool ToBoolean() const;
  50. // Convert this value to a float.
  51. float ToFloat() const;
  52. // Convert this value to an integer.
  53. int32 ToInteger() const;
  54. // Convert this value to a number.
  55. double ToNumber() const;
  56. // Convert this value to a string.
  57. FString ToString() const;
  58. // Convert this value to a date/time.
  59. FDateTime ToDateTime() const;
  60. // Convert this value to a GUID.
  61. FGuid ToGuid() const;
  62. // Convert this value to a LinearColor.
  63. FLinearColor ToLinearColor() const;
  64. // Convert this value to a color.
  65. FColor ToColor() const;
  66. // Convert this value to a JSON object.
  67. FMatureJsonObject ToObject(bool check = false) const;
  68. // Convert this value to a JSON array.
  69. FMatureJsonList ToList(bool check = false) const;
  70. // Convert this value to a 32-bit signed integer.
  71. int32 ToInt32() const;
  72. // Convert this value to a 64-bit signed integer.
  73. int64 ToInt64() const;
  74. // Convert this value to a 32-bit unsigned integer.
  75. uint32 ToUInt32() const;
  76. // Convert this value to a 64-bit unsigned integer.
  77. uint64 ToUInt64() const;
  78. bool GetValue(bool& value) const;
  79. bool GetValue(float& value) const;
  80. bool GetValue(int32& value)const;
  81. bool GetValue(int64& value)const;
  82. bool GetValue(uint32& value)const;
  83. bool GetValue(uint64& value)const;
  84. bool GetValue(double& value)const;
  85. bool GetValue(FString& value)const;
  86. bool GetValue(FDateTime& value)const;
  87. bool GetValue(FGuid& value)const;
  88. bool GetValue(FColor& value)const;
  89. bool GetValue(FLinearColor& value)const;
  90. bool GetValue(FRotator& value)const;
  91. bool GetValue(FVector& value)const;
  92. bool GetValue(FTransform& value)const;
  93. TSharedPtr<mature::Document> Document();
  94. TSharedPtr<mature::Document> Document() const;
  95. protected:
  96. class ValueWrap {
  97. public:
  98. ValueWrap();
  99. ValueWrap(TSharedPtr<mature::Document> Doc);
  100. ValueWrap(TSharedPtr<mature::Document> Doc, mature::Value& Ref);
  101. bool IsRoot();
  102. TSharedPtr<mature::Document> Document;
  103. mature::Value& ValueRef;
  104. };
  105. TSharedPtr<ValueWrap> ValueCache;
  106. inline mature::Value& ValueRef();
  107. mature::Value& ValueRef() const;
  108. inline mature::Allocator& GetAllocator();
  109. inline mature::Allocator& GetAllocator()const;
  110. //TSharedPtr<mature::Document> Document;
  111. //mature::Value ValueRef;
  112. FMatureJsonValue& SetValue(mature::Value& Value);
  113. public:
  114. // Check if this value is a date time.
  115. bool IsDateTime() const;
  116. // Check if this value is a guid.
  117. bool IsGuid() const;
  118. // Check if this value is a COLOR.
  119. bool IsColor(FString hex_string) const;
  120. bool Parse(const FString& Text);
  121. bool ParseFile(const FString& FileName);
  122. FString SaveString()const;
  123. bool SaveFile(FString filename)const;
  124. FMatureJsonValue& operator=(const FMatureJsonValue& Value);
  125. private:
  126. friend struct FMatureJsonList;
  127. friend struct FMatureJsonObject;
  128. };