InJsonBPLibrary.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #include "Kismet/BlueprintFunctionLibrary.h"
  3. #include "JsonUtilities.h"
  4. #include "InJson.h"
  5. #include "InJsonBPLibrary.generated.h"
  6. UCLASS()
  7. class UInJsonBPLibrary : public UBlueprintFunctionLibrary
  8. {
  9. GENERATED_BODY()
  10. public:
  11. UFUNCTION(BlueprintCallable, Category = "InJson", CustomThunk, meta = (CustomStructureParam = "Struct", DisplayName = "StructToJsonString"))
  12. static bool StructToJsonString(FString& OutJsonString, const UStruct* Struct);
  13. UFUNCTION(BlueprintCallable, Category = "InJson", CustomThunk, meta = (CustomStructureParam = "StructToFill", DisplayName = "JsonStringToStruct"))
  14. static bool JsonStringToStruct(const FString& JsonString, UStruct*& StructToFill);
  15. DECLARE_FUNCTION(execStructToJsonString)
  16. {
  17. P_GET_PROPERTY_REF(FStrProperty, OutJsonString);
  18. Stack.StepCompiledIn<FStructProperty>(nullptr);
  19. void* InStruct = Stack.MostRecentPropertyAddress;
  20. P_FINISH;
  21. bool bSuccess = false;
  22. FStructProperty* StructProp = CastField<FStructProperty>(Stack.MostRecentProperty);
  23. if (StructProp && InStruct)
  24. {
  25. UScriptStruct* StructType = StructProp->Struct;
  26. P_NATIVE_BEGIN;
  27. bSuccess = Inner_StructToJsonString(OutJsonString, StructType, InStruct);
  28. P_NATIVE_END;
  29. }
  30. *static_cast<bool*>(RESULT_PARAM) = bSuccess;
  31. }
  32. DECLARE_FUNCTION(execJsonStringToStruct)
  33. {
  34. P_GET_PROPERTY_REF(FStrProperty, JsonString);
  35. Stack.StepCompiledIn<FStructProperty>(NULL);
  36. void* StructPtr = Stack.MostRecentPropertyAddress;
  37. FStructProperty* StructProperty = CastField<FStructProperty>(Stack.MostRecentProperty);
  38. P_FINISH;
  39. UStruct* StructDefinition = StructProperty->Struct;
  40. bool bSuccess;
  41. P_NATIVE_BEGIN
  42. bSuccess = Inner_JsonStringToStruct(JsonString, StructDefinition, StructPtr);
  43. P_NATIVE_END
  44. *static_cast<bool*>(RESULT_PARAM) = bSuccess;
  45. }
  46. private:
  47. static bool Inner_StructToJsonString(FString& OutJsonString, const UStruct* StructDefinition, const void* Struct)
  48. {
  49. FJsonObjectConverter::UStructToJsonObjectString(StructDefinition, Struct, OutJsonString, 0, 0);
  50. return !OutJsonString.IsEmpty();
  51. }
  52. static bool Inner_JsonStringToStruct(const FString& JsonString, UStruct* StructDefinition, void* StructPtr)
  53. {
  54. TSharedPtr<FJsonObject> JsonObject;
  55. const TSharedRef<TJsonReader<>> JsonReader = TJsonReaderFactory<>::Create(JsonString);
  56. if (!FJsonSerializer::Deserialize(JsonReader, JsonObject) || !JsonObject.IsValid())
  57. {
  58. UE_LOG(LogInJsonModule, Warning, TEXT("JsonObjectStringToUStruct - Unable to parse json=[%s]"), *JsonString);
  59. return false;
  60. }
  61. if (!FJsonObjectConverter::JsonObjectToUStruct(JsonObject.ToSharedRef(), StructDefinition, StructPtr, 0, 0))
  62. {
  63. UE_LOG(LogInJsonModule, Warning, TEXT("JsonObjectStringToUStruct - Unable to deserialize. json=[%s]"), *JsonString);
  64. return false;
  65. }
  66. return true;
  67. }
  68. };