UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
NestedVariantJson.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
10
11// TODO: Update the JSON module to handle nested variants of any type directly so this file is not needed.
12
14class FJsonObject;
15class FJsonValue;
16
19
22
24
26{
30
38
69
70 COREONLINE_API static bool GetValueFromState(const StackState& State, FValue& OutValue);
74 COREONLINE_API static void ReadObjectStart(StackState& State);
76 COREONLINE_API static void ReadArrayStart(StackState& State);
78
79 template<class CharType>
81 {
82 OutValue.template Emplace<bool>(Reader.GetValueAsBoolean());
83 }
84
85 template<class CharType>
90
91 template<class CharType>
96
97 template<class CharType>
98 static bool IsNumberStringInteger(const CharType* Str)
99 {
100 if (*Str == '-' || *Str == '+')
101 {
102 Str++;
103 }
104
105 while (*Str != '\0')
106 {
107 if (*Str == '.')
108 {
109 return false;
110 }
111 else if (!FChar::IsDigit(*Str))
112 {
113 return false;
114 }
115
116 ++Str;
117 }
118
119 return true;
120 }
121
122 template<class CharType>
124 {
125 const typename TJsonReader<CharType>::StoredStringType& NumberString = Reader.GetValueAsNumberString();
126 if (IsNumberStringInteger(*NumberString))
127 {
129 }
130 else
131 {
132 OutValue.template Emplace<double>(Reader.GetValueAsNumber());
133 }
134 }
135
136 static void ReadNull(FValue& OutValue);
137 static void AddValueToObject(StackState& State, const FString& Identifier, FValue& NewValue);
138 static void AddValueToArray(StackState& State, FValue& NewValue);
139
140 template<class CharType, class PrintPolicy>
142 {
143 if(Element->Value.IsType<bool>())
144 {
145 if (bWriteValueOnly)
146 {
147 Writer.WriteValue(Element->Value.Get<bool>());
148 }
149 else
150 {
151 Writer.WriteValue(Element->Identifier, Element->Value.Get<bool>());
152 }
153
154 return true;
155 }
156
157 return false;
158 }
159
160 template<class CharType, class PrintPolicy>
162 {
163 if (Element->Value.IsType<long long>())
164 {
165 if (bWriteValueOnly)
166 {
167 Writer.WriteValue(Element->Value.Get<long long>());
168 }
169 else
170 {
171 Writer.WriteValue(Element->Identifier, Element->Value.Get<long long>());
172 }
173
174 return true;
175 }
176 else if(Element->Value.IsType<double>())
177 {
178 if (bWriteValueOnly)
179 {
180 Writer.WriteValue(Element->Value.Get<double>());
181 }
182 else
183 {
184 Writer.WriteValue(Element->Identifier, Element->Value.Get<double>());
185 }
186
187 return true;
188 }
189
190 return false;
191 }
192
193 template<class CharType, class PrintPolicy>
195 {
196 if(Element->Value.IsType<FString>())
197 {
198 if (bWriteValueOnly)
199 {
200 Writer.WriteValue(Element->Value.Get<FString>());
201 }
202 else
203 {
204 Writer.WriteValue(Element->Identifier, Element->Value.Get<FString>());
205 }
206
207 return true;
208 }
209
210 return false;
211 }
212
213 template<class CharType, class PrintPolicy>
215 {
216 return false;
217 }
218
219 template<class CharType, class PrintPolicy>
221 {
222 if(Element->Value.IsType<FNestedVariantJson::FArrayRef>())
223 {
224 if (Element->bHasBeenProcessed)
225 {
226 Writer.WriteArrayEnd();
227 }
228 else
229 {
230 Element->bHasBeenProcessed = true;
231 ElementStack.Push(Element);
232
233 if (bWriteValueOnly)
234 {
235 Writer.WriteArrayStart();
236 }
237 else
238 {
239 Writer.WriteArrayStart(Element->Identifier);
240 }
241
243
244 for (int Index = Values->Num() - 1; Index >= 0; --Index)
245 {
246 ElementStack.Push(MakeShared<FElement>((*Values)[Index]));
247 }
248 }
249
250 return true;
251 }
252
253 return false;
254 }
255
256 template<class CharType, class PrintPolicy>
258 {
259 if (Element->Value.IsType<FNestedVariantJson::FMapRef>())
260 {
261 if (Element->bHasBeenProcessed)
262 {
263 Writer.WriteObjectEnd();
264 }
265 else
266 {
267 Element->bHasBeenProcessed = true;
268 ElementStack.Push(Element);
269
270 if (bWriteValueOnly)
271 {
272 Writer.WriteObjectStart();
273 }
274 else
275 {
276 Writer.WriteObjectStart(Element->Identifier);
277 }
278
279 TArray<FString> Keys;
280 TArray<FValue> Values;
282 for (const FNestedVariantJson::FMap::ElementType& Entry : *ElementMap)
283 {
284 Keys.Add(Entry.Key);
285
286 Values.Add(Entry.Value);
287 }
288
289 check(Keys.Num() == Values.Num());
290
291 for (int Index = Values.Num() - 1; Index >= 0; --Index)
292 {
293 ElementStack.Push(MakeShared<FElement>(Keys[Index], Values[Index]));
294 }
295 }
296
297 return true;
298 }
299
300 return false;
301 }
302};
#define check(expr)
Definition AssertionMacros.h:314
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
return true
Definition ExternalRpcRegistry.cpp:601
EJson
Definition JsonTypes.h:23
COREONLINE_API void NestedVariantFromJsonObject(const TSharedPtr< FJsonObject > &JsonObject, FNestedVariantJson::FMapRef &Map)
Definition NestedVariantJson.cpp:55
COREONLINE_API void NestedVariantFromJson(const char *InJson, FNestedVariantJson::FMapRef &Map)
Definition NestedVariantJson.cpp:38
COREONLINE_API FString NestedVariantToJson(const FNestedVariantJson::FMapPtr &Map)
Definition NestedVariantJson.cpp:13
COREONLINE_API TSharedRef< FJsonObject > NestedVariantToJsonObject(const FNestedVariantJson::FMapRef &Map)
Definition NestedVariantJson.cpp:28
COREONLINE_API void LexFromString(FNestedVariantJson::FMap &OutValue, const TCHAR *InString)
Definition NestedVariantJson.cpp:64
Definition JsonObject.h:23
Definition JsonValue.h:22
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
UE_NODEBUG UE_FORCEINLINE_HINT SizeType Add(ElementType &&Item)
Definition Array.h:2696
Definition JsonReader.h:41
double GetValueAsNumber() const
Definition JsonReader.h:192
std::conditional_t< std::is_same_v< CharType, ANSICHAR >, FUtf8String, TString< CharType > > StoredStringType
Definition JsonReader.h:44
virtual StoredStringType StealInternalValueAsString()
Definition JsonReader.h:186
bool GetValueAsBoolean() const
Definition JsonReader.h:204
const StoredStringType & GetValueAsNumberString() const
Definition JsonReader.h:198
Definition JsonWriter.h:85
void WriteValue(uint8 Value)
Definition JsonWriter.h:201
void WriteObjectStart()
Definition JsonWriter.h:109
void WriteObjectEnd()
Definition JsonWriter.h:140
void WriteArrayStart()
Definition JsonWriter.h:153
void WriteArrayEnd()
Definition JsonWriter.h:183
Definition NestedVariant.h:662
Definition NestedVariant.h:696
TNestedVariantArrayPtr< KeyType, ValueTypes... > FArrayPtr
Definition NestedVariant.h:700
FNestedVariantValue< KeyType, ValueTypes... > FValue
Definition NestedVariant.h:706
TNestedVariantMapPtr< KeyType, ValueTypes... > FMapPtr
Definition NestedVariant.h:704
Definition SharedPointer.h:692
Definition SharedPointer.h:153
UE_FORCEINLINE_HINT TSharedPtr< ObjectType, Mode > ToSharedPtr() const
Definition SharedPointer.h:454
Definition TVariant.h:48
void Set(typename TIdentity< U >::Type &&Value)
Definition TVariant.h:193
U16 Index
Definition radfft.cpp:71
Definition NestedVariantJson.h:40
FElement(const FValue &InValue)
Definition NestedVariantJson.h:41
bool bHasBeenProcessed
Definition NestedVariantJson.h:66
FString Identifier
Definition NestedVariantJson.h:64
FValue Value
Definition NestedVariantJson.h:65
FElement(const FString &InIdentifier, const FValue &InValue)
Definition NestedVariantJson.h:58
FElement(const FArrayOfValues &Array)
Definition NestedVariantJson.h:52
FElement(const FMapOfValues &Object)
Definition NestedVariantJson.h:46
bool bIsKeyValuePair
Definition NestedVariantJson.h:67
Definition NestedVariantJson.h:32
EJson Type
Definition NestedVariantJson.h:33
FString Identifier
Definition NestedVariantJson.h:34
FNestedVariantJson::FMapPtr Object
Definition NestedVariantJson.h:36
FNestedVariantJson::FArrayPtr Array
Definition NestedVariantJson.h:35
Definition NestedVariantJson.h:26
static COREONLINE_API bool GetValueFromState(const StackState &State, FMapOfValues &OutMap)
static COREONLINE_API bool GetValueFromState(const StackState &State, FValue &OutValue)
Definition NestedVariantJson.cpp:72
static void ReadNumberAsString(TJsonReader< CharType > &Reader, FValue &OutValue)
Definition NestedVariantJson.h:92
static bool SerializeIfBool(TSharedRef< FElement > &Element, TJsonWriter< CharType, PrintPolicy > &Writer, bool bWriteValueOnly)
Definition NestedVariantJson.h:141
static bool SerializeIfNull(TSharedRef< FElement > &Element, TJsonWriter< CharType, PrintPolicy > &Writer, bool bWriteValueOnly)
Definition NestedVariantJson.h:214
static COREONLINE_API void ReadObjectEnd(StackState &State, FValue &OutValue)
Definition NestedVariantJson.cpp:142
static COREONLINE_API void ReadArrayEnd(StackState &State, FValue &OutValue)
Definition NestedVariantJson.cpp:153
static bool SerializeIfArray(TArray< TSharedRef< FElement > > &ElementStack, TSharedRef< FElement > &Element, TJsonWriter< CharType, PrintPolicy > &Writer, bool bWriteValueOnly)
Definition NestedVariantJson.h:220
static COREONLINE_API void ReadObjectStart(StackState &State)
Definition NestedVariantJson.cpp:136
static void ReadBoolean(TJsonReader< CharType > &Reader, FValue &OutValue)
Definition NestedVariantJson.h:80
static bool IsNumberStringInteger(const CharType *Str)
Definition NestedVariantJson.h:98
static COREONLINE_API void ResetValue(FValue &OutValue)
Definition NestedVariantJson.cpp:131
static void ReadNull(FValue &OutValue)
Definition NestedVariantJson.cpp:158
static bool SerializeIfNumber(TSharedRef< FElement > &Element, TJsonWriter< CharType, PrintPolicy > &Writer, bool bWriteValueOnly)
Definition NestedVariantJson.h:161
static void AddValueToArray(StackState &State, FValue &NewValue)
Definition NestedVariantJson.cpp:167
static COREONLINE_API void ReadArrayStart(StackState &State)
Definition NestedVariantJson.cpp:147
static void ReadNumber(TJsonReader< CharType > &Reader, FValue &OutValue)
Definition NestedVariantJson.h:123
static void AddValueToObject(StackState &State, const FString &Identifier, FValue &NewValue)
Definition NestedVariantJson.cpp:162
static bool SerializeIfObject(TArray< TSharedRef< FElement > > &ElementStack, TSharedRef< FElement > &Element, TJsonWriter< CharType, PrintPolicy > &Writer, bool bWriteValueOnly)
Definition NestedVariantJson.h:257
static void ReadString(TJsonReader< CharType > &Reader, FValue &OutValue)
Definition NestedVariantJson.h:86
FNestedVariantJson::FValue FValue
Definition NestedVariantJson.h:27
static bool SerializeIfString(TSharedRef< FElement > &Element, TJsonWriter< CharType, PrintPolicy > &Writer, bool bWriteValueOnly)
Definition NestedVariantJson.h:194
static bool IsDigit(CharType Char)
Definition Char.h:240