2014-12-07 19:09:38 -05:00
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
2014-10-01 18:20:53 -04:00
# include "SerializationPrivatePCH.h"
# include "JsonStructSerializerBackend.h"
/* Internal helpers
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
namespace JsonStructSerializerBackend
{
// Writes a property value to the serialization output.
template < typename ValueType >
2015-09-09 14:24:58 -04:00
void WritePropertyValue ( const TSharedRef < TJsonWriter < UCS2CHAR > > JsonWriter , const FStructSerializerState & State , const ValueType & Value )
2014-10-01 18:20:53 -04:00
{
2015-09-09 14:24:58 -04:00
if ( ( State . ValueProperty = = nullptr ) | | ( State . ValueProperty - > ArrayDim > 1 ) | | ( State . ValueProperty - > GetOuter ( ) - > GetClass ( ) = = UArrayProperty : : StaticClass ( ) ) )
2014-10-01 18:20:53 -04:00
{
JsonWriter - > WriteValue ( Value ) ;
}
2015-09-09 14:24:58 -04:00
else if ( State . KeyProperty ! = nullptr )
{
FString KeyString ;
State . KeyProperty - > ExportTextItem ( KeyString , State . KeyData , nullptr , nullptr , PPF_None ) ;
JsonWriter - > WriteValue ( KeyString , Value ) ;
}
2014-10-01 18:20:53 -04:00
else
{
2015-09-09 14:24:58 -04:00
JsonWriter - > WriteValue ( State . ValueProperty - > GetName ( ) , Value ) ;
2014-10-01 18:20:53 -04:00
}
}
// Writes a null value to the serialization output.
2015-09-09 14:24:58 -04:00
void WriteNull ( const TSharedRef < TJsonWriter < UCS2CHAR > > JsonWriter , const FStructSerializerState & State )
2014-10-01 18:20:53 -04:00
{
2015-09-09 14:24:58 -04:00
if ( ( State . ValueProperty = = nullptr ) | | ( State . ValueProperty - > ArrayDim > 1 ) | | ( State . ValueProperty - > GetOuter ( ) - > GetClass ( ) = = UArrayProperty : : StaticClass ( ) ) )
2014-10-01 18:20:53 -04:00
{
JsonWriter - > WriteNull ( ) ;
}
2015-09-09 14:24:58 -04:00
else if ( State . KeyProperty ! = nullptr )
{
FString KeyString ;
State . KeyProperty - > ExportTextItem ( KeyString , State . KeyData , nullptr , nullptr , PPF_None ) ;
JsonWriter - > WriteNull ( KeyString ) ;
}
2014-10-01 18:20:53 -04:00
else
{
2015-09-09 14:24:58 -04:00
JsonWriter - > WriteNull ( State . ValueProperty - > GetName ( ) ) ;
2014-10-01 18:20:53 -04:00
}
}
}
/* IStructSerializerBackend interface
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2015-09-09 13:45:13 -04:00
void FJsonStructSerializerBackend : : BeginArray ( const FStructSerializerState & State )
2014-10-01 18:20:53 -04:00
{
2015-09-09 13:45:13 -04:00
UObject * Outer = State . ValueProperty - > GetOuter ( ) ;
2014-10-01 18:20:53 -04:00
if ( ( Outer ! = nullptr ) & & ( Outer - > GetClass ( ) = = UArrayProperty : : StaticClass ( ) ) )
{
JsonWriter - > WriteArrayStart ( ) ;
}
2015-09-09 14:24:58 -04:00
else if ( State . KeyProperty ! = nullptr )
{
FString KeyString ;
State . KeyProperty - > ExportTextItem ( KeyString , State . KeyData , nullptr , nullptr , PPF_None ) ;
JsonWriter - > WriteArrayStart ( KeyString ) ;
}
2014-10-01 18:20:53 -04:00
else
{
2015-09-09 13:45:13 -04:00
JsonWriter - > WriteArrayStart ( State . ValueProperty - > GetName ( ) ) ;
2014-10-01 18:20:53 -04:00
}
}
2015-09-09 13:45:13 -04:00
void FJsonStructSerializerBackend : : BeginStructure ( const FStructSerializerState & State )
2014-10-01 18:20:53 -04:00
{
2015-09-09 13:45:13 -04:00
if ( State . ValueProperty ! = nullptr )
2014-10-01 18:20:53 -04:00
{
2015-09-09 13:45:13 -04:00
UObject * Outer = State . ValueProperty - > GetOuter ( ) ;
if ( ( Outer ! = nullptr ) & & ( Outer - > GetClass ( ) = = UArrayProperty : : StaticClass ( ) ) )
{
JsonWriter - > WriteObjectStart ( ) ;
}
2015-09-09 14:24:58 -04:00
else if ( State . KeyProperty ! = nullptr )
{
FString KeyString ;
State . KeyProperty - > ExportTextItem ( KeyString , State . KeyData , nullptr , nullptr , PPF_None ) ;
JsonWriter - > WriteObjectStart ( KeyString ) ;
}
2015-09-09 13:45:13 -04:00
else
{
JsonWriter - > WriteObjectStart ( State . ValueProperty - > GetName ( ) ) ;
}
2014-10-01 18:20:53 -04:00
}
else
{
2015-09-09 13:45:13 -04:00
JsonWriter - > WriteObjectStart ( ) ;
2014-10-01 18:20:53 -04:00
}
}
2015-09-09 13:45:13 -04:00
void FJsonStructSerializerBackend : : EndArray ( const FStructSerializerState & /*State*/ )
2014-10-01 18:20:53 -04:00
{
JsonWriter - > WriteArrayEnd ( ) ;
}
2015-09-09 13:45:13 -04:00
void FJsonStructSerializerBackend : : EndStructure ( const FStructSerializerState & /*State*/ )
2014-10-01 18:20:53 -04:00
{
JsonWriter - > WriteObjectEnd ( ) ;
}
2015-09-09 13:45:13 -04:00
void FJsonStructSerializerBackend : : WriteComment ( const FString & Comment )
2014-10-01 18:20:53 -04:00
{
// Json does not support comments
}
2015-09-09 13:45:13 -04:00
void FJsonStructSerializerBackend : : WriteProperty ( const FStructSerializerState & State , int32 ArrayIndex )
2014-10-01 18:20:53 -04:00
{
using namespace JsonStructSerializerBackend ;
// booleans
2015-09-09 13:45:13 -04:00
if ( State . ValueType = = UBoolProperty : : StaticClass ( ) )
2014-10-01 18:20:53 -04:00
{
2015-09-09 14:24:58 -04:00
WritePropertyValue ( JsonWriter , State , Cast < UBoolProperty > ( State . ValueProperty ) - > GetPropertyValue_InContainer ( State . ValueData , ArrayIndex ) ) ;
2014-10-01 18:20:53 -04:00
}
// unsigned bytes & enumerations
2015-09-09 13:45:13 -04:00
else if ( State . ValueType = = UByteProperty : : StaticClass ( ) )
2014-10-01 18:20:53 -04:00
{
2015-09-09 13:45:13 -04:00
UByteProperty * ByteProperty = Cast < UByteProperty > ( State . ValueProperty ) ;
2014-10-01 18:20:53 -04:00
if ( ByteProperty - > IsEnum ( ) )
{
2015-09-09 14:24:58 -04:00
WritePropertyValue ( JsonWriter , State , ByteProperty - > Enum - > GetEnumName ( ByteProperty - > GetPropertyValue_InContainer ( State . ValueData , ArrayIndex ) ) ) ;
2014-10-01 18:20:53 -04:00
}
else
{
2015-09-09 14:24:58 -04:00
WritePropertyValue ( JsonWriter , State , ( double ) Cast < UByteProperty > ( State . ValueProperty ) - > GetPropertyValue_InContainer ( State . ValueData , ArrayIndex ) ) ;
2014-10-01 18:20:53 -04:00
}
}
// floating point numbers
2015-09-09 13:45:13 -04:00
else if ( State . ValueType = = UDoubleProperty : : StaticClass ( ) )
2014-10-01 18:20:53 -04:00
{
2015-09-09 14:24:58 -04:00
WritePropertyValue ( JsonWriter , State , Cast < UDoubleProperty > ( State . ValueProperty ) - > GetPropertyValue_InContainer ( State . ValueData , ArrayIndex ) ) ;
2014-10-01 18:20:53 -04:00
}
2015-09-09 13:45:13 -04:00
else if ( State . ValueType = = UFloatProperty : : StaticClass ( ) )
2014-10-01 18:20:53 -04:00
{
2015-09-09 14:24:58 -04:00
WritePropertyValue ( JsonWriter , State , Cast < UFloatProperty > ( State . ValueProperty ) - > GetPropertyValue_InContainer ( State . ValueData , ArrayIndex ) ) ;
2014-10-01 18:20:53 -04:00
}
// signed integers
2015-09-09 13:45:13 -04:00
else if ( State . ValueType = = UIntProperty : : StaticClass ( ) )
2014-10-01 18:20:53 -04:00
{
2015-09-09 14:24:58 -04:00
WritePropertyValue ( JsonWriter , State , ( double ) Cast < UIntProperty > ( State . ValueProperty ) - > GetPropertyValue_InContainer ( State . ValueData , ArrayIndex ) ) ;
2014-10-01 18:20:53 -04:00
}
2015-09-09 13:45:13 -04:00
else if ( State . ValueType = = UInt8Property : : StaticClass ( ) )
2014-10-01 18:20:53 -04:00
{
2015-09-09 14:24:58 -04:00
WritePropertyValue ( JsonWriter , State , ( double ) Cast < UInt8Property > ( State . ValueProperty ) - > GetPropertyValue_InContainer ( State . ValueData , ArrayIndex ) ) ;
2014-10-01 18:20:53 -04:00
}
2015-09-09 13:45:13 -04:00
else if ( State . ValueType = = UInt16Property : : StaticClass ( ) )
2014-10-01 18:20:53 -04:00
{
2015-09-09 14:24:58 -04:00
WritePropertyValue ( JsonWriter , State , ( double ) Cast < UInt16Property > ( State . ValueProperty ) - > GetPropertyValue_InContainer ( State . ValueData , ArrayIndex ) ) ;
2014-10-01 18:20:53 -04:00
}
2015-09-09 13:45:13 -04:00
else if ( State . ValueType = = UInt64Property : : StaticClass ( ) )
2014-10-01 18:20:53 -04:00
{
2015-09-09 14:24:58 -04:00
WritePropertyValue ( JsonWriter , State , ( double ) Cast < UInt64Property > ( State . ValueProperty ) - > GetPropertyValue_InContainer ( State . ValueData , ArrayIndex ) ) ;
2014-10-01 18:20:53 -04:00
}
// unsigned integers
2015-09-09 13:45:13 -04:00
else if ( State . ValueType = = UUInt16Property : : StaticClass ( ) )
2014-10-01 18:20:53 -04:00
{
2015-09-09 14:24:58 -04:00
WritePropertyValue ( JsonWriter , State , ( double ) Cast < UUInt16Property > ( State . ValueProperty ) - > GetPropertyValue_InContainer ( State . ValueData , ArrayIndex ) ) ;
2014-10-01 18:20:53 -04:00
}
2015-09-09 13:45:13 -04:00
else if ( State . ValueType = = UUInt32Property : : StaticClass ( ) )
2014-10-01 18:20:53 -04:00
{
2015-09-09 14:24:58 -04:00
WritePropertyValue ( JsonWriter , State , ( double ) Cast < UUInt32Property > ( State . ValueProperty ) - > GetPropertyValue_InContainer ( State . ValueData , ArrayIndex ) ) ;
2014-10-01 18:20:53 -04:00
}
2015-09-09 13:45:13 -04:00
else if ( State . ValueType = = UUInt64Property : : StaticClass ( ) )
2014-10-01 18:20:53 -04:00
{
2015-09-09 14:24:58 -04:00
WritePropertyValue ( JsonWriter , State , ( double ) Cast < UUInt64Property > ( State . ValueProperty ) - > GetPropertyValue_InContainer ( State . ValueData , ArrayIndex ) ) ;
2014-10-01 18:20:53 -04:00
}
2015-07-17 16:38:09 -04:00
// names, strings & text
2015-09-09 13:45:13 -04:00
else if ( State . ValueType = = UNameProperty : : StaticClass ( ) )
2014-10-01 18:20:53 -04:00
{
2015-09-09 14:24:58 -04:00
WritePropertyValue ( JsonWriter , State , Cast < UNameProperty > ( State . ValueProperty ) - > GetPropertyValue_InContainer ( State . ValueData , ArrayIndex ) . ToString ( ) ) ;
2014-10-01 18:20:53 -04:00
}
2015-09-09 13:45:13 -04:00
else if ( State . ValueType = = UStrProperty : : StaticClass ( ) )
2014-10-01 18:20:53 -04:00
{
2015-09-09 14:24:58 -04:00
WritePropertyValue ( JsonWriter , State , Cast < UStrProperty > ( State . ValueProperty ) - > GetPropertyValue_InContainer ( State . ValueData , ArrayIndex ) ) ;
2014-10-01 18:20:53 -04:00
}
2015-09-09 13:45:13 -04:00
else if ( State . ValueType = = UTextProperty : : StaticClass ( ) )
2015-07-17 16:38:09 -04:00
{
2015-09-09 14:24:58 -04:00
WritePropertyValue ( JsonWriter , State , Cast < UTextProperty > ( State . ValueProperty ) - > GetPropertyValue_InContainer ( State . ValueData , ArrayIndex ) . ToString ( ) ) ;
2015-07-17 16:38:09 -04:00
}
2014-10-01 18:20:53 -04:00
// classes & objects
2015-09-09 13:45:13 -04:00
else if ( State . ValueType = = UClassProperty : : StaticClass ( ) )
2014-10-01 18:20:53 -04:00
{
2015-09-09 14:24:58 -04:00
WritePropertyValue ( JsonWriter , State , Cast < UClassProperty > ( State . ValueProperty ) - > GetPropertyValue_InContainer ( State . ValueData , ArrayIndex ) - > GetPathName ( ) ) ;
2014-10-01 18:20:53 -04:00
}
2015-09-09 13:45:13 -04:00
else if ( State . ValueType = = UObjectProperty : : StaticClass ( ) )
2014-10-01 18:20:53 -04:00
{
2015-09-09 14:24:58 -04:00
WriteNull ( JsonWriter , State ) ;
2014-10-01 18:20:53 -04:00
}
2015-09-09 13:45:13 -04:00
// unsupported property type
2014-10-01 18:20:53 -04:00
else
{
2015-09-09 13:45:13 -04:00
UE_LOG ( LogSerialization , Verbose , TEXT ( " FJsonStructSerializerBackend: Property %s cannot be serialized, because its type (%s) is not supported " ) , * State . ValueProperty - > GetFName ( ) . ToString ( ) , * State . ValueType - > GetFName ( ) . ToString ( ) ) ;
2014-10-01 18:20:53 -04:00
}
}