├── Config └── FilterPlugin.ini ├── Source └── JsonUE4 │ ├── Public │ ├── JsonUE4.h │ ├── JsonUE4Globals.h │ ├── IJsonUE4.h │ ├── JsonItemBP.h │ ├── JsonArrayBP.h │ ├── JsonTypes.h │ ├── JsonFactory.h │ ├── JsonValueBP.h │ └── JsonObjectBP.h │ ├── Private │ ├── JsonTypes.cpp │ ├── JsonUE4.cpp │ ├── JsonItemBP.cpp │ ├── JsonArrayBP.cpp │ ├── JsonFactory.cpp │ ├── JsonValueBP.cpp │ └── JsonObjectBP.cpp │ └── JsonUE4.Build.cs ├── .gitignore ├── .gitattributes ├── JsonUE4.uplugin ├── LICENSE └── README.md /Config/FilterPlugin.ini: -------------------------------------------------------------------------------- 1 | [FilterPlugin] 2 | -------------------------------------------------------------------------------- /Source/JsonUE4/Public/JsonUE4.h: -------------------------------------------------------------------------------- 1 | // Copyright Playspace S.L. 2017 2 | 3 | #pragma once 4 | 5 | // Logs and some main stuff 6 | #include "JsonUE4Globals.h" 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | Binaries 2 | DerivedDataCache 3 | Intermediate 4 | Saved 5 | *.VC.db 6 | *.opensdf 7 | *.opendb 8 | *.sdf 9 | *.sln 10 | *.suo 11 | *.xcodeproj 12 | *.xcworkspace 13 | -------------------------------------------------------------------------------- /Source/JsonUE4/Public/JsonUE4Globals.h: -------------------------------------------------------------------------------- 1 | // Copyright Playspace S.L. 2017 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | 7 | JSONUE4_API DECLARE_LOG_CATEGORY_EXTERN(LogJsonUE4, Log, All); 8 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | Content/** filter=lfs diff=lfs merge=lfs -text 2 | *.lib filter=lfs diff=lfs merge=lfs -text 3 | *.a filter=lfs diff=lfs merge=lfs -text 4 | *.pdb filter=lfs diff=lfs merge=lfs -text 5 | *.dll filter=lfs diff=lfs merge=lfs -text 6 | *.exe filter=lfs diff=lfs merge=lfs -text 7 | *.zip filter=lfs diff=lfs merge=lfs -text 8 | *.png filter=lfs diff=lfs merge=lfs -text 9 | -------------------------------------------------------------------------------- /Source/JsonUE4/Private/JsonTypes.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Playspace S.L. 2017 2 | 3 | #include "JsonTypes.h" 4 | 5 | typedef TJsonWriterFactory< TCHAR, TCondensedJsonPrintPolicy > FCondensedJsonStringWriterFactory; 6 | typedef TJsonWriter< TCHAR, TCondensedJsonPrintPolicy > FCondensedJsonStringWriter; 7 | 8 | typedef TJsonWriterFactory< TCHAR, TPrettyJsonPrintPolicy > FPrettyJsonStringWriterFactory; 9 | typedef TJsonWriter< TCHAR, TPrettyJsonPrintPolicy > FPrettyJsonStringWriter; 10 | -------------------------------------------------------------------------------- /JsonUE4.uplugin: -------------------------------------------------------------------------------- 1 | { 2 | "FileVersion" : 3, 3 | "Version" : 1, 4 | "VersionName" : "1.0", 5 | "FriendlyName" : "JsonUE4", 6 | "Description" : "JSON extension for UE4", 7 | "Category" : "Gameplay", 8 | "CreatedBy" : "Playspace S.L.", 9 | "CreatedByURL" : "https://www.playspace.com", 10 | "DocsURL" : "", 11 | "MarketplaceURL" : "", 12 | "SupportURL" : "", 13 | "EnabledByDefault" : true, 14 | "CanContainContent" : true, 15 | "IsBetaVersion" : false, 16 | "Installed" : false, 17 | "Modules": [ 18 | { 19 | "Name": "JsonUE4", 20 | "Type": "Runtime", 21 | "LoadingPhase": "PreDefault" 22 | } 23 | ] 24 | } 25 | -------------------------------------------------------------------------------- /Source/JsonUE4/Private/JsonUE4.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Playspace S.L. 2017 2 | 3 | #include "JsonUE4.h" 4 | #include "CoreMinimal.h" 5 | #include "Modules/ModuleManager.h" 6 | #include "IJsonUE4.h" 7 | #include "JsonUE4Globals.h" 8 | 9 | DEFINE_LOG_CATEGORY(LogJsonUE4); 10 | 11 | #define LOCTEXT_NAMESPACE "FJsonUE4" 12 | 13 | class FJsonUE4 : public IJsonUE4 14 | { 15 | public: 16 | 17 | /** IModuleInterface implementation */ 18 | virtual void StartupModule() override 19 | { 20 | 21 | } 22 | 23 | virtual void ShutdownModule() override 24 | { 25 | 26 | } 27 | }; 28 | 29 | IMPLEMENT_MODULE(FJsonUE4, JsonUE4) 30 | 31 | #undef LOCTEXT_NAMESPACE 32 | -------------------------------------------------------------------------------- /Source/JsonUE4/Private/JsonItemBP.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Playspace S.L. 2017 2 | 3 | #include "JsonItemBP.h" 4 | #include "JsonValueBP.h" 5 | #include "JsonTypes.h" 6 | #include "Serialization/JsonSerializer.h" 7 | 8 | // 9 | // JsonItem Wrapper 10 | // 11 | 12 | UJsonItemBP::UJsonItemBP() 13 | : Super() 14 | { 15 | Clear(); 16 | } 17 | 18 | void UJsonItemBP::Clear() 19 | { 20 | 21 | } 22 | 23 | bool UJsonItemBP::SetValue(FJsonValuePtr JsonValue) 24 | { 25 | return false; 26 | } 27 | 28 | FString UJsonItemBP::Encode(bool bPretty /* = false*/) const 29 | { 30 | return FString(); 31 | } 32 | 33 | bool UJsonItemBP::Decode(const FString& JsonString) 34 | { 35 | Clear(); 36 | UE_LOG(LogJsonUE4, Error, TEXT("Failed to decode from JSON string for: %s"), *JsonString); 37 | return false; 38 | } -------------------------------------------------------------------------------- /Source/JsonUE4/Public/IJsonUE4.h: -------------------------------------------------------------------------------- 1 | // Copyright Playspace S.L. 2017 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "Modules/ModuleInterface.h" 7 | 8 | 9 | /** 10 | * The public interface to this module. In most cases, this interface is only public to sibling modules 11 | * within this plugin. 12 | */ 13 | class IJsonUE4 : public IModuleInterface 14 | { 15 | 16 | public: 17 | 18 | /** 19 | * Singleton-like access to this module's interface. This is just for convenience! 20 | * Beware of calling this during the shutdown phase, though. Your module might have been unloaded already. 21 | * 22 | * @return Returns singleton instance, loading the module on demand if needed 23 | */ 24 | static inline IJsonUE4& Get() 25 | { 26 | return FModuleManager::LoadModuleChecked("JsonUE4"); 27 | } 28 | 29 | }; 30 | 31 | -------------------------------------------------------------------------------- /Source/JsonUE4/JsonUE4.Build.cs: -------------------------------------------------------------------------------- 1 | // Copyright Playspace S.L. 2017 2 | 3 | using UnrealBuildTool; 4 | 5 | namespace UnrealBuildTool.Rules 6 | { 7 | public class JsonUE4 : ModuleRules 8 | { 9 | public JsonUE4(ReadOnlyTargetRules Target) : base(Target) 10 | { 11 | PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs; 12 | PrivateIncludePaths.Add("JsonUE4/Private"); 13 | 14 | PrivateDependencyModuleNames.AddRange( 15 | new string[] { 16 | "Core", 17 | "CoreUObject", 18 | "Engine", 19 | "Json" 20 | }); 21 | 22 | PrivateIncludePathModuleNames.AddRange( 23 | new string[] { 24 | } 25 | ); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Source/JsonUE4/Public/JsonItemBP.h: -------------------------------------------------------------------------------- 1 | // Copyright Playspace S.L. 2017 2 | #pragma once 3 | 4 | #include "CoreMinimal.h" 5 | #include "UObject/ObjectMacros.h" 6 | #include "UObject/Object.h" 7 | #include "JsonTypes.h" 8 | #include "JsonItemBP.generated.h" 9 | 10 | class UJsonValueBP; 11 | 12 | UCLASS(BlueprintType, Blueprintable) 13 | class JSONUE4_API UJsonItemBP : public UObject 14 | { 15 | GENERATED_BODY() 16 | public: 17 | UJsonItemBP(); 18 | 19 | /** Clear the whole object */ 20 | UFUNCTION(BlueprintCallable, Category = "Json") 21 | virtual void Clear(); 22 | 23 | virtual bool SetValue(FJsonValuePtr JsonValue); 24 | 25 | /** Encode into a JSON string*/ 26 | UFUNCTION(BlueprintCallable, Category = "Json") 27 | virtual FString Encode(bool bPretty = false) const; 28 | 29 | /** Decode from a JSON string */ 30 | UFUNCTION(BlueprintCallable, Category = "Json") 31 | virtual bool Decode(const FString& JsonString); 32 | }; 33 | -------------------------------------------------------------------------------- /Source/JsonUE4/Public/JsonArrayBP.h: -------------------------------------------------------------------------------- 1 | // Copyright Playspace S.L. 2017 2 | 3 | #pragma once 4 | 5 | #include "CoreMinimal.h" 6 | #include "UObject/ObjectMacros.h" 7 | #include "JsonTypes.h" 8 | #include "JsonItemBP.h" 9 | #include "JsonArrayBP.generated.h" 10 | 11 | class UJsonValueBP; 12 | 13 | UCLASS(BlueprintType, Blueprintable) 14 | class JSONUE4_API UJsonArrayBP : public UJsonItemBP 15 | { 16 | GENERATED_BODY() 17 | public: 18 | UJsonArrayBP(); 19 | 20 | /** Clear the whole object */ 21 | virtual void Clear() override; 22 | 23 | virtual bool SetValue(FJsonValuePtr JsonValue) override; 24 | FJsonValuePtr GetValue(); 25 | 26 | /** Encode into a JSON string*/ 27 | virtual FString Encode(bool bPretty = false) const override; 28 | 29 | /** Decode from a JSON string */ 30 | virtual bool Decode(const FString& JsonString) override; 31 | 32 | /** Get the JsonValue as an array */ 33 | UFUNCTION(BlueprintCallable, Category = "Json") 34 | TArray GetArray() const; 35 | 36 | private: 37 | FJsonValuePtr JsonValuePtr; 38 | }; 39 | -------------------------------------------------------------------------------- /Source/JsonUE4/Public/JsonTypes.h: -------------------------------------------------------------------------------- 1 | // Copyright Playspace S.L. 2017 2 | #pragma once 3 | 4 | #include "CoreMinimal.h" 5 | #include "UObject/ObjectMacros.h" 6 | #include "Dom/JsonValue.h" 7 | #include "Dom/JsonObject.h" 8 | #include "Policies/CondensedJsonPrintPolicy.h" 9 | #include "Serialization/JsonWriter.h" 10 | #include "JsonTypes.generated.h" 11 | 12 | typedef TJsonWriterFactory< TCHAR, TCondensedJsonPrintPolicy > FCondensedJsonStringWriterFactory; 13 | typedef TJsonWriter< TCHAR, TCondensedJsonPrintPolicy > FCondensedJsonStringWriter; 14 | 15 | typedef TJsonWriterFactory< TCHAR, TPrettyJsonPrintPolicy > FPrettyJsonStringWriterFactory; 16 | typedef TJsonWriter< TCHAR, TPrettyJsonPrintPolicy > FPrettyJsonStringWriter; 17 | 18 | typedef TSharedPtr FJsonObjectPtr; 19 | typedef TSharedPtr FJsonValuePtr; 20 | 21 | /** Enum to specify distortion mesh size */ 22 | UENUM() 23 | enum class EJsonType : uint8 24 | { 25 | None, 26 | Null, 27 | String, 28 | Number, 29 | Boolean, 30 | Array, 31 | Object 32 | }; 33 | 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Playspace S.L. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JSON for UE4 2 | 3 | UE4 Blueprint JSON bindings. 4 | 5 | License 6 | ---- 7 | 8 | The MIT License (MIT) 9 | 10 | Copyright (c) 2017 Playspace S.L. 11 | 12 | Permission is hereby granted, free of charge, to any person obtaining a copy 13 | of this software and associated documentation files (the "Software"), to deal 14 | in the Software without restriction, including without limitation the rights 15 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 | copies of the Software, and to permit persons to whom the Software is 17 | furnished to do so, subject to the following conditions: 18 | 19 | The above copyright notice and this permission notice shall be included in all 20 | copies or substantial portions of the Software. 21 | 22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 28 | SOFTWARE. 29 | -------------------------------------------------------------------------------- /Source/JsonUE4/Public/JsonFactory.h: -------------------------------------------------------------------------------- 1 | // Copyright Playspace S.L. 2017 2 | #pragma once 3 | 4 | #include "CoreMinimal.h" 5 | #include "UObject/ObjectMacros.h" 6 | #include "Kismet/BlueprintFunctionLibrary.h" 7 | #include "JsonObjectBP.h" 8 | #include "JsonItemBP.h" 9 | #include "JsonArrayBP.h" 10 | #include "JsonFactory.generated.h" 11 | 12 | UCLASS() 13 | class JSONUE4_API UJsonFactory : public UBlueprintFunctionLibrary 14 | { 15 | GENERATED_BODY() 16 | 17 | public: 18 | 19 | UFUNCTION(BlueprintPure, meta = (DisplayName = "New Json Object", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "Json") 20 | static UJsonObjectBP* NewJsonObject_K2(UObject* WorldContextObject); 21 | static UJsonObjectBP* NewJsonObject(); 22 | 23 | UFUNCTION(BlueprintPure, meta = (DisplayName = "New Json Object (From String)", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "Json") 24 | static UJsonObjectBP* NewJsonObjectFromString_K2(UObject* WorldContextObject, const FString& JsonString); 25 | static UJsonObjectBP* NewJsonObjectFromString(const FString& JsonString); 26 | 27 | UFUNCTION(BlueprintPure, meta = (DisplayName = "New Json Array", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "Json") 28 | static UJsonArrayBP* NewJsonArray_K2(UObject* WorldContextObject); 29 | static UJsonArrayBP* NewJsonArray(); 30 | 31 | UFUNCTION(BlueprintPure, meta = (DisplayName = "New Json Array (From String)", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "Json") 32 | static UJsonArrayBP* NewJsonArrayFromString_K2(UObject* WorldContextObject, const FString& JsonString); 33 | static UJsonArrayBP* NewJsonArrayFromString(const FString& JsonString); 34 | 35 | UFUNCTION(BlueprintPure, meta = (DisplayName = "Parse Json Item", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "Json") 36 | static UJsonItemBP* ParseJsonItem_K2(UObject* WorldContextObject, const FString& JsonString); 37 | static UJsonItemBP* ParseJsonItem(const FString& JsonString); 38 | 39 | static UJsonItemBP* NewJsonItem(FJsonValuePtr JsonValue, UObject* WorldContextObject = (UObject*)GetTransientPackage()); 40 | }; 41 | 42 | -------------------------------------------------------------------------------- /Source/JsonUE4/Private/JsonArrayBP.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Playspace S.L. 2017 2 | 3 | #include "JsonArrayBP.h" 4 | #include "JsonValueBP.h" 5 | #include "JsonTypes.h" 6 | #include "Serialization/JsonSerializer.h" 7 | #include "JsonFactory.h" 8 | 9 | // 10 | // JsonObject Wrapper 11 | // 12 | 13 | UJsonArrayBP::UJsonArrayBP() 14 | : Super() 15 | { 16 | } 17 | 18 | void UJsonArrayBP::Clear() 19 | { 20 | if (JsonValuePtr.IsValid()) 21 | { 22 | JsonValuePtr.Reset(); 23 | } 24 | } 25 | 26 | bool UJsonArrayBP::SetValue(FJsonValuePtr JsonValue) 27 | { 28 | if (JsonValue.IsValid() && JsonValue->Type == EJson::Array) 29 | { 30 | JsonValuePtr = JsonValue; 31 | return true; 32 | } 33 | return false; 34 | } 35 | 36 | FJsonValuePtr UJsonArrayBP::GetValue() 37 | { 38 | return JsonValuePtr; 39 | } 40 | 41 | FString UJsonArrayBP::Encode(bool bPretty /* = false*/) const 42 | { 43 | if (!JsonValuePtr.IsValid()) 44 | { 45 | return TEXT("[]"); 46 | } 47 | 48 | FString OutputString; 49 | if (bPretty) 50 | { 51 | auto Writer = FPrettyJsonStringWriterFactory::Create(&OutputString); 52 | FJsonSerializer::Serialize(JsonValuePtr->AsArray(), Writer); 53 | } 54 | else 55 | { 56 | auto Writer = FCondensedJsonStringWriterFactory::Create(&OutputString); 57 | FJsonSerializer::Serialize(JsonValuePtr->AsArray(), Writer); 58 | } 59 | 60 | return OutputString; 61 | } 62 | 63 | bool UJsonArrayBP::Decode(const FString& JsonString) 64 | { 65 | auto Reader = TJsonReaderFactory<>::Create(*JsonString); 66 | FJsonValuePtr NewJsonValuePtr; 67 | if (FJsonSerializer::Deserialize(Reader, NewJsonValuePtr) && NewJsonValuePtr.IsValid() && NewJsonValuePtr->Type == EJson::Array) 68 | { 69 | SetValue(NewJsonValuePtr); 70 | return true; 71 | } 72 | Clear(); 73 | UE_LOG(LogJsonUE4, Error, TEXT("Failed to decode Array from JSON string for: %s"), *JsonString); 74 | return false; 75 | } 76 | 77 | TArray UJsonArrayBP::GetArray() const 78 | { 79 | TArray Result; 80 | if (JsonValuePtr.IsValid()) 81 | { 82 | auto Values = JsonValuePtr->AsArray(); 83 | for (auto Value : Values) 84 | { 85 | auto Item = UJsonFactory::NewJsonItem(Value); 86 | if (Item != nullptr) 87 | { 88 | Result.Add(Item); 89 | } 90 | } 91 | } 92 | return Result; 93 | } 94 | -------------------------------------------------------------------------------- /Source/JsonUE4/Public/JsonValueBP.h: -------------------------------------------------------------------------------- 1 | // Copyright Playspace S.L. 2017 2 | #pragma once 3 | 4 | #include "CoreMinimal.h" 5 | #include "UObject/ObjectMacros.h" 6 | #include "UObject/Object.h" 7 | #include "JsonUE4Globals.h" 8 | #include "JsonTypes.h" 9 | #include "JsonObjectBP.h" 10 | #include "JsonArrayBP.h" 11 | #include "JsonValueBP.generated.h" 12 | 13 | 14 | class UJsonObjectBP; 15 | 16 | UCLASS(BlueprintType, Blueprintable) 17 | class JSONUE4_API UJsonValueBP : public UObject 18 | { 19 | GENERATED_BODY() 20 | public: 21 | UJsonValueBP(); 22 | 23 | FJsonValuePtr& GetValue(); 24 | void SetValue(FJsonValuePtr& JsonValue); 25 | 26 | // 27 | // JSON API 28 | // 29 | 30 | /** Type of value */ 31 | UFUNCTION(BlueprintCallable, Category = "Json") 32 | EJsonType GetType() const; 33 | 34 | /** Type of value */ 35 | UFUNCTION(BlueprintCallable, Category = "Json") 36 | FName GetTypeAsName() const; 37 | 38 | /** Ceck if the type is of value None */ 39 | UFUNCTION(BlueprintCallable, Category = "Json") 40 | bool IsNone() const; 41 | 42 | /** Ceck if the type is of value Null */ 43 | UFUNCTION(BlueprintCallable, Category = "Json") 44 | bool IsNull() const; 45 | 46 | /** Ceck if the type is of value String */ 47 | UFUNCTION(BlueprintCallable, Category = "Json") 48 | bool IsString() const; 49 | 50 | /** Ceck if the type is of value Number */ 51 | UFUNCTION(BlueprintCallable, Category = "Json") 52 | bool IsNumber() const; 53 | 54 | /** Ceck if the type is of value Boolean */ 55 | UFUNCTION(BlueprintCallable, Category = "Json") 56 | bool IsBool() const; 57 | 58 | /** Ceck if the type is of value Array */ 59 | UFUNCTION(BlueprintCallable, Category = "Json") 60 | bool IsArray() const; 61 | 62 | /** Ceck if the type is of value JsonObject */ 63 | UFUNCTION(BlueprintCallable, Category = "Json") 64 | bool IsObject() const; 65 | 66 | /** Get the JsonValue as a string */ 67 | UFUNCTION(BlueprintCallable, Category = "Json") 68 | FString AsString() const; 69 | 70 | /** Get the JsonValue as a number */ 71 | UFUNCTION(BlueprintCallable, Category = "Json") 72 | float AsNumber() const; 73 | 74 | /** Get the JsonValue as a boolean */ 75 | UFUNCTION(BlueprintCallable, Category = "Json") 76 | bool AsBool() const; 77 | 78 | /** Get the JsonValue as an array */ 79 | UFUNCTION(BlueprintCallable, Category = "Json") 80 | UJsonArrayBP* AsArray() const; 81 | 82 | /** Get the JsonValue as an object */ 83 | UFUNCTION(BlueprintCallable, Category = "Json") 84 | UJsonObjectBP* AsObject() const; 85 | 86 | private: 87 | FJsonValuePtr JsonPtr; 88 | }; 89 | -------------------------------------------------------------------------------- /Source/JsonUE4/Private/JsonFactory.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Playspace S.L. 2017 2 | 3 | #include "JsonFactory.h" 4 | #include "JsonObjectBP.h" 5 | #include "JsonSerializer.h" 6 | #include "JsonArrayBP.h" 7 | 8 | // 9 | // JsonFactory 10 | // 11 | 12 | UJsonObjectBP* UJsonFactory::NewJsonObject_K2(UObject* WorldContextObject) 13 | { 14 | return NewObject(WorldContextObject); 15 | } 16 | 17 | UJsonObjectBP* UJsonFactory::NewJsonObject() 18 | { 19 | return NewObject(); 20 | } 21 | 22 | UJsonObjectBP* UJsonFactory::NewJsonObjectFromString_K2(UObject* WorldContextObject, const FString& JsonString) 23 | { 24 | auto JsonObj = NewJsonObject_K2(WorldContextObject); 25 | JsonObj->Decode(JsonString); 26 | return JsonObj; 27 | } 28 | 29 | UJsonObjectBP* UJsonFactory::NewJsonObjectFromString(const FString& JsonString) 30 | { 31 | auto JsonObj = NewJsonObject(); 32 | JsonObj->Decode(JsonString); 33 | return JsonObj; 34 | } 35 | 36 | UJsonArrayBP* UJsonFactory::NewJsonArray_K2(UObject* WorldContextObject) 37 | { 38 | return NewObject(WorldContextObject); 39 | } 40 | 41 | UJsonArrayBP* UJsonFactory::NewJsonArray() 42 | { 43 | return NewObject(); 44 | } 45 | 46 | UJsonArrayBP* UJsonFactory::NewJsonArrayFromString_K2(UObject* WorldContextObject, const FString& JsonString) 47 | { 48 | auto JsonArray = NewJsonArray_K2(WorldContextObject); 49 | JsonArray->Decode(JsonString); 50 | return JsonArray; 51 | } 52 | 53 | UJsonArrayBP* UJsonFactory::NewJsonArrayFromString(const FString& JsonString) 54 | { 55 | auto JsonArray = NewJsonArray(); 56 | JsonArray->Decode(JsonString); 57 | return JsonArray; 58 | } 59 | 60 | UJsonItemBP* UJsonFactory::ParseJsonItem_K2(UObject* WorldContextObject, const FString& JsonString) 61 | { 62 | auto Reader = TJsonReaderFactory<>::Create(*JsonString); 63 | FJsonValuePtr JsonValuePtr; 64 | if (FJsonSerializer::Deserialize(Reader, JsonValuePtr) && JsonValuePtr.IsValid()) 65 | { 66 | UJsonItemBP* item = nullptr; 67 | if (JsonValuePtr->Type == EJson::Object) 68 | { 69 | item = NewJsonObject_K2(WorldContextObject); 70 | } 71 | else if (JsonValuePtr->Type == EJson::Array) 72 | { 73 | item = NewJsonArray_K2(WorldContextObject); 74 | } 75 | 76 | if (item != nullptr) 77 | { 78 | item->SetValue(JsonValuePtr); 79 | return item; 80 | } 81 | } 82 | return nullptr; 83 | } 84 | 85 | UJsonItemBP* UJsonFactory::ParseJsonItem(const FString& JsonString) 86 | { 87 | auto Reader = TJsonReaderFactory<>::Create(*JsonString); 88 | FJsonValuePtr JsonValuePtr; 89 | if (FJsonSerializer::Deserialize(Reader, JsonValuePtr) && JsonValuePtr.IsValid()) 90 | { 91 | return NewJsonItem(JsonValuePtr); 92 | } 93 | return nullptr; 94 | } 95 | 96 | UJsonItemBP* UJsonFactory::NewJsonItem(FJsonValuePtr JsonValue, UObject* WorldContextObject /*= (UObject*)GetTransientPackage()*/) 97 | { 98 | if (JsonValue.IsValid()) 99 | { 100 | UJsonItemBP* Item = nullptr; 101 | if (JsonValue->Type == EJson::Object) 102 | { 103 | Item = NewJsonObject_K2(WorldContextObject); 104 | } 105 | else if (JsonValue->Type == EJson::Array) 106 | { 107 | Item = NewJsonArray_K2(WorldContextObject); 108 | } 109 | 110 | if (Item != nullptr) 111 | { 112 | Item->SetValue(JsonValue); 113 | return Item; 114 | } 115 | } 116 | return nullptr; 117 | } 118 | -------------------------------------------------------------------------------- /Source/JsonUE4/Private/JsonValueBP.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Playspace S.L. 2017 2 | 3 | #include "JsonValueBP.h" 4 | #include "JsonObjectBP.h" 5 | #include "JsonFactory.h" 6 | 7 | // 8 | // JsonValue Wrapper 9 | // 10 | 11 | UJsonValueBP::UJsonValueBP() 12 | : Super() 13 | { 14 | 15 | } 16 | 17 | TSharedPtr& UJsonValueBP::GetValue() 18 | { 19 | return JsonPtr; 20 | } 21 | 22 | void UJsonValueBP::SetValue(TSharedPtr& JsonValue) 23 | { 24 | JsonPtr = JsonValue; 25 | } 26 | 27 | EJsonType UJsonValueBP::GetType() const 28 | { 29 | if (JsonPtr.IsValid()) 30 | { 31 | switch (JsonPtr->Type) 32 | { 33 | case EJson::None: return EJsonType::None; 34 | case EJson::Null: return EJsonType::Null; 35 | case EJson::String: return EJsonType::String; 36 | case EJson::Number: return EJsonType::Number; 37 | case EJson::Boolean: return EJsonType::Boolean; 38 | case EJson::Array: return EJsonType::Array; 39 | case EJson::Object: return EJsonType::Object; 40 | default: return EJsonType::None; 41 | } 42 | } 43 | return EJsonType::None; 44 | } 45 | 46 | FName UJsonValueBP::GetTypeAsName() const 47 | { 48 | static FName InvalidName(TEXT("None")); 49 | static FName NullName(TEXT("Null")); 50 | static FName StringName(TEXT("String")); 51 | static FName NumberName(TEXT("Number")); 52 | static FName BooleanName(TEXT("Boolean")); 53 | static FName ArrayName(TEXT("Array")); 54 | static FName ObjectName(TEXT("Object")); 55 | if (JsonPtr.IsValid()) 56 | { 57 | switch (JsonPtr->Type) 58 | { 59 | case EJson::None: return InvalidName; 60 | case EJson::Null: return NullName; 61 | case EJson::String: return StringName; 62 | case EJson::Number: return NumberName; 63 | case EJson::Boolean: return BooleanName; 64 | case EJson::Array: return ArrayName; 65 | case EJson::Object: return ObjectName; 66 | default: return InvalidName; 67 | } 68 | } 69 | return InvalidName; 70 | } 71 | 72 | bool UJsonValueBP::IsNone() const 73 | { 74 | return !JsonPtr.IsValid() || JsonPtr->Type == EJson::None; 75 | } 76 | 77 | bool UJsonValueBP::IsNull() const 78 | { 79 | return !JsonPtr.IsValid() || JsonPtr->IsNull(); 80 | } 81 | 82 | bool UJsonValueBP::IsString() const 83 | { 84 | return JsonPtr.IsValid() && JsonPtr->Type == EJson::String; 85 | } 86 | 87 | bool UJsonValueBP::IsNumber() const 88 | { 89 | return JsonPtr.IsValid() && JsonPtr->Type == EJson::Number; 90 | } 91 | 92 | bool UJsonValueBP::IsBool() const 93 | { 94 | return JsonPtr.IsValid() && JsonPtr->Type == EJson::Boolean; 95 | } 96 | 97 | bool UJsonValueBP::IsArray() const 98 | { 99 | return JsonPtr.IsValid() && JsonPtr->Type == EJson::Array; 100 | } 101 | 102 | bool UJsonValueBP::IsObject() const 103 | { 104 | return JsonPtr.IsValid() && JsonPtr->Type == EJson::Object; 105 | } 106 | 107 | FString UJsonValueBP::AsString() const 108 | { 109 | return JsonPtr.IsValid() ? JsonPtr->AsString() : FString(); 110 | } 111 | 112 | float UJsonValueBP::AsNumber() const 113 | { 114 | return JsonPtr.IsValid() ? JsonPtr->AsNumber() : 0.0f; 115 | } 116 | 117 | bool UJsonValueBP::AsBool() const 118 | { 119 | return JsonPtr.IsValid() ? JsonPtr->AsBool() : false; 120 | } 121 | 122 | UJsonArrayBP* UJsonValueBP::AsArray() const 123 | { 124 | TArray Result; 125 | if (JsonPtr.IsValid() && IsArray()) 126 | { 127 | auto NewJsonArray = UJsonFactory::NewJsonArray(); 128 | NewJsonArray->SetValue(JsonPtr); 129 | return NewJsonArray; 130 | } 131 | return nullptr; 132 | } 133 | 134 | UJsonObjectBP* UJsonValueBP::AsObject() const 135 | { 136 | if (JsonPtr.IsValid() && IsObject()) 137 | { 138 | auto NewJsonObject = UJsonFactory::NewJsonObject(); 139 | NewJsonObject->SetValue(JsonPtr); 140 | return NewJsonObject; 141 | } 142 | return nullptr; 143 | } 144 | -------------------------------------------------------------------------------- /Source/JsonUE4/Public/JsonObjectBP.h: -------------------------------------------------------------------------------- 1 | // Copyright Playspace S.L. 2017 2 | #pragma once 3 | 4 | #include "CoreMinimal.h" 5 | #include "UObject/ObjectMacros.h" 6 | #include "JsonTypes.h" 7 | #include "JsonItemBP.h" 8 | #include "JsonObjectBP.generated.h" 9 | 10 | class UJsonValueBP; 11 | 12 | UCLASS(BlueprintType, Blueprintable) 13 | class JSONUE4_API UJsonObjectBP : public UJsonItemBP 14 | { 15 | GENERATED_BODY() 16 | public: 17 | UJsonObjectBP(); 18 | 19 | /** Clear the whole object */ 20 | virtual void Clear() override; 21 | 22 | virtual bool SetValue(FJsonValuePtr JsonValue) override; 23 | FJsonObjectPtr GetObject(); 24 | void SetObject(FJsonObjectPtr JsonObject); 25 | 26 | /** Encode into a JSON string*/ 27 | virtual FString Encode(bool bPretty = false) const override; 28 | 29 | /** Decode from a JSON string */ 30 | virtual bool Decode(const FString& JsonString) override; 31 | 32 | // 33 | // JSON API 34 | // 35 | 36 | /** Returns the list of keys the JSON object holds */ 37 | UFUNCTION(BlueprintCallable, Category = "Json") 38 | TArray GetKeys() const; 39 | 40 | /** Returns the number of keys the JSON object holds */ 41 | UFUNCTION(BlueprintCallable, Category = "Json") 42 | int32 Num() const; 43 | 44 | UFUNCTION(BlueprintCallable, Category = "Json") 45 | bool HasField(const FString& FieldName) const; 46 | 47 | UFUNCTION(BlueprintCallable, Category = "Json") 48 | void RemoveField(const FString& FieldName); 49 | 50 | UFUNCTION(BlueprintCallable, Category = "Json") 51 | UJsonValueBP* GetField(const FString& FieldName) const; 52 | 53 | UFUNCTION(BlueprintCallable, Category = "Json") 54 | UJsonValueBP* GetFieldRecursive(const FString& FieldName); 55 | 56 | UFUNCTION(BlueprintCallable, Category = "Json") 57 | void SetField(const FString& FieldName, UJsonValueBP* JsonValue); 58 | 59 | UFUNCTION(BlueprintCallable, Category = "Json") 60 | UJsonValueBP* AsValue() const; 61 | 62 | // 63 | // Field Access API 64 | // 65 | 66 | /** Set a Float value */ 67 | UFUNCTION(BlueprintCallable, Category = "Json") 68 | float GetFloat(const FString& FieldName, float DefaultValue) const; 69 | 70 | /** Get a float value */ 71 | UFUNCTION(BlueprintCallable, Category = "Json") 72 | UJsonObjectBP* SetFloat(const FString& FieldName, float Value); 73 | 74 | /** Get Int value */ 75 | UFUNCTION(BlueprintCallable, Category = "Json") 76 | int32 GetInt(const FString& FieldName, int32 DefaultValue) const; 77 | 78 | /** Set Int value */ 79 | UFUNCTION(BlueprintCallable, Category = "Json") 80 | UJsonObjectBP* SetInt(const FString& FieldName, int32 Value); 81 | 82 | /** Get String value */ 83 | UFUNCTION(BlueprintCallable, Category = "Json") 84 | FString GetString(const FString& FieldName, const FString& DefaultValue) const; 85 | 86 | /** Set String value */ 87 | UFUNCTION(BlueprintCallable, Category = "Json") 88 | UJsonObjectBP* SetString(const FString& FieldName, const FString& Value); 89 | 90 | /** Get bool value */ 91 | UFUNCTION(BlueprintCallable, Category = "Json") 92 | bool GetBool(const FString& FieldName, bool DefaultValue) const; 93 | 94 | /** Set bool value */ 95 | UFUNCTION(BlueprintCallable, Category = "Json") 96 | UJsonObjectBP* SetBool(const FString& FieldName, bool Value); 97 | 98 | /** Get JsonObject value */ 99 | UFUNCTION(BlueprintCallable, Category = "Json") 100 | UJsonObjectBP* GetObject(const FString& FieldName, UJsonObjectBP* DefaultValue) const; 101 | 102 | /** Set JsonObject value */ 103 | UFUNCTION(BlueprintCallable, Category = "Json") 104 | UJsonObjectBP* SetObject(const FString& FieldName, UJsonObjectBP* Value); 105 | 106 | // 107 | // Array properties 108 | // 109 | 110 | 111 | 112 | /** Get an array of float values */ 113 | UFUNCTION(BlueprintCallable, Category = "Json") 114 | TArray GetFloatArray(const FString& FieldName) const; 115 | 116 | /** Set an array of float values */ 117 | UFUNCTION(BlueprintCallable, Category = "Json") 118 | void SetFloatArray(const FString& FieldName, const TArray& Value); 119 | 120 | /** Get an array of string values */ 121 | UFUNCTION(BlueprintCallable, Category = "Json") 122 | TArray GetStringArray(const FString& FieldName) const; 123 | 124 | /** Set an array of string values */ 125 | UFUNCTION(BlueprintCallable, Category = "Json") 126 | void SetStringArray(const FString& FieldName, const TArray& Value); 127 | 128 | /** Get an array of bool values */ 129 | UFUNCTION(BlueprintCallable, Category = "Json") 130 | TArray GetBoolArray(const FString& FieldName) const; 131 | 132 | /** Set an array of bool values */ 133 | UFUNCTION(BlueprintCallable, Category = "Json") 134 | void SetBoolArray(const FString& FieldName, const TArray& Value); 135 | 136 | /** Get an array of JsonObject values */ 137 | UFUNCTION(BlueprintCallable, Category = "Json") 138 | TArray GetObjectArray(const FString& FieldName) const; 139 | 140 | /** Set an array of JsonObject values */ 141 | UFUNCTION(BlueprintCallable, Category = "Json") 142 | void SetObjectArray(const FString& FieldName, const TArray& Value); 143 | 144 | /** Get a JsonArray object */ 145 | UFUNCTION(BlueprintCallable, Category = "Json") 146 | UJsonArrayBP* GetArray(const FString& FieldName) const; 147 | 148 | /** Set a JsonArray object */ 149 | UFUNCTION(BlueprintCallable, Category = "Json") 150 | void SetArray(const FString& FieldName, UJsonArrayBP* Value); 151 | 152 | private: 153 | FJsonObjectPtr JsonPtr; 154 | }; 155 | -------------------------------------------------------------------------------- /Source/JsonUE4/Private/JsonObjectBP.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Playspace S.L. 2017 2 | 3 | #include "JsonObjectBP.h" 4 | #include "JsonValueBP.h" 5 | #include "JsonTypes.h" 6 | #include "JsonFactory.h" 7 | #include "Serialization/JsonSerializer.h" 8 | 9 | // 10 | // JsonObject Wrapper 11 | // 12 | 13 | UJsonObjectBP::UJsonObjectBP() 14 | : Super() 15 | { 16 | } 17 | 18 | void UJsonObjectBP::Clear() 19 | { 20 | if (JsonPtr.IsValid()) 21 | { 22 | JsonPtr.Reset(); 23 | } 24 | JsonPtr = MakeShareable(new FJsonObject()); 25 | } 26 | 27 | bool UJsonObjectBP::SetValue(FJsonValuePtr JsonValue) 28 | { 29 | if (JsonValue.IsValid() && JsonValue->Type == EJson::Object) 30 | { 31 | SetObject(JsonValue->AsObject()); 32 | return true; 33 | } 34 | return false; 35 | } 36 | 37 | TSharedPtr UJsonObjectBP::GetObject() 38 | { 39 | return JsonPtr; 40 | } 41 | 42 | void UJsonObjectBP::SetObject(TSharedPtr JsonObject) 43 | { 44 | JsonPtr = JsonObject; 45 | } 46 | 47 | FString UJsonObjectBP::Encode(bool bPretty /* = false*/) const 48 | { 49 | if (!JsonPtr.IsValid()) 50 | { 51 | return TEXT("{}"); 52 | } 53 | 54 | FString OutputString; 55 | if (bPretty) 56 | { 57 | auto Writer = FPrettyJsonStringWriterFactory::Create(&OutputString); 58 | FJsonSerializer::Serialize(JsonPtr.ToSharedRef(), Writer); 59 | } 60 | else 61 | { 62 | auto Writer = FCondensedJsonStringWriterFactory::Create(&OutputString); 63 | FJsonSerializer::Serialize(JsonPtr.ToSharedRef(), Writer); 64 | } 65 | 66 | return OutputString; 67 | } 68 | 69 | bool UJsonObjectBP::Decode(const FString& JsonString) 70 | { 71 | auto Reader = TJsonReaderFactory<>::Create(*JsonString); 72 | if (FJsonSerializer::Deserialize(Reader, JsonPtr) && JsonPtr.IsValid()) 73 | { 74 | return true; 75 | } 76 | Clear(); 77 | UE_LOG(LogJsonUE4, Error, TEXT("Failed to decode Object from JSON string for: %s"), *JsonString); 78 | return false; 79 | } 80 | 81 | TArray UJsonObjectBP::GetKeys() const 82 | { 83 | TArray Result; 84 | if (JsonPtr.IsValid()) 85 | { 86 | JsonPtr->Values.GetKeys(Result); 87 | } 88 | return Result; 89 | } 90 | 91 | int32 UJsonObjectBP::Num() const 92 | { 93 | return JsonPtr.IsValid() ? JsonPtr->Values.Num() : -1; 94 | } 95 | 96 | bool UJsonObjectBP::HasField(const FString& FieldName) const 97 | { 98 | return JsonPtr.IsValid() && !FieldName.IsEmpty() && JsonPtr->HasField(FieldName); 99 | } 100 | 101 | void UJsonObjectBP::RemoveField(const FString& FieldName) 102 | { 103 | if (JsonPtr.IsValid() && !FieldName.IsEmpty()) 104 | { 105 | JsonPtr->RemoveField(FieldName); 106 | } 107 | } 108 | 109 | UJsonValueBP* UJsonObjectBP::GetField(const FString& FieldName) const 110 | { 111 | if (JsonPtr.IsValid() && !FieldName.IsEmpty()) 112 | { 113 | TSharedPtr NewVal = JsonPtr->TryGetField(FieldName); 114 | if (NewVal.IsValid()) 115 | { 116 | UJsonValueBP* NewValue = NewObject(); 117 | NewValue->SetValue(NewVal); 118 | 119 | return NewValue; 120 | } 121 | } 122 | return nullptr; 123 | } 124 | 125 | UJsonValueBP* UJsonObjectBP::GetFieldRecursive(const FString& FieldName) 126 | { 127 | if (JsonPtr.IsValid() && !FieldName.IsEmpty()) 128 | { 129 | TArray KeyList; 130 | FieldName.ParseIntoArray(KeyList, TEXT("."), true); 131 | 132 | TSharedPtr CurrentValue = nullptr; 133 | TSharedPtr CurrentObject = JsonPtr; 134 | for (auto i = 0; i < KeyList.Num(); i++) 135 | { 136 | auto CurrentKey = KeyList[i]; 137 | 138 | // Get field 139 | CurrentValue = CurrentObject->TryGetField(CurrentKey); 140 | if (!CurrentValue.IsValid()) 141 | { 142 | return nullptr; 143 | } 144 | 145 | // Prepare for next field 146 | if (i < KeyList.Num() - 1) 147 | { 148 | // Get next 149 | CurrentObject = CurrentValue->AsObject(); 150 | if (!CurrentObject.IsValid()) 151 | { 152 | return nullptr; 153 | } 154 | } 155 | } 156 | if (CurrentValue.IsValid()) 157 | { 158 | UJsonValueBP* NewValue = NewObject(); 159 | NewValue->SetValue(CurrentValue); 160 | return NewValue; 161 | } 162 | } 163 | return nullptr; 164 | } 165 | 166 | void UJsonObjectBP::SetField(const FString& FieldName, UJsonValueBP* JsonValue) 167 | { 168 | if (JsonPtr.IsValid() && !FieldName.IsEmpty()) 169 | { 170 | JsonPtr->SetField(FieldName, JsonValue->GetValue()); 171 | } 172 | } 173 | 174 | UJsonValueBP* UJsonObjectBP::AsValue() const 175 | { 176 | if (JsonPtr.IsValid()) 177 | { 178 | auto newValue = NewObject(); 179 | FJsonValuePtr jsonValue = MakeShareable(new FJsonValueObject(JsonPtr)); 180 | newValue->SetValue(jsonValue); 181 | return newValue; 182 | } 183 | return nullptr; 184 | } 185 | 186 | float UJsonObjectBP::GetFloat(const FString& FieldName, float DefaultValue) const 187 | { 188 | if (JsonPtr.IsValid() && !FieldName.IsEmpty() && JsonPtr->HasTypedField(FieldName)) 189 | { 190 | return JsonPtr->GetNumberField(FieldName); 191 | } 192 | 193 | UE_LOG(LogJsonUE4, Warning, TEXT("No field with name %s of type Number"), *FieldName); 194 | return DefaultValue; 195 | } 196 | 197 | UJsonObjectBP* UJsonObjectBP::SetFloat(const FString& FieldName, float Value) 198 | { 199 | if (JsonPtr.IsValid() && !FieldName.IsEmpty()) 200 | { 201 | JsonPtr->SetNumberField(FieldName, Value); 202 | } 203 | return this; 204 | } 205 | 206 | int32 UJsonObjectBP::GetInt(const FString& FieldName, int32 DefaultValue) const 207 | { 208 | if (JsonPtr.IsValid() && !FieldName.IsEmpty() && JsonPtr->HasTypedField(FieldName)) 209 | { 210 | return JsonPtr->GetIntegerField(FieldName); 211 | } 212 | 213 | UE_LOG(LogJsonUE4, Warning, TEXT("No field with name %s of type Number"), *FieldName); 214 | return DefaultValue; 215 | } 216 | 217 | UJsonObjectBP* UJsonObjectBP::SetInt(const FString& FieldName, int32 Value) 218 | { 219 | if (JsonPtr.IsValid() && !FieldName.IsEmpty()) 220 | { 221 | JsonPtr->SetNumberField(FieldName, Value); 222 | } 223 | return this; 224 | } 225 | 226 | FString UJsonObjectBP::GetString(const FString& FieldName, const FString& DefaultValue) const 227 | { 228 | if (JsonPtr.IsValid() && !FieldName.IsEmpty() && JsonPtr->HasTypedField(FieldName)) 229 | { 230 | return JsonPtr->GetStringField(FieldName); 231 | } 232 | 233 | UE_LOG(LogJsonUE4, Warning, TEXT("No field with name %s of type String"), *FieldName); 234 | return DefaultValue; 235 | } 236 | 237 | UJsonObjectBP* UJsonObjectBP::SetString(const FString& FieldName, const FString& Value) 238 | { 239 | if (JsonPtr.IsValid() && !FieldName.IsEmpty()) 240 | { 241 | JsonPtr->SetStringField(FieldName, Value); 242 | } 243 | return this; 244 | } 245 | 246 | bool UJsonObjectBP::GetBool(const FString& FieldName, bool DefaultValue) const 247 | { 248 | if (JsonPtr.IsValid() && !FieldName.IsEmpty() && JsonPtr->HasTypedField(FieldName)) 249 | { 250 | return JsonPtr->GetBoolField(FieldName); 251 | } 252 | 253 | UE_LOG(LogJsonUE4, Warning, TEXT("No field with name %s of type Bool"), *FieldName); 254 | return DefaultValue; 255 | } 256 | 257 | UJsonObjectBP* UJsonObjectBP::SetBool(const FString& FieldName, bool Value) 258 | { 259 | if (JsonPtr.IsValid() && !FieldName.IsEmpty()) 260 | { 261 | JsonPtr->SetBoolField(FieldName, Value); 262 | } 263 | return this; 264 | } 265 | 266 | UJsonObjectBP* UJsonObjectBP::GetObject(const FString& FieldName, UJsonObjectBP* DefaultValue) const 267 | { 268 | if (JsonPtr.IsValid() && !FieldName.IsEmpty() && JsonPtr->HasTypedField(FieldName)) 269 | { 270 | auto JsonObjField = JsonPtr->GetObjectField(FieldName); 271 | auto JsonObj = NewObject(); 272 | JsonObj->SetObject(JsonObjField); 273 | return JsonObj; 274 | } 275 | 276 | UE_LOG(LogJsonUE4, Warning, TEXT("No field with name %s of type JsonObject"), *FieldName); 277 | return DefaultValue; 278 | } 279 | 280 | UJsonObjectBP* UJsonObjectBP::SetObject(const FString& FieldName, UJsonObjectBP* Value) 281 | { 282 | if (JsonPtr.IsValid() && !FieldName.IsEmpty() && Value != nullptr) 283 | { 284 | JsonPtr->SetObjectField(FieldName, Value->GetObject()); 285 | } 286 | return this; 287 | } 288 | 289 | TArray UJsonObjectBP::GetFloatArray(const FString& FieldName) const 290 | { 291 | TArray Result; 292 | if (JsonPtr.IsValid() && !FieldName.IsEmpty() && JsonPtr->HasTypedField(FieldName)) 293 | { 294 | auto JsonArrayValues = JsonPtr->GetArrayField(FieldName); 295 | for (TArray >::TConstIterator It(JsonArrayValues); It; ++It) 296 | { 297 | auto Value = (*It).Get(); 298 | if (Value->Type != EJson::Number) 299 | { 300 | UE_LOG(LogJsonUE4, Error, TEXT("NAN item in array %s"), *FieldName); 301 | continue; 302 | } 303 | 304 | Result.Add((*It)->AsNumber()); 305 | } 306 | } 307 | return Result; 308 | } 309 | 310 | void UJsonObjectBP::SetFloatArray(const FString& FieldName, const TArray& Value) 311 | { 312 | if (JsonPtr.IsValid() && !FieldName.IsEmpty()) 313 | { 314 | TArray< TSharedPtr > EntriesArray; 315 | for (auto Number : Value) 316 | { 317 | EntriesArray.Add(MakeShareable(new FJsonValueNumber(Number))); 318 | } 319 | JsonPtr->SetArrayField(FieldName, EntriesArray); 320 | } 321 | } 322 | 323 | TArray UJsonObjectBP::GetStringArray(const FString& FieldName) const 324 | { 325 | TArray Result; 326 | if (JsonPtr.IsValid() && !FieldName.IsEmpty() && JsonPtr->HasTypedField(FieldName)) 327 | { 328 | auto JsonArrayValues = JsonPtr->GetArrayField(FieldName); 329 | for (TArray >::TConstIterator It(JsonArrayValues); It; ++It) 330 | { 331 | auto Value = (*It).Get(); 332 | if (Value->Type != EJson::String) 333 | { 334 | UE_LOG(LogJsonUE4, Error, TEXT("Non string item in array %s"), *FieldName); 335 | continue; 336 | } 337 | 338 | Result.Add((*It)->AsString()); 339 | } 340 | } 341 | return Result; 342 | } 343 | 344 | void UJsonObjectBP::SetStringArray(const FString& FieldName, const TArray& Value) 345 | { 346 | if (JsonPtr.IsValid() && !FieldName.IsEmpty()) 347 | { 348 | TArray< TSharedPtr > EntriesArray; 349 | for (auto String : Value) 350 | { 351 | EntriesArray.Add(MakeShareable(new FJsonValueString(String))); 352 | } 353 | JsonPtr->SetArrayField(FieldName, EntriesArray); 354 | } 355 | } 356 | 357 | TArray UJsonObjectBP::GetBoolArray(const FString& FieldName) const 358 | { 359 | TArray Result; 360 | if (JsonPtr.IsValid() && !FieldName.IsEmpty() && JsonPtr->HasTypedField(FieldName)) 361 | { 362 | auto JsonArrayValues = JsonPtr->GetArrayField(FieldName); 363 | for (TArray >::TConstIterator It(JsonArrayValues); It; ++It) 364 | { 365 | auto Value = (*It).Get(); 366 | if (Value->Type != EJson::Boolean) 367 | { 368 | UE_LOG(LogJsonUE4, Error, TEXT("Non bool item in array %s"), *FieldName); 369 | continue; 370 | } 371 | 372 | Result.Add((*It)->AsBool()); 373 | } 374 | } 375 | return Result; 376 | } 377 | 378 | void UJsonObjectBP::SetBoolArray(const FString& FieldName, const TArray& Value) 379 | { 380 | if (JsonPtr.IsValid() && !FieldName.IsEmpty()) 381 | { 382 | TArray< TSharedPtr > EntriesArray; 383 | for (auto Bool : Value) 384 | { 385 | EntriesArray.Add(MakeShareable(new FJsonValueBoolean(Bool))); 386 | } 387 | JsonPtr->SetArrayField(FieldName, EntriesArray); 388 | } 389 | } 390 | 391 | TArray UJsonObjectBP::GetObjectArray(const FString& FieldName) const 392 | { 393 | TArray Result; 394 | if (JsonPtr.IsValid() && !FieldName.IsEmpty() && JsonPtr->HasTypedField(FieldName)) 395 | { 396 | auto JsonArrayValues = JsonPtr->GetArrayField(FieldName); 397 | for (TArray >::TConstIterator It(JsonArrayValues); It; ++It) 398 | { 399 | auto Value = (*It).Get(); 400 | if (Value->Type != EJson::Object) 401 | { 402 | UE_LOG(LogJsonUE4, Error, TEXT("Non JsonObject item in array %s"), *FieldName); 403 | continue; 404 | } 405 | 406 | auto JsonObjField = Value->AsObject(); 407 | auto JsonObj = NewObject(); 408 | JsonObj->SetObject(JsonObjField); 409 | 410 | Result.Add(JsonObj); 411 | } 412 | } 413 | return Result; 414 | } 415 | 416 | void UJsonObjectBP::SetObjectArray(const FString& FieldName, const TArray& Value) 417 | { 418 | if (JsonPtr.IsValid() && !FieldName.IsEmpty()) 419 | { 420 | TArray< TSharedPtr > EntriesArray; 421 | for (auto Object : Value) 422 | { 423 | EntriesArray.Add(MakeShareable(new FJsonValueObject(Object->GetObject()))); 424 | } 425 | JsonPtr->SetArrayField(FieldName, EntriesArray); 426 | } 427 | } 428 | 429 | UJsonArrayBP* UJsonObjectBP::GetArray(const FString& FieldName) const 430 | { 431 | if (JsonPtr.IsValid() && !FieldName.IsEmpty()) 432 | { 433 | 434 | TSharedPtr NewVal = JsonPtr->TryGetField(FieldName); 435 | if (NewVal.IsValid() && NewVal->Type == EJson::Array) 436 | { 437 | auto newArray = UJsonFactory::NewJsonArray(); 438 | newArray->SetValue(NewVal); 439 | return newArray; 440 | } 441 | } 442 | return nullptr; 443 | } 444 | 445 | void UJsonObjectBP::SetArray(const FString& FieldName, UJsonArrayBP* Value) 446 | { 447 | if (JsonPtr.IsValid() && !FieldName.IsEmpty() && Value->GetValue().IsValid()) 448 | { 449 | JsonPtr->SetArrayField(FieldName, Value->GetValue()->AsArray()); 450 | } 451 | } --------------------------------------------------------------------------------