From eb730eb147814c6692b6b65bdff44f971c74ac14 Mon Sep 17 00:00:00 2001 From: "v.alyamkin" Date: Thu, 17 Jan 2019 19:39:29 +0300 Subject: [PATCH 01/10] Format all with clang-format --- Source/.clang-format | 22 + .../Classes/VaRest_BreakJson.h | 7 +- .../Private/VaRestEditorPlugin.cpp | 2 +- .../Private/VaRest_BreakJson.cpp | 166 ++-- .../Public/VaRestEditorPlugin.h | 1 - .../VaRestPlugin/Classes/VaRestJsonObject.h | 19 +- Source/VaRestPlugin/Classes/VaRestJsonValue.h | 26 +- Source/VaRestPlugin/Classes/VaRestLibrary.h | 16 +- .../VaRestPlugin/Classes/VaRestRequestJSON.h | 60 +- Source/VaRestPlugin/Classes/VaRestSettings.h | 3 +- Source/VaRestPlugin/Classes/VaRestTypes.h | 2 + .../VaRestPlugin/Private/VaRestJsonObject.cpp | 129 ++- .../VaRestPlugin/Private/VaRestJsonParser.cpp | 752 +++++++++--------- .../VaRestPlugin/Private/VaRestJsonParser.h | 128 +-- .../VaRestPlugin/Private/VaRestJsonValue.cpp | 11 +- Source/VaRestPlugin/Private/VaRestLibrary.cpp | 30 +- Source/VaRestPlugin/Private/VaRestPlugin.cpp | 9 +- .../Private/VaRestPluginPrivatePCH.h | 10 +- .../Private/VaRestRequestJSON.cpp | 53 +- .../VaRestPlugin/Private/VaRestSettings.cpp | 1 - Source/VaRestPlugin/Public/VaRestPlugin.h | 7 +- 21 files changed, 717 insertions(+), 737 deletions(-) create mode 100644 Source/.clang-format diff --git a/Source/.clang-format b/Source/.clang-format new file mode 100644 index 00000000..de124b78 --- /dev/null +++ b/Source/.clang-format @@ -0,0 +1,22 @@ +--- +Language: Cpp +BasedOnStyle: LLVM +IndentWidth: 4 +TabWidth: 4 +UseTab: Always +Standard: Cpp11 +AccessModifierOffset: -4 +AlignAfterOpenBracket: DontAlign +AlignEscapedNewlines: Right +AlignTrailingComments: true +AllowShortCaseLabelsOnASingleLine: true +AllowShortFunctionsOnASingleLine: InlineOnly +BreakBeforeBraces: Allman +BreakConstructorInitializersBeforeComma: true +ColumnLimit: 0 +PointerAlignment: Left +SpacesInAngles: false +... +--- +Language: ObjC +... diff --git a/Source/VaRestEditorPlugin/Classes/VaRest_BreakJson.h b/Source/VaRestEditorPlugin/Classes/VaRest_BreakJson.h index 417cb9ce..8917b63f 100644 --- a/Source/VaRestEditorPlugin/Classes/VaRest_BreakJson.h +++ b/Source/VaRestEditorPlugin/Classes/VaRest_BreakJson.h @@ -61,15 +61,13 @@ class VARESTEDITORPLUGIN_API UVaRest_MakeJson : public UK2Node // End UK2Node interface. protected: - virtual void CreateProjectionPins(UEdGraphPin *Source); + virtual void CreateProjectionPins(UEdGraphPin* Source); public: UPROPERTY(EditAnywhere, Category = PinOptions) TArray Inputs; - }; - UCLASS(BlueprintType, Blueprintable) class VARESTEDITORPLUGIN_API UVaRest_BreakJson : public UK2Node { @@ -92,10 +90,9 @@ class VARESTEDITORPLUGIN_API UVaRest_BreakJson : public UK2Node // End UK2Node interface. protected: - virtual void CreateProjectionPins(UEdGraphPin *Source); + virtual void CreateProjectionPins(UEdGraphPin* Source); public: UPROPERTY(EditAnywhere, Category = PinOptions) TArray Outputs; - }; diff --git a/Source/VaRestEditorPlugin/Private/VaRestEditorPlugin.cpp b/Source/VaRestEditorPlugin/Private/VaRestEditorPlugin.cpp index f274a924..19f843be 100644 --- a/Source/VaRestEditorPlugin/Private/VaRestEditorPlugin.cpp +++ b/Source/VaRestEditorPlugin/Private/VaRestEditorPlugin.cpp @@ -17,5 +17,5 @@ void FVaRestEditorPluginModule::ShutdownModule() } #undef LOCTEXT_NAMESPACE - + IMPLEMENT_MODULE(FVaRestEditorPluginModule, VaRestEditorPlugin) diff --git a/Source/VaRestEditorPlugin/Private/VaRest_BreakJson.cpp b/Source/VaRestEditorPlugin/Private/VaRest_BreakJson.cpp index 49100dc4..418e7ebc 100644 --- a/Source/VaRestEditorPlugin/Private/VaRest_BreakJson.cpp +++ b/Source/VaRestEditorPlugin/Private/VaRest_BreakJson.cpp @@ -1,15 +1,14 @@ // Copyright 2015 Vladimir Alyamkin. All Rights Reserved. // Original code by https://github.com/unktomi - #include "VaRest_BreakJson.h" -#include "KismetCompiler.h" -#include "EditorCategoryUtils.h" +#include "BlueprintActionDatabaseRegistrar.h" +#include "BlueprintNodeSpawner.h" #include "EdGraph/EdGraph.h" #include "EdGraph/EdGraphNodeUtils.h" // for FNodeTextCache #include "EdGraphSchema_K2.h" -#include "BlueprintNodeSpawner.h" -#include "BlueprintActionDatabaseRegistrar.h" +#include "EditorCategoryUtils.h" +#include "KismetCompiler.h" #include "EdGraphUtilities.h" #include "Runtime/Launch/Resources/Version.h" @@ -39,10 +38,10 @@ class FKCHandler_BreakJson : public FNodeHandlingFunctor } } - UEdGraphPin *InNet = FEdGraphUtilities::GetNetFromPin(InputPin); - UClass *Class = Cast(StaticLoadObject(UClass::StaticClass(), NULL, TEXT("class'VaRestPlugin.VaRestJsonObject'"))); + UEdGraphPin* InNet = FEdGraphUtilities::GetNetFromPin(InputPin); + UClass* Class = Cast(StaticLoadObject(UClass::StaticClass(), NULL, TEXT("class'VaRestPlugin.VaRestJsonObject'"))); - FBPTerminal **SourceTerm = Context.NetMap.Find(InNet); + FBPTerminal** SourceTerm = Context.NetMap.Find(InNet); if (SourceTerm == nullptr) { return; @@ -58,14 +57,14 @@ class FKCHandler_BreakJson : public FNodeHandlingFunctor continue; } - FBPTerminal **Target = Context.NetMap.Find(Pin); + FBPTerminal** Target = Context.NetMap.Find(Pin); #if ENGINE_MINOR_VERSION >= 19 - const FName &FieldName = Pin->PinName; - const FName &FieldType = Pin->PinType.PinCategory; + const FName& FieldName = Pin->PinName; + const FName& FieldType = Pin->PinType.PinCategory; #else - const FString &FieldName = Pin->PinName; - const FString &FieldType = Pin->PinType.PinCategory; + const FString& FieldName = Pin->PinName; + const FString& FieldType = Pin->PinType.PinCategory; #endif FBPTerminal* FieldNameTerm = Context.CreateLocalTerminal(ETerminalSpecification::TS_Literal); @@ -84,10 +83,10 @@ class FKCHandler_BreakJson : public FNodeHandlingFunctor FieldNameTerm->TextLiteral = FText::FromString(FieldName); #endif - FBlueprintCompiledStatement& Statement = Context.AppendStatementForNode(Node); + FBlueprintCompiledStatement& Statement = Context.AppendStatementForNode(Node); FName FunctionName; - bool bIsArray = Pin->PinType.ContainerType == EPinContainerType::Array; + bool bIsArray = Pin->PinType.ContainerType == EPinContainerType::Array; if (FieldType == CompilerContext.GetSchema()->PC_Boolean) { FunctionName = bIsArray ? TEXT("GetBoolArrayField") : TEXT("GetBoolField"); @@ -109,7 +108,7 @@ class FKCHandler_BreakJson : public FNodeHandlingFunctor continue; } - UFunction *FunctionPtr = Class->FindFunctionByName(FunctionName); + UFunction* FunctionPtr = Class->FindFunctionByName(FunctionName); Statement.Type = KCST_CallFunction; Statement.FunctionToCall = FunctionPtr; Statement.FunctionContext = *SourceTerm; @@ -137,11 +136,11 @@ class FKCHandler_BreakJson : public FNodeHandlingFunctor // Find structure source net UEdGraphPin* Net = FEdGraphUtilities::GetNetFromPin(InputPin); - FBPTerminal **TermPtr = Context.NetMap.Find(Net); + FBPTerminal** TermPtr = Context.NetMap.Find(Net); if (!TermPtr) { - FBPTerminal *Term = Context.CreateLocalTerminalFromPinAutoChooseScope(Net, Context.NetNameMap->MakeValidName(Net)); + FBPTerminal* Term = Context.CreateLocalTerminalFromPinAutoChooseScope(Net, Context.NetNameMap->MakeValidName(Net)); Context.NetMap.Add(Net, Term); @@ -153,7 +152,7 @@ class FKCHandler_BreakJson : public FNodeHandlingFunctor void RegisterOutputTerm(FKismetFunctionContext& Context, UEdGraphPin* OutputPin, FBPTerminal* ContextTerm) { - FBPTerminal *Term = Context.CreateLocalTerminalFromPinAutoChooseScope(OutputPin, Context.NetNameMap->MakeValidName(OutputPin)); + FBPTerminal* Term = Context.CreateLocalTerminalFromPinAutoChooseScope(OutputPin, Context.NetNameMap->MakeValidName(OutputPin)); Context.NetMap.Add(OutputPin, Term); } @@ -181,7 +180,7 @@ class FKCHandler_BreakJson : public FNodeHandlingFunctor /** * Main node class */ -UVaRest_BreakJson::UVaRest_BreakJson(const FObjectInitializer &ObjectInitializer) +UVaRest_BreakJson::UVaRest_BreakJson(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) { } @@ -195,13 +194,13 @@ void UVaRest_BreakJson::AllocateDefaultPins() { const UEdGraphSchema_K2* K2Schema = GetDefault(); - UClass *Class = Cast(StaticLoadObject(UClass::StaticClass(), NULL, TEXT("class'VaRestPlugin.VaRestJsonObject'"))); + UClass* Class = Cast(StaticLoadObject(UClass::StaticClass(), NULL, TEXT("class'VaRestPlugin.VaRestJsonObject'"))); UEdGraphPin* Pin = CreatePin(EGPD_Input, K2Schema->PC_Object, TEXT(""), Class, TEXT("Target")); #if ENGINE_MINOR_VERSION >= 17 - K2Schema->SetPinAutogeneratedDefaultValueBasedOnType(Pin); + K2Schema->SetPinAutogeneratedDefaultValueBasedOnType(Pin); #else - K2Schema->SetPinDefaultValueBasedOnType(Pin); + K2Schema->SetPinDefaultValueBasedOnType(Pin); #endif CreateProjectionPins(Pin); @@ -264,10 +263,10 @@ FText UVaRest_BreakJson::GetMenuCategory() const return CachedCategory; } -void UVaRest_BreakJson::CreateProjectionPins(UEdGraphPin *Source) +void UVaRest_BreakJson::CreateProjectionPins(UEdGraphPin* Source) { const UEdGraphSchema_K2* K2Schema = GetDefault(); - UClass *Class = Cast(StaticLoadObject(UClass::StaticClass(), NULL, TEXT("class'VaRestPlugin.VaRestJsonObject'"))); + UClass* Class = Cast(StaticLoadObject(UClass::StaticClass(), NULL, TEXT("class'VaRestPlugin.VaRestJsonObject'"))); for (TArray::TIterator it(Outputs); it; ++it) { @@ -277,31 +276,32 @@ void UVaRest_BreakJson::CreateProjectionPins(UEdGraphPin *Source) FString Type; #endif - UObject *Subtype = nullptr; + UObject* Subtype = nullptr; FString FieldName = (*it).Name; - switch ((*it).Type) { - case EVaRest_JsonType::JSON_Bool: - Type = K2Schema->PC_Boolean; - break; - - case EVaRest_JsonType::JSON_Number: - Type = K2Schema->PC_Float; - break; - - case EVaRest_JsonType::JSON_String: - Type = K2Schema->PC_String; - break; - - case EVaRest_JsonType::JSON_Object: - Type = K2Schema->PC_Object; - Subtype = Class; - break; + switch ((*it).Type) + { + case EVaRest_JsonType::JSON_Bool: + Type = K2Schema->PC_Boolean; + break; + + case EVaRest_JsonType::JSON_Number: + Type = K2Schema->PC_Float; + break; + + case EVaRest_JsonType::JSON_String: + Type = K2Schema->PC_String; + break; + + case EVaRest_JsonType::JSON_Object: + Type = K2Schema->PC_Object; + Subtype = Class; + break; } UEdGraphNode::FCreatePinParams OutputPinParams; OutputPinParams.ContainerType = (*it).bIsArray ? EPinContainerType::Array : EPinContainerType::None; - UEdGraphPin *OutputPin = CreatePin(EGPD_Output, Type, TEXT(""), Subtype, FName(*(*it).Name), OutputPinParams); + UEdGraphPin* OutputPin = CreatePin(EGPD_Output, Type, TEXT(""), Subtype, FName(*(*it).Name), OutputPinParams); } } @@ -333,16 +333,16 @@ class FKCHandler_MakeJson : public FNodeHandlingFunctor } } - UClass *Class = Cast(StaticLoadObject(UClass::StaticClass(), NULL, TEXT("class'VaRestPlugin.VaRestJsonObject'"))); + UClass* Class = Cast(StaticLoadObject(UClass::StaticClass(), NULL, TEXT("class'VaRestPlugin.VaRestJsonObject'"))); - FBPTerminal **TargetTerm = Context.NetMap.Find(OutputPin); + FBPTerminal** TargetTerm = Context.NetMap.Find(OutputPin); if (TargetTerm == nullptr) { return; } { FName FunctionName = TEXT("ConstructJsonObject"); - UFunction *FunctionPtr = Class->FindFunctionByName(FunctionName); + UFunction* FunctionPtr = Class->FindFunctionByName(FunctionName); FBlueprintCompiledStatement& Statement = Context.AppendStatementForNode(Node); Statement.Type = KCST_CallFunction; Statement.FunctionToCall = FunctionPtr; @@ -362,14 +362,14 @@ class FKCHandler_MakeJson : public FNodeHandlingFunctor if (Pin && (EGPD_Input == Pin->Direction)) { - FBPTerminal **Source = Context.NetMap.Find(FEdGraphUtilities::GetNetFromPin(Pin)); + FBPTerminal** Source = Context.NetMap.Find(FEdGraphUtilities::GetNetFromPin(Pin)); #if ENGINE_MINOR_VERSION >= 19 - const FName &FieldName = Pin->PinName; - const FName &FieldType = Pin->PinType.PinCategory; + const FName& FieldName = Pin->PinName; + const FName& FieldType = Pin->PinType.PinCategory; #else - const FString &FieldName = Pin->PinName; - const FString &FieldType = Pin->PinType.PinCategory; + const FString& FieldName = Pin->PinName; + const FString& FieldType = Pin->PinType.PinCategory; #endif FBPTerminal* FieldNameTerm = Context.CreateLocalTerminal(ETerminalSpecification::TS_Literal); @@ -379,7 +379,7 @@ class FKCHandler_MakeJson : public FNodeHandlingFunctor #else FieldNameTerm->Source = Pin; #endif - + #if ENGINE_MINOR_VERSION >= 19 FieldNameTerm->Name = FieldName.ToString(); FieldNameTerm->TextLiteral = FText::FromName(FieldName); @@ -413,7 +413,7 @@ class FKCHandler_MakeJson : public FNodeHandlingFunctor continue; } - UFunction *FunctionPtr = Class->FindFunctionByName(FunctionName); + UFunction* FunctionPtr = Class->FindFunctionByName(FunctionName); Statement.Type = KCST_CallFunction; Statement.FunctionToCall = FunctionPtr; Statement.FunctionContext = *TargetTerm; @@ -429,11 +429,11 @@ class FKCHandler_MakeJson : public FNodeHandlingFunctor { // Find structure source net UEdGraphPin* Net = FEdGraphUtilities::GetNetFromPin(InputPin); - FBPTerminal **TermPtr = Context.NetMap.Find(Net); + FBPTerminal** TermPtr = Context.NetMap.Find(Net); if (!TermPtr) { - FBPTerminal *Term = Context.CreateLocalTerminalFromPinAutoChooseScope(Net, Context.NetNameMap->MakeValidName(Net)); + FBPTerminal* Term = Context.CreateLocalTerminalFromPinAutoChooseScope(Net, Context.NetNameMap->MakeValidName(Net)); Context.NetMap.Add(Net, Term); @@ -445,7 +445,7 @@ class FKCHandler_MakeJson : public FNodeHandlingFunctor void RegisterOutputTerm(FKismetFunctionContext& Context, UEdGraphPin* OutputPin) { - FBPTerminal *Term = Context.CreateLocalTerminalFromPinAutoChooseScope(OutputPin, Context.NetNameMap->MakeValidName(OutputPin)); + FBPTerminal* Term = Context.CreateLocalTerminalFromPinAutoChooseScope(OutputPin, Context.NetNameMap->MakeValidName(OutputPin)); Context.NetMap.Add(OutputPin, Term); } @@ -475,7 +475,7 @@ class FKCHandler_MakeJson : public FNodeHandlingFunctor /** * Main node class */ -UVaRest_MakeJson::UVaRest_MakeJson(const FObjectInitializer &ObjectInitializer) +UVaRest_MakeJson::UVaRest_MakeJson(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) { } @@ -489,13 +489,13 @@ void UVaRest_MakeJson::AllocateDefaultPins() { const UEdGraphSchema_K2* K2Schema = GetDefault(); - UClass *Class = Cast(StaticLoadObject(UClass::StaticClass(), NULL, TEXT("class'VaRestPlugin.VaRestJsonObject'"))); + UClass* Class = Cast(StaticLoadObject(UClass::StaticClass(), NULL, TEXT("class'VaRestPlugin.VaRestJsonObject'"))); UEdGraphPin* Pin = CreatePin(EGPD_Output, K2Schema->PC_Object, TEXT(""), Class, TEXT("Target")); #if ENGINE_MINOR_VERSION >= 17 - K2Schema->SetPinAutogeneratedDefaultValueBasedOnType(Pin); + K2Schema->SetPinAutogeneratedDefaultValueBasedOnType(Pin); #else - K2Schema->SetPinDefaultValueBasedOnType(Pin); + K2Schema->SetPinDefaultValueBasedOnType(Pin); #endif CreateProjectionPins(Pin); @@ -558,39 +558,40 @@ FText UVaRest_MakeJson::GetMenuCategory() const return CachedCategory; } -void UVaRest_MakeJson::CreateProjectionPins(UEdGraphPin *Source) +void UVaRest_MakeJson::CreateProjectionPins(UEdGraphPin* Source) { const UEdGraphSchema_K2* K2Schema = GetDefault(); - UClass *Class = Cast(StaticLoadObject(UClass::StaticClass(), NULL, TEXT("class'VaRestPlugin.VaRestJsonObject'"))); + UClass* Class = Cast(StaticLoadObject(UClass::StaticClass(), NULL, TEXT("class'VaRestPlugin.VaRestJsonObject'"))); for (TArray::TIterator it(Inputs); it; ++it) { FName Type; - UObject *Subtype = nullptr; + UObject* Subtype = nullptr; FString FieldName = (*it).Name; - switch ((*it).Type) { - case EVaRest_JsonType::JSON_Bool: - Type = K2Schema->PC_Boolean; - break; - - case EVaRest_JsonType::JSON_Number: - Type = K2Schema->PC_Float; - break; - - case EVaRest_JsonType::JSON_String: - Type = K2Schema->PC_String; - break; - - case EVaRest_JsonType::JSON_Object: - Type = K2Schema->PC_Object; - Subtype = Class; - break; + switch ((*it).Type) + { + case EVaRest_JsonType::JSON_Bool: + Type = K2Schema->PC_Boolean; + break; + + case EVaRest_JsonType::JSON_Number: + Type = K2Schema->PC_Float; + break; + + case EVaRest_JsonType::JSON_String: + Type = K2Schema->PC_String; + break; + + case EVaRest_JsonType::JSON_Object: + Type = K2Schema->PC_Object; + Subtype = Class; + break; } UEdGraphNode::FCreatePinParams InputPinParams; InputPinParams.ContainerType = (*it).bIsArray ? EPinContainerType::Array : EPinContainerType::None; - UEdGraphPin *InputPin = CreatePin(EGPD_Input, Type, TEXT(""), Subtype, FName(*(*it).Name), InputPinParams); + UEdGraphPin* InputPin = CreatePin(EGPD_Input, Type, TEXT(""), Subtype, FName(*(*it).Name), InputPinParams); InputPin->SetSavePinIfOrphaned(false); } } @@ -600,5 +601,4 @@ FText UVaRest_MakeJson::GetNodeTitle(ENodeTitleType::Type TitleType) const return LOCTEXT("VaRest_Make_Json.NodeTitle", "Make Json"); } - #undef LOCTEXT_NAMESPACE diff --git a/Source/VaRestEditorPlugin/Public/VaRestEditorPlugin.h b/Source/VaRestEditorPlugin/Public/VaRestEditorPlugin.h index 9f5c2082..6e1cb4a5 100644 --- a/Source/VaRestEditorPlugin/Public/VaRestEditorPlugin.h +++ b/Source/VaRestEditorPlugin/Public/VaRestEditorPlugin.h @@ -12,5 +12,4 @@ class FVaRestEditorPluginModule : public IModuleInterface /** IModuleInterface implementation */ virtual void StartupModule() override; virtual void ShutdownModule() override; - }; diff --git a/Source/VaRestPlugin/Classes/VaRestJsonObject.h b/Source/VaRestPlugin/Classes/VaRestJsonObject.h index 474917ca..7ec9e8a1 100644 --- a/Source/VaRestPlugin/Classes/VaRestJsonObject.h +++ b/Source/VaRestPlugin/Classes/VaRestJsonObject.h @@ -30,7 +30,6 @@ class VARESTPLUGIN_API UVaRestJsonObject : public UObject /** Set the root Json object */ void SetRootObject(TSharedPtr& JsonObject); - ////////////////////////////////////////////////////////////////////////// // Serialization @@ -46,7 +45,6 @@ class VARESTPLUGIN_API UVaRestJsonObject : public UObject UFUNCTION(BlueprintCallable, Category = "VaRest|Json") bool DecodeJson(const FString& JsonString); - ////////////////////////////////////////////////////////////////////////// // FJsonObject API @@ -77,12 +75,11 @@ class VARESTPLUGIN_API UVaRestJsonObject : public UObject /** Set an ObjectField named FieldName and value of Json Array */ UFUNCTION(BlueprintCallable, Category = "VaRest|Json") void SetArrayField(const FString& FieldName, const TArray& InArray); - + /** Adds all of the fields from one json object to this one */ UFUNCTION(BlueprintCallable, Category = "VaRest|Json") void MergeJsonObject(UVaRestJsonObject* InJsonObject, bool Overwrite); - ////////////////////////////////////////////////////////////////////////// // FJsonObject API Helpers (easy to use with simple Json objects) @@ -128,7 +125,6 @@ class VARESTPLUGIN_API UVaRestJsonObject : public UObject UFUNCTION(BlueprintCallable, Category = "VaRest|Json") void SetObjectField(const FString& FieldName, UVaRestJsonObject* JsonObject); - ////////////////////////////////////////////////////////////////////////// // Array fields helpers (uniform arrays) @@ -166,25 +162,23 @@ class VARESTPLUGIN_API UVaRestJsonObject : public UObject UFUNCTION(BlueprintCallable, Category = "VaRest|Json") void SetObjectArrayField(const FString& FieldName, const TArray& ObjectArray); - ////////////////////////////////////////////////////////////////////////// // Deserialize - + public: /** Deserialize byte content to json */ int32 DeserializeFromUTF8Bytes(const ANSICHAR* Bytes, int32 Size); - + /** Deserialize byte content to json */ int32 DeserializeFromTCHARBytes(const TCHAR* Bytes, int32 Size); - + /** Deserialize byte stream from reader */ void DecodeFromArchive(TUniquePtr& Reader); - + /** Save json to file */ bool WriteToFile(const FString& Path); - + static bool WriteStringToArchive(FArchive& Ar, const TCHAR* StrPtr, int64 Len); - ////////////////////////////////////////////////////////////////////////// // Data @@ -192,5 +186,4 @@ class VARESTPLUGIN_API UVaRestJsonObject : public UObject private: /** Internal JSON data */ TSharedPtr JsonObj; - }; diff --git a/Source/VaRestPlugin/Classes/VaRestJsonValue.h b/Source/VaRestPlugin/Classes/VaRestJsonValue.h index 4b7ad64f..b5b163e2 100644 --- a/Source/VaRestPlugin/Classes/VaRestJsonValue.h +++ b/Source/VaRestPlugin/Classes/VaRestJsonValue.h @@ -14,16 +14,16 @@ class FJsonValue; UENUM(BlueprintType) namespace EVaJson { - enum Type - { - None, - Null, - String, - Number, - Boolean, - Array, - Object, - }; +enum Type +{ + None, + Null, + String, + Number, + Boolean, + Array, + Object, +}; } /** @@ -53,7 +53,7 @@ class VARESTPLUGIN_API UVaRestJsonValue : public UObject /** Create new Json Object value */ UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json Object Value", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "VaRest|Json") - static UVaRestJsonValue* ConstructJsonValueObject(UObject* WorldContextObject, UVaRestJsonObject *JsonObject); + static UVaRestJsonValue* ConstructJsonValueObject(UObject* WorldContextObject, UVaRestJsonObject* JsonObject); /** Create new Json value from FJsonValue (to be used from VaRestJsonObject) */ static UVaRestJsonValue* ConstructJsonValue(UObject* WorldContextObject, const TSharedPtr& InValue); @@ -64,7 +64,6 @@ class VARESTPLUGIN_API UVaRestJsonValue : public UObject /** Set the root Json value */ void SetRootValue(TSharedPtr& JsonValue); - ////////////////////////////////////////////////////////////////////////// // FJsonValue API @@ -101,7 +100,6 @@ class VARESTPLUGIN_API UVaRestJsonValue : public UObject UFUNCTION(BlueprintCallable, Category = "VaRest|Json") UVaRestJsonObject* AsObject(); - ////////////////////////////////////////////////////////////////////////// // Data @@ -109,12 +107,10 @@ class VARESTPLUGIN_API UVaRestJsonValue : public UObject /** Internal JSON data */ TSharedPtr JsonVal; - ////////////////////////////////////////////////////////////////////////// // Helpers protected: /** Simple error logger */ void ErrorMessage(const FString& InType) const; - }; diff --git a/Source/VaRestPlugin/Classes/VaRestLibrary.h b/Source/VaRestPlugin/Classes/VaRestLibrary.h index 226fb4c2..96e477ac 100644 --- a/Source/VaRestPlugin/Classes/VaRestLibrary.h +++ b/Source/VaRestPlugin/Classes/VaRestLibrary.h @@ -5,6 +5,7 @@ #include "Kismet/BlueprintFunctionLibrary.h" #include "VaRestTypes.h" + #include "VaRestLibrary.generated.h" class UVaRestRequestJSON; @@ -16,25 +17,24 @@ USTRUCT() struct FVaRestCallResponse { GENERATED_USTRUCT_BODY() - + UPROPERTY() UVaRestRequestJSON* Request; - + UPROPERTY() UObject* WorldContextObject; - + UPROPERTY() FVaRestCallDelegate Callback; - + FDelegateHandle CompleteDelegateHandle; FDelegateHandle FailDelegateHandle; - + FVaRestCallResponse() : Request(nullptr) , WorldContextObject(nullptr) { } - }; /** @@ -45,7 +45,6 @@ class VARESTPLUGIN_API UVaRestLibrary : public UBlueprintFunctionLibrary { GENERATED_BODY() - ////////////////////////////////////////////////////////////////////////// // Helpers @@ -92,7 +91,6 @@ class VARESTPLUGIN_API UVaRestLibrary : public UBlueprintFunctionLibrary UFUNCTION(BlueprintCallable, Category = "VaRest|Utility", meta = (DisplayName = "Base64 Decode Data")) static bool Base64DecodeData(const FString& Source, TArray& Dest); - ////////////////////////////////////////////////////////////////////////// // File system integration @@ -104,7 +102,6 @@ class VARESTPLUGIN_API UVaRestLibrary : public UBlueprintFunctionLibrary UFUNCTION(BlueprintCallable, Category = "VaRest|Utility", meta = (WorldContext = "WorldContextObject")) static class UVaRestJsonObject* LoadJsonFromFile(UObject* WorldContextObject, const FString& Path); - ////////////////////////////////////////////////////////////////////////// // Easy URL processing @@ -118,5 +115,4 @@ class VARESTPLUGIN_API UVaRestLibrary : public UBlueprintFunctionLibrary private: static TMap RequestMap; - }; diff --git a/Source/VaRestPlugin/Classes/VaRestRequestJSON.h b/Source/VaRestPlugin/Classes/VaRestRequestJSON.h index 63527e45..a0497562 100644 --- a/Source/VaRestPlugin/Classes/VaRestRequestJSON.h +++ b/Source/VaRestPlugin/Classes/VaRestRequestJSON.h @@ -3,41 +3,43 @@ #pragma once #include "Engine/LatentActionManager.h" -#include "LatentActions.h" #include "Http.h" +#include "LatentActions.h" #include "VaRestTypes.h" + #include "VaRestRequestJSON.generated.h" /** * @author Original latent action class by https://github.com/unktomi */ -template class FVaRestLatentAction : public FPendingLatentAction +template +class FVaRestLatentAction : public FPendingLatentAction { public: - virtual void Call(const T &Value) + virtual void Call(const T& Value) { Result = Value; Called = true; } - void operator()(const T &Value) + void operator()(const T& Value) { Call(Value); } void Cancel(); - - FVaRestLatentAction(FWeakObjectPtr RequestObj, T& ResultParam, const FLatentActionInfo& LatentInfo) : - Called(false), - Request(RequestObj), - ExecutionFunction(LatentInfo.ExecutionFunction), - OutputLink(LatentInfo.Linkage), - CallbackTarget(LatentInfo.CallbackTarget), - Result(ResultParam) + + FVaRestLatentAction(FWeakObjectPtr RequestObj, T& ResultParam, const FLatentActionInfo& LatentInfo) + : Called(false) + , Request(RequestObj) + , ExecutionFunction(LatentInfo.ExecutionFunction) + , OutputLink(LatentInfo.Linkage) + , CallbackTarget(LatentInfo.CallbackTarget) + , Result(ResultParam) { } - + virtual void UpdateOperation(FLatentResponse& Response) override { Response.FinishAndTriggerIf(Called, ExecutionFunction, OutputLink, CallbackTarget); @@ -61,10 +63,9 @@ template class FVaRestLatentAction : public FPendingLatentAction const FName ExecutionFunction; const int32 OutputLink; const FWeakObjectPtr CallbackTarget; - T &Result; + T& Result; }; - /** Generate a delegates for callback events */ DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnRequestComplete, class UVaRestRequestJSON*, Request); DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnRequestFail, class UVaRestRequestJSON*, Request); @@ -72,7 +73,6 @@ DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnRequestFail, class UVaRestRequest DECLARE_MULTICAST_DELEGATE_OneParam(FOnStaticRequestComplete, class UVaRestRequestJSON*); DECLARE_MULTICAST_DELEGATE_OneParam(FOnStaticRequestFail, class UVaRestRequestJSON*); - /** * General helper class http requests via blueprints */ @@ -108,21 +108,20 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject /** Set content type of the request for binary post data */ UFUNCTION(BlueprintCallable, Category = "VaRest|Request") - void SetBinaryContentType(const FString &ContentType); + void SetBinaryContentType(const FString& ContentType); /** Set content of the request for binary post data */ UFUNCTION(BlueprintCallable, Category = "VaRest|Request") - void SetBinaryRequestContent(const TArray &Content); + void SetBinaryRequestContent(const TArray& Content); /** Set content of the request as a plain string */ UFUNCTION(BlueprintCallable, Category = "VaRest|Request") - void SetStringRequestContent(const FString &Content); + void SetStringRequestContent(const FString& Content); /** Sets optional header info */ UFUNCTION(BlueprintCallable, Category = "VaRest|Request") void SetHeader(const FString& HeaderName, const FString& HeaderValue); - ////////////////////////////////////////////////////////////////////////// // Destruction and reset @@ -142,7 +141,6 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject UFUNCTION(BlueprintCallable, Category = "VaRest|Response") void Cancel(); - ////////////////////////////////////////////////////////////////////////// // JSON data accessors @@ -162,7 +160,6 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject UFUNCTION(BlueprintCallable, Category = "VaRest|Response") void SetResponseObject(UVaRestJsonObject* JsonObject); - /////////////////////////////////////////////////////////////////////////// // Request/response data access @@ -181,12 +178,11 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject /** Get value of desired response header */ UFUNCTION(BlueprintCallable, Category = "VaRest|Response") FString GetResponseHeader(const FString HeaderName); - + /** Get list of all response headers */ UFUNCTION(BlueprintCallable, Category = "VaRest|Response") TArray GetAllResponseHeaders(); - ////////////////////////////////////////////////////////////////////////// // URL processing @@ -201,7 +197,7 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject /** Open URL in latent mode */ UFUNCTION(BlueprintCallable, Category = "VaRest|Request", meta = (Latent, LatentInfo = "LatentInfo", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject")) - virtual void ApplyURL(const FString& Url, UVaRestJsonObject *&Result, UObject* WorldContextObject, struct FLatentActionInfo LatentInfo); + virtual void ApplyURL(const FString& Url, UVaRestJsonObject*& Result, UObject* WorldContextObject, struct FLatentActionInfo LatentInfo); /** Check URL and execute process request */ UFUNCTION(BlueprintCallable, Category = "VaRest|Request") @@ -211,7 +207,6 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject /** Apply current internal setup to request and process it */ void ProcessRequest(); - ////////////////////////////////////////////////////////////////////////// // Request callbacks @@ -227,13 +222,12 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject /** Event occured when the request wasn't successfull */ UPROPERTY(BlueprintAssignable, Category = "VaRest|Event") FOnRequestFail OnRequestFail; - + /** Event occured when the request has been completed */ FOnStaticRequestComplete OnStaticRequestComplete; - + /** Event occured when the request wasn't successfull */ FOnStaticRequestFail OnStaticRequestFail; - ////////////////////////////////////////////////////////////////////////// // Tags @@ -259,7 +253,6 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject /** Array of tags that can be used for grouping and categorizing */ TArray Tags; - ////////////////////////////////////////////////////////////////////////// // Data @@ -275,7 +268,7 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject /** Response size */ UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "VaRest|Response") int32 ResponseSize; - + /** DEPRECATED: Please use GetResponseContentAsString() instead */ UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "VaRest|Response") FString ResponseContent; @@ -283,7 +276,7 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject /** Is the response valid JSON? */ UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "VaRest|Response") bool bIsValidJsonResponse; - + protected: /** Default value for deprecated ResponseContent variable */ static FString DeprecatedResponseString; @@ -327,9 +320,8 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject /** Request we're currently processing */ TSharedRef HttpRequest = FHttpModule::Get().CreateRequest(); - + public: /** Returns reference to internal request object */ TSharedRef GetHttpRequest() const { return HttpRequest; }; - }; diff --git a/Source/VaRestPlugin/Classes/VaRestSettings.h b/Source/VaRestPlugin/Classes/VaRestSettings.h index 1352f763..3de0258c 100644 --- a/Source/VaRestPlugin/Classes/VaRestSettings.h +++ b/Source/VaRestPlugin/Classes/VaRestSettings.h @@ -8,10 +8,9 @@ UCLASS(config = Engine, defaultconfig) class VARESTPLUGIN_API UVaRestSettings : public UObject { GENERATED_UCLASS_BODY() - + public: /** You can disable request content logging to avoid security vulnerability */ UPROPERTY(Config, EditAnywhere, Category = "VaRest") bool bExtendedLog; - }; diff --git a/Source/VaRestPlugin/Classes/VaRestTypes.h b/Source/VaRestPlugin/Classes/VaRestTypes.h index 0ba037e2..c1a61497 100644 --- a/Source/VaRestPlugin/Classes/VaRestTypes.h +++ b/Source/VaRestPlugin/Classes/VaRestTypes.h @@ -14,6 +14,7 @@ enum class ERequestVerb : uint8 CUSTOM }; +// clang-format off /** Content type (json, urlencoded, etc.) used by the request */ UENUM(BlueprintType) enum class ERequestContentType : uint8 @@ -23,6 +24,7 @@ enum class ERequestContentType : uint8 json, binary }; +// clang-format on /** Enumerates the current state of an Http request */ UENUM(BlueprintType) diff --git a/Source/VaRestPlugin/Private/VaRestJsonObject.cpp b/Source/VaRestPlugin/Private/VaRestJsonObject.cpp index 1cc2663c..a84b0917 100644 --- a/Source/VaRestPlugin/Private/VaRestJsonObject.cpp +++ b/Source/VaRestPlugin/Private/VaRestJsonObject.cpp @@ -5,8 +5,8 @@ #include "VaRestJsonValue.h" #include "VaRestPluginPrivatePCH.h" -typedef TJsonWriterFactory< TCHAR, TCondensedJsonPrintPolicy > FCondensedJsonStringWriterFactory; -typedef TJsonWriter< TCHAR, TCondensedJsonPrintPolicy > FCondensedJsonStringWriter; +typedef TJsonWriterFactory> FCondensedJsonStringWriterFactory; +typedef TJsonWriter> FCondensedJsonStringWriter; UVaRestJsonObject::UVaRestJsonObject(const class FObjectInitializer& PCIP) : Super(PCIP) @@ -47,7 +47,6 @@ void UVaRestJsonObject::SetRootObject(TSharedPtr& JsonObject) } } - ////////////////////////////////////////////////////////////////////////// // Serialization @@ -59,7 +58,7 @@ FString UVaRestJsonObject::EncodeJson() const } FString OutputString; - TSharedRef< FCondensedJsonStringWriter > Writer = FCondensedJsonStringWriterFactory::Create(&OutputString); + TSharedRef Writer = FCondensedJsonStringWriterFactory::Create(&OutputString); FJsonSerializer::Serialize(JsonObj.ToSharedRef(), Writer); return OutputString; @@ -71,7 +70,7 @@ FString UVaRestJsonObject::EncodeJsonToSingleString() const // Remove line terminators OutputString.Replace(LINE_TERMINATOR, TEXT("")); - + // Remove tabs OutputString.Replace(LINE_TERMINATOR, TEXT("\t")); @@ -81,7 +80,7 @@ FString UVaRestJsonObject::EncodeJsonToSingleString() const bool UVaRestJsonObject::DecodeJson(const FString& JsonString) { DeserializeFromTCHARBytes(JsonString.GetCharArray().GetData(), JsonString.Len()); - + if (JsonObj.IsValid()) { return true; @@ -95,21 +94,20 @@ bool UVaRestJsonObject::DecodeJson(const FString& JsonString) return false; } - ////////////////////////////////////////////////////////////////////////// // FJsonObject API TArray UVaRestJsonObject::GetFieldNames() const { TArray Result; - + if (!JsonObj.IsValid()) { return Result; } - + JsonObj->Values.GetKeys(Result); - + return Result; } @@ -148,7 +146,7 @@ UVaRestJsonValue* UVaRestJsonObject::GetField(const FString& FieldName) const return NewValue; } - + return nullptr; } @@ -162,7 +160,6 @@ void UVaRestJsonObject::SetField(const FString& FieldName, UVaRestJsonValue* Jso JsonObj->SetField(FieldName, JsonValue->GetRootValue()); } - ////////////////////////////////////////////////////////////////////////// // FJsonObject API Helpers (easy to use with simple Json objects) @@ -215,7 +212,7 @@ FString UVaRestJsonObject::GetStringField(const FString& FieldName) const UE_LOG(LogVaRest, Warning, TEXT("No field with name %s of type String"), *FieldName); return TEXT(""); } - + return JsonObj->GetStringField(FieldName); } @@ -264,7 +261,7 @@ TArray UVaRestJsonObject::GetArrayField(const FString& FieldN return OutArray; } - TArray< TSharedPtr > ValArray = JsonObj->GetArrayField(FieldName); + TArray> ValArray = JsonObj->GetArrayField(FieldName); for (auto Value : ValArray) { UVaRestJsonValue* NewValue = NewObject(); @@ -283,7 +280,7 @@ void UVaRestJsonObject::SetArrayField(const FString& FieldName, const TArray > ValArray; + TArray> ValArray; // Process input array and COPY original values for (auto InVal : InArray) @@ -335,14 +332,14 @@ void UVaRestJsonObject::MergeJsonObject(UVaRestJsonObject* InJsonObject, bool Ov } TArray Keys = InJsonObject->GetFieldNames(); - + for (auto Key : Keys) { if (Overwrite == false && HasField(Key)) { continue; } - + SetField(Key, InJsonObject->GetField(Key)); } } @@ -373,7 +370,6 @@ void UVaRestJsonObject::SetObjectField(const FString& FieldName, UVaRestJsonObje JsonObj->SetObjectField(FieldName, JsonObject->GetRootObject()); } - ////////////////////////////////////////////////////////////////////////// // Array fields helpers (uniform arrays) @@ -386,15 +382,15 @@ TArray UVaRestJsonObject::GetNumberArrayField(const FString& FieldName) c return NumberArray; } - TArray > JsonArrayValues = JsonObj->GetArrayField(FieldName); - for (TArray >::TConstIterator It(JsonArrayValues); It; ++It) + TArray> JsonArrayValues = JsonObj->GetArrayField(FieldName); + for (TArray>::TConstIterator It(JsonArrayValues); It; ++It) { auto Value = (*It).Get(); if (Value->Type != EJson::Number) { UE_LOG(LogVaRest, Error, TEXT("Not Number element in array with field name %s"), *FieldName); } - + NumberArray.Add((*It)->AsNumber()); } @@ -408,7 +404,7 @@ void UVaRestJsonObject::SetNumberArrayField(const FString& FieldName, const TArr return; } - TArray< TSharedPtr > EntriesArray; + TArray> EntriesArray; for (auto Number : NumberArray) { @@ -427,8 +423,8 @@ TArray UVaRestJsonObject::GetStringArrayField(const FString& FieldName) return StringArray; } - TArray > JsonArrayValues = JsonObj->GetArrayField(FieldName); - for (TArray >::TConstIterator It(JsonArrayValues); It; ++It) + TArray> JsonArrayValues = JsonObj->GetArrayField(FieldName); + for (TArray>::TConstIterator It(JsonArrayValues); It; ++It) { auto Value = (*It).Get(); if (Value->Type != EJson::String) @@ -449,7 +445,7 @@ void UVaRestJsonObject::SetStringArrayField(const FString& FieldName, const TArr return; } - TArray< TSharedPtr > EntriesArray; + TArray> EntriesArray; for (auto String : StringArray) { EntriesArray.Add(MakeShareable(new FJsonValueString(String))); @@ -467,8 +463,8 @@ TArray UVaRestJsonObject::GetBoolArrayField(const FString& FieldName) cons return BoolArray; } - TArray > JsonArrayValues = JsonObj->GetArrayField(FieldName); - for (TArray >::TConstIterator It(JsonArrayValues); It; ++It) + TArray> JsonArrayValues = JsonObj->GetArrayField(FieldName); + for (TArray>::TConstIterator It(JsonArrayValues); It; ++It) { auto Value = (*It).Get(); if (Value->Type != EJson::Boolean) @@ -489,7 +485,7 @@ void UVaRestJsonObject::SetBoolArrayField(const FString& FieldName, const TArray return; } - TArray< TSharedPtr > EntriesArray; + TArray> EntriesArray; for (auto Boolean : BoolArray) { EntriesArray.Add(MakeShareable(new FJsonValueBoolean(Boolean))); @@ -507,7 +503,7 @@ TArray UVaRestJsonObject::GetObjectArrayField(const FString& return OutArray; } - TArray< TSharedPtr > ValArray = JsonObj->GetArrayField(FieldName); + TArray> ValArray = JsonObj->GetArrayField(FieldName); for (auto Value : ValArray) { if (Value->Type != EJson::Object) @@ -533,7 +529,7 @@ void UVaRestJsonObject::SetObjectArrayField(const FString& FieldName, const TArr return; } - TArray< TSharedPtr > EntriesArray; + TArray> EntriesArray; for (auto Value : ObjectArray) { EntriesArray.Add(MakeShareable(new FJsonValueObject(Value->GetRootObject()))); @@ -570,19 +566,19 @@ int32 UVaRestJsonObject::DeserializeFromUTF8Bytes(const ANSICHAR* Bytes, int32 S while (Bytes < EndByte) { TCHAR Char = FUTF8ToTCHAR_Convert::utf8codepoint(&Bytes); - + if (Char > 0xFFFF) { Char = UNICODE_BOGUS_CHAR_CODEPOINT; } - + if (!Reader.Read(Char)) { break; } } #endif - + SetRootObject(Reader.State.Root); return Reader.State.Size; } @@ -590,7 +586,7 @@ int32 UVaRestJsonObject::DeserializeFromUTF8Bytes(const ANSICHAR* Bytes, int32 S int32 UVaRestJsonObject::DeserializeFromTCHARBytes(const TCHAR* Bytes, int32 Size) { FJSONReader Reader; - + int32 i = 0; while (i < Size) { @@ -599,7 +595,7 @@ int32 UVaRestJsonObject::DeserializeFromTCHARBytes(const TCHAR* Bytes, int32 Siz break; } } - + SetRootObject(Reader.State.Root); return Reader.State.Size; } @@ -608,38 +604,38 @@ void UVaRestJsonObject::DecodeFromArchive(TUniquePtr& Reader) { FArchive& Ar = (*Reader.Get()); uint8 SymbolBytes[2]; - + // Read first two bytes Ar << SymbolBytes[0]; Ar << SymbolBytes[1]; - + bool bIsIntelByteOrder = true; - - if(SymbolBytes[0] == 0xff && SymbolBytes[1] == 0xfe) + + if (SymbolBytes[0] == 0xff && SymbolBytes[1] == 0xfe) { // Unicode Intel byte order. Less 1 for the FFFE header, additional 1 for null terminator. bIsIntelByteOrder = true; } - else if(SymbolBytes[0] == 0xfe && SymbolBytes[1] == 0xff) + else if (SymbolBytes[0] == 0xfe && SymbolBytes[1] == 0xff) { // Unicode non-Intel byte order. Less 1 for the FFFE header, additional 1 for null terminator. bIsIntelByteOrder = false; } - + FJSONReader JsonReader; TCHAR Char; - + while (!Ar.AtEnd()) { Ar << SymbolBytes[0]; - + if (Ar.AtEnd()) { break; } - + Ar << SymbolBytes[1]; - + if (bIsIntelByteOrder) { Char = CharCast((UCS2CHAR)((uint16)SymbolBytes[0] + (uint16)SymbolBytes[1] * 256)); @@ -648,20 +644,19 @@ void UVaRestJsonObject::DecodeFromArchive(TUniquePtr& Reader) { Char = CharCast((UCS2CHAR)((uint16)SymbolBytes[1] + (uint16)SymbolBytes[0] * 256)); } - + if (!JsonReader.Read(Char)) { break; } } - + SetRootObject(JsonReader.State.Root); - + if (!Ar.Close()) { UE_LOG(LogVaRest, Error, TEXT("UVaRestJsonObject::DecodeFromArchive: Error! Can't close file!")); } - } bool UVaRestJsonObject::WriteToFile(const FString& Path) @@ -673,49 +668,49 @@ bool UVaRestJsonObject::WriteToFile(const FString& Path) { return false; } - + FArchive& Ar = *FileWriter.Get(); - + UCS2CHAR BOM = UNICODE_BOM; - Ar.Serialize( &BOM, sizeof(UCS2CHAR) ); - - FString Str = FString(TEXT("{")); + Ar.Serialize(&BOM, sizeof(UCS2CHAR)); + + FString Str = FString(TEXT("{")); WriteStringToArchive(Ar, *Str, Str.Len()); - + int32 ElementCount = 0; FJSONWriter JsonWriter; for (auto JsonObjectValuePair : JsonObj->Values) { Str = FString(TEXT("\"")); WriteStringToArchive(Ar, *Str, Str.Len()); - + const TCHAR* BufferPtr = *JsonObjectValuePair.Key; for (int i = 0; i < JsonObjectValuePair.Key.Len(); ++i) { - Str = FString(1, &BufferPtr[i]); + Str = FString(1, &BufferPtr[i]); #if PLATFORM_WINDOWS WriteStringToArchive(Ar, *Str, Str.Len() - 1); #else WriteStringToArchive(Ar, *Str, Str.Len()); #endif } - + Str = FString(TEXT("\"")); WriteStringToArchive(Ar, *Str, Str.Len()); - + Str = FString(TEXT(":")); WriteStringToArchive(Ar, *Str, Str.Len()); - + ++ElementCount; - + JsonWriter.Write(JsonObjectValuePair.Value, FileWriter.Get(), ElementCount >= JsonObj->Values.Num()); } - + Str = FString(TEXT("}")); WriteStringToArchive(Ar, *Str, Str.Len()); - + FileWriter->Close(); - + return true; } else @@ -725,10 +720,10 @@ bool UVaRestJsonObject::WriteToFile(const FString& Path) } } -bool UVaRestJsonObject::WriteStringToArchive( FArchive& Ar, const TCHAR* StrPtr, int64 Len) +bool UVaRestJsonObject::WriteStringToArchive(FArchive& Ar, const TCHAR* StrPtr, int64 Len) { auto Src = StringCast(StrPtr, Len); - Ar.Serialize( (UCS2CHAR*)Src.Get(), Src.Length() * sizeof(UCS2CHAR) ); - + Ar.Serialize((UCS2CHAR*)Src.Get(), Src.Length() * sizeof(UCS2CHAR)); + return true; } diff --git a/Source/VaRestPlugin/Private/VaRestJsonParser.cpp b/Source/VaRestPlugin/Private/VaRestJsonParser.cpp index 1755b4ca..2c3ab7bc 100644 --- a/Source/VaRestPlugin/Private/VaRestJsonParser.cpp +++ b/Source/VaRestPlugin/Private/VaRestJsonParser.cpp @@ -1,27 +1,27 @@ // Copyright 2015-2017 Mail.Ru Group. All Rights Reserved. #include "VaRestJsonParser.h" -#include "VaRestJsonObject.h" #include "Dom/JsonObject.h" #include "Dom/JsonValue.h" #include "Logging/LogMacros.h" +#include "VaRestJsonObject.h" FJSONState::FJSONState() -: Notation(EJSONNotation::NONE) -, bEscape(false) -, bError(false) -, Quote(UNICODE_BOGUS_CHAR_CODEPOINT) + : Notation(EJSONNotation::NONE) + , bEscape(false) + , bError(false) + , Quote(UNICODE_BOGUS_CHAR_CODEPOINT) { Key.Reserve(1024); Data.Reserve(4096); - + Root = TSharedPtr(nullptr); - + Objects.Reserve(64); Tokens.Reserve(64); - + Tokens.Add(EJSONToken::ROOT); - + Size = 0; } @@ -58,7 +58,7 @@ void FJSONState::PopToken(int32 Num) bError = true; } } - + Notation = EJSONNotation::NONE; } @@ -72,7 +72,7 @@ void FJSONState::PopObject() return; } } - + bError = true; } @@ -86,7 +86,7 @@ void FJSONState::PopArray() return; } } - + bError = true; } @@ -107,86 +107,86 @@ void FJSONState::PopValue(bool bCheckType) Objects.Pop(false); if (Objects.Num() > 0) { - switch(Value->Type) + switch (Value->Type) { - case EJson::Null: - { - auto LowerCase = Data.ToLower(); - if (LowerCase != TEXT("null")) - { - bError = true; - } - break; - } - case EJson::String: + case EJson::Null: + { + auto LowerCase = Data.ToLower(); + if (LowerCase != TEXT("null")) { - FJsonValueNonConstString* JsonValueString = ((FJsonValueNonConstString*) Value.Get()); - JsonValueString->AsNonConstString() = Data; - JsonValueString->AsNonConstString().Shrink(); - Size += JsonValueString->AsNonConstString().GetAllocatedSize(); - break; + bError = true; } - case EJson::Number: + break; + } + case EJson::String: + { + FJsonValueNonConstString* JsonValueString = ((FJsonValueNonConstString*)Value.Get()); + JsonValueString->AsNonConstString() = Data; + JsonValueString->AsNonConstString().Shrink(); + Size += JsonValueString->AsNonConstString().GetAllocatedSize(); + break; + } + case EJson::Number: + { + FString LowerCase = Data.ToLower(); + int32 ePosition = INDEX_NONE; + LowerCase.FindChar('e', ePosition); + if (ePosition == INDEX_NONE) { - FString LowerCase = Data.ToLower(); - int32 ePosition = INDEX_NONE; - LowerCase.FindChar('e', ePosition); - if (ePosition == INDEX_NONE) + if (LowerCase.IsNumeric()) { - if (LowerCase.IsNumeric()) - { - ((FJsonValueNonConstNumber*) Value.Get())->AsNonConstNumber() = FCString::Atod(*LowerCase); - } - else - { - bError = true; - } - } - else if (LowerCase.Len() > ePosition + 2) - { - FString Left = LowerCase.Left(ePosition); - FString Rigth = LowerCase.Right(LowerCase.Len() - ePosition - 1); - if (Left.IsNumeric() && Rigth.IsNumeric()) - { - ((FJsonValueNonConstNumber*) Value.Get())->AsNonConstNumber() = FCString::Atod(*Left) * FMath::Pow(10.f, FCString::Atoi(*Rigth)); - } - else - { - bError = true; - } + ((FJsonValueNonConstNumber*)Value.Get())->AsNonConstNumber() = FCString::Atod(*LowerCase); } else { bError = true; } - break; } - case EJson::Boolean: + else if (LowerCase.Len() > ePosition + 2) { - auto LowerCase = Data.ToLower(); - if (LowerCase == TEXT("true")) + FString Left = LowerCase.Left(ePosition); + FString Rigth = LowerCase.Right(LowerCase.Len() - ePosition - 1); + if (Left.IsNumeric() && Rigth.IsNumeric()) { - ((FJsonValueNonConstBoolean*) Value.Get())->AsNonConstBool() = true; - } - else if (LowerCase == TEXT("false")) - { - ((FJsonValueNonConstBoolean*) Value.Get())->AsNonConstBool() = false; + ((FJsonValueNonConstNumber*)Value.Get())->AsNonConstNumber() = FCString::Atod(*Left) * FMath::Pow(10.f, FCString::Atoi(*Rigth)); } else { bError = true; } - break; } - default: + else + { + bError = true; + } + break; + } + case EJson::Boolean: + { + auto LowerCase = Data.ToLower(); + if (LowerCase == TEXT("true")) + { + ((FJsonValueNonConstBoolean*)Value.Get())->AsNonConstBool() = true; + } + else if (LowerCase == TEXT("false")) + { + ((FJsonValueNonConstBoolean*)Value.Get())->AsNonConstBool() = false; + } + else { bError = true; - return; } + break; } - + default: + { + bError = true; + return; + } + } + ClearData(); - + auto Container = Objects.Last(0); if (Container->Type == EJson::Object) { @@ -205,7 +205,7 @@ void FJSONState::PopValue(bool bCheckType) } else if (Container->Type == EJson::Array) { - ((FJsonValueNonConstArray*) Container.Get())->AsNonConstArray().Add(Value); + ((FJsonValueNonConstArray*)Container.Get())->AsNonConstArray().Add(Value); } else { @@ -239,7 +239,7 @@ FJsonValueObject* FJSONState::GetObject() FJsonValue* Value = GetLast(); if (Value != nullptr && Value->Type == EJson::Object) { - return (FJsonValueObject*) Value; + return (FJsonValueObject*)Value; } bError = true; return nullptr; @@ -250,7 +250,7 @@ FJsonValueNonConstArray* FJSONState::GetArray() FJsonValue* Value = GetLast(); if (Value != nullptr && Value->Type == EJson::Array) { - return (FJsonValueNonConstArray*) Value; + return (FJsonValueNonConstArray*)Value; } bError = true; return nullptr; @@ -337,7 +337,6 @@ void FJSONState::Error() FJSONReader::FJSONReader() { - } bool FJSONReader::IsNewLine(const TCHAR& Char) @@ -356,17 +355,17 @@ bool FJSONReader::FindToken(const TCHAR& Char) { return false; } - + if (State.Notation != EJSONNotation::STRING) { - switch(Char) + switch (Char) { - case '{': State.Tokens.Add(EJSONToken::CURLY_BEGIN); return true; - case '}': State.Tokens.Add(EJSONToken::CURLY_END); return true; - case '[': State.Tokens.Add(EJSONToken::SQUARE_BEGIN); return true; - case ']': State.Tokens.Add(EJSONToken::SQUARE_END); return true; - case ',': State.Tokens.Add(EJSONToken::COMMA); return true; - case ':': State.Tokens.Add(EJSONToken::COLON); return true; + case '{': State.Tokens.Add(EJSONToken::CURLY_BEGIN); return true; + case '}': State.Tokens.Add(EJSONToken::CURLY_END); return true; + case '[': State.Tokens.Add(EJSONToken::SQUARE_BEGIN); return true; + case ']': State.Tokens.Add(EJSONToken::SQUARE_END); return true; + case ',': State.Tokens.Add(EJSONToken::COMMA); return true; + case ':': State.Tokens.Add(EJSONToken::COLON); return true; } } return false; @@ -374,214 +373,214 @@ bool FJSONReader::FindToken(const TCHAR& Char) void FJSONReader::UpdateNotation() { - switch(State.GetToken()) + switch (State.GetToken()) + { + case EJSONToken::ROOT: { - case EJSONToken::ROOT: + return; + } + case EJSONToken::CURLY_BEGIN: + { + if (State.CheckTokens(EJSONToken::SQUARE_BEGIN, EJSONToken::CURLY_BEGIN)) // Object in array "[{" { - return; + State.Notation = EJSONNotation::OBJECT; + auto Value = State.GetArray(); + if (Value != nullptr) + { + Value->AsNonConstArray().Add(State.PushObject()); + } + else + { + State.Error(); + } } - case EJSONToken::CURLY_BEGIN: + else if (State.CheckTokens(EJSONToken::CURLY_BEGIN, EJSONToken::COLON, EJSONToken::CURLY_BEGIN)) // Object in key "{:{" { - if (State.CheckTokens(EJSONToken::SQUARE_BEGIN, EJSONToken::CURLY_BEGIN)) // Object in array "[{" + if (State.Key.Len() > 0) { State.Notation = EJSONNotation::OBJECT; - auto Value = State.GetArray(); + auto Value = State.GetObject(); if (Value != nullptr) { - Value->AsNonConstArray().Add(State.PushObject()); + Value->AsObject()->SetField(State.Key, State.PushObject()); + State.ClearKey(); } else { State.Error(); } } - else if (State.CheckTokens(EJSONToken::CURLY_BEGIN, EJSONToken::COLON, EJSONToken::CURLY_BEGIN)) // Object in key "{:{" + else { - if (State.Key.Len() > 0) - { - State.Notation = EJSONNotation::OBJECT; - auto Value = State.GetObject(); - if (Value != nullptr) - { - Value->AsObject()->SetField(State.Key, State.PushObject()); - State.ClearKey(); - } - else - { - State.Error(); - } - } - else - { - State.Error(); - } + State.Error(); } - else if (State.CheckTokens(EJSONToken::ROOT, EJSONToken::CURLY_BEGIN)) // Root object "{" + } + else if (State.CheckTokens(EJSONToken::ROOT, EJSONToken::CURLY_BEGIN)) // Root object "{" + { + if (State.Root.IsValid()) { - if (State.Root.IsValid()) - { - State.Error(); - } - else - { - State.Root = TSharedPtr(new FJsonObject()); - State.PushObject(State.Root); // add root object - State.Notation = EJSONNotation::OBJECT; - } + State.Error(); } else { - State.Error(); + State.Root = TSharedPtr(new FJsonObject()); + State.PushObject(State.Root); // add root object + State.Notation = EJSONNotation::OBJECT; } - break; } - case EJSONToken::CURLY_END: + else { - if (State.CheckTokens(EJSONToken::CURLY_BEGIN, EJSONToken::CURLY_END)) // Close object "{}" - { - State.PopToken(2); // pop "{}" - State.PopObject(); // remove object - } - else if (State.CheckTokens(EJSONToken::CURLY_BEGIN, EJSONToken::COLON, EJSONToken::CURLY_END)) // Close object "{:}" + State.Error(); + } + break; + } + case EJSONToken::CURLY_END: + { + if (State.CheckTokens(EJSONToken::CURLY_BEGIN, EJSONToken::CURLY_END)) // Close object "{}" + { + State.PopToken(2); // pop "{}" + State.PopObject(); // remove object + } + else if (State.CheckTokens(EJSONToken::CURLY_BEGIN, EJSONToken::COLON, EJSONToken::CURLY_END)) // Close object "{:}" + { + State.PopToken(3); // pop "{:}" + State.PopValue(); // remove value + State.PopObject(); // remove object + } + else + { + State.Error(); + } + + if (State.CheckTokens(EJSONToken::COLON)) // Object in object ":" + { + State.PopToken(1); // pop ":" + } + + State.Notation = EJSONNotation::SKIP; + + break; + } + case EJSONToken::SQUARE_BEGIN: + { + if (State.CheckTokens(EJSONToken::SQUARE_BEGIN, EJSONToken::SQUARE_BEGIN)) // Array in array "[[" + { + State.Notation = EJSONNotation::ARRAY; + auto Value = State.GetArray(); + if (Value != nullptr) { - State.PopToken(3); // pop "{:}" - State.PopValue(); // remove value - State.PopObject(); // remove object + Value->AsNonConstArray().Add(State.PushArray()); } else { State.Error(); } - - if (State.CheckTokens(EJSONToken::COLON)) // Object in object ":" - { - State.PopToken(1); // pop ":" - } - - State.Notation = EJSONNotation::SKIP; - - break; } - case EJSONToken::SQUARE_BEGIN: + else if (State.CheckTokens(EJSONToken::CURLY_BEGIN, EJSONToken::COLON, EJSONToken::SQUARE_BEGIN)) // Array in key "{:[" { - if (State.CheckTokens(EJSONToken::SQUARE_BEGIN, EJSONToken::SQUARE_BEGIN)) // Array in array "[[" + State.Notation = EJSONNotation::ARRAY; + if (State.Key.Len() > 0) { - State.Notation = EJSONNotation::ARRAY; - auto Value = State.GetArray(); + auto Value = State.GetObject(); if (Value != nullptr) { - Value->AsNonConstArray().Add(State.PushArray()); + Value->AsObject()->SetField(State.Key, State.PushArray()); + State.ClearKey(); } else { State.Error(); } } - else if (State.CheckTokens(EJSONToken::CURLY_BEGIN, EJSONToken::COLON, EJSONToken::SQUARE_BEGIN)) // Array in key "{:[" - { - State.Notation = EJSONNotation::ARRAY; - if (State.Key.Len() > 0) - { - auto Value = State.GetObject(); - if (Value != nullptr) - { - Value->AsObject()->SetField(State.Key, State.PushArray()); - State.ClearKey(); - } - else - { - State.Error(); - } - } - else - { - State.Error(); - } - } - else if (State.CheckTokens(EJSONToken::ROOT, EJSONToken::SQUARE_BEGIN)) // Root array "{" - { - State.Error(); // Not support - } else { State.Error(); } - break; } - case EJSONToken::SQUARE_END: + else if (State.CheckTokens(EJSONToken::ROOT, EJSONToken::SQUARE_BEGIN)) // Root array "{" { - if (State.CheckTokens(EJSONToken::SQUARE_BEGIN, EJSONToken::SQUARE_END)) // Close array "[]" - { - State.PopToken(2); // remove token "[]" - State.PopValue(false); // remove value if exists - State.PopArray(); // remove array - - if (State.CheckTokens(EJSONToken::COLON)) // Array in object ":" - { - State.PopToken(1); // pop ":" - } - } - else - { - State.Error(); - } - - State.Notation = EJSONNotation::SKIP; - - break; + State.Error(); // Not support } - case EJSONToken::COMMA: + else { - if (State.CheckTokens(EJSONToken::CURLY_BEGIN, EJSONToken::COLON, EJSONToken::COMMA)) // Next record in object "{:," - { - State.PopToken(2); // remove token ":," - State.PopValue(false); // remove value - State.Notation = EJSONNotation::OBJECT; - } - else if (State.CheckTokens(EJSONToken::CURLY_BEGIN, EJSONToken::COMMA)) // Next record in object "{," - { - State.PopToken(1); // remove token "," - State.Notation = EJSONNotation::OBJECT; - } - else if (State.CheckTokens(EJSONToken::SQUARE_BEGIN, EJSONToken::COMMA)) // Next record in array "[," - { - State.PopToken(1); // remove token "," - State.PopValue(false); // remove value - State.Notation = EJSONNotation::ARRAY; - } - else + State.Error(); + } + break; + } + case EJSONToken::SQUARE_END: + { + if (State.CheckTokens(EJSONToken::SQUARE_BEGIN, EJSONToken::SQUARE_END)) // Close array "[]" + { + State.PopToken(2); // remove token "[]" + State.PopValue(false); // remove value if exists + State.PopArray(); // remove array + + if (State.CheckTokens(EJSONToken::COLON)) // Array in object ":" { - State.Error(); + State.PopToken(1); // pop ":" } - break; } - case EJSONToken::COLON: + else + { + State.Error(); + } + + State.Notation = EJSONNotation::SKIP; + + break; + } + case EJSONToken::COMMA: + { + if (State.CheckTokens(EJSONToken::CURLY_BEGIN, EJSONToken::COLON, EJSONToken::COMMA)) // Next record in object "{:," { - if (State.CheckTokens(EJSONToken::CURLY_BEGIN, EJSONToken::COLON)) // Object key close "{:" + State.PopToken(2); // remove token ":," + State.PopValue(false); // remove value + State.Notation = EJSONNotation::OBJECT; + } + else if (State.CheckTokens(EJSONToken::CURLY_BEGIN, EJSONToken::COMMA)) // Next record in object "{," + { + State.PopToken(1); // remove token "," + State.Notation = EJSONNotation::OBJECT; + } + else if (State.CheckTokens(EJSONToken::SQUARE_BEGIN, EJSONToken::COMMA)) // Next record in array "[," + { + State.PopToken(1); // remove token "," + State.PopValue(false); // remove value + State.Notation = EJSONNotation::ARRAY; + } + else + { + State.Error(); + } + break; + } + case EJSONToken::COLON: + { + if (State.CheckTokens(EJSONToken::CURLY_BEGIN, EJSONToken::COLON)) // Object key close "{:" + { + State.Notation = EJSONNotation::OBJECT; + if (State.Data.Len() > 0) { - State.Notation = EJSONNotation::OBJECT; - if (State.Data.Len() > 0) - { - State.DataToKey(); - } - else - { - State.Error(); - } + State.DataToKey(); } else { State.Error(); } - break; } - case EJSONToken::ERROR: + else { State.Error(); - break; } + break; } - + case EJSONToken::ERROR: + { + State.Error(); + break; + } + } + if (!State.bError && State.Notation == EJSONNotation::NONE) { UpdateNotation(); @@ -595,7 +594,7 @@ void FJSONReader::ReadAsString(const TCHAR& Char) State.Error(); return; } - + if (!State.bEscape && State.Quote == Char) { State.Quote = UNICODE_BOGUS_CHAR_CODEPOINT; @@ -605,11 +604,11 @@ void FJSONReader::ReadAsString(const TCHAR& Char) { if (State.bEscape) { - switch(Char) + switch (Char) { - case 'n': State.Data.AppendChar('\n'); break; - case 't': State.Data.AppendChar('\t'); break; - default: State.Data.AppendChar(Char); break; + case 'n': State.Data.AppendChar('\n'); break; + case 't': State.Data.AppendChar('\t'); break; + default: State.Data.AppendChar(Char); break; } } else @@ -626,7 +625,7 @@ void FJSONReader::ReadAsStringSpecial(const TCHAR& Char) State.Notation = EJSONNotation::SKIP; return; } - + State.Data.AppendChar(Char); } @@ -637,7 +636,7 @@ void FJSONReader::ReadAsNumber(const TCHAR& Char) State.Notation = EJSONNotation::SKIP; return; } - + if ((Char >= '0' && Char <= '9') || Char == '-' || Char == '.' || Char == '+' || Char == 'e' || Char == 'E') { State.Data.AppendChar(Char); @@ -650,36 +649,36 @@ void FJSONReader::ReadAsNumber(const TCHAR& Char) void FJSONReader::ReadBasicValue(const TCHAR& Char) { - switch(Char) + switch (Char) { - case 'T': - case 't': - case 'F': - case 'f': - { - State.PushBoolean(); - State.Notation = EJSONNotation::STRING_SPECIAL; - ReadAsStringSpecial(Char); - return; - } - case 'N': - case 'n': - { - State.PushNull(); - State.Notation = EJSONNotation::STRING_SPECIAL; - ReadAsStringSpecial(Char); - return; - } - case '\'': - case '"': - { - State.PushString(); - State.Notation = EJSONNotation::STRING; - State.Quote = Char; - return; - } + case 'T': + case 't': + case 'F': + case 'f': + { + State.PushBoolean(); + State.Notation = EJSONNotation::STRING_SPECIAL; + ReadAsStringSpecial(Char); + return; + } + case 'N': + case 'n': + { + State.PushNull(); + State.Notation = EJSONNotation::STRING_SPECIAL; + ReadAsStringSpecial(Char); + return; + } + case '\'': + case '"': + { + State.PushString(); + State.Notation = EJSONNotation::STRING; + State.Quote = Char; + return; } - + } + if ((Char >= '0' && Char <= '9') || Char == '-') { State.PushNumber(); @@ -704,7 +703,7 @@ void FJSONReader::ReadAsObject(const TCHAR& Char) { return; } - + if (State.CheckTokens(EJSONToken::CURLY_BEGIN)) // read key "{" { if (Char == '\'' || Char == '"') @@ -739,62 +738,61 @@ bool FJSONReader::Read(const TCHAR Char) State.bEscape = true; return true; } - + if (FindToken(Char)) { State.Notation = EJSONNotation::NONE; UpdateNotation(); return true; } - - switch(State.Notation) + + switch (State.Notation) { - case EJSONNotation::NONE: UpdateNotation(); break; - - case EJSONNotation::STRING: ReadAsString(Char); break; - case EJSONNotation::STRING_SPECIAL: ReadAsStringSpecial(Char); break; - case EJSONNotation::NUMBER: ReadAsNumber(Char); break; - case EJSONNotation::ARRAY: ReadAsArray(Char); break; - case EJSONNotation::OBJECT: ReadAsObject(Char); break; - - case EJSONNotation::SKIP: Skip(Char); break; + case EJSONNotation::NONE: UpdateNotation(); break; + + case EJSONNotation::STRING: ReadAsString(Char); break; + case EJSONNotation::STRING_SPECIAL: ReadAsStringSpecial(Char); break; + case EJSONNotation::NUMBER: ReadAsNumber(Char); break; + case EJSONNotation::ARRAY: ReadAsArray(Char); break; + case EJSONNotation::OBJECT: ReadAsObject(Char); break; + + case EJSONNotation::SKIP: Skip(Char); break; } - + if (State.bError) { State.Root = TSharedPtr(nullptr); State.Size = 0; return false; } - + State.bEscape = false; - + return true; } FJSONWriter::FJSONWriter() { - } bool FJSONWriter::GetStartChar(const TSharedPtr& JsonValue, FString& Str) { switch (JsonValue->Type) { - case EJson::Object: - Str = FString(TEXT("{")); - break; - case EJson::Array: - Str = FString(TEXT("[")); - break; - case EJson::String: - Str = FString(TEXT("\"")); - break; - default: - return false; - break; + case EJson::Object: + Str = FString(TEXT("{")); + break; + case EJson::Array: + Str = FString(TEXT("[")); + break; + case EJson::String: + Str = FString(TEXT("\"")); + break; + default: + return false; + break; } - + return true; } @@ -802,20 +800,20 @@ bool FJSONWriter::GetEndChar(const TSharedPtr& JsonValue, FString& S { switch (JsonValue->Type) { - case EJson::Object: - Str = FString(TEXT("}")); - break; - case EJson::Array: - Str = FString(TEXT("]")); - break; - case EJson::String: - Str = FString(TEXT("\"")); - break; - default: - return false; - break; + case EJson::Object: + Str = FString(TEXT("}")); + break; + case EJson::Array: + Str = FString(TEXT("]")); + break; + case EJson::String: + Str = FString(TEXT("\"")); + break; + default: + return false; + break; } - + return true; } @@ -823,108 +821,108 @@ void FJSONWriter::Write(TSharedPtr JsonValue, FArchive* Writer, bool { FString Str; FArchive& Ar = *Writer; - + if (GetStartChar(JsonValue, Str)) { UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); } - + switch (JsonValue->Type) { - case EJson::Object: + case EJson::Object: + { + int ElementsCount = 0; + auto Values = JsonValue->AsObject()->Values; + + for (auto& ChildJsonPair : Values) { - int ElementsCount = 0; - auto Values = JsonValue->AsObject()->Values; - - for (auto& ChildJsonPair : Values) + Str = FString(TEXT("\"")); + UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); + + const TCHAR* BufferPtr = *ChildJsonPair.Key; + for (int i = 0; i < ChildJsonPair.Key.Len(); ++i) { - Str = FString(TEXT("\"")); - UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); - - const TCHAR* BufferPtr = *ChildJsonPair.Key; - for (int i = 0; i < ChildJsonPair.Key.Len(); ++i) - { - Str = FString(1, &ChildJsonPair.Key[i]); + Str = FString(1, &ChildJsonPair.Key[i]); #if PLATFORM_WINDOWS - UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len() - 1); + UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len() - 1); #else - UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); -#endif - } - - Str = FString(TEXT("\"")); UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); - - Str = FString(TEXT(":")); - UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); - - ++ElementsCount; - - Write(ChildJsonPair.Value, Writer, ElementsCount >= Values.Num()); +#endif } - break; + + Str = FString(TEXT("\"")); + UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); + + Str = FString(TEXT(":")); + UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); + + ++ElementsCount; + + Write(ChildJsonPair.Value, Writer, ElementsCount >= Values.Num()); + } + break; + } + case EJson::Array: + { + int ElementsCount = 0; + auto Array = JsonValue->AsArray(); + + for (auto& ChildJsonValue : Array) + { + ++ElementsCount; + Write(ChildJsonValue, Writer, ElementsCount >= Array.Num()); } - case EJson::Array: + break; + } + default: + { + FString Value = JsonValue->AsString(); + + const TCHAR* BufferPtr = *Value; + for (int i = 0; i < Value.Len(); ++i) { - int ElementsCount = 0; - auto Array = JsonValue->AsArray(); - - for (auto& ChildJsonValue : Array) + Str = FString(1, &BufferPtr[i]); + if (Str == TEXT("\"")) { - ++ElementsCount; - Write(ChildJsonValue, Writer, ElementsCount >= Array.Num()); + Str = FString(TEXT("\\")); + UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); + Str = FString(1, &BufferPtr[i]); } - break; - } - default: - { - FString Value = JsonValue->AsString(); - - const TCHAR* BufferPtr = *Value; - for (int i = 0; i < Value.Len(); ++i) + if (Str == TEXT("\n")) { + Str = FString(TEXT("\\")); + UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); + Str = FString(TEXT("n")); + UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); Str = FString(1, &BufferPtr[i]); - if (Str == TEXT("\"")) - { - Str = FString(TEXT("\\")); - UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); - Str = FString(1, &BufferPtr[i]); - } - if (Str == TEXT("\n")) - { - Str = FString(TEXT("\\")); - UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); - Str = FString(TEXT("n")); - UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); - Str = FString(1, &BufferPtr[i]); - } - else if (Str == TEXT("\t")) - { - Str = FString(TEXT("\\")); - UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); - Str = FString(TEXT("t")); - UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); - Str = FString(1, &BufferPtr[i]); - } - else - { + } + else if (Str == TEXT("\t")) + { + Str = FString(TEXT("\\")); + UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); + Str = FString(TEXT("t")); + UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); + Str = FString(1, &BufferPtr[i]); + } + else + { #if PLATFORM_WINDOWS - UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len() - 1); + UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len() - 1); #else - UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); + UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); #endif - } } - - break; } + + break; + } } - + if (GetEndChar(JsonValue, Str)) { UVaRestJsonObject::WriteStringToArchive(Ar, *Str, Str.Len()); } - + if (!IsLastElement) { Str = FString(TEXT(",")); diff --git a/Source/VaRestPlugin/Private/VaRestJsonParser.h b/Source/VaRestPlugin/Private/VaRestJsonParser.h index 2595158d..c1b7cd30 100644 --- a/Source/VaRestPlugin/Private/VaRestJsonParser.h +++ b/Source/VaRestPlugin/Private/VaRestJsonParser.h @@ -7,8 +7,11 @@ class FJsonValueNonConstArray : public FJsonValueArray { public: - FJsonValueNonConstArray(const TArray>& InArray) : FJsonValueArray(InArray) {} - + FJsonValueNonConstArray(const TArray>& InArray) + : FJsonValueArray(InArray) + { + } + /** return non const array */ TArray>& AsNonConstArray() { return Value; } }; @@ -16,8 +19,11 @@ class FJsonValueNonConstArray : public FJsonValueArray class FJsonValueNonConstBoolean : public FJsonValueBoolean { public: - FJsonValueNonConstBoolean(bool InBool) : FJsonValueBoolean(InBool) {} - + FJsonValueNonConstBoolean(bool InBool) + : FJsonValueBoolean(InBool) + { + } + /** return non const bool */ bool& AsNonConstBool() { return Value; } }; @@ -25,8 +31,11 @@ class FJsonValueNonConstBoolean : public FJsonValueBoolean class FJsonValueNonConstString : public FJsonValueString { public: - FJsonValueNonConstString(const FString& InString) : FJsonValueString(InString) {} - + FJsonValueNonConstString(const FString& InString) + : FJsonValueString(InString) + { + } + /** return non const string */ FString& AsNonConstString() { return Value; } }; @@ -34,8 +43,11 @@ class FJsonValueNonConstString : public FJsonValueString class FJsonValueNonConstNumber : public FJsonValueNumber { public: - FJsonValueNonConstNumber(double InNumber) : FJsonValueNumber(InNumber) {} - + FJsonValueNonConstNumber(double InNumber) + : FJsonValueNumber(InNumber) + { + } + /** return non const number */ double& AsNonConstNumber() { return Value; } }; @@ -67,129 +79,125 @@ struct FJSONState { /** Key */ FString Key; - + /** Data */ FString Data; - + /** Root object */ TSharedPtr Root; - + /** Object list */ TArray> Objects; - + /** Tokens */ TArray Tokens; - + /** Notation */ EJSONNotation Notation; - + /** Current char has escape */ bool bEscape; - + /** Has error */ int32 bError; - + /** Las quote for string */ TCHAR Quote; - + /** Size */ int32 Size; - + /** Default constructor */ FJSONState(); - + EJSONToken GetToken(int32 Index = 0); - + FORCEINLINE bool CheckTokens(EJSONToken T1); - + FORCEINLINE bool CheckTokens(EJSONToken T1, EJSONToken T2); - + FORCEINLINE bool CheckTokens(EJSONToken T1, EJSONToken T2, EJSONToken T3); - + FORCEINLINE void PopToken(int32 Num); - + FORCEINLINE void PopObject(); - + FORCEINLINE void PopArray(); - + FORCEINLINE void PopValue(bool bCheckType = true); - + FORCEINLINE FJsonValue* GetLast(); - + FORCEINLINE FJsonValueObject* GetObject(); - + FORCEINLINE FJsonValueNonConstArray* GetArray(); - + FORCEINLINE TSharedPtr PushObject(); - + FORCEINLINE TSharedPtr PushObject(TSharedPtr Object); - + FORCEINLINE TSharedPtr PushArray(); - + FORCEINLINE TSharedPtr PushBoolean(); - + FORCEINLINE TSharedPtr PushNull(); - + FORCEINLINE TSharedPtr PushNumber(); - + FORCEINLINE TSharedPtr PushString(); - + FORCEINLINE void ClearData(); - + FORCEINLINE void ClearKey(); - + FORCEINLINE void DataToKey(); - + FORCEINLINE void Error(); - }; struct FJSONReader { /** State */ FJSONState State; - + /** Default constructor */ FJSONReader(); - + private: - FORCEINLINE bool IsNewLine(const TCHAR& Char); - + FORCEINLINE bool IsSpace(const TCHAR& Char); - + FORCEINLINE bool FindToken(const TCHAR& Char); - + FORCEINLINE void UpdateNotation(); - + FORCEINLINE void ReadAsString(const TCHAR& Char); - + FORCEINLINE void ReadAsStringSpecial(const TCHAR& Char); - + FORCEINLINE void ReadAsNumber(const TCHAR& Char); - + FORCEINLINE void ReadBasicValue(const TCHAR& Char); - + FORCEINLINE void ReadAsArray(const TCHAR& Char); - + FORCEINLINE void ReadAsObject(const TCHAR& Char); - + FORCEINLINE void Skip(const TCHAR& Char); - + public: - bool Read(const TCHAR Char); // @Pushkin - }; struct FJSONWriter { FJSONWriter(); - + FORCEINLINE bool GetStartChar(const TSharedPtr& JsonValue, FString& Char); - + FORCEINLINE bool GetEndChar(const TSharedPtr& JsonValue, FString& Char); - + public: void Write(TSharedPtr JsonValue, FArchive* Writer, bool IsLastElement); // @Pushkin }; diff --git a/Source/VaRestPlugin/Private/VaRestJsonValue.cpp b/Source/VaRestPlugin/Private/VaRestJsonValue.cpp index a4d5c887..6b80b25c 100644 --- a/Source/VaRestPlugin/Private/VaRestJsonValue.cpp +++ b/Source/VaRestPlugin/Private/VaRestJsonValue.cpp @@ -7,7 +7,6 @@ UVaRestJsonValue::UVaRestJsonValue(const class FObjectInitializer& PCIP) : Super(PCIP) { - } UVaRestJsonValue* UVaRestJsonValue::ConstructJsonValueNumber(UObject* WorldContextObject, float Number) @@ -43,7 +42,7 @@ UVaRestJsonValue* UVaRestJsonValue::ConstructJsonValueBool(UObject* WorldContext UVaRestJsonValue* UVaRestJsonValue::ConstructJsonValueArray(UObject* WorldContextObject, const TArray& InArray) { // Prepare data array to create new value - TArray< TSharedPtr > ValueArray; + TArray> ValueArray; for (auto InVal : InArray) { ValueArray.Add(InVal->GetRootValue()); @@ -57,7 +56,7 @@ UVaRestJsonValue* UVaRestJsonValue::ConstructJsonValueArray(UObject* WorldContex return NewValue; } -UVaRestJsonValue* UVaRestJsonValue::ConstructJsonValueObject(UObject* WorldContextObject, UVaRestJsonObject *JsonObject) +UVaRestJsonValue* UVaRestJsonValue::ConstructJsonValueObject(UObject* WorldContextObject, UVaRestJsonObject* JsonObject) { TSharedPtr NewVal = MakeShareable(new FJsonValueObject(JsonObject->GetRootObject())); @@ -87,7 +86,6 @@ void UVaRestJsonValue::SetRootValue(TSharedPtr& JsonValue) JsonVal = JsonValue; } - ////////////////////////////////////////////////////////////////////////// // FJsonValue API @@ -161,7 +159,7 @@ FString UVaRestJsonValue::GetTypeString() const } } -bool UVaRestJsonValue::IsNull() const +bool UVaRestJsonValue::IsNull() const { if (!JsonVal.IsValid()) { @@ -214,7 +212,7 @@ TArray UVaRestJsonValue::AsArray() const return OutArray; } - TArray< TSharedPtr > ValArray = JsonVal->AsArray(); + TArray> ValArray = JsonVal->AsArray(); for (auto Value : ValArray) { UVaRestJsonValue* NewValue = NewObject(); @@ -242,7 +240,6 @@ UVaRestJsonObject* UVaRestJsonValue::AsObject() return JsonObj; } - ////////////////////////////////////////////////////////////////////////// // Helpers diff --git a/Source/VaRestPlugin/Private/VaRestLibrary.cpp b/Source/VaRestPlugin/Private/VaRestLibrary.cpp index 28d8b24d..610d0db0 100644 --- a/Source/VaRestPlugin/Private/VaRestLibrary.cpp +++ b/Source/VaRestPlugin/Private/VaRestLibrary.cpp @@ -1,10 +1,10 @@ // Copyright 2016 Vladimir Alyamkin. All Rights Reserved. #include "VaRestLibrary.h" -#include "VaRestRequestJSON.h" +#include "Misc/Base64.h" #include "VaRestJsonObject.h" #include "VaRestPluginPrivatePCH.h" -#include "Misc/Base64.h" +#include "VaRestRequestJSON.h" ////////////////////////////////////////////////////////////////////////// // Helpers @@ -31,7 +31,7 @@ bool UVaRestLibrary::Base64EncodeData(const TArray& Data, FString& Dest) Dest = FBase64::Encode(Data); return true; } - + return false; } @@ -40,7 +40,6 @@ bool UVaRestLibrary::Base64DecodeData(const FString& Source, TArray& Dest return FBase64::Decode(Source, Dest); } - ////////////////////////////////////////////////////////////////////////// // File system integration @@ -68,7 +67,6 @@ class UVaRestJsonObject* UVaRestLibrary::LoadJsonFromFile(UObject* WorldContextO return nullptr; } - ////////////////////////////////////////////////////////////////////////// // Easy URL processing @@ -82,29 +80,29 @@ void UVaRestLibrary::CallURL(UObject* WorldContextObject, const FString& URL, ER UE_LOG(LogVaRest, Error, TEXT("UVaRestLibrary: Wrong world context")) return; } - + // Check we have valid data json if (VaRestJson == nullptr) { VaRestJson = UVaRestJsonObject::ConstructJsonObject(WorldContextObject); } - + UVaRestRequestJSON* Request = NewObject(); - + Request->SetVerb(Verb); Request->SetContentType(ContentType); Request->SetRequestObject(VaRestJson); - + FVaRestCallResponse Response; Response.Request = Request; Response.WorldContextObject = WorldContextObject; Response.Callback = Callback; - + Response.CompleteDelegateHandle = Request->OnStaticRequestComplete.AddStatic(&UVaRestLibrary::OnCallComplete); Response.FailDelegateHandle = Request->OnStaticRequestFail.AddStatic(&UVaRestLibrary::OnCallComplete); - + RequestMap.Add(Request, Response); - + Request->ResetResponseData(); Request->ProcessURL(URL); } @@ -115,14 +113,14 @@ void UVaRestLibrary::OnCallComplete(UVaRestRequestJSON* Request) { return; } - + FVaRestCallResponse* Response = RequestMap.Find(Request); - + Request->OnStaticRequestComplete.Remove(Response->CompleteDelegateHandle); Request->OnStaticRequestFail.Remove(Response->FailDelegateHandle); - + Response->Callback.ExecuteIfBound(Request); - + Response->WorldContextObject = nullptr; Response->Request = nullptr; RequestMap.Remove(Request); diff --git a/Source/VaRestPlugin/Private/VaRestPlugin.cpp b/Source/VaRestPlugin/Private/VaRestPlugin.cpp index dc31ac78..7bef598b 100644 --- a/Source/VaRestPlugin/Private/VaRestPlugin.cpp +++ b/Source/VaRestPlugin/Private/VaRestPlugin.cpp @@ -1,11 +1,11 @@ // Copyright 2014 Vladimir Alyamkin. All Rights Reserved. #include "VaRestPlugin.h" -#include "VaRestSettings.h" #include "VaRestJsonObject.h" #include "VaRestJsonValue.h" -#include "VaRestRequestJSON.h" #include "VaRestPluginPrivatePCH.h" +#include "VaRestRequestJSON.h" +#include "VaRestSettings.h" //#include "UObject/Package.h" //#include "Misc/ConfigCacheIni.h" @@ -30,8 +30,7 @@ class FVaRestPlugin : public IVaRestPlugin SettingsModule->RegisterSettings("Project", "Plugins", "VaRest", LOCTEXT("RuntimeSettingsName", "VaRest Kit"), LOCTEXT("RuntimeSettingsDescription", "Configure API keys for VaRest"), - GetMutableDefault() - ); + GetMutableDefault()); } } @@ -44,7 +43,7 @@ class FVaRestPlugin : public IVaRestPlugin } }; -IMPLEMENT_MODULE( FVaRestPlugin, VaRestPlugin ) +IMPLEMENT_MODULE(FVaRestPlugin, VaRestPlugin) DEFINE_LOG_CATEGORY(LogVaRest); diff --git a/Source/VaRestPlugin/Private/VaRestPluginPrivatePCH.h b/Source/VaRestPlugin/Private/VaRestPluginPrivatePCH.h index 2f4595fa..c9fd4f5a 100644 --- a/Source/VaRestPlugin/Private/VaRestPluginPrivatePCH.h +++ b/Source/VaRestPlugin/Private/VaRestPluginPrivatePCH.h @@ -6,8 +6,8 @@ #if ENGINE_MINOR_VERSION >= 15 #include "CoreMinimal.h" -#include "EngineDefines.h" #include "Engine/Engine.h" +#include "EngineDefines.h" #include "UObject/Object.h" #include "UObject/ScriptMacros.h" #else @@ -15,8 +15,8 @@ #include "Engine.h" #endif -#include "Http.h" #include "Containers/Map.h" +#include "Http.h" #include "Json.h" #include "LatentActions.h" @@ -28,6 +28,6 @@ DECLARE_LOG_CATEGORY_EXTERN(LogVaRest, Log, All); -#define VA_FUNC (FString(__FUNCTION__)) // Current Class Name + Function Name where this is called -#define VA_LINE (FString::FromInt(__LINE__)) // Current Line Number in the code where this is called -#define VA_FUNC_LINE (VA_FUNC + "(" + VA_LINE + ")") // Current Class and Line Number where this is called! +#define VA_FUNC (FString(__FUNCTION__)) // Current Class Name + Function Name where this is called +#define VA_LINE (FString::FromInt(__LINE__)) // Current Line Number in the code where this is called +#define VA_FUNC_LINE (VA_FUNC + "(" + VA_LINE + ")") // Current Class and Line Number where this is called! diff --git a/Source/VaRestPlugin/Private/VaRestRequestJSON.cpp b/Source/VaRestPlugin/Private/VaRestRequestJSON.cpp index 7d6d9b3c..70d87719 100644 --- a/Source/VaRestPlugin/Private/VaRestRequestJSON.cpp +++ b/Source/VaRestPlugin/Private/VaRestRequestJSON.cpp @@ -3,17 +3,18 @@ #include "VaRestRequestJSON.h" #include "VaRestJsonObject.h" #include "VaRestLibrary.h" -#include "VaRestSettings.h" #include "VaRestPluginPrivatePCH.h" +#include "VaRestSettings.h" #include "Misc/CoreMisc.h" #include "Runtime/Launch/Resources/Version.h" FString UVaRestRequestJSON::DeprecatedResponseString(TEXT("DEPRECATED: Please use GetResponseContentAsString() instead")); -template void FVaRestLatentAction::Cancel() +template +void FVaRestLatentAction::Cancel() { - UObject *Obj = Request.Get(); + UObject* Obj = Request.Get(); if (Obj != nullptr) { ((UVaRestRequestJSON*)Obj)->Cancel(); @@ -21,8 +22,8 @@ template void FVaRestLatentAction::Cancel() } UVaRestRequestJSON::UVaRestRequestJSON(const class FObjectInitializer& PCIP) - : Super(PCIP), - BinaryContentType(TEXT("application/octet-stream")) + : Super(PCIP) + , BinaryContentType(TEXT("application/octet-stream")) { ContinueAction = nullptr; @@ -38,8 +39,8 @@ UVaRestRequestJSON* UVaRestRequestJSON::ConstructRequest(UObject* WorldContextOb } UVaRestRequestJSON* UVaRestRequestJSON::ConstructRequestExt( - UObject* WorldContextObject, - ERequestVerb Verb, + UObject* WorldContextObject, + ERequestVerb Verb, ERequestContentType ContentType) { UVaRestRequestJSON* Request = ConstructRequest(WorldContextObject); @@ -85,7 +86,6 @@ void UVaRestRequestJSON::SetHeader(const FString& HeaderName, const FString& Hea RequestHeaders.Add(HeaderName, HeaderValue); } - ////////////////////////////////////////////////////////////////////////// // Destruction and reset @@ -141,7 +141,6 @@ void UVaRestRequestJSON::Cancel() ResetResponseData(); } - ////////////////////////////////////////////////////////////////////////// // JSON data accessors @@ -165,7 +164,6 @@ void UVaRestRequestJSON::SetResponseObject(UVaRestJsonObject* JsonObject) ResponseJsonObj = JsonObject; } - /////////////////////////////////////////////////////////////////////////// // Response data access @@ -207,7 +205,6 @@ TArray UVaRestRequestJSON::GetAllResponseHeaders() return Result; } - ////////////////////////////////////////////////////////////////////////// // URL processing @@ -223,7 +220,7 @@ void UVaRestRequestJSON::SetURL(const FString& Url) TrimmedUrl.Trim(); TrimmedUrl.TrimTrailing(); #endif - + HttpRequest->SetURL(TrimmedUrl); } @@ -233,7 +230,7 @@ void UVaRestRequestJSON::ProcessURL(const FString& Url) ProcessRequest(); } -void UVaRestRequestJSON::ApplyURL(const FString& Url, UVaRestJsonObject *&Result, UObject* WorldContextObject, FLatentActionInfo LatentInfo) +void UVaRestRequestJSON::ApplyURL(const FString& Url, UVaRestJsonObject*& Result, UObject* WorldContextObject, FLatentActionInfo LatentInfo) { // Be sure to trim URL because it can break links on iOS FString TrimmedUrl = Url; @@ -252,7 +249,7 @@ void UVaRestRequestJSON::ApplyURL(const FString& Url, UVaRestJsonObject *&Result if (UWorld* World = GEngine->GetWorldFromContextObjectChecked(WorldContextObject)) { FLatentActionManager& LatentActionManager = World->GetLatentActionManager(); - FVaRestLatentAction *Kont = LatentActionManager.FindExistingAction>(LatentInfo.CallbackTarget, LatentInfo.UUID); + FVaRestLatentAction* Kont = LatentActionManager.FindExistingAction>(LatentInfo.CallbackTarget, LatentInfo.UUID); if (Kont != nullptr) { @@ -296,7 +293,7 @@ void UVaRestRequestJSON::ProcessRequest() case ERequestVerb::PUT: HttpRequest->SetVerb(TEXT("PUT")); break; - + case ERequestVerb::DEL: HttpRequest->SetVerb(TEXT("DELETE")); break; @@ -407,7 +404,7 @@ void UVaRestRequestJSON::ProcessRequest() // Serialize data to json string FString OutputString; - TSharedRef< TJsonWriter<> > Writer = TJsonWriterFactory<>::Create(&OutputString); + TSharedRef> Writer = TJsonWriterFactory<>::Create(&OutputString); FJsonSerializer::Serialize(RequestJsonObj->GetRootObject().ToSharedRef(), Writer); // Set Json content @@ -427,7 +424,7 @@ void UVaRestRequestJSON::ProcessRequest() { HttpRequest->SetHeader(It.Key(), It.Value()); } - + // Bind event HttpRequest->OnProcessRequestComplete().BindUObject(this, &UVaRestRequestJSON::OnProcessRequestComplete); @@ -435,7 +432,6 @@ void UVaRestRequestJSON::ProcessRequest() HttpRequest->ProcessRequest(); } - ////////////////////////////////////////////////////////////////////////// // Request callbacks @@ -445,7 +441,7 @@ void UVaRestRequestJSON::OnProcessRequestComplete(FHttpRequestPtr Request, FHttp ResetResponseData(); // Check we have a response and save response code as int32 - if(Response.IsValid()) + if (Response.IsValid()) { ResponseCode = Response->GetResponseCode(); } @@ -478,21 +474,21 @@ void UVaRestRequestJSON::OnProcessRequestComplete(FHttpRequestPtr Request, FHttp ResponseHeaders.Add(Key, Value); } } - + // Try to deserialize data to JSON const TArray& Bytes = Response->GetContent(); - ResponseSize = ResponseJsonObj->DeserializeFromUTF8Bytes((const ANSICHAR*) Bytes.GetData(), Bytes.Num()); - + ResponseSize = ResponseJsonObj->DeserializeFromUTF8Bytes((const ANSICHAR*)Bytes.GetData(), Bytes.Num()); + // Decide whether the request was successful bIsValidJsonResponse = bWasSuccessful && (ResponseSize > 0); - + if (!bIsValidJsonResponse) { // Save response data as a string ResponseContent = Response->GetContentAsString(); ResponseSize = ResponseContent.GetAllocatedSize(); } - + // Log errors if (!bIsValidJsonResponse) { @@ -505,8 +501,7 @@ void UVaRestRequestJSON::OnProcessRequestComplete(FHttpRequestPtr Request, FHttp } // Broadcast the result events on next tick - AsyncTask(ENamedThreads::GameThread, [this]() - { + AsyncTask(ENamedThreads::GameThread, [this]() { OnRequestComplete.Broadcast(this); OnStaticRequestComplete.Broadcast(this); }); @@ -514,14 +509,13 @@ void UVaRestRequestJSON::OnProcessRequestComplete(FHttpRequestPtr Request, FHttp // Finish the latent action if (ContinueAction) { - FVaRestLatentAction *K = ContinueAction; + FVaRestLatentAction* K = ContinueAction; ContinueAction = nullptr; K->Call(ResponseJsonObj); } } - ////////////////////////////////////////////////////////////////////////// // Tags @@ -543,7 +537,6 @@ bool UVaRestRequestJSON::HasTag(FName Tag) const return (Tag != NAME_None) && Tags.Contains(Tag); } - ////////////////////////////////////////////////////////////////////////// // Data @@ -571,7 +564,7 @@ FString UVaRestRequestJSON::GetResponseContentAsString(bool bCacheResponseConten UE_LOG(LogVaRest, Warning, TEXT("%s: Use of uncashed getter could be slow"), *VA_FUNC_LINE); return ResponseJsonObj->EncodeJson(); } - + // Check that we haven't cached content yet if (ResponseContent == DeprecatedResponseString) { diff --git a/Source/VaRestPlugin/Private/VaRestSettings.cpp b/Source/VaRestPlugin/Private/VaRestSettings.cpp index 0f8c7e2c..e4d28ff2 100644 --- a/Source/VaRestPlugin/Private/VaRestSettings.cpp +++ b/Source/VaRestPlugin/Private/VaRestSettings.cpp @@ -5,5 +5,4 @@ UVaRestSettings::UVaRestSettings(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) { - } diff --git a/Source/VaRestPlugin/Public/VaRestPlugin.h b/Source/VaRestPlugin/Public/VaRestPlugin.h index f2419436..52b1938a 100644 --- a/Source/VaRestPlugin/Public/VaRestPlugin.h +++ b/Source/VaRestPlugin/Public/VaRestPlugin.h @@ -4,7 +4,6 @@ #include "Modules/ModuleManager.h" - /** * The public interface to this module. In most cases, this interface is only public to sibling modules * within this plugin. @@ -13,7 +12,6 @@ class IVaRestPlugin : public IModuleInterface { public: - /** * Singleton-like access to this module's interface. This is just for convenience! * Beware of calling this during the shutdown phase, though. Your module might have been unloaded already. @@ -22,7 +20,7 @@ class IVaRestPlugin : public IModuleInterface */ static inline IVaRestPlugin& Get() { - return FModuleManager::LoadModuleChecked< IVaRestPlugin >( "VaRestPlugin" ); + return FModuleManager::LoadModuleChecked("VaRestPlugin"); } /** @@ -32,7 +30,6 @@ class IVaRestPlugin : public IModuleInterface */ static inline bool IsAvailable() { - return FModuleManager::Get().IsModuleLoaded( "VaRestPlugin" ); + return FModuleManager::Get().IsModuleLoaded("VaRestPlugin"); } }; - From 168b37eb14a0a7eaf04fb720ad207d8f33cc64e5 Mon Sep 17 00:00:00 2001 From: "v.alyamkin" Date: Fri, 18 Jan 2019 08:44:19 +0300 Subject: [PATCH 02/10] Enum classes replacement for old-style namespaced enums --- Source/VaRestPlugin/Classes/VaRestJsonValue.h | 7 ++----- Source/VaRestPlugin/Private/VaRestJsonValue.cpp | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Source/VaRestPlugin/Classes/VaRestJsonValue.h b/Source/VaRestPlugin/Classes/VaRestJsonValue.h index b5b163e2..9b5036b2 100644 --- a/Source/VaRestPlugin/Classes/VaRestJsonValue.h +++ b/Source/VaRestPlugin/Classes/VaRestJsonValue.h @@ -12,9 +12,7 @@ class FJsonValue; * Represents all the types a Json Value can be. */ UENUM(BlueprintType) -namespace EVaJson -{ -enum Type +enum class EVaJson : uint8 { None, Null, @@ -24,7 +22,6 @@ enum Type Array, Object, }; -} /** * Blueprintable FJsonValue wrapper @@ -69,7 +66,7 @@ class VARESTPLUGIN_API UVaRestJsonValue : public UObject /** Get type of Json value (Enum) */ UFUNCTION(BlueprintCallable, Category = "VaRest|Json") - EVaJson::Type GetType() const; + EVaJson GetType() const; /** Get type of Json value (String) */ UFUNCTION(BlueprintCallable, Category = "VaRest|Json") diff --git a/Source/VaRestPlugin/Private/VaRestJsonValue.cpp b/Source/VaRestPlugin/Private/VaRestJsonValue.cpp index 6b80b25c..dfe20434 100644 --- a/Source/VaRestPlugin/Private/VaRestJsonValue.cpp +++ b/Source/VaRestPlugin/Private/VaRestJsonValue.cpp @@ -89,7 +89,7 @@ void UVaRestJsonValue::SetRootValue(TSharedPtr& JsonValue) ////////////////////////////////////////////////////////////////////////// // FJsonValue API -EVaJson::Type UVaRestJsonValue::GetType() const +EVaJson UVaRestJsonValue::GetType() const { if (!JsonVal.IsValid()) { From 52b91f06283e6be11be0e67c5588b47512701b10 Mon Sep 17 00:00:00 2001 From: "v.alyamkin" Date: Wed, 30 Jan 2019 10:19:51 +0300 Subject: [PATCH 03/10] Minor copyrights fix --- Source/VaRestPlugin/Private/VaRestJsonParser.cpp | 2 +- Source/VaRestPlugin/Private/VaRestJsonParser.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/VaRestPlugin/Private/VaRestJsonParser.cpp b/Source/VaRestPlugin/Private/VaRestJsonParser.cpp index 2c3ab7bc..6a83aedc 100644 --- a/Source/VaRestPlugin/Private/VaRestJsonParser.cpp +++ b/Source/VaRestPlugin/Private/VaRestJsonParser.cpp @@ -1,4 +1,4 @@ -// Copyright 2015-2017 Mail.Ru Group. All Rights Reserved. +// Copyright 2015-2019 Mail.Ru Group. All Rights Reserved. #include "VaRestJsonParser.h" #include "Dom/JsonObject.h" diff --git a/Source/VaRestPlugin/Private/VaRestJsonParser.h b/Source/VaRestPlugin/Private/VaRestJsonParser.h index c1b7cd30..ce25ff59 100644 --- a/Source/VaRestPlugin/Private/VaRestJsonParser.h +++ b/Source/VaRestPlugin/Private/VaRestJsonParser.h @@ -1,4 +1,4 @@ -// Copyright 2015-2017 Mail.Ru Group. All Rights Reserved. +// Copyright 2015-2019 Mail.Ru Group. All Rights Reserved. #pragma once From b052938b87963e3713c4a6bebad18813bd083388 Mon Sep 17 00:00:00 2001 From: "v.alyamkin" Date: Thu, 7 Feb 2019 09:09:17 +0300 Subject: [PATCH 04/10] Incremental parses is an option now --- .../VaRestPlugin/Classes/VaRestJsonObject.h | 2 +- .../VaRestPlugin/Private/VaRestJsonObject.cpp | 20 +++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Source/VaRestPlugin/Classes/VaRestJsonObject.h b/Source/VaRestPlugin/Classes/VaRestJsonObject.h index 7ec9e8a1..06efb2fa 100644 --- a/Source/VaRestPlugin/Classes/VaRestJsonObject.h +++ b/Source/VaRestPlugin/Classes/VaRestJsonObject.h @@ -43,7 +43,7 @@ class VARESTPLUGIN_API UVaRestJsonObject : public UObject /** Construct Json object from string */ UFUNCTION(BlueprintCallable, Category = "VaRest|Json") - bool DecodeJson(const FString& JsonString); + bool DecodeJson(const FString& JsonString, bool bUseIncrementalParser = true); ////////////////////////////////////////////////////////////////////////// // FJsonObject API diff --git a/Source/VaRestPlugin/Private/VaRestJsonObject.cpp b/Source/VaRestPlugin/Private/VaRestJsonObject.cpp index a84b0917..7dead66e 100644 --- a/Source/VaRestPlugin/Private/VaRestJsonObject.cpp +++ b/Source/VaRestPlugin/Private/VaRestJsonObject.cpp @@ -77,13 +77,25 @@ FString UVaRestJsonObject::EncodeJsonToSingleString() const return OutputString; } -bool UVaRestJsonObject::DecodeJson(const FString& JsonString) +bool UVaRestJsonObject::DecodeJson(const FString& JsonString, bool bUseIncrementalParser) { - DeserializeFromTCHARBytes(JsonString.GetCharArray().GetData(), JsonString.Len()); + if (bUseIncrementalParser) + { + int32 BytesRead = DeserializeFromTCHARBytes(JsonString.GetCharArray().GetData(), JsonString.Len()); - if (JsonObj.IsValid()) + // JsonObj is always valid, but read bytes is zero when something went wrong + if (BytesRead > 0) + { + return true; + } + } + else { - return true; + TSharedRef> Reader = TJsonReaderFactory<>::Create(*JsonString); + if (FJsonSerializer::Deserialize(Reader, JsonObj) && JsonObj.IsValid()) + { + return true; + } } // If we've failed to deserialize the string, we should clear our internal data From b3dbe00a6b4fe06562a919f612cb5a8c0018b1bd Mon Sep 17 00:00:00 2001 From: Maxim Fedorenko Date: Fri, 22 Feb 2019 14:25:17 +0300 Subject: [PATCH 05/10] Add missed const qualifier --- Source/VaRestPlugin/Classes/VaRestJsonObject.h | 2 +- Source/VaRestPlugin/Private/VaRestJsonObject.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/VaRestPlugin/Classes/VaRestJsonObject.h b/Source/VaRestPlugin/Classes/VaRestJsonObject.h index 06efb2fa..a54cb053 100644 --- a/Source/VaRestPlugin/Classes/VaRestJsonObject.h +++ b/Source/VaRestPlugin/Classes/VaRestJsonObject.h @@ -28,7 +28,7 @@ class VARESTPLUGIN_API UVaRestJsonObject : public UObject TSharedPtr& GetRootObject(); /** Set the root Json object */ - void SetRootObject(TSharedPtr& JsonObject); + void SetRootObject(const TSharedPtr& JsonObject); ////////////////////////////////////////////////////////////////////////// // Serialization diff --git a/Source/VaRestPlugin/Private/VaRestJsonObject.cpp b/Source/VaRestPlugin/Private/VaRestJsonObject.cpp index 7dead66e..dd0a8581 100644 --- a/Source/VaRestPlugin/Private/VaRestJsonObject.cpp +++ b/Source/VaRestPlugin/Private/VaRestJsonObject.cpp @@ -34,7 +34,7 @@ TSharedPtr& UVaRestJsonObject::GetRootObject() return JsonObj; } -void UVaRestJsonObject::SetRootObject(TSharedPtr& JsonObject) +void UVaRestJsonObject::SetRootObject(const TSharedPtr& JsonObject) { if (JsonObject.IsValid()) { From f534cf83f09af54139660abb70c6f534faae6830 Mon Sep 17 00:00:00 2001 From: Maxim Fedorenko Date: Thu, 28 Mar 2019 10:49:10 +0300 Subject: [PATCH 06/10] Make sure that VaRestJsonObject root object is always valid and reduce unnecessary validations --- .../VaRestPlugin/Classes/VaRestJsonObject.h | 4 +- .../VaRestPlugin/Private/VaRestJsonObject.cpp | 150 ++++++++---------- .../Private/VaRestRequestJSON.cpp | 21 ++- 3 files changed, 75 insertions(+), 100 deletions(-) diff --git a/Source/VaRestPlugin/Classes/VaRestJsonObject.h b/Source/VaRestPlugin/Classes/VaRestJsonObject.h index a54cb053..430ca09c 100644 --- a/Source/VaRestPlugin/Classes/VaRestJsonObject.h +++ b/Source/VaRestPlugin/Classes/VaRestJsonObject.h @@ -25,7 +25,7 @@ class VARESTPLUGIN_API UVaRestJsonObject : public UObject void Reset(); /** Get the root Json object */ - TSharedPtr& GetRootObject(); + TSharedRef& GetRootObject(); /** Set the root Json object */ void SetRootObject(const TSharedPtr& JsonObject); @@ -185,5 +185,5 @@ class VARESTPLUGIN_API UVaRestJsonObject : public UObject private: /** Internal JSON data */ - TSharedPtr JsonObj; + TSharedRef JsonObj; }; diff --git a/Source/VaRestPlugin/Private/VaRestJsonObject.cpp b/Source/VaRestPlugin/Private/VaRestJsonObject.cpp index dd0a8581..366525c7 100644 --- a/Source/VaRestPlugin/Private/VaRestJsonObject.cpp +++ b/Source/VaRestPlugin/Private/VaRestJsonObject.cpp @@ -10,8 +10,8 @@ typedef TJsonWriter> FCondensedJsonStrin UVaRestJsonObject::UVaRestJsonObject(const class FObjectInitializer& PCIP) : Super(PCIP) + , JsonObj(MakeShared()) { - Reset(); } UVaRestJsonObject* UVaRestJsonObject::ConstructJsonObject(UObject* WorldContextObject) @@ -21,15 +21,10 @@ UVaRestJsonObject* UVaRestJsonObject::ConstructJsonObject(UObject* WorldContextO void UVaRestJsonObject::Reset() { - if (JsonObj.IsValid()) - { - JsonObj.Reset(); - } - - JsonObj = MakeShareable(new FJsonObject()); + JsonObj = MakeShared(); } -TSharedPtr& UVaRestJsonObject::GetRootObject() +TSharedRef& UVaRestJsonObject::GetRootObject() { return JsonObj; } @@ -38,7 +33,7 @@ void UVaRestJsonObject::SetRootObject(const TSharedPtr& JsonObject) { if (JsonObject.IsValid()) { - JsonObj = JsonObject; + JsonObj = JsonObject.ToSharedRef(); } else { @@ -52,14 +47,9 @@ void UVaRestJsonObject::SetRootObject(const TSharedPtr& JsonObject) FString UVaRestJsonObject::EncodeJson() const { - if (!JsonObj.IsValid()) - { - return TEXT(""); - } - FString OutputString; TSharedRef Writer = FCondensedJsonStringWriterFactory::Create(&OutputString); - FJsonSerializer::Serialize(JsonObj.ToSharedRef(), Writer); + FJsonSerializer::Serialize(JsonObj, Writer); return OutputString; } @@ -92,8 +82,10 @@ bool UVaRestJsonObject::DecodeJson(const FString& JsonString, bool bUseIncrement else { TSharedRef> Reader = TJsonReaderFactory<>::Create(*JsonString); - if (FJsonSerializer::Deserialize(Reader, JsonObj) && JsonObj.IsValid()) + TSharedPtr OutJsonObj; + if (FJsonSerializer::Deserialize(Reader, OutJsonObj)) { + JsonObj = OutJsonObj.ToSharedRef(); return true; } } @@ -112,12 +104,6 @@ bool UVaRestJsonObject::DecodeJson(const FString& JsonString, bool bUseIncrement TArray UVaRestJsonObject::GetFieldNames() const { TArray Result; - - if (!JsonObj.IsValid()) - { - return Result; - } - JsonObj->Values.GetKeys(Result); return Result; @@ -125,7 +111,7 @@ TArray UVaRestJsonObject::GetFieldNames() const bool UVaRestJsonObject::HasField(const FString& FieldName) const { - if (!JsonObj.IsValid() || FieldName.IsEmpty()) + if (FieldName.IsEmpty()) { return false; } @@ -135,7 +121,7 @@ bool UVaRestJsonObject::HasField(const FString& FieldName) const void UVaRestJsonObject::RemoveField(const FString& FieldName) { - if (!JsonObj.IsValid() || FieldName.IsEmpty()) + if (FieldName.IsEmpty()) { return; } @@ -145,7 +131,7 @@ void UVaRestJsonObject::RemoveField(const FString& FieldName) UVaRestJsonValue* UVaRestJsonObject::GetField(const FString& FieldName) const { - if (!JsonObj.IsValid() || FieldName.IsEmpty()) + if (FieldName.IsEmpty()) { return nullptr; } @@ -164,7 +150,7 @@ UVaRestJsonValue* UVaRestJsonObject::GetField(const FString& FieldName) const void UVaRestJsonObject::SetField(const FString& FieldName, UVaRestJsonValue* JsonValue) { - if (!JsonObj.IsValid() || FieldName.IsEmpty()) + if (FieldName.IsEmpty()) { return; } @@ -177,7 +163,7 @@ void UVaRestJsonObject::SetField(const FString& FieldName, UVaRestJsonValue* Jso float UVaRestJsonObject::GetNumberField(const FString& FieldName) const { - if (!JsonObj.IsValid() || !JsonObj->HasTypedField(FieldName)) + if (!JsonObj->HasTypedField(FieldName)) { UE_LOG(LogVaRest, Warning, TEXT("No field with name %s of type Number"), *FieldName); return 0.0f; @@ -188,7 +174,7 @@ float UVaRestJsonObject::GetNumberField(const FString& FieldName) const void UVaRestJsonObject::SetNumberField(const FString& FieldName, float Number) { - if (!JsonObj.IsValid() || FieldName.IsEmpty()) + if (FieldName.IsEmpty()) { return; } @@ -198,7 +184,7 @@ void UVaRestJsonObject::SetNumberField(const FString& FieldName, float Number) int32 UVaRestJsonObject::GetIntegerField(const FString& FieldName) const { - if (!JsonObj.IsValid() || !JsonObj->HasTypedField(FieldName)) + if (!JsonObj->HasTypedField(FieldName)) { UE_LOG(LogVaRest, Warning, TEXT("No field with name %s of type Number"), *FieldName); return 0; @@ -209,7 +195,7 @@ int32 UVaRestJsonObject::GetIntegerField(const FString& FieldName) const void UVaRestJsonObject::SetIntegerField(const FString& FieldName, int32 Number) { - if (!JsonObj.IsValid() || FieldName.IsEmpty()) + if (FieldName.IsEmpty()) { return; } @@ -219,7 +205,7 @@ void UVaRestJsonObject::SetIntegerField(const FString& FieldName, int32 Number) FString UVaRestJsonObject::GetStringField(const FString& FieldName) const { - if (!JsonObj.IsValid() || !JsonObj->HasTypedField(FieldName)) + if (!JsonObj->HasTypedField(FieldName)) { UE_LOG(LogVaRest, Warning, TEXT("No field with name %s of type String"), *FieldName); return TEXT(""); @@ -230,7 +216,7 @@ FString UVaRestJsonObject::GetStringField(const FString& FieldName) const void UVaRestJsonObject::SetStringField(const FString& FieldName, const FString& StringValue) { - if (!JsonObj.IsValid() || FieldName.IsEmpty()) + if (FieldName.IsEmpty()) { return; } @@ -240,7 +226,7 @@ void UVaRestJsonObject::SetStringField(const FString& FieldName, const FString& bool UVaRestJsonObject::GetBoolField(const FString& FieldName) const { - if (!JsonObj.IsValid() || !JsonObj->HasTypedField(FieldName)) + if (!JsonObj->HasTypedField(FieldName)) { UE_LOG(LogVaRest, Warning, TEXT("No field with name %s of type Boolean"), *FieldName); return false; @@ -251,7 +237,7 @@ bool UVaRestJsonObject::GetBoolField(const FString& FieldName) const void UVaRestJsonObject::SetBoolField(const FString& FieldName, bool InValue) { - if (!JsonObj.IsValid() || FieldName.IsEmpty()) + if (FieldName.IsEmpty()) { return; } @@ -262,7 +248,7 @@ void UVaRestJsonObject::SetBoolField(const FString& FieldName, bool InValue) TArray UVaRestJsonObject::GetArrayField(const FString& FieldName) const { TArray OutArray; - if (!JsonObj.IsValid() || FieldName.IsEmpty()) + if (FieldName.IsEmpty()) { return OutArray; } @@ -287,7 +273,7 @@ TArray UVaRestJsonObject::GetArrayField(const FString& FieldN void UVaRestJsonObject::SetArrayField(const FString& FieldName, const TArray& InArray) { - if (!JsonObj.IsValid() || FieldName.IsEmpty()) + if (FieldName.IsEmpty()) { return; } @@ -358,7 +344,7 @@ void UVaRestJsonObject::MergeJsonObject(UVaRestJsonObject* InJsonObject, bool Ov UVaRestJsonObject* UVaRestJsonObject::GetObjectField(const FString& FieldName) const { - if (!JsonObj.IsValid() || !JsonObj->HasTypedField(FieldName)) + if (!JsonObj->HasTypedField(FieldName)) { UE_LOG(LogVaRest, Warning, TEXT("%s: No field with name %s of type Object"), *VA_FUNC_LINE, *FieldName); return nullptr; @@ -374,7 +360,7 @@ UVaRestJsonObject* UVaRestJsonObject::GetObjectField(const FString& FieldName) c void UVaRestJsonObject::SetObjectField(const FString& FieldName, UVaRestJsonObject* JsonObject) { - if (!JsonObj.IsValid() || FieldName.IsEmpty() || !JsonObject || !JsonObject->IsValidLowLevel()) + if (FieldName.IsEmpty() || !JsonObject || !JsonObject->IsValidLowLevel()) { return; } @@ -388,7 +374,7 @@ void UVaRestJsonObject::SetObjectField(const FString& FieldName, UVaRestJsonObje TArray UVaRestJsonObject::GetNumberArrayField(const FString& FieldName) const { TArray NumberArray; - if (!JsonObj.IsValid() || !JsonObj->HasTypedField(FieldName) || FieldName.IsEmpty()) + if (!JsonObj->HasTypedField(FieldName) || FieldName.IsEmpty()) { UE_LOG(LogVaRest, Warning, TEXT("%s: No field with name %s of type Array"), *VA_FUNC_LINE, *FieldName); return NumberArray; @@ -411,7 +397,7 @@ TArray UVaRestJsonObject::GetNumberArrayField(const FString& FieldName) c void UVaRestJsonObject::SetNumberArrayField(const FString& FieldName, const TArray& NumberArray) { - if (!JsonObj.IsValid() || FieldName.IsEmpty()) + if (FieldName.IsEmpty()) { return; } @@ -429,7 +415,7 @@ void UVaRestJsonObject::SetNumberArrayField(const FString& FieldName, const TArr TArray UVaRestJsonObject::GetStringArrayField(const FString& FieldName) const { TArray StringArray; - if (!JsonObj.IsValid() || !JsonObj->HasTypedField(FieldName) || FieldName.IsEmpty()) + if (!JsonObj->HasTypedField(FieldName) || FieldName.IsEmpty()) { UE_LOG(LogVaRest, Warning, TEXT("%s: No field with name %s of type Array"), *VA_FUNC_LINE, *FieldName); return StringArray; @@ -452,7 +438,7 @@ TArray UVaRestJsonObject::GetStringArrayField(const FString& FieldName) void UVaRestJsonObject::SetStringArrayField(const FString& FieldName, const TArray& StringArray) { - if (!JsonObj.IsValid() || FieldName.IsEmpty()) + if (FieldName.IsEmpty()) { return; } @@ -469,7 +455,7 @@ void UVaRestJsonObject::SetStringArrayField(const FString& FieldName, const TArr TArray UVaRestJsonObject::GetBoolArrayField(const FString& FieldName) const { TArray BoolArray; - if (!JsonObj.IsValid() || !JsonObj->HasTypedField(FieldName) || FieldName.IsEmpty()) + if (!JsonObj->HasTypedField(FieldName) || FieldName.IsEmpty()) { UE_LOG(LogVaRest, Warning, TEXT("%s: No field with name %s of type Array"), *VA_FUNC_LINE, *FieldName); return BoolArray; @@ -492,7 +478,7 @@ TArray UVaRestJsonObject::GetBoolArrayField(const FString& FieldName) cons void UVaRestJsonObject::SetBoolArrayField(const FString& FieldName, const TArray& BoolArray) { - if (!JsonObj.IsValid() || FieldName.IsEmpty()) + if (FieldName.IsEmpty()) { return; } @@ -509,7 +495,7 @@ void UVaRestJsonObject::SetBoolArrayField(const FString& FieldName, const TArray TArray UVaRestJsonObject::GetObjectArrayField(const FString& FieldName) const { TArray OutArray; - if (!JsonObj.IsValid() || !JsonObj->HasTypedField(FieldName) || FieldName.IsEmpty()) + if (!JsonObj->HasTypedField(FieldName) || FieldName.IsEmpty()) { UE_LOG(LogVaRest, Warning, TEXT("%s: No field with name %s of type Array"), *VA_FUNC_LINE, *FieldName); return OutArray; @@ -536,7 +522,7 @@ TArray UVaRestJsonObject::GetObjectArrayField(const FString& void UVaRestJsonObject::SetObjectArrayField(const FString& FieldName, const TArray& ObjectArray) { - if (!JsonObj.IsValid() || FieldName.IsEmpty()) + if (FieldName.IsEmpty()) { return; } @@ -673,63 +659,55 @@ void UVaRestJsonObject::DecodeFromArchive(TUniquePtr& Reader) bool UVaRestJsonObject::WriteToFile(const FString& Path) { - if (JsonObj.IsValid()) + TUniquePtr FileWriter(IFileManager::Get().CreateFileWriter(*Path)); + if (!FileWriter) { - TUniquePtr FileWriter(IFileManager::Get().CreateFileWriter(*Path)); - if (!FileWriter) - { - return false; - } + return false; + } - FArchive& Ar = *FileWriter.Get(); + FArchive& Ar = *FileWriter.Get(); - UCS2CHAR BOM = UNICODE_BOM; - Ar.Serialize(&BOM, sizeof(UCS2CHAR)); + UCS2CHAR BOM = UNICODE_BOM; + Ar.Serialize(&BOM, sizeof(UCS2CHAR)); - FString Str = FString(TEXT("{")); + FString Str = FString(TEXT("{")); + WriteStringToArchive(Ar, *Str, Str.Len()); + + int32 ElementCount = 0; + FJSONWriter JsonWriter; + for (auto JsonObjectValuePair : JsonObj->Values) + { + Str = FString(TEXT("\"")); WriteStringToArchive(Ar, *Str, Str.Len()); - int32 ElementCount = 0; - FJSONWriter JsonWriter; - for (auto JsonObjectValuePair : JsonObj->Values) + const TCHAR* BufferPtr = *JsonObjectValuePair.Key; + for (int i = 0; i < JsonObjectValuePair.Key.Len(); ++i) { - Str = FString(TEXT("\"")); - WriteStringToArchive(Ar, *Str, Str.Len()); - - const TCHAR* BufferPtr = *JsonObjectValuePair.Key; - for (int i = 0; i < JsonObjectValuePair.Key.Len(); ++i) - { - Str = FString(1, &BufferPtr[i]); + Str = FString(1, &BufferPtr[i]); #if PLATFORM_WINDOWS - WriteStringToArchive(Ar, *Str, Str.Len() - 1); + WriteStringToArchive(Ar, *Str, Str.Len() - 1); #else - WriteStringToArchive(Ar, *Str, Str.Len()); + WriteStringToArchive(Ar, *Str, Str.Len()); #endif - } + } - Str = FString(TEXT("\"")); - WriteStringToArchive(Ar, *Str, Str.Len()); + Str = FString(TEXT("\"")); + WriteStringToArchive(Ar, *Str, Str.Len()); - Str = FString(TEXT(":")); - WriteStringToArchive(Ar, *Str, Str.Len()); + Str = FString(TEXT(":")); + WriteStringToArchive(Ar, *Str, Str.Len()); - ++ElementCount; + ++ElementCount; - JsonWriter.Write(JsonObjectValuePair.Value, FileWriter.Get(), ElementCount >= JsonObj->Values.Num()); - } + JsonWriter.Write(JsonObjectValuePair.Value, FileWriter.Get(), ElementCount >= JsonObj->Values.Num()); + } - Str = FString(TEXT("}")); - WriteStringToArchive(Ar, *Str, Str.Len()); + Str = FString(TEXT("}")); + WriteStringToArchive(Ar, *Str, Str.Len()); - FileWriter->Close(); + FileWriter->Close(); - return true; - } - else - { - UE_LOG(LogVaRest, Error, TEXT("UVaRestJsonObject::WriteToFile: Root object is invalid!")); - return false; - } + return true; } bool UVaRestJsonObject::WriteStringToArchive(FArchive& Ar, const TCHAR* StrPtr, int64 Len) diff --git a/Source/VaRestPlugin/Private/VaRestRequestJSON.cpp b/Source/VaRestPlugin/Private/VaRestRequestJSON.cpp index 70d87719..168fe713 100644 --- a/Source/VaRestPlugin/Private/VaRestRequestJSON.cpp +++ b/Source/VaRestPlugin/Private/VaRestRequestJSON.cpp @@ -405,7 +405,7 @@ void UVaRestRequestJSON::ProcessRequest() // Serialize data to json string FString OutputString; TSharedRef> Writer = TJsonWriterFactory<>::Create(&OutputString); - FJsonSerializer::Serialize(RequestJsonObj->GetRootObject().ToSharedRef(), Writer); + FJsonSerializer::Serialize(RequestJsonObj->GetRootObject(), Writer); // Set Json content HttpRequest->SetContentAsString(OutputString); @@ -479,6 +479,14 @@ void UVaRestRequestJSON::OnProcessRequestComplete(FHttpRequestPtr Request, FHttp const TArray& Bytes = Response->GetContent(); ResponseSize = ResponseJsonObj->DeserializeFromUTF8Bytes((const ANSICHAR*)Bytes.GetData(), Bytes.Num()); + // Log errors + if (ResponseSize == 0) + { + // As we assume it's recommended way to use current class, but not the only one, + // it will be the warning instead of error + UE_LOG(LogVaRest, Warning, TEXT("JSON could not be decoded!")); + } + // Decide whether the request was successful bIsValidJsonResponse = bWasSuccessful && (ResponseSize > 0); @@ -489,17 +497,6 @@ void UVaRestRequestJSON::OnProcessRequestComplete(FHttpRequestPtr Request, FHttp ResponseSize = ResponseContent.GetAllocatedSize(); } - // Log errors - if (!bIsValidJsonResponse) - { - if (!ResponseJsonObj->GetRootObject().IsValid()) - { - // As we assume it's recommended way to use current class, but not the only one, - // it will be the warning instead of error - UE_LOG(LogVaRest, Warning, TEXT("JSON could not be decoded!")); - } - } - // Broadcast the result events on next tick AsyncTask(ENamedThreads::GameThread, [this]() { OnRequestComplete.Broadcast(this); From 9fc6b00a353d9ef6f99395904c9ea1080fb1de69 Mon Sep 17 00:00:00 2001 From: Vladimir Alyamkin Date: Thu, 28 Mar 2019 11:02:28 +0300 Subject: [PATCH 07/10] Fix includes for non-unity build --- Source/VaRestPlugin/Classes/VaRestJsonObject.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/VaRestPlugin/Classes/VaRestJsonObject.h b/Source/VaRestPlugin/Classes/VaRestJsonObject.h index 430ca09c..f2c27573 100644 --- a/Source/VaRestPlugin/Classes/VaRestJsonObject.h +++ b/Source/VaRestPlugin/Classes/VaRestJsonObject.h @@ -3,10 +3,11 @@ #pragma once +#include "Dom/JsonObject.h" + #include "VaRestJsonObject.generated.h" class UVaRestJsonValue; -class FJsonObject; /** * Blueprintable FJsonObject wrapper From 7f2e477829db6a2d23120f48c9fb45b0e5772388 Mon Sep 17 00:00:00 2001 From: Maxim Fedorenko Date: Thu, 28 Mar 2019 11:05:54 +0300 Subject: [PATCH 08/10] Fixup missed header instead of forward declaration for FJsonObject --- Source/VaRestPlugin/Classes/VaRestJsonObject.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/VaRestPlugin/Classes/VaRestJsonObject.h b/Source/VaRestPlugin/Classes/VaRestJsonObject.h index 430ca09c..f2c27573 100644 --- a/Source/VaRestPlugin/Classes/VaRestJsonObject.h +++ b/Source/VaRestPlugin/Classes/VaRestJsonObject.h @@ -3,10 +3,11 @@ #pragma once +#include "Dom/JsonObject.h" + #include "VaRestJsonObject.generated.h" class UVaRestJsonValue; -class FJsonObject; /** * Blueprintable FJsonObject wrapper From a98cf2d95635b45fb305e30755ebc40aab03551d Mon Sep 17 00:00:00 2001 From: Vladimir Alyamkin Date: Wed, 3 Apr 2019 11:00:24 +0300 Subject: [PATCH 09/10] Up plugin version to UE 4.22. Close #203 --- VaRestPlugin.uplugin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VaRestPlugin.uplugin b/VaRestPlugin.uplugin index 1e42162c..81e067ef 100644 --- a/VaRestPlugin.uplugin +++ b/VaRestPlugin.uplugin @@ -6,7 +6,7 @@ "VersionName" : "1.1-r25", "CreatedBy" : "Vladimir Alyamkin", "CreatedByURL" : "http://alyamkin.com", - "EngineVersion" : "4.21.0", + "EngineVersion" : "4.22.0", "Description" : "Plugin that makes REST (JSON) server communication easy to use", "Category" : "Network", "MarketplaceURL" : "com.epicgames.launcher://ue/marketplace/content/e47be161e7a24e928560290abd5dcc4f", From a31033b632dcc3e8b5bf5b3950ac37322e3b0936 Mon Sep 17 00:00:00 2001 From: Vladimir Alyamkin Date: Tue, 9 Apr 2019 12:20:53 +0300 Subject: [PATCH 10/10] Update version to UE 4.22 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 755af229..f84acf13 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Key features: Check the [Wiki](https://hiazma.atlassian.net/wiki/display/VAR) for plugin usage examples and installation notes. -Current version: **1.1 R 25** (UE 4.21) +Current version: **1.1 R 25** (UE 4.22) ![SCREENSHOT](SCREENSHOT.jpg)