Skip to content

FBS Annotations

James Courtney edited this page Oct 13, 2020 · 17 revisions

FBS Annotations

FlatSharp supports the general FBS syntax. It does not implement every feature, but tries to warn you in those cases. It also supports some custom extensions that are specific to FlatSharp schemas. Those are documented here.

PrecompiledSerializer

Description: Instructs the FlatSharp compiler to pre-generate a serializer for this type and all that it encompasses. Only one serializer may be generated per type. Without this option, FlatSharp will only generate the data contracts. Precompiled Serializers are required for types exposed in gRPC definitions.

Valid On: Tables only

Value Required: False

Default Value: Default

Valid Values:

  • Default (GreedyMutable)
  • GreedyMutable
  • Greedy
  • VectorCacheMutable
  • VectorCache
  • PropertyCache
  • Lazy

Examples:

// Table with a default precompiled serializer
table MyTable (PrecompiledSerializer) { Value : int; }

// Table with a lazy precompiled serializer
table LazyTable (PrecompiledSerializer:"Lazy") { Value : int; }

NonVirtual

Description: Marks a single field (or all the fields in a table/struct) as being non-virtual in the generated C#. Non-Virtual properties are deserialized greedily and perform faster due to JIT inlining.

Valid On: Tables, Structs, and Fields

Value Required: False

Default Value: true

Valid Values:

  • True
  • False

Examples:

// Table that marks all fields as non-virtual
table MyTable (PrecompiledSerializer, NonVirtual:"true") 
{ 
   Value : int;  // Inherits the NonVirtual setting from the table.
   Value2 : int (NonVirtual:"false"); // overrides the table setting for this field. This will be generated as a virtual property.
}

Setter

Description: Controls how a property setter is generated for the given field. This, along with the ObsoleteDefaultConstructor attribute can assist in defining immutable objects.

Valid On: Fields

Value Required: False

Default Value: Public

Valid Values:

  • None
  • ProtectedInternal
  • Protected
  • Public

Examples:

table MyTable
{ 
   Value  : int; // generates a public setter property.
   Value2 : int (Setter:"None"); // does not generate a setter property.
   Value3 : int (Setter:"ProtectedInternal"); // generates a setter with the "protected internal" access modifier
   Value4 : int (Setter:"Protected"); // generates a setter with the "protected" modifier.
   Value5 : int (Setter:"Public"); // generates a setter with the "public" modifier.
}

VectorType

Description: Controls the type of vector generated.

Valid On: Vector fields

Value Required: False

Default Value: IList

Valid Values:

  • IList
  • IReadOnlyList
  • Array
  • Memory
  • ReadOnlyMemory
  • IIndexedVector

Examples:

table MyTable
{ 
   // Generates an IList<int> vector
   Value  : [int]; 
   
   // Generates an IList<int> vector
   Value2 : [int] (VectorType:"IList"); 
   
   // Vector of type IReadOnlyList<int>
   Value3 : [int] (VectorType:"IReadOnlyList"); 
   
   // Vector of type Memory<byte>. Depending on deserialization mode, this may map a raw segment
   // of the input buffer. Memory vectors only support unsigned bytes.
   Value4 : [ubyte] (VectorType:"Memory") 

   // Same as above, but with ReadOnlyMemory<byte>
   Value5 : [int] (VectorType:"ReadOnlyMemory");

   // Generates an int[] vector. Array vectors cannot be lazily allocated.
   Value6 : [int] (VectorType:"Array");

   // Generates an IIndexedVector<OtherTable> vector. Indexed vectors are always sorted, can only
   // contain tables, and those tables must have a 'Key' attribute specified.
   Value7 : [OtherTable] (VectorType:"IIndexedVector");
}

SortedVector

Description: Controls whether a given vector is sorted at serialization time. Sorted Vectors must contain tables, and the tables must have a single field with the Key attribute.

Valid On: Vector fields

Value Required: False

Default Value: True

Valid Values:

  • True
  • False

Examples:

table MyTable
{ 
   Value  : [ItemTable] (SortedVector);
}

table ItemTable
{
   Value : string (Key);
}

Key

Description: Indicates that the field in a given table that should be used as the sort key when the table is used as the item of a sorted vector.

Valid On: Table fields of type string, int8, uint8, int16, uint16, int32, uint32, int64, uint64, float, or double.

Value Required: False

Default Value: True

Valid Values:

  • True
  • False

Examples:

table MyTable
{ 
   Value  : [ItemTable] (SortedVector);
}

table ItemTable
{
   Value : string (Key);
}

SharedString

Description: The given string field or string vector should be a SharedString. SharedStrings are FlatSharp's way of enabling string reuse inside a buffer, which can lead to smaller buffers and better performance in some cases. Note that simply enabling SharedStrings is not enough to enable string sharing. Please refer to the Shared Strings sample for more information.

Valid On: String fields and String Vectors in tables.

Value Required: False

Default Value: True

Valid Values:

  • True
  • False

Examples:

table MyTable
{ 
   // Generates an IList<SharedString> vector
   Value  : [string] (SharedString);
   
   // Generates a SharedString field
   Value2 : string (SharedString); 
}

ObsoleteDefaultConstructor

Description: FlatSharp requires that serialized objects expose a public, parameterless default constructor. This option flags that default constructor on generated tables and structs with the [Obsolete] attribute, which can prevent user code from invoking a constructor that does not properly initialize the object. This, along with the Setter attribute, can assist with creating immutable objects

Valid On: Tables and Structs

Value Required: False

Default Value: True

Valid Values:

  • True
  • False

Examples:

table MyTable (ObsoleteDefaultConstructor)
{ 
   Value  : int; 
}

NoCustomType

Description: FlatSharp generates custom Union types for unions defined in FBS files. Use of this attribute tells FlatSharp to not generate such a type and rely on the built-in FlatBufferUnion<T> type instead. This is primarily intended to support legacy scenarios that predate union custom types.

Valid On: Unions

Value Required: False

Default Value: True

Valid Values: None. Presence of the attribute is all that matters.

Examples:

// Generated code will contain 'FlatBufferUnion<Dog, Cat, Fish>' instead of 'BasicPet'
union BasicPet (NoCustomType) { Dog, Cat, Fish }
Clone this wiki locally