Files
UnrealEngineUWP/Engine/Source/Runtime/JsonUtilities/Private/JsonObjectWrapper.cpp
ryan durand 0f0464a30e Updating copyright for Engine Runtime.
#rnx
#rb none


#ROBOMERGE-OWNER: ryan.durand
#ROBOMERGE-AUTHOR: ryan.durand
#ROBOMERGE-SOURCE: CL 10869210 via CL 10869511 via CL 10869900
#ROBOMERGE-BOT: (v613-10869866)

[CL 10870549 by ryan durand in Main branch]
2019-12-26 14:45:42 -05:00

88 lines
2.2 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "JsonObjectWrapper.h"
#include "Policies/CondensedJsonPrintPolicy.h"
#include "Serialization/JsonReader.h"
#include "Serialization/JsonSerializer.h"
bool FJsonObjectWrapper::ImportTextItem(const TCHAR*& Buffer, int32 PortFlags, UObject* Parent, FOutputDevice* ErrorText)
{
// read JSON string from Buffer
FString Json;
if (*Buffer == TCHAR('"'))
{
int32 NumCharsRead = 0;
if (!FParse::QuotedString(Buffer, Json, &NumCharsRead))
{
ErrorText->Logf(ELogVerbosity::Warning, TEXT("FJsonObjectWrapper::ImportTextItem: Bad quoted string: %s\n"), Buffer);
return false;
}
Buffer += NumCharsRead;
}
else
{
// consume the rest of the string (this happens on Paste)
Json = Buffer;
Buffer += Json.Len();
}
// empty string yields empty shared pointer
if (Json.IsEmpty())
{
JsonString.Empty();
JsonObject.Reset();
return true;
}
// parse the json
if (!JsonObjectFromString(Json))
{
if (ErrorText)
{
ErrorText->Logf(ELogVerbosity::Warning, TEXT("FJsonObjectWrapper::ImportTextItem - Unable to parse json: %s\n"), *Json);
}
return false;
}
JsonString = Json;
return true;
}
bool FJsonObjectWrapper::ExportTextItem(FString& ValueStr, FJsonObjectWrapper const& DefaultValue, UObject* Parent, int32 PortFlags, UObject* ExportRootScope) const
{
// empty pointer yields empty string
if (!JsonObject.IsValid())
{
ValueStr.Empty();
return true;
}
// serialize the json
return JsonObjectToString(ValueStr);
}
void FJsonObjectWrapper::PostSerialize(const FArchive& Ar)
{
if (!JsonString.IsEmpty())
{
// try to parse JsonString
if (!JsonObjectFromString(JsonString))
{
// do not abide a string that won't parse
JsonString.Empty();
}
}
}
bool FJsonObjectWrapper::JsonObjectToString(FString& Str) const
{
TSharedRef<TJsonWriter<TCHAR, TCondensedJsonPrintPolicy<TCHAR>>> JsonWriter = TJsonWriterFactory<TCHAR, TCondensedJsonPrintPolicy<TCHAR>>::Create(&Str, 0);
return FJsonSerializer::Serialize(JsonObject.ToSharedRef(), JsonWriter, true);
}
bool FJsonObjectWrapper::JsonObjectFromString(const FString& Str)
{
TSharedRef<TJsonReader<>> JsonReader = TJsonReaderFactory<>::Create(Str);
return FJsonSerializer::Deserialize(JsonReader, JsonObject);
}