JsonLibraryObject.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // Copyright 2022 Tracer Interactive, LLC. All Rights Reserved.
  2. #pragma once
  3. #include "Dom/JsonValue.h"
  4. #include "Dom/JsonObject.h"
  5. #include "Serialization/JsonReader.h"
  6. #include "Serialization/JsonSerializer.h"
  7. #include "Serialization/JsonTypes.h"
  8. #include "UObject/StructOnScope.h"
  9. #include "JsonLibraryEnums.h"
  10. #include "JsonLibraryValue.h"
  11. #include "JsonLibraryObject.generated.h"
  12. typedef struct FJsonLibraryList FJsonLibraryList;
  13. DECLARE_DYNAMIC_DELEGATE_FourParams( FJsonLibraryObjectNotify, const FJsonLibraryValue&, Object, EJsonLibraryNotifyAction, Action, const FString&, Key, const FJsonLibraryValue&, Value );
  14. USTRUCT(BlueprintType, meta = (DisplayName = "JSON Object"))
  15. struct JSONLIBRARY_API FJsonLibraryObject
  16. {
  17. friend struct FJsonLibraryList;
  18. friend struct FJsonLibraryValue;
  19. friend class UJsonLibraryBlueprintHelpers;
  20. GENERATED_USTRUCT_BODY()
  21. protected:
  22. FJsonLibraryObject( const TSharedPtr<FJsonValue>& Value );
  23. FJsonLibraryObject( const TSharedPtr<FJsonValueObject>& Value );
  24. FJsonLibraryObject( const UStruct* StructType, const void* StructPtr );
  25. public:
  26. FJsonLibraryObject( const TSharedPtr<FStructOnScope>& StructData );
  27. template<typename StructType>
  28. FJsonLibraryObject( const StructType& Struct )
  29. : FJsonLibraryObject( StructType::StaticStruct(), &Struct )
  30. {
  31. }
  32. FJsonLibraryObject();
  33. FJsonLibraryObject( const FJsonLibraryObjectNotify& Notify );
  34. FJsonLibraryObject( const FLinearColor& Value );
  35. FJsonLibraryObject( const FRotator& Value );
  36. FJsonLibraryObject( const FTransform& Value );
  37. FJsonLibraryObject( const FVector& Value );
  38. FJsonLibraryObject( const TMap<FString, FJsonLibraryValue>& Value );
  39. FJsonLibraryObject( const TMap<FString, bool>& Value );
  40. FJsonLibraryObject( const TMap<FString, float>& Value );
  41. FJsonLibraryObject( const TMap<FString, double>& Value );
  42. FJsonLibraryObject( const TMap<FString, int32>& Value );
  43. FJsonLibraryObject( const TMap<FString, FString>& Value );
  44. FJsonLibraryObject( const TMap<FString, FDateTime>& Value );
  45. FJsonLibraryObject( const TMap<FString, FGuid>& Value );
  46. FJsonLibraryObject( const TMap<FString, FColor>& Value );
  47. FJsonLibraryObject( const TMap<FString, FLinearColor>& Value );
  48. FJsonLibraryObject( const TMap<FString, FRotator>& Value );
  49. FJsonLibraryObject( const TMap<FString, FTransform>& Value );
  50. FJsonLibraryObject( const TMap<FString, FVector>& Value );
  51. // Check if this object equals another JSON object.
  52. bool Equals( const FJsonLibraryObject& Object ) const;
  53. // Get the number of properties in this object.
  54. int32 Count() const;
  55. // Clear the properties in this object.
  56. void Clear();
  57. // Check if this object has a property.
  58. bool HasKey( const FString& Key ) const;
  59. // Remove a property from this object.
  60. void RemoveKey( const FString& Key );
  61. // Add a JSON object to this object.
  62. void Add( const FJsonLibraryObject& Object );
  63. // Add a map of booleans to this object.
  64. void AddBooleanMap( const TMap<FString, bool>& Map );
  65. // Add a map of floats to this object.
  66. void AddFloatMap( const TMap<FString, float>& Map );
  67. // Add a map of integers to this object.
  68. void AddIntegerMap( const TMap<FString, int32>& Map );
  69. // Add a map of numbers to this object.
  70. void AddNumberMap( const TMap<FString, double>& Map );
  71. // Add a map of strings to this object.
  72. void AddStringMap( const TMap<FString, FString>& Map );
  73. // Add a map of date/times to this object.
  74. void AddDateTimeMap( const TMap<FString, FDateTime>& Map );
  75. // Add a map of GUIDs to this object.
  76. void AddGuidMap( const TMap<FString, FGuid>& Map );
  77. // Add a map of colors to this object.
  78. void AddColorMap( const TMap<FString, FColor>& Map );
  79. // Add a map of linear colors to this object.
  80. void AddLinearColorMap( const TMap<FString, FLinearColor>& Map );
  81. // Add a map of rotators to this object.
  82. void AddRotatorMap( const TMap<FString, FRotator>& Map );
  83. // Add a map of transforms to this object.
  84. void AddTransformMap( const TMap<FString, FTransform>& Map );
  85. // Add a map of vectors to this object.
  86. void AddVectorMap( const TMap<FString, FVector>& Map );
  87. // Get the keys of this object as an array of strings.
  88. TArray<FString> GetKeys() const;
  89. // Get the values of this object as an array of JSON values.
  90. TArray<FJsonLibraryValue> GetValues() const;
  91. // Get a property as a boolean.
  92. bool GetBoolean( const FString& Key ) const;
  93. // Get a property as a float.
  94. float GetFloat( const FString& Key ) const;
  95. // Get a property as an integer.
  96. int32 GetInteger( const FString& Key ) const;
  97. // Get a property as a number.
  98. double GetNumber( const FString& Key ) const;
  99. // Get a property as a string.
  100. FString GetString( const FString& Key ) const;
  101. // Get a property as a date/time.
  102. FDateTime GetDateTime( const FString& Key ) const;
  103. // Get a property as a GUID.
  104. FGuid GetGuid( const FString& Key ) const;
  105. // Get a property as a color.
  106. FColor GetColor( const FString& Key ) const;
  107. // Get a property as a linear color.
  108. FLinearColor GetLinearColor( const FString& Key ) const;
  109. // Get a property as a rotator.
  110. FRotator GetRotator( const FString& Key ) const;
  111. // Get a property as a transform.
  112. FTransform GetTransform( const FString& Key ) const;
  113. // Get a property as a vector.
  114. FVector GetVector( const FString& Key ) const;
  115. // Get a property as a JSON value.
  116. FJsonLibraryValue GetValue( const FString& Key ) const;
  117. // Get a property as a JSON object.
  118. FJsonLibraryObject GetObject( const FString& Key ) const;
  119. // Get a property as a JSON array.
  120. FJsonLibraryList GetList( const FString& Key ) const;
  121. // Get a property as an array of JSON values.
  122. TArray<FJsonLibraryValue> GetArray( const FString& Key ) const;
  123. // Get a property as a map of JSON values.
  124. TMap<FString, FJsonLibraryValue> GetMap( const FString& Key ) const;
  125. // Set a property as a boolean.
  126. void SetBoolean( const FString& Key, bool Value );
  127. // Set a property as a float.
  128. void SetFloat( const FString& Key, float Value );
  129. // Set a property as an integer.
  130. void SetInteger( const FString& Key, int32 Value );
  131. // Set a property as a number.
  132. void SetNumber( const FString& Key, double Value );
  133. // Set a property as a string.
  134. void SetString( const FString& Key, const FString& Value );
  135. // Set a property as a date/time.
  136. void SetDateTime( const FString& Key, const FDateTime& Value );
  137. // Set a property as a GUID.
  138. void SetGuid( const FString& Key, const FGuid& Value );
  139. // Set a property as a color.
  140. void SetColor( const FString& Key, const FColor& Value );
  141. // Set a property as a linear color.
  142. void SetLinearColor( const FString& Key, const FLinearColor& Value );
  143. // Set a property as a rotator.
  144. void SetRotator( const FString& Key, const FRotator& Value );
  145. // Set a property as a transform.
  146. void SetTransform( const FString& Key, const FTransform& Value );
  147. // Set a property as a vector.
  148. void SetVector( const FString& Key, const FVector& Value );
  149. // Set a property as a JSON value.
  150. void SetValue( const FString& Key, const FJsonLibraryValue& Value );
  151. // Set a property as a JSON object.
  152. void SetObject( const FString& Key, const FJsonLibraryObject& Value );
  153. // Set a property as a JSON array.
  154. void SetList( const FString& Key, const FJsonLibraryList& Value );
  155. // Set a property as an array of JSON values.
  156. void SetArray( const FString& Key, const TArray<FJsonLibraryValue>& Value );
  157. // Set a property as a map of JSON values.
  158. void SetMap( const FString& Key, const TMap<FString, FJsonLibraryValue>& Value );
  159. protected:
  160. TSharedPtr<FJsonValueObject> JsonObject;
  161. const TSharedPtr<FJsonObject> GetJsonObject() const;
  162. TSharedPtr<FJsonObject> SetJsonObject();
  163. bool TryParse( const FString& Text, bool bStripComments = false, bool bStripTrailingCommas = false );
  164. bool TryStringify( FString& Text, bool bCondensed = true ) const;
  165. private:
  166. FJsonLibraryObjectNotify OnNotify;
  167. bool bNotifyHasKey;
  168. TSharedPtr<FJsonValue> NotifyValue;
  169. void NotifyAddOrChange( const FString& Key, const FJsonLibraryValue& Value );
  170. bool NotifyCheck();
  171. bool NotifyCheck( const FString& Key );
  172. void NotifyClear();
  173. void NotifyParse();
  174. void NotifyRemove( const FString& Key );
  175. public:
  176. // Check if this object is valid.
  177. bool IsValid() const;
  178. // Check if this object is empty.
  179. bool IsEmpty() const;
  180. // Check if this object is a linear color.
  181. bool IsLinearColor() const;
  182. // Check if this object is a rotator.
  183. bool IsRotator() const;
  184. // Check if this object is a transform.
  185. bool IsTransform() const;
  186. // Check if this object is a vector.
  187. bool IsVector() const;
  188. // Parse a JSON string.
  189. static FJsonLibraryObject Parse( const FString& Text );
  190. // Parse a JSON string.
  191. static FJsonLibraryObject Parse( const FString& Text, const FJsonLibraryObjectNotify& Notify );
  192. // Parse a relaxed JSON string.
  193. static FJsonLibraryObject ParseRelaxed( const FString& Text, bool bStripComments = true, bool bStripTrailingCommas = true );
  194. // Stringify this object as a JSON string.
  195. FString Stringify( bool bCondensed = true ) const;
  196. protected:
  197. bool ToStruct( const UStruct* StructType, void* StructPtr ) const;
  198. public:
  199. // Convert this object to structure memory.
  200. TSharedPtr<FStructOnScope> ToStruct( const UStruct* StructType ) const;
  201. // Convert this object to a structure.
  202. template<typename StructType>
  203. StructType ToStruct() const
  204. {
  205. StructType Struct;
  206. if ( ToStruct( StructType::StaticStruct(), &Struct ) )
  207. return Struct;
  208. return StructType();
  209. }
  210. // Convert this object to a linear color.
  211. FLinearColor ToLinearColor() const;
  212. // Convert this object to a rotator.
  213. FRotator ToRotator() const;
  214. // Convert this object to a transform.
  215. FTransform ToTransform() const;
  216. // Convert this object to a vector.
  217. FVector ToVector() const;
  218. // Copy a JSON object to a map of JSON values.
  219. TMap<FString, FJsonLibraryValue> ToMap() const;
  220. // Copy a JSON object to a map of booleans.
  221. TMap<FString, bool> ToBooleanMap() const;
  222. // Copy a JSON object to a map of floats.
  223. TMap<FString, float> ToFloatMap() const;
  224. // Copy a JSON object to a map of integers.
  225. TMap<FString, int32> ToIntegerMap() const;
  226. // Copy a JSON object to a map of numbers.
  227. TMap<FString, double> ToNumberMap() const;
  228. // Copy a JSON object to a map of strings.
  229. TMap<FString, FString> ToStringMap() const;
  230. // Copy a JSON object to a map of date/times.
  231. TMap<FString, FDateTime> ToDateTimeMap() const;
  232. // Copy a JSON object to a map of GUIDs.
  233. TMap<FString, FGuid> ToGuidMap() const;
  234. // Copy a JSON object to a map of colors.
  235. TMap<FString, FColor> ToColorMap() const;
  236. // Copy a JSON object to a map of linear colors.
  237. TMap<FString, FLinearColor> ToLinearColorMap() const;
  238. // Copy a JSON object to a map of rotators.
  239. TMap<FString, FRotator> ToRotatorMap() const;
  240. // Copy a JSON object to a map of transforms.
  241. TMap<FString, FTransform> ToTransformMap() const;
  242. // Copy a JSON object to a map of vectors.
  243. TMap<FString, FVector> ToVectorMap() const;
  244. bool operator==( const FJsonLibraryObject& Object ) const;
  245. bool operator!=( const FJsonLibraryObject& Object ) const;
  246. bool operator==( const FJsonLibraryValue& Value ) const;
  247. bool operator!=( const FJsonLibraryValue& Value ) const;
  248. };