You've already forked UnrealEngineUWP
mirror of
https://github.com/izzy2lost/UnrealEngineUWP.git
synced 2026-03-26 18:15:20 -07:00
- Fix serialization of lists, directories and arrays that are set to null. - Throw an exception when serializing a type that does not have any explicit compact binary field attributes. This is usually an error, caused by serializing a framework type with no matching converter, but can also occur when serializing an empty base class instance. To handle these situations, the class can be marked with [CbObject] to explicitly opt-in to being valid for serialization. #fyi Joe.Kirchoff #preflight none [CL 19526671 by Ben Marsh in ue5-main branch]
86 lines
1.7 KiB
C#
86 lines
1.7 KiB
C#
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
using EpicGames.Core;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace EpicGames.Serialization.Converters
|
|
{
|
|
/// <summary>
|
|
/// Converter for array types
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
class CbArrayConverter<T> : CbConverterBase<T[]>
|
|
{
|
|
/// <inheritdoc/>
|
|
public override T[] Read(CbField field)
|
|
{
|
|
if (field.IsNull())
|
|
{
|
|
return null!;
|
|
}
|
|
|
|
List<T> list = new List<T>();
|
|
foreach (CbField elementField in field)
|
|
{
|
|
list.Add(CbSerializer.Deserialize<T>(elementField));
|
|
}
|
|
return list.ToArray();
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public override void Write(CbWriter writer, T[] list)
|
|
{
|
|
if (list == null)
|
|
{
|
|
writer.WriteNullValue();
|
|
}
|
|
else
|
|
{
|
|
writer.BeginArray();
|
|
foreach (T element in list)
|
|
{
|
|
CbSerializer.Serialize<T>(writer, element);
|
|
}
|
|
writer.EndArray();
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public override void WriteNamed(CbWriter writer, Utf8String name, T[] array)
|
|
{
|
|
if (array == null)
|
|
{
|
|
writer.WriteNull(name);
|
|
}
|
|
else
|
|
{
|
|
writer.BeginArray(name);
|
|
foreach (T element in array)
|
|
{
|
|
CbSerializer.Serialize<T>(writer, element);
|
|
}
|
|
writer.EndArray();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Factory for CbListConverter
|
|
/// </summary>
|
|
class CbArrayConverterFactory : CbConverterFactory
|
|
{
|
|
/// <inheritdoc/>
|
|
public override ICbConverter? CreateConverter(Type type)
|
|
{
|
|
if (type.IsArray)
|
|
{
|
|
Type elementType = type.GetElementType()!;
|
|
Type converterType = typeof(CbArrayConverter<>).MakeGenericType(elementType);
|
|
return (ICbConverter)Activator.CreateInstance(converterType)!;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|