Skip to content

AnnoMods.BBDom: Serialization

taubenangriff edited this page Mar 13, 2024 · 3 revisions

Object Serialization

AnnoMods.BBDom supports Object Serialization.

using AnnoMods.BBDom.ObjectSerializer;

Serialization

To a Stream

Sample Serialization:

MyObjectType Object = new MyObjectType()
Stream Result = BBConvert.SerializeObject<MyObjectType>(Object, new FileDBSerializerOptions() { Version = FileDBDocumentVersion.Version2 });

Into a BBDocument

MyObjectType Object = new MyObjectType()
BBDocument result = BBConvert.SerializeObjectToDocument<MyObjectType>(Object, new FileDBSerializerOptions() { Version = FileDBDocumentVersion.Version2 });

Deserialization

From a Stream

Stream myBBStream; 
MyObjectType Result = FileDBConvert.DeserializeObject<MyObjectType>(myBBStream, new FileDBSerializerOptions() { Version = FileDBDocumentVersion.Version2 });

From a BBDocument

BBDocument myBBdoc; 
MyObjectType Result = FileDBConvert.DeserializeObjectFromDocument<MyObjectType>(myBBdoc, new FileDBSerializerOptions() { Version = FileDBDocumentVersion.Version2 });

Prerequisites

  • You can serialize public Properties with get and set access, not Fields.
public int TestInt { get; set; }
  • For now, the only enumerables supported are Arrays.

  • Strings are a bit of a sketchy topic because of encoding, since it must be known for each String or String[] Property individually. In order to manage that, you can use EncodingAwareStrings as a datatype.

OnSerialized method

If an object has a public OnSerialized() method, it will be invoked after it has been deserialized.

FlatArrayAttribute

On occasions like maptemplates, you will sometimes find arrays that are not contained by any wrapping tag, providing you with the following structure:

MapTemplate
  ElementCount
  PlayableArea
  TemplateElement
    SomeValues
  TemplateElement
    SomeValues
  TemplateElement
    SomeValues

for ease of serialization, an Array can be marked with the FlatArray attribute. Each Array Entry will be named after the properties Name.

class MapTemplate
{
    [FlatArray]
    public TemplateElement[] TemplateElement { get; set; }
}

Options

in class BBSerializerOptions:

  • Version: Specifies the version used for serialization.
  • DefaultEncoding: Default Encoding used for Strings that aren't specified as EncodingAwareStrings
  • IgnoreMissingProperties: Ignores missing property errors during deserialization
  • SkipDefaultedValues: Only serializes a value if it is not 0 (for Value Types) or null (for reference types)

Nullable Types

  • Nullable is treated exactly like T would.

EncodingAwareStrings

using FileDBSerializing.EncodingAwareStrings;

These Strings an implementation that offer convenient access to a string that knows its Encoding.

Constructors:

//assigns the value of S to the EncodingAwareString upon construction
public UTF8String(String s)

//assigns the value of b to the EncodingAwareString upon construction
public UTF8String(byte[] b)

Properties:

public StringBuilder Content { get; }

The Content StringBuilder is meant to enable manipulation on the content in a non-destructive manner.

Methods:

//returns the byte representation of the string in its encoding
public byte[] GetBytes();
//returns the String itself
public String ToString();
//Returns a deep copy of the string as a UTF8String
public UTF8String ToUTF8();
//Returns a deep copy of the string as a UnicodeString 
public UnicodeString ToUnicode();
//Returns a deep copy of the string as a UTF32String  
public UTF32String ToUTF32();
//Returns a deep copy of the string as a ASCIIString 
public ASCIIString ToAscii();
  • you can implicitly assign an EncodingAwareString from a String and vice-versa.
  • You can implicitly assign an EncodingAwareString from a byte array.

Clone this wiki locally