// Copyright Epic Games, Inc. All Rights Reserved. #include "Serialization/JsonSerializable.h" #include "Serialization/JsonSerializerReader.h" #include "Serialization/JsonSerializerWriter.h" FJsonSerializable::~FJsonSerializable() { } const FString FJsonSerializable::ToJson(bool bPrettyPrint/*=true*/) const { // Strip away const, because we use a single method that can read/write which requires non-const semantics // Otherwise, we'd have to have 2 separate macros for declaring const to json and non-const from json return const_cast(this)->ToJson(bPrettyPrint); } const FUtf8String FJsonSerializable::ToJsonUtf8(bool bPrettyPrint/*=true*/) const { // Strip away const, because we use a single method that can read/write which requires non-const semantics // Otherwise, we'd have to have 2 separate macros for declaring const to json and non-const from json return const_cast(this)->ToJsonUtf8(bPrettyPrint); } namespace FJsonSerializablePrivate { template const StringType ToJsonStringWithType(FJsonSerializable& Json, bool bPrettyPrint) { using CharType = typename StringType::ElementType; StringType JsonStr; if (bPrettyPrint) { TSharedRef> JsonWriter = TJsonWriterFactory::Create(&JsonStr); FJsonSerializerWriter Serializer(JsonWriter); Json.Serialize(Serializer, false); JsonWriter->Close(); } else { TSharedRef>> JsonWriter = TJsonWriterFactory>::Create(&JsonStr); FJsonSerializerWriter> Serializer(JsonWriter); Json.Serialize(Serializer, false); JsonWriter->Close(); } return JsonStr; } } const FString FJsonSerializable::ToJson(bool bPrettyPrint/*=true*/) { return FJsonSerializablePrivate::ToJsonStringWithType(*this, bPrettyPrint); } const FUtf8String FJsonSerializable::ToJsonUtf8(bool bPrettyPrint/*=true*/) { return FJsonSerializablePrivate::ToJsonStringWithType(*this, bPrettyPrint); } bool FJsonSerializable::FromJson(const TCHAR* Json) { return FromJsonStringView(FStringView(Json)); } bool FJsonSerializable::FromJson(const UTF8CHAR* Json) { return FromJsonStringView(FUtf8StringView(Json)); } bool FJsonSerializable::FromJson(const FString& Json) { return FromJsonStringView(FStringView(Json)); } bool FJsonSerializable::FromJson(const FUtf8String& Json) { return FromJsonStringView(FUtf8StringView(Json)); } bool FJsonSerializable::FromJson(FString&& Json) { TSharedPtr JsonObject; TSharedRef > JsonReader = TJsonReaderFactory<>::Create(MoveTemp(Json)); if (FJsonSerializer::Deserialize(JsonReader, JsonObject) && JsonObject.IsValid()) { FJsonSerializerReader Serializer(JsonObject); Serialize(Serializer, false); return true; } UE_LOG(LogJson, Warning, TEXT("Failed to parse Json from a string: %s"), *JsonReader->GetErrorMessage()); return false; } bool FJsonSerializable::FromJson(FUtf8String&& Json) { return FromJsonStringView(FUtf8StringView(Json)); } namespace UE::JsonSerializable::Private { template bool FromJsonStringView(FJsonSerializable* Serializable, TStringView JsonStringView) { TSharedPtr JsonObject; TSharedRef > JsonReader = TJsonReaderFactory::CreateFromView(JsonStringView); if (FJsonSerializer::Deserialize(JsonReader,JsonObject) && JsonObject.IsValid()) { FJsonSerializerReader Serializer(JsonObject); Serializable->Serialize(Serializer, false); return true; } UE_LOG(LogJson, Warning, TEXT("Failed to parse Json from a string: %s"), *JsonReader->GetErrorMessage()); return false; } } bool FJsonSerializable::FromJsonStringView(FUtf8StringView JsonStringView) { return UE::JsonSerializable::Private::FromJsonStringView(this, JsonStringView); } bool FJsonSerializable::FromJsonStringView(FWideStringView JsonStringView) { return UE::JsonSerializable::Private::FromJsonStringView(this, JsonStringView); } bool FJsonSerializable::FromJson(TSharedPtr JsonObject) { if (JsonObject.IsValid()) { FJsonSerializerReader Serializer(JsonObject); Serialize(Serializer, false); return true; } return false; }