SIOJConvert.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Modifications Copyright 2018-current Getnamo. All Rights Reserved
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "UObject/Package.h"
  5. #include "UObject/ObjectMacros.h"
  6. #include "Runtime/Json/Public/Dom/JsonObject.h"
  7. #include "Runtime/Json/Public/Dom/JsonValue.h"
  8. #include "SIOJConvert.generated.h"
  9. struct FTrimmedKeyMap
  10. {
  11. FString LongKey;
  12. TMap<FString, TSharedPtr<FTrimmedKeyMap>> SubMap;
  13. FString ToString();
  14. };
  15. /**
  16. *
  17. */
  18. UCLASS()
  19. class SIOJSON_API USIOJConvert : public UObject
  20. {
  21. GENERATED_BODY()
  22. public:
  23. //encode/decode json convenience wrappers
  24. static FString ToJsonString(const TSharedPtr<FJsonObject>& JsonObject);
  25. static FString ToJsonString(const TSharedPtr<FJsonValue>& JsonValue);
  26. static FString ToJsonString(const TArray<TSharedPtr<FJsonValue>>& JsonValueArray);
  27. static TSharedPtr<FJsonObject> ToJsonObject(const FString& JsonString);
  28. static TSharedPtr<FJsonObject> MakeJsonObject();
  29. //Structs
  30. //Will trim names if specified as blueprint
  31. static TSharedPtr<FJsonObject> ToJsonObject(UStruct* Struct, void* StructPtr, bool IsBlueprintStruct = false, bool BinaryStructCppSupport = false);
  32. //Expects a JsonObject, if blueprint struct it will lengthen the names to fill properly
  33. static bool JsonObjectToUStruct(TSharedPtr<FJsonObject> JsonObject, UStruct* Struct, void* StructPtr, bool IsBlueprintStruct = false, bool BinaryStructCppSupport = false);
  34. //Files - convenience read/write files
  35. static bool JsonFileToUStruct(const FString& FilePath, UStruct* Struct, void* StructPtr, bool IsBlueprintStruct = false);
  36. static bool ToJsonFile(const FString& FilePath, UStruct* Struct, void* StructPtr, bool IsBlueprintStruct = false);
  37. //typically from callbacks
  38. static class USIOJsonValue* ToSIOJsonValue(const TArray<TSharedPtr<FJsonValue>>& JsonValueArray);
  39. //Convenience overrides for JsonValues
  40. static TSharedPtr<FJsonValue> ToJsonValue(const TSharedPtr<FJsonObject>& JsonObject);
  41. static TSharedPtr<FJsonValue> ToJsonValue(const FString& StringValue);
  42. static TSharedPtr<FJsonValue> ToJsonValue(double NumberValue);
  43. static TSharedPtr<FJsonValue> ToJsonValue(bool BoolValue);
  44. static TSharedPtr<FJsonValue> ToJsonValue(const TArray<uint8>& BinaryValue);
  45. static TSharedPtr<FJsonValue> ToJsonValue(const TArray<TSharedPtr<FJsonValue>>& ArrayValue);
  46. static TSharedPtr<FJsonValue> JsonStringToJsonValue(const FString& JsonString);
  47. static TArray<TSharedPtr<FJsonValue>> JsonStringToJsonArray(const FString& JsonString);
  48. //internal utility, exposed for modularity
  49. static void TrimValueKeyNames(const TSharedPtr<FJsonValue>& JsonValue);
  50. static bool TrimKey(const FString& InLongKey, FString& OutTrimmedKey);
  51. static void SetTrimmedKeyMapForStruct(TSharedPtr<FTrimmedKeyMap>& InMap, UStruct* Struct);
  52. static void SetTrimmedKeyMapForProp(TSharedPtr<FTrimmedKeyMap>& InMap, FProperty* ArrayInnerProp);
  53. static void ReplaceJsonValueNamesWithMap(TSharedPtr<FJsonValue>& InValue, TSharedPtr<FTrimmedKeyMap> KeyMap);
  54. };