UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
JsonSerializable.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Dom/JsonObject.h"
11#include "Misc/TVariant.h"
12
17{
22
28 JSON_API const FString ToJson(bool bPrettyPrint = true) const;
29 JSON_API const FUtf8String ToJsonUtf8(bool bPrettyPrint = true) const;
30
37 JSON_API virtual const FString ToJson(bool bPrettyPrint=true);
38 JSON_API virtual const FUtf8String ToJsonUtf8(bool bPrettyPrint = true);
39
46 template<class CharType, class PrintPolicy, ESPMode SPMode>
47 void ToJson(TSharedRef<TJsonWriter<CharType, PrintPolicy>, SPMode> JsonWriter, bool bFlatObject = false) const;
48
54 JSON_API virtual bool FromJson(const TCHAR* Json);
55 JSON_API virtual bool FromJson(const UTF8CHAR* Json);
56 JSON_API virtual bool FromJson(const FString& Json);
57 JSON_API virtual bool FromJson(const FUtf8String& Json);
58
64 JSON_API virtual bool FromJson(FString&& Json);
65 JSON_API virtual bool FromJson(FUtf8String&& Json);
66
73
80
81 JSON_API virtual bool FromJson(TSharedPtr<FJsonObject> JsonObject);
82
89 virtual void Serialize(FJsonSerializerBase& Serializer, bool bFlatObject) = 0;
90};
91
92template<class CharType, class PrintPolicy, ESPMode SPMode>
94{
96 const_cast<FJsonSerializable*>(this)->Serialize(Serializer, bFlatObject);
97}
98
100{
101
102namespace Private
103{
104
105template<typename T, typename CharType>
107{
108 OutArray.Reset();
109
113 {
115 {
117 if (Value.IsValid() && Value->TryGetObject(ArrayEntry))
118 {
119 if (ArrayEntry && ArrayEntry->IsValid())
120 {
122 OutArray.Add_GetRef(T()).Serialize(Serializer, false);
123 }
124 else
125 {
126 UE_LOG(LogJson, Error, TEXT("Failed to parse Json from array"));
127 return false;
128 }
129 }
130 }
131
132 return true;
133 }
134
135 return false;
136}
137
138using ReturnStringArgs = TTuple<FString* /*OutValue*/, bool /*bPrettyPrint*/>;
139
143
145
148
149template<typename T, typename...SerializerArgsT>
151{
152 for (T& ArrayEntry : InArray)
153 {
154 ArrayEntry.Serialize(Args...);
155 }
156}
157
158template<typename T, typename...SerializerArgsT>
160{
161 for (T* ArrayEntry : InArray)
162 {
163 ArrayEntry->Serialize(Args...);
164 }
165}
166
167template<typename T>
168inline void ToJson(TArray<T>& InArray, const ToJsonVariantArgs& InArgs)
169{
172
174
176 {
177 using StoredValueType = std::decay_t<decltype(StoredValue)>;
178 if constexpr (std::is_same_v<StoredValueType, ReturnStringArgs>)
179 {
180 if (StoredValue.template Get<1>())
181 {
184 }
185 else
186 {
189 }
190 }
191 else
192 {
193 return ::Visit([](auto& StoredWriter)
194 {
195 using StoredWriterType = std::decay_t<decltype(StoredWriter)>;
196 if constexpr (std::is_same_v<StoredWriterType, PrettyWriter>)
197 {
199 }
200 else if constexpr (std::is_same_v<StoredWriterType, CondensedWriter>)
201 {
203 }
204 }, StoredValue);
205 }
206 }, InArgs);
207
208
209 const bool bCloseWriter = InArgs.IsType<ReturnStringArgs>();
210
212 {
213 StoredSerializer.template Get<0>().StartArray();
214
216
217 StoredSerializer.template Get<0>().EndArray();
218
219 if (bCloseWriter)
220 {
221 StoredSerializer.template Get<1>()->Close();
222 }
223 }, SerializerToUse);
224}
225} // namespace Private
226
227template<typename T>
228static bool FromJson(TArray<T>& OutArray, const FString& JsonString)
229{
230 return Private::FromJson(OutArray, FStringView(JsonString));
231}
232
233template<typename T>
234static bool FromJson(TArray<T>& OutArray, FString&& JsonString)
235{
236 return Private::FromJson(OutArray, FStringView(MoveTemp(JsonString)));
237}
238
239template<typename T>
241{
243}
244
245template<typename T>
247{
249}
250
251/* non-const due to T::Serialize being a non-const function */
252template<typename T>
253static const FString ToJson(TArray<T>& InArray, const bool bPrettyPrint = true)
254{
255 FString JsonStr;
257 return JsonStr;
258}
259
260template<typename T>
261static void ToJson(TArray<T>& InArray, Private::PrettyWriter& JsonWriter)
262{
264}
265
266template<typename T>
267static void ToJson(TArray<T>& InArray, Private::CondensedWriter& JsonWriter)
268{
270}
271
272} // namespace UE::JsonArray
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::UTF8CHAR UTF8CHAR
An 8-bit character containing a UTF8 (Unicode, 8-bit, variable-width) code unit.
Definition Platform.h:1137
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define UE_LOG(CategoryName, Verbosity, Format,...)
Definition LogMacros.h:270
TStringView< TCHAR > FStringView
Definition StringFwd.h:45
decltype(auto) Visit(Func &&Callable, Variants &&... Args)
Definition TVariant.h:271
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
Definition JsonSerializerReader.h:12
Definition JsonSerializerWriter.h:16
virtual void Serialize(FStringView Name, int32 &Value) override
Definition JsonSerializerWriter.h:86
Definition Array.h:670
static TSharedRef< TJsonReader< CharType > > CreateFromView(TStringView< CharType > JsonString)
Definition JsonReader.h:1085
static bool Deserialize(const TSharedRef< TJsonReader< CharType > > &Reader, typename Policy::FValue &OutValue, EFlags InOptions=EFlags::None)
Definition JsonSerializer.h:283
Definition JsonWriter.h:783
static TSharedRef< TJsonWriter< CharType, PrintPolicy > > Create(FArchive *const Stream, int32 InitialIndent=0)
Definition JsonWriter.h:787
Definition JsonWriter.h:85
Definition SharedPointer.h:692
Definition SharedPointer.h:153
Definition TVariant.h:48
bool IsType() const
Definition TVariant.h:138
Definition OverriddenPropertySet.cpp:45
bool FromJson(TArray< T > &OutArray, TStringView< CharType > JsonString)
Definition JsonSerializable.h:106
void ToJson_SerializeArrayElements(TArray< T > &InArray, SerializerArgsT...Args)
Definition JsonSerializable.h:150
TSharedRef< TJsonWriter<> > PrettyWriter
Definition JsonSerializable.h:140
TVariant< PrettyWriter, CondensedWriter > WriterVariants
Definition JsonSerializable.h:142
TVariant< ReturnStringArgs, WriterVariants > ToJsonVariantArgs
Definition JsonSerializable.h:144
FJsonSerializerWriter<> PrettySerializer
Definition JsonSerializable.h:146
TSharedRef< TJsonWriter< TCHAR, TCondensedJsonPrintPolicy< TCHAR > > > CondensedWriter
Definition JsonSerializable.h:141
void ToJson(TArray< T > &InArray, const ToJsonVariantArgs &InArgs)
Definition JsonSerializable.h:168
FJsonSerializerWriter< TCHAR, TCondensedJsonPrintPolicy< TCHAR > > CondensedSerializer
Definition JsonSerializable.h:147
Definition JsonSerializable.h:100
Definition JsonSerializable.h:17
virtual JSON_API ~FJsonSerializable()
Definition JsonSerializable.cpp:7
JSON_API const FUtf8String ToJsonUtf8(bool bPrettyPrint=true) const
Definition JsonSerializable.cpp:18
virtual JSON_API bool FromJson(const TCHAR *Json)
Definition JsonSerializable.cpp:60
virtual void Serialize(FJsonSerializerBase &Serializer, bool bFlatObject)=0
JSON_API const FString ToJson(bool bPrettyPrint=true) const
Definition JsonSerializable.cpp:11
JSON_API bool FromJsonStringView(FUtf8StringView JsonStringView)
Definition JsonSerializable.cpp:121
Definition JsonSerializerBase.h:12
Definition TVariant.h:13
Definition Tuple.h:652