@@ -835,36 +835,52 @@ bool USIOJConvert::JsonObjectToUStruct(TSharedPtr<FJsonObject> JsonObject, UStru
835835bool USIOJConvert::JsonFileToUStruct (const FString& FilePath, UStruct* Struct, void * StructPtr, bool IsBlueprintStruct /* = false*/ )
836836{
837837 // Read bytes from file
838- TArray<uint8> OutBytes ;
839- if (!FFileHelper::LoadFileToArray (OutBytes , *FilePath))
838+ TArray<uint8> ReadBytes ;
839+ if (!FFileHelper::LoadFileToArray (ReadBytes , *FilePath))
840840 {
841841 return false ;
842842 }
843843
844- // Convert to json string
845- FString JsonString;
846- FFileHelper::BufferToString (JsonString, OutBytes.GetData (), OutBytes.Num ());
847-
848- // Read into struct
849- return JsonObjectToUStruct (ToJsonObject (JsonString), Struct, StructPtr, IsBlueprintStruct);
844+ return BytesToStruct (ReadBytes, Struct, StructPtr, IsBlueprintStruct);
850845}
851846
852847bool USIOJConvert::ToJsonFile (const FString& FilePath, UStruct* Struct, void * StructPtr, bool IsBlueprintStruct /* = false*/ )
848+ {
849+ TArray<uint8> Bytes;
850+ StructToBytes (Struct, StructPtr, Bytes, IsBlueprintStruct);
851+
852+ // flush to disk
853+ return FFileHelper::SaveArrayToFile (Bytes, *FilePath);
854+ }
855+
856+ bool USIOJConvert::StructToBytes (UStruct* Struct, void * StructPtr, TArray<uint8>& OutBytes, bool IsBlueprintStruct)
853857{
854858 // Get json object with trimmed values
855859 TSharedPtr<FJsonObject> JsonObject = ToJsonObject (Struct, StructPtr, IsBlueprintStruct);
856860 TSharedPtr<FJsonValue> TrimmedValue = MakeShareable (new FJsonValueObject (JsonObject));
857- TrimValueKeyNames (TrimmedValue);
861+
862+ if (IsBlueprintStruct)
863+ {
864+ TrimValueKeyNames (TrimmedValue);
865+ }
858866
859867 // Convert to string
860868 FString JsonString = ToJsonString (TrimmedValue);
861869 FTCHARToUTF8 Utf8String (*JsonString);
862870
863- TArray<uint8> Bytes;
864- Bytes.Append ((uint8*)Utf8String.Get (), Utf8String.Length ());
871+ OutBytes.Append ((uint8*)Utf8String.Get (), Utf8String.Length ());
865872
866- // flush to disk
867- return FFileHelper::SaveArrayToFile (Bytes, *FilePath);
873+ return true ;
874+ }
875+
876+ bool USIOJConvert::BytesToStruct (const TArray<uint8>& InBytes, UStruct* Struct, void * StructPtr, bool IsBlueprintStruct)
877+ {
878+ // Convert to json string
879+ FString JsonString;
880+ FFileHelper::BufferToString (JsonString, InBytes.GetData (), InBytes.Num ());
881+
882+ // Read into struct
883+ return JsonObjectToUStruct (ToJsonObject (JsonString), Struct, StructPtr, IsBlueprintStruct);
868884}
869885
870886void USIOJConvert::TrimValueKeyNames (const TSharedPtr<FJsonValue>& JsonValue)
0 commit comments