class GlobalSerializer (Niantic.ARDK.Utilities.BinarySerialization.GlobalSerializer)

Overview

This static class manages all individual "item serializers" for types. This way, we have a single point in our application to serialize data, any data, in a binary format. This global serializer allows item-serializers to be mapped to types that are otherwise non-serializable (that is, types don't need to have the [Serializable] or implement any interface to be able to be serialized by using this class/framework). More...

class GlobalSerializer {
public:
    // structs

    struct _SerializerInfo;

    // methods

    static object Deserialize(Stream stream);
    static IItemSerializer GetItemSerializerOrThrow(Type type);
    static IItemSerializer<T> GetItemSerializerOrThrow< T >();

    static void RegisterItemSerializer< T >(
        IItemSerializer<T> itemSerializer,
        string serializationName = null,
        bool registerDefaultArraySerializerOfT = true,
        bool registerDefaultReadOnlyCollectionOfT = true
    );

    static void RegisterUntypedItemSerializer(
        IItemSerializer untypedItemSerializer,
        string serializationName = null
    );

    static void Serialize(Stream stream, object item);
    static IItemSerializer TryGetItemSerializer(Type dataType);
    static IItemSerializer<T> TryGetItemSerializer< T >();
};

Detailed Documentation

This static class manages all individual "item serializers" for types. This way, we have a single point in our application to serialize data, any data, in a binary format. This global serializer allows item-serializers to be mapped to types that are otherwise non-serializable (that is, types don't need to have the [Serializable] or implement any interface to be able to be serialized by using this class/framework).

Methods

static object Deserialize(Stream stream)

Deserializes an object from the given stream. This method can very easily throw if the stream is corrupted, references an unregistered type or similar.

static IItemSerializer GetItemSerializerOrThrow(Type type)

Gets the untyped item-serializer for the given run-time type, or throws an InvalidOperationException if there's no serializer for the given type.

static IItemSerializer<T> GetItemSerializerOrThrow< T >()

Gets the item-serializer for the generic type T, or throws an InvalidOperationException if there's no serializer for such a type.

static void RegisterItemSerializer< T >(
    IItemSerializer<T> itemSerializer,
    string serializationName = null,
    bool registerDefaultArraySerializerOfT = true,
    bool registerDefaultReadOnlyCollectionOfT = true
)

Registers the given item-serializer, optionally allowing to use a different "serialization name" and to tell if the default array serializer should be used for this type (by default, the arrays are always registered for any given type).

This method is not thread-safe and is supposed to be called only during the initialization of the application, before any serialization or deserialization takes place.

static void Serialize(Stream stream, object item)

Serializes the given object (including null) into the given stream, or throws if a serializer for the given type of any of its members is not found.

Parameters:

stream

Required. The stream where the data will be serialized.

item

Can be null. The item to serialize. null value is a valid item to serialize.

static IItemSerializer TryGetItemSerializer(Type dataType)

Gets the item serializer for the given type, or returns null if a serializer for that type is not found.

static IItemSerializer<T> TryGetItemSerializer< T >()

Gets the item serializer for the generic type T, or returns null if a serializer for that type is not found. This method is particularly useful if users don't have direct access to the item-serializer for the type they plan to serialize, and such type is a struct. By first getting the item serializer, and using it to serialize the values, serializing the type-information is skipped, and there's no boxing. Note that if the type is class/reference type, it might still be preferable to use the Serialize() method if it is possible the value is null, as item serializers can't deal with null values.