UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
JsonValue.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"
9#include "CoreMinimal.h"
10#include "HAL/Platform.h"
11#include "Misc/CString.h"
14
15class FJsonObject;
16
22{
23public:
24
26 JSON_API double AsNumber() const;
27
29 JSON_API FString AsString() const;
30
33
35 JSON_API bool AsBool() const;
36
39
41 JSON_API virtual const TSharedPtr<FJsonObject>& AsObject() const;
42
44 JSON_API virtual bool TryGetNumber(double& OutNumber) const;
45
47 JSON_API virtual bool TryGetNumber(float& OutNumber) const;
48
50 JSON_API virtual bool TryGetNumber(int8& OutNumber) const;
51
53 JSON_API virtual bool TryGetNumber(int16& OutNumber) const;
54
56 JSON_API virtual bool TryGetNumber(int32& OutNumber) const;
57
59 JSON_API virtual bool TryGetNumber(int64& OutNumber) const;
60
62 JSON_API virtual bool TryGetNumber(uint8& OutNumber) const;
63
65 JSON_API virtual bool TryGetNumber(uint16& OutNumber) const;
66
68 JSON_API virtual bool TryGetNumber(uint32& OutNumber) const;
69
71 JSON_API virtual bool TryGetNumber(uint64& OutNumber) const;
72
74 JSON_API virtual bool TryGetString(FString& OutString) const;
75
77 JSON_API virtual bool TryGetUtf8String(FUtf8String& OutString) const;
78
80 JSON_API virtual bool TryGetBool(bool& OutBool) const;
81
83 JSON_API virtual bool TryGetArray(const TArray<TSharedPtr<FJsonValue>>*& OutArray) const;
84
87
89 JSON_API virtual bool TryGetObject(const TSharedPtr<FJsonObject>*& Object) const;
90
93
95 JSON_API virtual bool PreferStringRepresentation() const;
96
98 bool IsNull() const
99 {
100 return Type == EJson::Null || Type == EJson::None;
101 }
102
104 void AsArgumentType(double& Value)
105 {
106 Value = AsNumber();
107 }
108
109 void AsArgumentType(FString& Value)
110 {
111 Value = AsString();
112 }
113
115 {
116 Value = AsBool();
117 }
118
127
132 JSON_API virtual SIZE_T GetMemoryFootprint() const;
133
135
138
139 static JSON_API bool CompareEqual(const FJsonValue& Lhs, const FJsonValue& Rhs);
140
141protected:
143
146
147 virtual FString GetType() const = 0;
148
149 JSON_API void ErrorMessage(const FString& InType) const;
150
151 friend inline bool operator==(const FJsonValue& Lhs, const FJsonValue& Rhs)
152 {
153 return FJsonValue::CompareEqual(Lhs, Rhs);
154 }
155
156 friend inline bool operator!=(const FJsonValue& Lhs, const FJsonValue& Rhs)
157 {
158 return !FJsonValue::CompareEqual(Lhs, Rhs);
159 }
160};
161
163template <typename CharType>
165{
166public:
169
170 inline virtual bool TryGetString(FString& OutString) const override;
171 inline virtual bool TryGetUtf8String(FUtf8String& OutString) const override;
172 inline virtual bool TryGetNumber(double& OutDouble) const override;
173 inline virtual bool TryGetNumber(int32& OutValue) const override;
174 inline virtual bool TryGetNumber(uint32& OutValue) const override;
175 inline virtual bool TryGetNumber(int64& OutValue) const override;
176 inline virtual bool TryGetNumber(uint64& OutValue) const override;
177 inline virtual bool TryGetBool(bool& OutBool) const override;
178
179 // Way to check if string value is empty without copying the string
180 inline bool IsEmpty() const
181 {
182 return Value.IsEmpty();
183 }
184
185 inline virtual SIZE_T GetMemoryFootprint() const override;
186
187protected:
189
190 inline virtual FString GetType() const override;
191
193 inline SIZE_T GetAllocatedSize() const;
194};
195
197
198//** A Json Number Value. */
200{
201public:
204;
205 JSON_API virtual bool TryGetNumber(double& OutNumber) const override;
206 JSON_API virtual bool TryGetBool(bool& OutBool) const override;
207 JSON_API virtual bool TryGetString(FString& OutString) const override;
208 JSON_API virtual bool TryGetUtf8String(FUtf8String& OutString) const override;
209 JSON_API virtual SIZE_T GetMemoryFootprint() const override;
210
211protected:
212 double Value;
213
214 JSON_API virtual FString GetType() const override;
215};
216
218template <typename CharType>
220{
221public:
224
225 inline virtual bool TryGetString(FString& OutString) const override;
226 inline virtual bool TryGetUtf8String(FUtf8String& OutString) const override;
227 inline virtual bool TryGetNumber(double& OutDouble) const override;
228 inline virtual bool TryGetNumber(float &OutDouble) const override;
229 inline virtual bool TryGetNumber(int8& OutValue) const override;
230 inline virtual bool TryGetNumber(int16& OutValue) const override;
231 inline virtual bool TryGetNumber(int32& OutValue) const override;
232 inline virtual bool TryGetNumber(int64& OutValue) const override;
233 inline virtual bool TryGetNumber(uint8& OutValue) const override;
234 inline virtual bool TryGetNumber(uint16& OutValue) const override;
235 inline virtual bool TryGetNumber(uint32& OutValue) const override;
236 inline virtual bool TryGetNumber(uint64& OutValue) const override;
237 inline virtual bool TryGetBool(bool& OutBool) const override;
238 inline virtual bool PreferStringRepresentation() const override;
239 inline virtual SIZE_T GetMemoryFootprint() const override;
240
241protected:
243
244 inline virtual FString GetType() const override;
245
247 inline SIZE_T GetAllocatedSize() const;
248};
249
251
254{
255public:
256 JSON_API FJsonValueBoolean(bool InBool);
258
259 JSON_API virtual bool TryGetNumber(double& OutNumber) const override;
260 JSON_API virtual bool TryGetBool(bool& OutBool) const override;
261 JSON_API virtual bool TryGetString(FString& OutString) const override;
262 JSON_API virtual bool TryGetUtf8String(FUtf8String& OutString) const override;
263 JSON_API virtual SIZE_T GetMemoryFootprint() const override;
264
265protected:
266 bool Value;
267
268 JSON_API virtual FString GetType() const override;
269};
270
273{
274public:
278
279 JSON_API virtual bool TryGetArray(const TArray<TSharedPtr<FJsonValue>>*& OutArray) const override;
281 JSON_API virtual SIZE_T GetMemoryFootprint() const override;
282
283protected:
285
286 JSON_API virtual FString GetType() const override;
289};
290
291
294{
295public:
298
299 JSON_API virtual bool TryGetObject(const TSharedPtr<FJsonObject>*& OutObject) const override;
301 JSON_API virtual SIZE_T GetMemoryFootprint() const override;
302protected:
304
305 JSON_API virtual FString GetType() const override;
308};
309
310
313{
314public:
317
318 JSON_API virtual SIZE_T GetMemoryFootprint() const override;
319
320protected:
321 JSON_API virtual FString GetType() const override;
322};
323
324namespace UE::Json
325{
326
327template<typename T, typename = typename std::enable_if<!std::is_same_v<T, FJsonValue>>>
329{
330 using InSimpleValueType = std::decay_t<decltype(InSimpleValue)>;
331 if constexpr (std::is_same_v<InSimpleValueType, bool> || std::is_same_v<InSimpleValueType, FString>)
332 {
334 }
335 else
336 {
338 }
339}
340
342
343} // namespace UE::Json
344
345JSON_API FString ToString(const JsonNumberValueVariants& InNumberVariant);
346
347/* Global operators */
350JSON_API bool operator==(const JsonNumberValueVariants& Lhs, const FString& Rhs);
351
352
353#if !PLATFORM_COMPILER_HAS_GENERATED_COMPARISON_OPERATORS
354
355inline bool operator==(const FString& Lhs, const JsonNumberValueVariants& Rhs)
356{
357 return Rhs == Lhs;
358}
359
360inline bool operator!=(const JsonNumberValueVariants& Lhs, const FString& Rhs)
361{
362 return !(Lhs == Rhs);
363}
364
365inline bool operator!=(const FString& Lhs, const JsonNumberValueVariants& Rhs)
366{
367 return !(Lhs == Rhs);
368}
369
371{
372 return !(Lhs == Rhs);
373}
374
376{
377 return !(Lhs == Rhs);
378}
379#endif
380
381namespace
382{
383 template <typename CharType>
384 struct TJsonValueStringType
385 {
386 static_assert(sizeof(CharType) == 0, "Unsupported type");
387 };
388
389 template <>
390 struct TJsonValueStringType<TCHAR>
391 {
392 static FString GetType()
393 {
394 return TEXT("String");
395 }
396 };
397
398 template <>
399 struct TJsonValueStringType<UTF8CHAR>
400 {
401 static FString GetType()
402 {
403 return TEXT("Utf8String");
404 }
405 };
406}
407
408template <typename CharType>
414
415template <typename CharType>
421
422template <typename CharType>
423bool TJsonValueString<CharType>::TryGetString(FString& OutString) const
424{
425 if constexpr (std::is_same_v<FString, TString<CharType>>)
426 {
427 OutString = Value;
428 }
429 else
430 {
431 OutString = FString(Value);
432 }
433 return true;
434}
435
436template <typename CharType>
438{
439 if constexpr (std::is_same_v<FUtf8String, TString<CharType>>)
440 {
441 OutString = Value;
442 }
443 else
444 {
445 OutString = FUtf8String(Value);
446 }
447 return true;
448}
449
450template <typename CharType>
452{
453 if (Value.IsNumeric())
454 {
456 return true;
457 }
458 else
459 {
460 return false;
461 }
462}
463
464template <typename CharType>
470
471template <typename CharType>
477
478template <typename CharType>
484
485template <typename CharType>
491
492template <typename CharType>
494{
495 OutBool = Value.ToBool();
496 return true;
497}
498
499template <typename CharType>
501{
502 return sizeof(*this) + GetAllocatedSize();
503}
504
505template <typename CharType>
507{
508 return TJsonValueStringType<CharType>::GetType();
509}
510
511template <typename CharType>
513{
514 return Value.GetAllocatedSize();
515}
516
517namespace
518{
519 template <typename CharType>
520 struct TJsonValueNumberType
521 {
522 static_assert(sizeof(CharType), "Unsupported type");
523 };
524
525 template <>
526 struct TJsonValueNumberType<TCHAR>
527 {
528 static FString GetType()
529 {
530 return TEXT("NumberString");
531 }
532 };
533
534 template <>
535 struct TJsonValueNumberType<UTF8CHAR>
536 {
537 static FString GetType()
538 {
539 return TEXT("Utf8NumberString");
540 }
541 };
542}
543
544template <typename CharType>
550
551template <typename CharType>
557
558template <typename CharType>
560{
561 if constexpr (std::is_same_v<FString, TString<CharType>>)
562 {
563 OutString = Value;
564 }
565 else
566 {
567 OutString = FString(Value);
568 }
569 return true;
570}
571
572template <typename CharType>
574{
575 if constexpr (std::is_same_v<FUtf8String, TString<CharType>>)
576 {
577 OutString = Value;
578 }
579 else
580 {
581 OutString = FUtf8String(Value);
582 }
583 return true;
584}
585
586template <typename CharType>
591
592template <typename CharType>
597
598template <typename CharType>
603
604template <typename CharType>
609
610template <typename CharType>
615
616template <typename CharType>
621
622template <typename CharType>
627
628template <typename CharType>
633
634template <typename CharType>
639
640template <typename CharType>
645
646template <typename CharType>
648{
649 OutBool = Value.ToBool();
650 return true;
651}
652
653template <typename CharType>
658
659template <typename CharType>
661{
662 return sizeof(*this) + GetAllocatedSize();
663}
664
665template <typename CharType>
667{
668 return TJsonValueNumberType<CharType>::GetType();
669}
670
671template <typename CharType>
673{
674 return Value.GetAllocatedSize();
675}
#define UE_NONCOPYABLE(TypeName)
Definition CoreMiscDefines.h:457
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::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
void LexFromString(EAudioFeature &OutFeature, const TCHAR *String)
Definition IOSAppDelegate.cpp:163
TVariant< int32, uint32, int64, float, double > JsonNumberValueVariants
Definition JsonTypes.h:85
EJson
Definition JsonTypes.h:23
TVariant< bool, JsonNumberValueVariants, FString > JsonSimpleValueVariant
Definition JsonTypes.h:87
JSON_API bool operator==(const JsonSimpleValueVariant &Lhs, const JsonSimpleValueVariant &Rhs)
Definition JsonValue.cpp:695
bool operator!=(const JsonNumberValueVariants &Lhs, const FString &Rhs)
Definition JsonValue.h:360
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
Definition JsonValue.h:273
virtual JSON_API FString GetType() const override
Definition JsonValue.cpp:552
virtual JSON_API bool TryGetArray(const TArray< TSharedPtr< FJsonValue > > *&OutArray) const override
Definition JsonValue.cpp:524
JSON_API SIZE_T GetAllocatedSize() const
Definition JsonValue.cpp:541
virtual JSON_API ~FJsonValueArray()
TArray< TSharedPtr< FJsonValue > > Value
Definition JsonValue.h:284
virtual JSON_API SIZE_T GetMemoryFootprint() const override
Definition JsonValue.cpp:536
Definition JsonValue.h:254
virtual JSON_API bool TryGetString(FString &OutString) const override
Definition JsonValue.cpp:488
bool Value
Definition JsonValue.h:266
virtual JSON_API FString GetType() const override
Definition JsonValue.cpp:505
virtual JSON_API bool TryGetNumber(double &OutNumber) const override
Definition JsonValue.cpp:476
virtual JSON_API SIZE_T GetMemoryFootprint() const override
Definition JsonValue.cpp:500
virtual JSON_API bool TryGetBool(bool &OutBool) const override
Definition JsonValue.cpp:482
virtual JSON_API bool TryGetUtf8String(FUtf8String &OutString) const override
Definition JsonValue.cpp:494
virtual JSON_API ~FJsonValueBoolean()
Definition JsonValue.h:313
virtual JSON_API SIZE_T GetMemoryFootprint() const override
Definition JsonValue.cpp:599
JSON_API FJsonValueNull()
Definition JsonValue.cpp:592
virtual JSON_API ~FJsonValueNull()
virtual JSON_API FString GetType() const override
Definition JsonValue.cpp:604
Definition JsonValue.h:200
virtual JSON_API bool TryGetBool(bool &OutBool) const override
Definition JsonValue.cpp:440
virtual JSON_API bool TryGetString(FString &OutString) const override
Definition JsonValue.cpp:446
double Value
Definition JsonValue.h:212
virtual JSON_API FString GetType() const override
Definition JsonValue.cpp:463
virtual JSON_API SIZE_T GetMemoryFootprint() const override
Definition JsonValue.cpp:458
virtual JSON_API ~FJsonValueNumber()
virtual JSON_API bool TryGetUtf8String(FUtf8String &OutString) const override
Definition JsonValue.cpp:452
virtual JSON_API bool TryGetNumber(double &OutNumber) const override
Definition JsonValue.cpp:434
Definition JsonValue.h:294
JSON_API SIZE_T GetAllocatedSize() const
Definition JsonValue.cpp:582
virtual JSON_API bool TryGetObject(const TSharedPtr< FJsonObject > *&OutObject) const override
Definition JsonValue.cpp:565
virtual JSON_API FString GetType() const override
Definition JsonValue.cpp:587
virtual JSON_API SIZE_T GetMemoryFootprint() const override
Definition JsonValue.cpp:577
TSharedPtr< FJsonObject > Value
Definition JsonValue.h:303
virtual JSON_API ~FJsonValueObject()
Definition JsonValue.h:22
JSON_API FString AsString() const
Definition JsonValue.cpp:26
virtual JSON_API bool TryGetArray(const TArray< TSharedPtr< FJsonValue > > *&OutArray) const
Definition JsonValue.cpp:212
virtual JSON_API bool TryGetBool(bool &OutBool) const
Definition JsonValue.cpp:207
friend bool operator==(const FJsonValue &Lhs, const FJsonValue &Rhs)
Definition JsonValue.h:151
EJson Type
Definition JsonValue.h:134
void AsArgumentType(TSharedPtr< FJsonObject > &Value)
Definition JsonValue.h:123
JSON_API FUtf8String AsUtf8String() const
Definition JsonValue.cpp:38
virtual JSON_API ~FJsonValue()
virtual JSON_API const TSharedPtr< FJsonObject > & AsObject() const
Definition JsonValue.cpp:78
JSON_API void ErrorMessage(const FString &InType) const
Definition JsonValue.cpp:414
virtual JSON_API bool TryGetObject(const TSharedPtr< FJsonObject > *&Object) const
Definition JsonValue.cpp:222
JSON_API bool AsBool() const
Definition JsonValue.cpp:50
friend bool operator!=(const FJsonValue &Lhs, const FJsonValue &Rhs)
Definition JsonValue.h:156
void AsArgumentType(bool &Value)
Definition JsonValue.h:114
void AsArgumentType(FString &Value)
Definition JsonValue.h:109
virtual JSON_API bool TryGetUtf8String(FUtf8String &OutString) const
Definition JsonValue.cpp:202
virtual JSON_API bool TryGetNumber(double &OutNumber) const
Definition JsonValue.cpp:139
void AsArgumentType(TArray< TSharedPtr< FJsonValue > > &Value)
Definition JsonValue.h:119
JSON_API const TArray< TSharedPtr< FJsonValue > > & AsArray() const
Definition JsonValue.cpp:63
static JSON_API bool CompareEqual(const FJsonValue &Lhs, const FJsonValue &Rhs)
Definition JsonValue.cpp:243
virtual JSON_API bool TryGetString(FString &OutString) const
Definition JsonValue.cpp:197
void AsArgumentType(double &Value)
Definition JsonValue.h:104
virtual JSON_API SIZE_T GetMemoryFootprint() const
Definition JsonValue.cpp:237
JSON_API FJsonValue()
Definition JsonValue.cpp:6
bool IsNull() const
Definition JsonValue.h:98
virtual JSON_API bool PreferStringRepresentation() const
Definition JsonValue.cpp:232
JSON_API double AsNumber() const
Definition JsonValue.cpp:13
virtual FString GetType() const =0
Definition Array.h:670
Definition JsonValue.h:220
virtual bool TryGetNumber(double &OutDouble) const override
Definition JsonValue.h:587
SIZE_T GetAllocatedSize() const
Definition JsonValue.h:672
TString< CharType > Value
Definition JsonValue.h:242
virtual FString GetType() const override
Definition JsonValue.h:666
virtual bool TryGetBool(bool &OutBool) const override
Definition JsonValue.h:647
virtual bool TryGetUtf8String(FUtf8String &OutString) const override
Definition JsonValue.h:573
virtual bool TryGetString(FString &OutString) const override
Definition JsonValue.h:559
virtual SIZE_T GetMemoryFootprint() const override
Definition JsonValue.h:660
TJsonValueNumberString(const TString< CharType > &InString)
Definition JsonValue.h:545
virtual bool PreferStringRepresentation() const override
Definition JsonValue.h:654
Definition JsonValue.h:165
virtual FString GetType() const override
Definition JsonValue.h:506
virtual bool TryGetBool(bool &OutBool) const override
Definition JsonValue.h:493
bool IsEmpty() const
Definition JsonValue.h:180
virtual bool TryGetUtf8String(FUtf8String &OutString) const override
Definition JsonValue.h:437
TJsonValueString(const TString< CharType > &InString)
Definition JsonValue.h:409
virtual bool TryGetString(FString &OutString) const override
Definition JsonValue.h:423
virtual bool TryGetNumber(double &OutDouble) const override
Definition JsonValue.h:451
SIZE_T GetAllocatedSize() const
Definition JsonValue.h:512
virtual SIZE_T GetMemoryFootprint() const override
Definition JsonValue.h:500
TString< CharType > Value
Definition JsonValue.h:188
Definition SharedPointer.h:692
Definition TVariant.h:48
ECollisionShapeType GetType(const Chaos::FImplicitObject &InGeometry)
Definition ChaosInterfaceWrapperCore.h:105
Definition JsonValue.cpp:610
JsonSimpleValueVariant ToSimpleJsonVariant(const FJsonValue &InJsonValue)
Definition JsonValue.cpp:611
static UE_FORCEINLINE_HINT double Atod(const CharType *String)
Definition CString.h:1191
Definition TVariant.h:13