UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
JsonObjectConverter.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 "CoreTypes.h"
10#include "Delegates/Delegate.h"
11#include "Dom/JsonObject.h"
12#include "Dom/JsonValue.h"
14#include "JsonGlobals.h"
15#include "JsonObjectWrapper.h"
16#include "Logging/LogCategory.h"
17#include "Logging/LogMacros.h"
24#include "UObject/Class.h"
25#include "Templates/Models.h"
27
28#define LOCTEXT_NAMESPACE "JsonObjectConverter"
29
31{
32 None = 0,
33 SkipStandardizeCase = 1 << 0,
34
40
47};
48
50
51class FProperty;
52class UStruct;
53
56{
57public:
58
60 static JSONUTILITIES_API FString StandardizeCase(const FString &StringIn);
61
64
66 static JSONUTILITIES_API bool GetTextFromField(const FString& FieldName, const TSharedPtr<FJsonValue>& FieldValue, FText& TextOut);
67
68public: // UStruct -> JSON
69
76
83
85
95 template<typename InStructType>
97 {
99 if (UStructToJsonObject(InStructType::StaticStruct(), &InStruct, JsonObject, CheckFlags, SkipFlags, ExportCb))
100 {
101 return JsonObject;
102 }
103 return TSharedPtr<FJsonObject>(); // something went wrong
104 }
105
120
135 static JSONUTILITIES_API bool UStructToJsonObjectString(const UStruct* StructDefinition, const void* Struct, FString& OutJsonString, int64 CheckFlags = 0, int64 SkipFlags = 0, int32 Indent = 0, const CustomExportCallback* ExportCb = nullptr, bool bPrettyPrint = true);
136
150 template<typename InStructType>
151 static bool UStructToJsonObjectString(const InStructType& InStruct, FString& OutJsonString, int64 CheckFlags = 0, int64 SkipFlags = 0, int32 Indent = 0, const CustomExportCallback* ExportCb = nullptr, bool bPrettyPrint = true)
152 {
154 {
155 return UStructToJsonObjectString(InStructType::StaticClass(), &InStruct, OutJsonString, CheckFlags, SkipFlags, Indent, ExportCb, bPrettyPrint);
156 }
157 else
158 {
159 return UStructToJsonObjectString(InStructType::StaticStruct(), &InStruct, OutJsonString, CheckFlags, SkipFlags, Indent, ExportCb, bPrettyPrint);
160 }
161 }
162
166 template<typename CharType, template<typename> class PrintPolicy>
168 {
171 {
173
174 if (FJsonSerializer::Serialize(JsonObject, JsonWriter))
175 {
176 JsonWriter->Close();
177 return true;
178 }
179 else
180 {
181 UE_LOG(LogJson, Warning, TEXT("UStructToFormattedObjectString - Unable to write out json"));
182 JsonWriter->Close();
183 }
184 }
185
186 return false;
187 }
188
203
204 /* * Converts from a FProperty to a Json Value using exportText
205 *
206 * @param Property The property to export
207 * @param Value Pointer to the value of the property
208 * @param CheckFlags Only convert properties that match at least one of these flags. If 0 check all properties.
209 * @param SkipFlags Skip properties that match any of these flags
210 * @param ExportCb Optional callback to override export behavior, if this returns null it will fallback to the default
211 * @param OuterProperty If applicable, the Array/Set/Map Property that contains this property
212 * @param ConversionFlags Bitwise flags to customize the conversion behavior
213 *
214 * @return The constructed JsonValue from the property
215 */
217
218public: // JSON -> UStruct
219
234 static JSONUTILITIES_API bool JsonObjectToUStruct(const TSharedRef<FJsonObject>& JsonObject, const UStruct* StructDefinition, void* OutStruct, int64 CheckFlags = 0, int64 SkipFlags = 0, const bool bStrictMode = false, FText* OutFailReason = nullptr, const CustomImportCallback* ImportCb = nullptr);
235
249 template<typename OutStructType>
250 static bool JsonObjectToUStruct(const TSharedRef<FJsonObject>& JsonObject, OutStructType* OutStruct, int64 CheckFlags = 0, int64 SkipFlags = 0, const bool bStrictMode = false, FText* OutFailReason = nullptr, const CustomImportCallback* ImportCb = nullptr)
251 {
253 {
254 return JsonObjectToUStruct(JsonObject, OutStructType::StaticClass(), OutStruct, CheckFlags, SkipFlags, bStrictMode, OutFailReason, ImportCb);
255 }
256 else
257 {
258 return JsonObjectToUStruct(JsonObject, OutStructType::StaticStruct(), OutStruct, CheckFlags, SkipFlags, bStrictMode, OutFailReason, ImportCb);
259 }
260 }
261
276 static JSONUTILITIES_API bool JsonAttributesToUStruct(const TMap< FString, TSharedPtr<FJsonValue> >& JsonAttributes, const UStruct* StructDefinition, void* OutStruct, int64 CheckFlags = 0, int64 SkipFlags = 0, const bool bStrictMode = false, FText* OutFailReason = nullptr, const CustomImportCallback* ImportCb = nullptr);
277
292 static JSONUTILITIES_API bool JsonValueToUProperty(const TSharedPtr<FJsonValue>& JsonValue, FProperty* Property, void* OutValue, int64 CheckFlags = 0, int64 SkipFlags = 0, const bool bStrictMode = false, FText* OutFailReason = nullptr, const CustomImportCallback* ImportCb = nullptr);
293
307 template<typename OutStructType>
308 static bool JsonObjectStringToUStruct(const FString& JsonString, OutStructType* OutStruct, int64 CheckFlags = 0, int64 SkipFlags = 0, const bool bStrictMode = false, FText* OutFailReason = nullptr, const CustomImportCallback* ImportCb = nullptr)
309 {
310 TSharedPtr<FJsonObject> JsonObject;
312 if (!FJsonSerializer::Deserialize(JsonReader, JsonObject) || !JsonObject.IsValid())
313 {
314 UE_LOG(LogJson, Warning, TEXT("JsonObjectStringToUStruct - Unable to parse. json=[%s]"), *JsonString);
315 if (OutFailReason)
316 {
317 *OutFailReason = FText::Format(LOCTEXT("FailJsonObjectDeserialize", "JsonObjectStringToUStruct - Unable to parse. json=[{0}]"), FText::FromString(*JsonString));
318 }
319 return false;
320 }
322 {
323 UE_LOG(LogJson, Warning, TEXT("JsonObjectStringToUStruct - Unable to deserialize. json=[%s]"), *JsonString);
324 if (OutFailReason)
325 {
326 *OutFailReason = FText::Format(LOCTEXT("FailJsonObjectConversion", "JsonObjectStringToUStruct - Unable to deserialize. json=[{0}]\n{1}"), FText::FromString(*JsonString), *OutFailReason);
327 }
328 return false;
329 }
330 return true;
331 }
332
346 template<typename OutStructType>
347 static bool JsonArrayStringToUStruct(const FString& JsonString, TArray<OutStructType>* OutStructArray, int64 CheckFlags = 0, int64 SkipFlags = 0, const bool bStrictMode = false, FText* OutFailReason = nullptr, const CustomImportCallback* ImportCb = nullptr)
348 {
351 if (!FJsonSerializer::Deserialize(JsonReader, JsonArray))
352 {
353 UE_LOG(LogJson, Warning, TEXT("JsonArrayStringToUStruct - Unable to parse. json=[%s]"), *JsonString);
354 if (OutFailReason)
355 {
356 *OutFailReason = FText::Format(LOCTEXT("FailJsonArrayDeserialize", "JsonArrayStringToUStruct - Unable to parse. json=[{0}]"), FText::FromString(*JsonString));
357 }
358 return false;
359 }
361 {
362 UE_LOG(LogJson, Warning, TEXT("JsonArrayStringToUStruct - Error parsing one of the elements. json=[%s]"), *JsonString);
363 if (OutFailReason)
364 {
365 *OutFailReason = FText::Format(LOCTEXT("FailJsonArrayConversion", "JsonArrayStringToUStruct - Error parsing one of the elements. json=[{0}]\n{1}"), FText::FromString(*JsonString), *OutFailReason);
366 }
367 return false;
368 }
369 return true;
370 }
371
385 template<typename OutStructType>
386 static bool JsonArrayToUStruct(const TArray<TSharedPtr<FJsonValue>>& JsonArray, TArray<OutStructType>* OutStructArray, int64 CheckFlags = 0, int64 SkipFlags = 0, const bool bStrictMode = false, FText* OutFailReason = nullptr, const CustomImportCallback* ImportCb = nullptr)
387 {
388 OutStructArray->SetNum(JsonArray.Num());
389 for (int32 i = 0; i < JsonArray.Num(); ++i)
390 {
391 const auto& Value = JsonArray[i];
392 if (Value->Type != EJson::Object)
393 {
394 UE_LOG(LogJson, Warning, TEXT("JsonArrayToUStruct - Array element [%i] was not an object."), i);
395 if (OutFailReason)
396 {
397 *OutFailReason = FText::Format(LOCTEXT("FailJsonArrayElementObject", "JsonArrayToUStruct - Array element [{0}] was not an object."), i);
398 }
399 return false;
400 }
401 if (!FJsonObjectConverter::JsonObjectToUStruct(Value->AsObject().ToSharedRef(), OutStructType::StaticStruct(), &(*OutStructArray)[i], CheckFlags, SkipFlags, bStrictMode, OutFailReason, ImportCb))
402 {
403 UE_LOG(LogJson, Warning, TEXT("JsonArrayToUStruct - Unable to convert element [%i]."), i);
404 if (OutFailReason)
405 {
406 *OutFailReason = FText::Format(LOCTEXT("FailJsonArrayElementConversion", "JsonArrayToUStruct - Unable to convert element [{0}].\n{1}"), i, *OutFailReason);
407 }
408 return false;
409 }
410 }
411 return true;
412 }
413
414 /*
415 * Parses text arguments from Json into a map
416 * @param JsonObject Object to parse arguments from
417 */
419};
420
421#undef LOCTEXT_NAMESPACE
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
SharedPointerInternals::TRawPtrProxy< ObjectType > MakeShareable(ObjectType *InObject)
Definition SharedPointer.h:1947
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define ENUM_CLASS_FLAGS(Enum)
Definition EnumClassFlags.h:6
#define LOCTEXT(InKey, InTextLiteral)
Definition Internationalization.h:295
EJsonObjectConversionFlags
Definition JsonObjectConverter.h:31
#define UE_LOG(CategoryName, Verbosity, Format,...)
Definition LogMacros.h:270
const bool
Definition NetworkReplayStreaming.h:178
Definition JsonObjectConverter.h:56
static JSONUTILITIES_API TSharedPtr< FJsonValue > UPropertyToJsonValue(FProperty *Property, const void *Value, int64 CheckFlags=0, int64 SkipFlags=0, const CustomExportCallback *ExportCb=nullptr, FProperty *OuterProperty=nullptr, EJsonObjectConversionFlags ConversionFlags=EJsonObjectConversionFlags::None)
Definition JsonObjectConverter.cpp:419
static JSONUTILITIES_API bool GetTextFromObject(const TSharedRef< FJsonObject > &Obj, FText &TextOut)
Definition JsonObjectConverter.cpp:478
static bool UStructToJsonObjectString(const InStructType &InStruct, FString &OutJsonString, int64 CheckFlags=0, int64 SkipFlags=0, int32 Indent=0, const CustomExportCallback *ExportCb=nullptr, bool bPrettyPrint=true)
Definition JsonObjectConverter.h:151
TDelegate< bool(const TSharedPtr< FJsonValue > &JsonValue, FProperty *Property, void *Value)> CustomImportCallback
Definition JsonObjectConverter.h:82
static TSharedPtr< FJsonObject > UStructToJsonObject(const InStructType &InStruct, int64 CheckFlags=0, int64 SkipFlags=0, const CustomExportCallback *ExportCb=nullptr)
Definition JsonObjectConverter.h:96
static JSONUTILITIES_API FFormatNamedArguments ParseTextArgumentsFromJson(const TSharedPtr< const FJsonObject > &JsonObject)
Definition JsonObjectConverter.cpp:1296
static bool JsonObjectToUStruct(const TSharedRef< FJsonObject > &JsonObject, OutStructType *OutStruct, int64 CheckFlags=0, int64 SkipFlags=0, const bool bStrictMode=false, FText *OutFailReason=nullptr, const CustomImportCallback *ImportCb=nullptr)
Definition JsonObjectConverter.h:250
static bool JsonObjectStringToUStruct(const FString &JsonString, OutStructType *OutStruct, int64 CheckFlags=0, int64 SkipFlags=0, const bool bStrictMode=false, FText *OutFailReason=nullptr, const CustomImportCallback *ImportCb=nullptr)
Definition JsonObjectConverter.h:308
static JSONUTILITIES_API bool GetTextFromField(const FString &FieldName, const TSharedPtr< FJsonValue > &FieldValue, FText &TextOut)
Definition JsonObjectConverter.cpp:1235
TDelegate< TSharedPtr< FJsonValue >(FProperty *Property, const void *Value)> CustomExportCallback
Definition JsonObjectConverter.h:75
static JSONUTILITIES_API bool UStructToJsonAttributes(const UStruct *StructDefinition, const void *Struct, TMap< FString, TSharedPtr< FJsonValue > > &OutJsonAttributes, int64 CheckFlags=0, int64 SkipFlags=0, const CustomExportCallback *ExportCb=nullptr, EJsonObjectConversionFlags ConversionFlags=EJsonObjectConversionFlags::None)
Definition JsonObjectConverter.cpp:429
static JSONUTILITIES_API bool JsonValueToUProperty(const TSharedPtr< FJsonValue > &JsonValue, FProperty *Property, void *OutValue, int64 CheckFlags=0, int64 SkipFlags=0, const bool bStrictMode=false, FText *OutFailReason=nullptr, const CustomImportCallback *ImportCb=nullptr)
Definition JsonObjectConverter.cpp:1219
static JSONUTILITIES_API bool JsonObjectToUStruct(const TSharedRef< FJsonObject > &JsonObject, const UStruct *StructDefinition, void *OutStruct, int64 CheckFlags=0, int64 SkipFlags=0, const bool bStrictMode=false, FText *OutFailReason=nullptr, const CustomImportCallback *ImportCb=nullptr)
Definition JsonObjectConverter.cpp:1224
static JSONUTILITIES_API bool UStructToJsonObjectString(const UStruct *StructDefinition, const void *Struct, FString &OutJsonString, int64 CheckFlags=0, int64 SkipFlags=0, int32 Indent=0, const CustomExportCallback *ExportCb=nullptr, bool bPrettyPrint=true)
Definition JsonObjectConverter.cpp:450
static bool JsonArrayStringToUStruct(const FString &JsonString, TArray< OutStructType > *OutStructArray, int64 CheckFlags=0, int64 SkipFlags=0, const bool bStrictMode=false, FText *OutFailReason=nullptr, const CustomImportCallback *ImportCb=nullptr)
Definition JsonObjectConverter.h:347
static bool JsonArrayToUStruct(const TArray< TSharedPtr< FJsonValue > > &JsonArray, TArray< OutStructType > *OutStructArray, int64 CheckFlags=0, int64 SkipFlags=0, const bool bStrictMode=false, FText *OutFailReason=nullptr, const CustomImportCallback *ImportCb=nullptr)
Definition JsonObjectConverter.h:386
static JSONUTILITIES_API bool JsonAttributesToUStruct(const TMap< FString, TSharedPtr< FJsonValue > > &JsonAttributes, const UStruct *StructDefinition, void *OutStruct, int64 CheckFlags=0, int64 SkipFlags=0, const bool bStrictMode=false, FText *OutFailReason=nullptr, const CustomImportCallback *ImportCb=nullptr)
Definition JsonObjectConverter.cpp:1229
static bool UStructToFormattedJsonObjectString(const UStruct *StructDefinition, const void *Struct, FString &OutJsonString, int64 CheckFlags=0, int64 SkipFlags=0, int32 Indent=0, const CustomExportCallback *ExportCb=nullptr, EJsonObjectConversionFlags ConversionFlags=EJsonObjectConversionFlags::None)
Definition JsonObjectConverter.h:167
static JSONUTILITIES_API FString StandardizeCase(const FString &StringIn)
Definition JsonObjectConverter.cpp:24
static JSONUTILITIES_API const CustomExportCallback ExportCallback_WriteISO8601Dates
Definition JsonObjectConverter.h:84
Definition JsonObject.h:23
Definition UnrealType.h:174
Definition Text.h:385
static CORE_API FText FromString(const ANSICHAR *String)
Definition Text.cpp:1081
static CORE_API FText Format(FTextFormat Fmt, const FFormatNamedArguments &InArguments)
Definition Text.cpp:469
Definition Array.h:670
Definition DelegateSignatureImpl.inl:310
static TSharedRef< TJsonReader< TElementType_T< StringType > > > Create(StringType &&JsonString)
Definition JsonReader.h:1070
static bool Deserialize(const TSharedRef< TJsonReader< CharType > > &Reader, typename Policy::FValue &OutValue, EFlags InOptions=EFlags::None)
Definition JsonSerializer.h:283
static bool Serialize(const typename Policy::FValue &Value, const FString &Identifier, const TSharedRef< TJsonWriter< CharType, PrintPolicy > > &Writer, bool bCloseWriter=true)
Definition JsonSerializer.h:359
Definition JsonWriter.h:783
Definition UnrealString.h.inl:34
Definition SharedPointer.h:692
TSharedRef< ObjectType, Mode > ToSharedRef() const &
Definition SharedPointer.h:1028
UE_FORCEINLINE_HINT const bool IsValid() const
Definition SharedPointer.h:1085
Definition SharedPointer.h:153
Definition Class.h:480
Definition Models.h:64