A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
VaRestJsonObject.h
Go to the documentation of this file.
1// Copyright 2014-2019 Vladimir Alyamkin. All Rights Reserved.
2
3#pragma once
4
5#include "VaRestDefines.h"
6
7#include "Dom/JsonObject.h"
8#include "Templates/UnrealTypeTraits.h"
9
10#include "VaRestJsonObject.generated.h"
11
13
17UCLASS(BlueprintType, Blueprintable)
18class VAREST_API UVaRestJsonObject : public UObject
19{
20 GENERATED_UCLASS_BODY()
21
22public:
24 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
25 void Reset();
26
28 TSharedRef<FJsonObject>& GetRootObject();
29
31 void SetRootObject(const TSharedPtr<FJsonObject>& JsonObject);
32
34 // Serialization
35
37 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
38 FString EncodeJson() const;
39
41 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
42 FString EncodeJsonToSingleString() const;
43
45 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
46 bool DecodeJson(const FString& JsonString, bool bUseIncrementalParser = true);
47
49 // FJsonObject API
50
52 UFUNCTION(BlueprintPure, Category = "VaRest|Json")
53 TArray<FString> GetFieldNames() const;
54
56 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
57 bool HasField(const FString& FieldName) const;
58
60 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
61 void RemoveField(const FString& FieldName);
62
64 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
65 UVaRestJsonValue* GetField(const FString& FieldName) const;
66
68 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
69 void SetField(const FString& FieldName, UVaRestJsonValue* JsonValue);
70
72 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
73 TArray<UVaRestJsonValue*> GetArrayField(const FString& FieldName) const;
74
76 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
77 void SetArrayField(const FString& FieldName, const TArray<UVaRestJsonValue*>& InArray);
78
80 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
81 void MergeJsonObject(UVaRestJsonObject* InJsonObject, bool Overwrite);
82
84 // FJsonObject API Helpers (easy to use with simple Json objects)
85
88 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
89 float GetNumberField(const FString& FieldName) const;
90
93 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
94 void SetNumberField(const FString& FieldName, float Number);
95
97 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
98 int32 GetIntegerField(const FString& FieldName) const;
99
101 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
102 void SetIntegerField(const FString& FieldName, int32 Number);
103
105 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
106 int64 GetInt64Field(const FString& FieldName) const;
107
109 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
110 void SetInt64Field(const FString& FieldName, int64 Number);
111
113 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
114 FString GetStringField(const FString& FieldName) const;
115
117 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
118 void SetStringField(const FString& FieldName, const FString& StringValue);
119
121 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
122 bool GetBoolField(const FString& FieldName) const;
123
125 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
126 void SetBoolField(const FString& FieldName, bool InValue);
127
129 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
130 UVaRestJsonObject* GetObjectField(const FString& FieldName) const;
131
133 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
134 void SetObjectField(const FString& FieldName, UVaRestJsonObject* JsonObject);
135
137 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
138 void SetMapFields_string(const TMap<FString, FString>& Fields);
139
141 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
142 void SetMapFields_uint8(const TMap<FString, uint8>& Fields);
143
145 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
146 void SetMapFields_int32(const TMap<FString, int32>& Fields);
147
149 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
150 void SetMapFields_int64(const TMap<FString, int64>& Fields);
151
153 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
154 void SetMapFields_bool(const TMap<FString, bool>& Fields);
155
156private:
158 template <typename T>
159 void SetMapFields_Impl(const TMap<FString, T>& Fields)
160 {
161 for (auto& field : Fields)
162 {
163 // No need to support all int types as they're not supported by BP
164 if (TIsSame<T, uint8>::Value || TIsSame<T, int32>::Value || TIsSame<T, int64>::Value || TIsSame<T, float>::Value)
165 {
166 SetNumberField(field.Key, field.Value);
167 }
168 else if (TIsSame<T, bool>::Value)
169 {
170 SetBoolField(field.Key, (bool)field.Value);
171 }
172 }
173 }
174
176 template <typename T>
177 TArray<T> GetTypeArrayField(const FString& FieldName) const
178 {
179 TArray<T> NumberArray;
180 if (!JsonObj->HasTypedField<EJson::Array>(FieldName) || FieldName.IsEmpty())
181 {
182 UE_LOG(LogVaRest, Warning, TEXT("%s: No field with name %s of type Array"), *VA_FUNC_LINE, *FieldName);
183 return NumberArray;
184 }
185
186 const TArray<TSharedPtr<FJsonValue>> JsonArrayValues = JsonObj->GetArrayField(FieldName);
187 for (TArray<TSharedPtr<FJsonValue>>::TConstIterator It(JsonArrayValues); It; ++It)
188 {
189 const auto Value = (*It).Get();
190 if (Value->Type != EJson::Number)
191 {
192 UE_LOG(LogVaRest, Error, TEXT("Not Number element in array with field name %s"), *FieldName);
193 }
194
195 NumberArray.Add((*It)->AsNumber());
196 }
197
198 return NumberArray;
199 }
200
202 // Array fields helpers (uniform arrays)
203
204public:
207 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
208 TArray<float> GetNumberArrayField(const FString& FieldName) const;
209
211 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
212 TArray<int32> GetIntegerArrayField(const FString& FieldName) const;
213
216 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
217 void SetNumberArrayField(const FString& FieldName, const TArray<float>& NumberArray);
218
220 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
221 TArray<FString> GetStringArrayField(const FString& FieldName) const;
222
224 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
225 void SetStringArrayField(const FString& FieldName, const TArray<FString>& StringArray);
226
228 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
229 TArray<bool> GetBoolArrayField(const FString& FieldName) const;
230
232 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
233 void SetBoolArrayField(const FString& FieldName, const TArray<bool>& BoolArray);
234
236 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
237 TArray<UVaRestJsonObject*> GetObjectArrayField(const FString& FieldName) const;
238
240 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
241 void SetObjectArrayField(const FString& FieldName, const TArray<UVaRestJsonObject*>& ObjectArray);
242
244 // Deserialize
245
246public:
248 int32 DeserializeFromUTF8Bytes(const ANSICHAR* Bytes, int32 Size);
249
251 int32 DeserializeFromTCHARBytes(const TCHAR* Bytes, int32 Size);
252
254 void DecodeFromArchive(TUniquePtr<FArchive>& Reader);
255
257 // Serialize
258
259public:
261 bool WriteToFile(const FString& Path) const;
262
268 UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
269 bool WriteToFilePath(const FString& Path, const bool bIsRelativeToProjectDir = true);
270
271 static bool WriteStringToArchive(FArchive& Ar, const TCHAR* StrPtr, int64 Len);
272
274 // Data
275
276private:
278 TSharedRef<FJsonObject> JsonObj;
279};
#define VA_FUNC_LINE
UCLASS(BlueprintType, Blueprintable)
TArray< T > GetTypeArrayField(const FString &FieldName) const
TSharedRef< FJsonObject > JsonObj
void SetMapFields_Impl(const TMap< FString, T > &Fields)
UCLASS(BlueprintType, Blueprintable)