FullSerializer
This class allows arbitrary code to easily register global converters. To add a converter,
simply declare a new field called "Register_*" that stores the type of converter you would
like to add. Alternatively, you can do the same with a method called "Register_*"; just add
the converter type to the `Converters` list.
Supports serialization for DateTime, DateTimeOffset, and TimeSpan.
Serializes and deserializes enums by their current name.
Returns true if the given value is contained within the specified array.
This allows you to forward serialization of an object to one of its members. For example,
[fsForward("Values")]
struct Wrapper {
public int[] Values;
}
Then `Wrapper` will be serialized into a JSON array of integers. It will be as if `Wrapper`
doesn't exist.
The name of the member we should serialize as.
Forward object serialization to an instance member. See class comment.
The name of the member that we should serialize this object as.
Serializes and deserializes guids.
Provides serialization support for anything which extends from `IEnumerable` and has an `Add` method.
Fetches the element type for objects inside of the collection.
The reflected converter will properly serialize nullable types. However, we do it here
instead as we can emit less serialization data.
Serializes and deserializes WeakReferences.
Simple option type. This is akin to nullable types.
This wraps reflection types so that it is portable across different Unity runtimes.
Returns true if the given attribute is defined on the given element.
Returns true if the given attribute is defined on the given element.
Returns true if the given attribute is defined on the given element.
Returns true if the given attribute is defined on the given element.
Fetches the given attribute from the given MemberInfo. This method applies caching
and is allocation free (after caching has been performed).
The MemberInfo the get the attribute from.
The type of attribute to fetch.
The attribute or null.
Fetches the given attribute from the given MemberInfo.
The type of attribute to fetch.
The MemberInfo to get the attribute from.
Should this computation be cached? If this is the only time it will ever be done, don't bother caching.
The attribute or null.
Returns a pretty name for the type in the style of one that you'd see in C# without the namespace.
Returns a pretty name for the type in the style of one that you'd see in C#.
Should the name include namespaces?
Verifies that the given type has constructors to migrate from all ancestor types.
Verifies that the given version graph contains only unique versions.
The direct ancestors that this type can import.
The identifying string that is unique among all ancestors.
The modeling type that this versioned type maps back to.
Migrate from an instance of an ancestor.
A property or field on a MetaType. This unifies the FieldInfo and PropertyInfo classes.
Internal handle to the reflected member.
The type of value that is stored inside of the property. For example, for an int field,
StorageType will be typeof(int).
A custom fsBaseConverter instance to use for this field/property, if requested. This will be
null if the default converter selection algorithm should be used. This is specified using the
[fsObject] annotation with the Converter field.
Can this property be read?
Can this property be written to?
The serialized name of the property, as it should appear in JSON.
The name of the actual member.
Is this member public?
Is this type readonly? We can modify readonly properties using reflection, but not
using generated C#.
Writes a value to the property that this MetaProperty represents, using given object
instance as the context.
Reads a value from the property that this MetaProperty represents, using the given
object instance as the context.
MetaType contains metadata about a type. This is used by the reflection serializer.
Clears out the cached type results. Useful if some prior assumptions become invalid, ie, the default member
serialization mode.
Returns if the given property should be serialized.
Should a property without any annotations be serialized?
Attempt to emit an AOT compiled direct converter for this type.
True if AOT data was emitted, false otherwise.
Returns true if the type represented by this metadata contains a default constructor.
Creates a new instance of the type that this metadata points back to. If this type has a
default constructor, then Activator.CreateInstance will be used to construct the type
(or Array.CreateInstance if it an array). Otherwise, an uninitialized object created via
FormatterServices.GetSafeUninitializedObject is used to construct the instance.
Searches for a particular implementation of the given interface type inside of the type.
This is particularly useful if the interface type is an open type, ie, typeof(IFace{}),
because this method will then return IFace{} but with appropriate type parameters
inserted.
The base type to search for interface
The interface type to search for. Can be an open generic
type.
The actual interface type that the type contains, or null if there is no
implementation of the given interfaceType on type.
Caches type name to type lookups. Type lookups occur in all loaded assemblies.
Cache from fully qualified type name to type instances.
Assemblies indexed by their name.
A list of assemblies, by index.
Does a direct lookup for the given type, ie, goes directly to the assembly identified by
assembly name and finds it there.
The assembly to find the type in.
The name of the type.
The found type.
True if the type was found, false otherwise.
Tries to do an indirect type lookup by scanning through every loaded assembly until the
type is found in one of them.
The name of the type.
The found type.
True if the type was found, false otherwise.
Removes any cached type lookup results.
Find a type with the given name. An exception is thrown if no type with the given name
can be found. This method searches all currently loaded assemblies for the given type. If the type cannot
be found, then null will be returned.
The fully qualified name of the type.
Find a type with the given name. An exception is thrown if no type with the given name
can be found. This method searches all currently loaded assemblies for the given type. If the type cannot
be found, then null will be returned.
The fully qualified name of the type.
A hint for the assembly to start the search with. Use null if unknown.
The AOT compilation manager
Ahead of time compilations that are available. The type maps to the object type the generated converter
will serialize/deserialize, and the string is the text content for a converter that will do the serialization.
The generated serializer is completely independent and you don't need to do anything. Simply add the file to
your project and it'll get used instead of the reflection based one.
This is a helper method that makes it simple to run an AOT compilation on the given type.
The configuration to use when running AOT compilation.
The type to perform the AOT compilation on.
The AOT class. Add this C# code to your project.
True if AOT compilation was successful.
Adds a new AOT compilation unit.
The type of object we are AOT compiling.
The members on the object which will be serialized/deserialized.
AOT compiles the object (in C#).
The serialization converter allows for customization of the serialization process.
You do not want to derive from this class - there is no way to actually use it within
the serializer.. Instead, derive from either fsConverter or fsDirectConverter
The serializer that was owns this converter.
Construct an object instance that will be passed to TryDeserialize. This should **not**
deserialize the object.
The data the object was serialized with.
The field/property type that is storing the instance.
An object instance
If true, then the serializer will support cyclic references with the given converted
type.
The field/property type that is currently storing the object
that is being serialized.
If true, then the serializer will include inheritance data for the given converter.
The field/property type that is currently storing the object
that is being serialized.
Serialize the actual object into the given data storage.
The object instance to serialize. This will never be null.
The serialized state.
The field/property type that is storing this instance.
If serialization was successful.
Deserialize data into the object instance.
Serialization data to deserialize from.
The object instance to deserialize into.
The field/property type that is storing the instance.
True if serialization was successful, false otherwise.
Should deserialization be case sensitive? If this is false and the JSON has multiple members with the
same keys only separated by case, then this results in undefined behavior.
If exceptions are allowed internally, then additional date formats can be deserialized.
Note that the Full Serializer public API will *not* throw exceptions with this enabled;
errors will still be returned in a fsResult instance.
This string will be used to prefix fields used internally by FullSerializer.
Enables some top-level customization of Full Serializer.
The attributes that will force a field or property to be serialized.
The attributes that will force a field or property to *not* be serialized.
The default member serialization.
Convert a C# field/property name into the key used for the JSON object. For example, you could
force all JSON names to lowercase with:
fsConfig.GetJsonNameFromMemberName = (name, info) => name.ToLower();
This will only be used when the name is not explicitly specified with fsProperty.
If false, then *all* property serialization support will be disabled - even properties
explicitly annotated with fsProperty or any other opt-in annotation.
Setting this to false means that SerializeNonAutoProperties and
SerializeNonPublicSetProperties will be completely ignored.
Should the default serialization behaviour include non-auto properties?
Should the default serialization behaviour include properties with non-public setters?
If not null, this string format will be used for DateTime instead of the default one.
Int64 and UInt64 will be serialized and deserialized as string for compatibility
Enums are serialized using their names by default. Setting this to true will serialize them as integers instead.
fsContext stores global metadata that can be used to customize how fsConverters operate
during serialization.
All of the context objects.
Removes all context objects from the context.
Sets the context object for the given type with the given value.
Returns true if there is a context object for the given type.
Fetches the context object for the given type.
The serialization converter allows for customization of the serialization process.
Can this converter serialize and deserialize the given object type?
The given object type.
True if the converter can serialize it, false otherwise.
The actual type that a JsonData instance can store.
A union type that stores a serialized value. The stored type can be one of six different
types: null, boolean, double, Int64, string, Dictionary, or List.
The raw value that this serialized data stores. It can be one of six different types; a
boolean, a double, Int64, a string, a Dictionary, or a List.
Creates a fsData instance that holds null.
Creates a fsData instance that holds a boolean.
Creates a fsData instance that holds a double.
Creates a new fsData instance that holds an integer.
Creates a fsData instance that holds a string.
Creates a fsData instance that holds a dictionary of values.
Creates a fsData instance that holds a list of values.
Helper method to create a fsData instance that holds a dictionary.
Helper method to create a fsData instance that holds a list.
Helper method to create a fsData instance that holds a list with the initial capacity.
Transforms the internal fsData instance into a dictionary.
Returns a shallow clone of this data instance.
Returns true if this fsData instance maps back to null.
Returns true if this fsData instance maps back to a double.
Returns true if this fsData instance maps back to an Int64.
Returns true if this fsData instance maps back to a boolean.
Returns true if this fsData instance maps back to a string.
Returns true if this fsData instance maps back to a Dictionary.
Returns true if this fsData instance maps back to a List.
Casts this fsData to a double. Throws an exception if it is not a double.
Casts this fsData to an Int64. Throws an exception if it is not an Int64.
Casts this fsData to a boolean. Throws an exception if it is not a boolean.
Casts this fsData to a string. Throws an exception if it is not a string.
Casts this fsData to a Dictionary. Throws an exception if it is not a
Dictionary.
Casts this fsData to a List. Throws an exception if it is not a List.
Internal helper method to cast the underlying storage to the given type or throw a
pretty printed exception on failure.
Determines whether the specified object is equal to the current object.
Determines whether the specified object is equal to the current object.
Returns true iff a == b.
Returns true iff a != b.
Returns a hash code for this instance.
A hash code for this instance, suitable for use in hashing algorithms and data
structures like a hash table.
The direct converter is similar to a regular converter, except that it targets specifically only one type.
This means that it can be used without performance impact when discovering converters. It is strongly
recommended that you derive from fsDirectConverter{TModel}.
Due to the way that direct converters operate, inheritance is *not* supported. Direct converters
will only be used with the exact ModelType object.
Extend this interface on your type to receive notifications about serialization/deserialization events. If you don't
have access to the type itself, then you can write an fsObjectProcessor instead.
Called before serialization.
Called after serialization.
The field/property type that is storing the instance.
The data that was serialized.
Called before deserialization.
The field/property type that is storing the instance.
The data that will be used for deserialization.
Called after deserialization.
The field/property type that is storing the instance.
The type of the instance.
The given property or field annotated with [JsonIgnore] will not be serialized.
A simple recursive descent parser for JSON.
Skips input such that Character() will return a non-whitespace character
Parses numbers that follow the regular expression [-+](\d+|\d*\.\d*)
Parses a string
Parses an array
Parses the specified input. Returns a failure state if parsing failed.
The input to parse.
The parsed data. This is undefined if parsing fails.
The parsed input.
Helper method for Parse that does not allow the error information
to be recovered.
Inserts the given number of indents into the builder.
Escapes a string.
Formats this data into the given builder.
Writes the pretty JSON output data to the given stream.
The data to print.
Where to write the printed data.
Returns the data in a pretty printed JSON format.
Writes the compressed JSON output data to the given stream.
The data to print.
Where to write the printed data.
Returns the data in a relatively compressed JSON format.
Utility method that converts a double to a string.
Controls how the reflected converter handles member serialization.
Only members with [SerializeField] or [fsProperty] attributes are serialized.
Only members with [NotSerialized] or [fsIgnore] will not be serialized.
The default member serialization behavior is applied.
This attribute controls some serialization behavior for a type. See the comments
on each of the fields for more information.
The previous model that should be used if an old version of this
object is encountered. Using this attribute also requires that the
type have a public constructor that takes only one parameter, an object
instance of the given type. Use of this parameter *requires* that
the VersionString parameter is also set.
The version string to use for this model. This should be unique among all
prior versions of this model that is supported for importation. If PreviousModel
is set, then this attribute must also be set. A good valid example for this
is "v1", "v2", "v3", ...
This controls the behavior for member serialization.
The default behavior is fsMemberSerialization.Default.
Specify a custom converter to use for serialization. The converter type needs
to derive from fsBaseConverter. This defaults to null.
Specify a custom processor to use during serialization. The processor type needs
to derive from fsObjectProcessor and the call to CanProcess is not invoked. This
defaults to null.
Enables injecting code before/after an object has been serialized. This is most
useful if you want to run the default serialization process but apply a pre/post
processing step.
Multiple object processors can be active at the same time. When running they are
called in a "nested" fashion - if we have processor1 and process2 added to the
serializer in that order (p1 then p2), then the execution order will be
p1#Before p2#Before /serialization/ p2#After p1#After.
Is the processor interested in objects of the given type?
The given type.
True if the processor should be applied, false otherwise.
Called before serialization.
The field/property type that is storing the instance.
The type of the instance.
Called after serialization.
The field/property type that is storing the instance.
The type of the instance.
The data that was serialized.
Called before deserialization.
The field/property type that is storing the instance.
The data that will be used for deserialization.
Called before deserialization has begun but *after* the object instance has been created. This will get
invoked even if the user passed in an existing instance.
**IMPORTANT**: The actual instance that gets passed here is *not* guaranteed to be an a subtype of storageType, since
the value for instance is whatever the active converter returned for CreateInstance() - ie, some converters will return
dummy types in CreateInstance() if instance creation cannot be separated from deserialization (ie, KeyValuePair).
The field/property type that is storing the instance.
The created object instance. No deserialization has been applied to it.
The data that will be used for deserialization.
Called after deserialization.
The field/property type that is storing the instance.
The type of the instance.
Explicitly mark a property to be serialized. This can also be used to give the name that the
property should use during serialization.
The name of that the property will use in JSON serialization.
Use a custom converter for the given type. Specify the converter to use using typeof.
The result of some sort of operation. A result is either successful or not, but if it
is successful then there may be a set of warnings/messages associated with it. These
warnings describe the performed error recovery operations.
Is this result successful?
This is intentionally a `success` state so that when the object
is default constructed it defaults to a failure state.
The warning or error messages associated with the result. This may be null if
there are no messages.
Adds a new message to this result.
Adds only the messages from the other result into this result, ignoring
the success/failure status of the other result.
Merges the other result into this one. If the other result failed, then
this one too will have failed.
Note that you can use += instead of this method so that you don't bury
the actual method call that is generating the other fsResult.
A successful result.
Create a result that is successful but contains the given warning message.
Create a result that failed.
Only use this as +=!
Did this result fail? If so, you can see the reasons why in `RawMessages`.
Was the result a success? Note that even successful operations may have
warning messages (`RawMessages`) associated with them.
Does this result have any warnings? This says nothing about if it failed
or succeeded, just if it has warning messages associated with it.
A simply utility method that will assert that this result is successful. If it
is not, then an exception is thrown.
A simple utility method that will assert that this result is successful and that
there are no warning messages. This throws an exception if either of those
asserts are false.
Utility method to convert the result to an exception. This method is only defined
is `Failed` returns true.
Returns true if the given key is a special keyword that full serializer uses to
add additional metadata on top of the emitted JSON.
This is an object reference in part of a cyclic graph.
This is an object definition, as part of a cyclic graph.
This specifies the actual type of an object (the instance type was different from
the field type).
The version string for the serialized data.
If we have to add metadata but the original serialized state was not a dictionary,
then this will contain the original data.
Strips all deserialization metadata from the object, like $type and $content fields.
After making this call, you will *not* be able to deserialize the same object instance. The metadata is
strictly necessary for deserialization!
This function converts legacy serialization data into the new format, so that
the import process can be unified and ignore the old format.
Ensures that the data is a dictionary. If it is not, then it is wrapped inside of one.
This manages instance writing so that we do not write unnecessary $id fields. We
only need to write out an $id field when there is a corresponding $ref field. This is able
to write $id references lazily because the fsData instance is not actually written out to text
until we have entirely finished serializing it.
A cache from type to it's converter.
A cache from type to the set of processors that are interested in it.
Converters that can be used for type registration.
Direct converters (optimized _converters). We use these so we don't have to
perform a scan through every item in _converters and can instead just do an O(1)
lookup. This is potentially important to perf when there are a ton of direct
converters.
Processors that are available.
Reference manager for cycle detection.
A context object that fsConverters can use to customize how they operate.
Configuration options. Also see fsGlobalConfig.
Add a new processor to the serializer. Multiple processors can run at the same time in the
same order they were added in.
The processor to add.
Remove all processors which derive from TProcessor.
Fetches all of the processors for the given type.
Adds a new converter that can be used to customize how an object is serialized and
deserialized.
Fetches a converter that can serialize/deserialize the given type.
Helper method that simply forwards the call to TrySerialize(typeof(T), instance, out data);
Generic wrapper around TryDeserialize that simply forwards the call.
Serialize the given value.
The type of field/property that stores the object instance. This is
important particularly for inheritance, as a field storing an IInterface instance
should have type information included.
The actual object instance to serialize.
The serialized state of the object.
If serialization was successful.
Serialize the given value.
The type of field/property that stores the object instance. This is
important particularly for inheritance, as a field storing an IInterface instance
should have type information included.
An fsBaseConverter derived type that will be used to serialize
the object instead of the converter found via the normal discovery mechanisms.
The actual object instance to serialize.
The serialized state of the object.
If serialization was successful.
Attempts to deserialize a value from a serialized state.
Attempts to deserialize a value from a serialized state.