UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
JsonObject.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Containers/Array.h"
6#include "Containers/Map.h"
8#include "CoreMinimal.h"
9#include "Dom/JsonValue.h"
10#include "HAL/Platform.h"
11#include "JsonGlobals.h"
12#include "Logging/LogCategory.h"
13#include "Logging/LogMacros.h"
17
23{
24public:
25
27
28 template<EJson JsonType>
30 {
31 return GetField(FieldName, JsonType);
32 }
33
35 {
36 const TSharedPtr<FJsonValue>* Field = Values.FindByHash(GetTypeHash(FieldName), FieldName);
37 if (Field != nullptr && Field->IsValid())
38 {
39 if (JsonType == EJson::None || (*Field)->Type == JsonType)
40 {
41 return (*Field);
42 }
43 else
44 {
45 UE_LOG(LogJson, Warning, TEXT("Field %.*s is of the wrong type."), FieldName.Len(), FieldName.GetData());
46 }
47 }
48 else
49 {
50 UE_LOG(LogJson, Warning, TEXT("Field %.*s was not found."), FieldName.Len(), FieldName.GetData());
51 }
52
54 }
55
63 {
64 const TSharedPtr<FJsonValue>* Field = Values.FindByHash(GetTypeHash(FieldName), FieldName);
65 return (Field != nullptr && Field->IsValid()) ? *Field : TSharedPtr<FJsonValue>();
66 }
67
74 bool HasField(FStringView FieldName) const
75 {
76 const TSharedPtr<FJsonValue>* Field = Values.FindByHash(GetTypeHash(FieldName), FieldName);
77 if(Field && Field->IsValid())
78 {
79 return true;
80 }
81
82 return false;
83 }
84
92 template<EJson JsonType>
93 bool HasTypedField(FStringView FieldName) const
94 {
95 return HasTypedField(FieldName, JsonType);
96 }
97
106 {
107 const TSharedPtr<FJsonValue>* Field = Values.FindByHash(GetTypeHash(FieldName), FieldName);
108 if (Field && Field->IsValid() && ((*Field)->Type == JsonType))
109 {
110 return true;
111 }
112
113 return false;
114 }
115
122 JSON_API void SetField(FString&& FieldName, const TSharedPtr<FJsonValue>& Value);
123 JSON_API void SetField(const FString& FieldName, const TSharedPtr<FJsonValue>& Value);
124
130 JSON_API void RemoveField(FStringView FieldName);
131
140 JSON_API double GetNumberField(FStringView FieldName) const;
141
145 inline int32 GetIntegerField(FStringView FieldName) const
146 {
147 return (int32)GetNumberField(FieldName);
148 }
149
151 JSON_API bool TryGetNumberField(FStringView FieldName, float& OutNumber) const;
152
154 JSON_API bool TryGetNumberField(FStringView FieldName, double& OutNumber) const;
155
157 JSON_API bool TryGetNumberField(FStringView FieldName, int8& OutNumber) const;
158
160 JSON_API bool TryGetNumberField(FStringView FieldName, int16& OutNumber) const;
161
163 JSON_API bool TryGetNumberField(FStringView FieldName, int32& OutNumber) const;
164
166 JSON_API bool TryGetNumberField(FStringView FieldName, int64& OutNumber) const;
167
169 JSON_API bool TryGetNumberField(FStringView FieldName, uint8& OutNumber) const;
170
172 JSON_API bool TryGetNumberField(FStringView FieldName, uint16& OutNumber) const;
173
175 JSON_API bool TryGetNumberField(FStringView FieldName, uint32& OutNumber) const;
176
178 JSON_API bool TryGetNumberField(FStringView FieldName, uint64& OutNumber) const;
179
181 JSON_API void SetNumberField(FString&& FieldName, double Number);
182 JSON_API void SetNumberField(const FString& FieldName, double Number);
183
185 JSON_API FString GetStringField(FStringView FieldName) const;
186
189
191 JSON_API bool TryGetStringField(FStringView FieldName, FString& OutString) const;
192
195
197 template<typename TEnum>
199 {
200 TArray<FString> Strings;
201 if (!TryGetStringArrayField(FieldName, Strings))
202 {
203 return false;
204 }
205
206 OutArray.Empty();
207 for (const FString& String : Strings)
208 {
209 TEnum Value;
211 {
212 OutArray.Add(Value);
213 }
214 }
215 return true;
216 }
217
219 void SetStringField(FString&& FieldName, const ANSICHAR* StringValue)
220 {
221 SetStringField(MoveTemp(FieldName), FUtf8String(StringValue));
222 }
223 void SetStringField(const FString& FieldName, const ANSICHAR* StringValue)
224 {
225 SetStringField(FieldName, FUtf8String(StringValue));
226 }
227 void SetStringField(FString&& FieldName, const TCHAR* StringValue)
228 {
229 SetStringField(MoveTemp(FieldName), FString(StringValue));
230 }
231 void SetStringField(const FString& FieldName, const TCHAR* StringValue)
232 {
233 SetStringField(FieldName, FString(StringValue));
234 }
235 void SetStringField(FString&& FieldName, const UTF8CHAR* StringValue)
236 {
237 SetStringField(MoveTemp(FieldName), FUtf8String(StringValue));
238 }
239 void SetStringField(const FString& FieldName, const UTF8CHAR* StringValue)
240 {
241 SetStringField(FieldName, FUtf8String(StringValue));
242 }
243
244 JSON_API void SetStringField(FString&& FieldName, FString&& StringValue);
245 JSON_API void SetStringField(FString&& FieldName, const FString& StringValue);
246 JSON_API void SetStringField(const FString& FieldName, FString&& StringValue);
247 JSON_API void SetStringField(const FString& FieldName, const FString& StringValue);
248 JSON_API void SetStringField(FString&& FieldName, FUtf8String&& StringValue);
249 JSON_API void SetStringField(FString&& FieldName, const FUtf8String& StringValue);
250 JSON_API void SetStringField(const FString& FieldName, FUtf8String&& StringValue);
251 JSON_API void SetStringField(const FString& FieldName, const FUtf8String& StringValue);
252
261 JSON_API bool GetBoolField(FStringView FieldName) const;
262
264 JSON_API bool TryGetBoolField(FStringView FieldName, bool& OutBool) const;
265
267 JSON_API void SetBoolField(FString&& FieldName, bool InValue);
268 JSON_API void SetBoolField(const FString& FieldName, bool InValue);
269
272
275
277 JSON_API void SetArrayField(FString&& FieldName, TArray<TSharedPtr<FJsonValue>>&& Array);
278 JSON_API void SetArrayField(FString&& FieldName, const TArray<TSharedPtr<FJsonValue>>& Array);
279 JSON_API void SetArrayField(const FString& FieldName, TArray<TSharedPtr<FJsonValue>>&& Array);
280 JSON_API void SetArrayField(const FString& FieldName, const TArray<TSharedPtr<FJsonValue>>& Array);
281
291
294
296 JSON_API void SetObjectField(FString&& FieldName, const TSharedPtr<FJsonObject>& JsonObject);
297 JSON_API void SetObjectField(const FString& FieldName, const TSharedPtr<FJsonObject>& JsonObject);
298
302 SIZE_T GetMemoryFootprint() const { return sizeof(*this) + GetAllocatedSize(); }
303private:
305 JSON_API SIZE_T GetAllocatedSize() const;
306
307public:
308 static JSON_API void Duplicate(const TSharedPtr<const FJsonObject>& Source, const TSharedPtr<FJsonObject>& Dest);
309 static JSON_API void Duplicate(const TSharedPtr<FJsonObject>& Source, TSharedPtr<FJsonObject>& Dest);
310
311#if !PLATFORM_TCHAR_IS_UTF8CHAR
312
313 template <EJson JsonType>
314 UE_DEPRECATED(5.4, "Passing an ANSI string to GetField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
316 {
317 return GetField<JsonType>(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()));
318 }
319
320 UE_DEPRECATED(5.4, "Passing an ANSI string to TryGetField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
322 {
323 return TryGetField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()));
324 }
325
326 UE_DEPRECATED(5.4, "Passing an ANSI string to HasField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
328 {
329 return HasField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()));
330 }
331
332 template <EJson JsonType>
333 UE_DEPRECATED(5.4, "Passing an ANSI string to HasTypedField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
335 {
336 return HasTypedField<JsonType>(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()));
337 }
338
339 UE_DEPRECATED(5.4, "Passing an ANSI string to RemoveField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
341 {
342 return RemoveField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()));
343 }
344
345 UE_DEPRECATED(5.4, "Passing an ANSI string to GetNumberField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
347 {
348 return GetNumberField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()));
349 }
350
351 UE_DEPRECATED(5.4, "Passing an ANSI string to GetIntegerField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
353 {
354 return GetIntegerField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()));
355 }
356
357 UE_DEPRECATED(5.4, "Passing an ANSI string to TryGetNumberField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
359 {
360 return TryGetNumberField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()), OutNumber);
361 }
362
363 UE_DEPRECATED(5.4, "Passing an ANSI string to TryGetNumberField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
365 {
366 return TryGetNumberField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()), OutNumber);
367 }
368
369 UE_DEPRECATED(5.4, "Passing an ANSI string to TryGetNumberField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
371 {
372 return TryGetNumberField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()), OutNumber);
373 }
374
375 UE_DEPRECATED(5.4, "Passing an ANSI string to TryGetNumberField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
377 {
378 return TryGetNumberField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()), OutNumber);
379 }
380
381 UE_DEPRECATED(5.4, "Passing an ANSI string to TryGetNumberField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
383 {
384 return TryGetNumberField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()), OutNumber);
385 }
386
387 UE_DEPRECATED(5.4, "Passing an ANSI string to TryGetNumberField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
389 {
390 return TryGetNumberField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()), OutNumber);
391 }
392
393 UE_DEPRECATED(5.4, "Passing an ANSI string to TryGetNumberField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
395 {
396 return TryGetNumberField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()), OutNumber);
397 }
398
399 UE_DEPRECATED(5.4, "Passing an ANSI string to TryGetNumberField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
401 {
402 return TryGetNumberField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()), OutNumber);
403 }
404
405 UE_DEPRECATED(5.4, "Passing an ANSI string to TryGetNumberField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
407 {
408 return TryGetNumberField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()), OutNumber);
409 }
410
411 UE_DEPRECATED(5.4, "Passing an ANSI string to TryGetNumberField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
413 {
414 return TryGetNumberField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()), OutNumber);
415 }
416
417 UE_DEPRECATED(5.4, "Passing an ANSI string to GetStringField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
418 FString GetStringField(FAnsiStringView FieldName) const
419 {
420 return GetStringField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()));
421 }
422
423 UE_DEPRECATED(5.4, "Passing an ANSI string to TryGetStringField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
424 bool TryGetStringField(FAnsiStringView FieldName, FString& OutString) const
425 {
426 return TryGetStringField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()), OutString);
427 }
428
429 UE_DEPRECATED(5.4, "Passing an ANSI string to TryGetStringArrayField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
431 {
432 return TryGetStringArrayField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()), OutArray);
433 }
434
435 template <typename TEnum>
436 UE_DEPRECATED(5.4, "Passing an ANSI string to TryGetEnumArrayField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
438 {
439 return TryGetEnumArrayField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()), OutArray);
440 }
441
442 UE_DEPRECATED(5.4, "Passing an ANSI string to GetBoolField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
444 {
445 return GetBoolField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()));
446 }
447
448 UE_DEPRECATED(5.4, "Passing an ANSI string to TryGetBoolField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
450 {
451 return TryGetBoolField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()), OutBool);
452 }
453
454 UE_DEPRECATED(5.4, "Passing an ANSI string to GetArrayField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
456 {
457 return GetArrayField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()));
458 }
459
460 UE_DEPRECATED(5.4, "Passing an ANSI string to TryGetArrayField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
462 {
463 return TryGetArrayField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()), OutArray);
464 }
465
466 UE_DEPRECATED(5.4, "Passing an ANSI string to GetObjectField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
468 {
469 return GetObjectField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()));
470 }
471
472 UE_DEPRECATED(5.4, "Passing an ANSI string to TryGetObjectField has been deprecated outside of UTF-8 mode. Please use the overload that takes a TCHAR string.")
474 {
475 return TryGetObjectField(StringCast<TCHAR>(FieldName.GetData(), FieldName.Len()), OutObject);
476 }
477
478#endif // !PLATFORM_TCHAR_IS_UTF8CHAR
479
480};
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
FPlatformTypes::int16 int16
A 16-bit signed integer.
Definition Platform.h:1123
FPlatformTypes::int8 int8
An 8-bit signed integer.
Definition Platform.h:1121
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::SIZE_T SIZE_T
An unsigned integer the same size as a pointer, the same as UPTRINT.
Definition Platform.h:1150
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
FPlatformTypes::UTF8CHAR UTF8CHAR
An 8-bit character containing a UTF8 (Unicode, 8-bit, variable-width) code unit.
Definition Platform.h:1137
FPlatformTypes::ANSICHAR ANSICHAR
An ANSI character. Normally a signed type.
Definition Platform.h:1131
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
bool LexTryParseString(EBuildConfiguration &OutConfiguration, const TCHAR *Configuration)
Definition GenericPlatformMisc.cpp:270
EJson
Definition JsonTypes.h:23
#define UE_LOG(CategoryName, Verbosity, Format,...)
Definition LogMacros.h:270
const bool
Definition NetworkReplayStreaming.h:178
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
uint8_t uint8
Definition binka_ue_file_header.h:8
uint16_t uint16
Definition binka_ue_file_header.h:7
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition JsonObject.h:23
void SetStringField(const FString &FieldName, const TCHAR *StringValue)
Definition JsonObject.h:231
JSON_API void SetBoolField(FString &&FieldName, bool InValue)
Definition JsonObject.cpp:193
bool HasField(FStringView FieldName) const
Definition JsonObject.h:74
bool HasTypedField(FStringView FieldName) const
Definition JsonObject.h:93
JSON_API void SetObjectField(FString &&FieldName, const TSharedPtr< FJsonObject > &JsonObject)
Definition JsonObject.cpp:245
JSON_API bool TryGetStringField(FStringView FieldName, FString &OutString) const
Definition JsonObject.cpp:105
void SetStringField(FString &&FieldName, const TCHAR *StringValue)
Definition JsonObject.h:227
SIZE_T GetMemoryFootprint() const
Definition JsonObject.h:302
void SetStringField(const FString &FieldName, const UTF8CHAR *StringValue)
Definition JsonObject.h:239
JSON_API void RemoveField(FStringView FieldName)
Definition JsonObject.cpp:15
void SetStringField(FString &&FieldName, const UTF8CHAR *StringValue)
Definition JsonObject.h:235
JSON_API bool TryGetArrayField(FStringView FieldName, const TArray< TSharedPtr< FJsonValue > > *&OutArray) const
Definition JsonObject.cpp:208
TSharedPtr< FJsonValue > GetField(FStringView FieldName, EJson JsonType) const
Definition JsonObject.h:34
JSON_API double GetNumberField(FStringView FieldName) const
Definition JsonObject.cpp:20
JSON_API const TArray< TSharedPtr< FJsonValue > > & GetArrayField(FStringView FieldName) const
Definition JsonObject.cpp:203
TMap< FString, TSharedPtr< FJsonValue > > Values
Definition JsonObject.h:26
JSON_API FUtf8String GetUtf8StringField(FStringView FieldName) const
Definition JsonObject.cpp:100
JSON_API const TSharedPtr< FJsonObject > & GetObjectField(FStringView FieldName) const
Definition JsonObject.cpp:234
JSON_API bool TryGetNumberField(FStringView FieldName, float &OutNumber) const
Definition JsonObject.cpp:25
bool TryGetEnumArrayField(FStringView FieldName, TArray< TEnum > &OutArray) const
Definition JsonObject.h:198
int32 GetIntegerField(FStringView FieldName) const
Definition JsonObject.h:145
void SetStringField(const FString &FieldName, const ANSICHAR *StringValue)
Definition JsonObject.h:223
JSON_API bool TryGetStringArrayField(FStringView FieldName, TArray< FString > &OutArray) const
Definition JsonObject.cpp:111
JSON_API void SetArrayField(FString &&FieldName, TArray< TSharedPtr< FJsonValue > > &&Array)
Definition JsonObject.cpp:214
JSON_API bool TryGetBoolField(FStringView FieldName, bool &OutBool) const
Definition JsonObject.cpp:187
JSON_API bool GetBoolField(FStringView FieldName) const
Definition JsonObject.cpp:182
TSharedPtr< FJsonValue > TryGetField(FStringView FieldName) const
Definition JsonObject.h:62
JSON_API void SetNumberField(FString &&FieldName, double Number)
Definition JsonObject.cpp:85
bool HasTypedField(FStringView FieldName, EJson JsonType) const
Definition JsonObject.h:105
TSharedPtr< FJsonValue > GetField(FStringView FieldName) const
Definition JsonObject.h:29
JSON_API bool TryGetObjectField(FStringView FieldName, const TSharedPtr< FJsonObject > *&OutObject) const
Definition JsonObject.cpp:239
JSON_API FString GetStringField(FStringView FieldName) const
Definition JsonObject.cpp:95
void SetStringField(FString &&FieldName, const ANSICHAR *StringValue)
Definition JsonObject.h:219
JSON_API void SetField(FString &&FieldName, const TSharedPtr< FJsonValue > &Value)
Definition JsonObject.cpp:5
Definition JsonValue.h:22
Definition Array.h:670
void Empty(SizeType Slack=0)
Definition Array.h:2273
Definition UnrealString.h.inl:34
Definition SharedPointer.h:692
constexpr int32 Len() const
Definition StringView.h:174
constexpr const CharType * GetData() const
Definition StringView.h:160
Definition FieldSystemNoiseAlgo.cpp:6