UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
JSON.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2// uLang JOSN support
3#pragma once
4
8
10#include "rapidjson/document.h"
11#include "rapidjson/prettywriter.h"
13
14namespace uLang
15{
16
17//====================================================================================
18// RapidJSON configuration
19//====================================================================================
20
25{
26public:
27 static void* Malloc(size_t Size)
28 {
30 }
31 static void* Realloc(void* OriginalPtr, size_t OriginalSize, size_t NewSize)
32 {
33 return GetSystemParams()._HeapRealloc(OriginalPtr, NewSize);
34 }
35 static void Free(void* Ptr)
36 {
38 }
39};
40
41using JSONMemoryPoolAllocator = rapidjson::MemoryPoolAllocator<JSONAllocator>;
42using JSONDocument = rapidjson::GenericDocument<rapidjson::UTF8<char>, JSONMemoryPoolAllocator, JSONAllocator>;
43using JSONGenericMemberIterator = rapidjson::GenericMemberIterator<false, rapidjson::UTF8<char>, JSONMemoryPoolAllocator>;
44using JSONValue = JSONDocument::ValueType;
45using JSONStringBuffer = rapidjson::StringBuffer;
46using JSONStringWriter = rapidjson::PrettyWriter<JSONStringBuffer>;
47using JSONStringRef = rapidjson::GenericStringRef<char>;
48
49//====================================================================================
50// Utility functions
51//====================================================================================
52
57
60
61
62//====================================================================================
63// JSON -> C++ conversion functions
64// Overloads of the function FromJSON for various data types
65// Supplement these by adding your own overloads for FromJSON
66//====================================================================================
67
71inline bool FromJSON(const JSONValue& JSON, bool* Value)
72{
73 if (JSON.IsBool())
74 {
75 *Value = JSON.GetBool();
76 return true;
77 }
78 return false;
79}
80
84inline bool FromJSON(const JSONValue& JSON, int* Value)
85{
86 if (JSON.IsInt())
87 {
88 *Value = JSON.GetInt();
89 return true;
90 }
91 return false;
92}
93
97inline bool FromJSON(const JSONValue& JSON, uint32_t* Value)
98{
99 if (JSON.IsUint())
100 {
101 *Value = JSON.GetUint();
102 return true;
103 }
104 return false;
105}
106
110inline bool FromJSON(const JSONValue& JSON, uint64_t* Value)
111{
112 if (JSON.IsUint64())
113 {
114 *Value = JSON.GetUint64();
115 return true;
116 }
117 return false;
118}
119
124{
125 if (JSON.IsString())
126 {
127 *Value = CUTF8StringView(JSON.GetString(), JSON.GetStringLength());
128 return true;
129 }
130 return false;
131}
132
134{
135 if (JSON.IsString())
136 {
137 *Value = CUTF8StringView(JSON.GetString(), JSON.GetStringLength());
138 return true;
139 }
140 return false;
141}
142
146template<class T>
147bool FromJSON(const JSONValue& JSON, TOptional<T>* OptionalValue)
148{
149 if (JSON.IsNull())
150 {
151 *OptionalValue = EResult::Unspecified;
152 return true;
153 }
154
155 T Value;
156 if (FromJSON(JSON, &Value))
157 {
158 *OptionalValue = Value;
159 return true;
160 }
161
162 return false;
163}
164
168template<class T>
169bool FromJSON(const JSONValue& JSON, TArray<T>* ArrayValue)
170{
171 if (JSON.IsArray())
172 {
173 ArrayValue->SetNum(JSON.Size());
174 for (uint32_t i = 0; i < JSON.Size(); ++i)
175 {
176 if (!FromJSON(JSON[i], &(*ArrayValue)[i]))
177 {
178 return false;
179 }
180 }
181 return true;
182 }
183
184 return false;
185}
186
190template<class T>
191bool FromJSON(const JSONValue& JSON, const char* MemberName, T* MemberValue, TOptional<bool> bRequired = EResult::Unspecified)
192{
193 if (!JSON.IsObject())
194 {
195 return false;
196 }
197
198 auto Member = JSON.FindMember(MemberName);
199 if (Member != JSON.MemberEnd())
200 {
201 return FromJSON(Member->value, MemberValue);
202 }
203
204 // Fail if required or if T wasn't optional
205 return bRequired ? !*bRequired : bool(TIsOptional<typename TRemovePointer<T>::Type>::Value);
206}
207
212{
213 if (!JSON)
214 {
215 return false;
216 }
217
218 JSON->SetBool(Value);
219 return true;
220}
221
226{
227 if (!JSON)
228 {
229 return false;
230 }
231
232 JSON->SetInt(Value);
233 return true;
234}
235
240{
241 if (!JSON)
242 {
243 return false;
244 }
245
246 JSON->SetUint(Value);
247 return true;
248}
249
254{
255 if (!JSON)
256 {
257 return false;
258 }
259
260 JSON->SetString((const char*)Value._Begin, Value.ByteLen(), Allocator);
261 return true;
262}
263
267template<class T>
269{
270 if (!OptionalValue.IsSet())
271 {
272 return true;
273 }
274
275 if (ToJSON(*OptionalValue, JSON, Allocator))
276 {
277 return true;
278 }
279
280 return false;
281}
282
286template<class T>
288{
289 if (!JSON)
290 {
291 return false;
292 }
293
294 JSON->SetArray();
295 JSON->Reserve(ArrayValue.Num(), Allocator);
296
297 for (const T& i : ArrayValue)
298 {
300 if (!ToJSON(i, &Elem, Allocator))
301 {
302 return false;
303 }
304
305 JSON->PushBack(Elem, Allocator);
306 }
307
308 return true;
309}
310
314template<class T>
315bool ToJSON(const T& MemberValue, const char* MemberName, JSONValue* JSON, JSONMemoryPoolAllocator& Allocator)
316{
317 if (!JSON)
318 {
319 return false;
320 }
321
322 JSONValue Member;
323 if (!ToJSON(MemberValue, &Member, Allocator))
324 {
325 return false;
326 }
327
328 JSON->AddMember(JSONStringRef(MemberName), Member, Allocator);
329
330 return true;
331}
332
336template<class T>
338{
339 if (!JSON)
340 {
341 return false;
342 }
343
344 if (!MemberValue.IsSet())
345 {
346 return true;
347 }
348
349 return ToJSON(MemberValue.GetValue(), MemberName, JSON, Allocator);
350}
351
352} // namespace uLang
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
const bool
Definition NetworkReplayStreaming.h:178
#define ULANG_THIRD_PARTY_INCLUDES_END
Definition Common.h:109
#define ULANG_THIRD_PARTY_INCLUDES_START
Definition Common.h:108
uint32 Size
Definition VulkanMemory.cpp:4034
Definition UTF8StringView.h:15
Definition JSON.h:25
static void Free(void *Ptr)
Definition JSON.h:35
static void * Malloc(size_t Size)
Definition JSON.h:27
static void * Realloc(void *OriginalPtr, size_t OriginalSize, size_t NewSize)
Definition JSON.h:31
Definition Array.h:51
void SetNum(int32_t NewNum, bool bAllowShrinking=true)
Definition Array.h:1341
ULANG_FORCEINLINE int32_t Num() const
Definition Array.h:402
Definition VVMEngineEnvironment.h:23
@ Unspecified
Not sure if success or failure.
rapidjson::GenericStringRef< char > JSONStringRef
Definition JSON.h:47
SSystemParams & GetSystemParams()
Global variable for efficient access.
Definition Common.cpp:9
rapidjson::GenericMemberIterator< false, rapidjson::UTF8< char >, JSONMemoryPoolAllocator > JSONGenericMemberIterator
Definition JSON.h:43
rapidjson::StringBuffer JSONStringBuffer
Definition JSON.h:45
bool FromJSON(const JSONValue &JSON, bool *Value)
Definition JSON.h:71
CUTF8String EscapeJSON(const UTF8Char Ch)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition JSON.cpp:9
rapidjson::PrettyWriter< JSONStringBuffer > JSONStringWriter
Definition JSON.h:46
rapidjson::GenericDocument< rapidjson::UTF8< char >, JSONMemoryPoolAllocator, JSONAllocator > JSONDocument
Definition JSON.h:42
uint8_t UTF8Char
UTF-8 octet.
Definition Unicode.h:20
bool ToJSON(bool Value, JSONValue *JSON, JSONMemoryPoolAllocator &)
Definition JSON.h:211
rapidjson::MemoryPoolAllocator< JSONAllocator > JSONMemoryPoolAllocator
Definition JSON.h:41
JSONDocument::ValueType JSONValue
Definition JSON.h:44
FRealloc _HeapRealloc
Reallocate system heap memory.
Definition Common.h:414
FFree _HeapFree
Free system heap memory.
Definition Common.h:415
FMalloc _HeapMalloc
Allocate system heap memory.
Definition Common.h:413
Definition Optional.h:187
Definition Optional.h:23
ULANG_FORCEINLINE bool IsSet() const
Definition Optional.h:161
T Type
Definition References.h:77