Photon Quantum 3.0.0

Classes | Public Member Functions | Protected Member Functions | Properties | List of all members
Quantum.QuantumUnityJsonSerializer Class Reference

Asset serializer implementation based on UnityEngine.JsonUtility and a set of utility methods from JsonUtilityExtensions. Can use surrogates to replace asset types with more efficient representations (RegisterSurrogate<AssetType,SurrogateType>. Additionally, any Unity-object references are serialized as null by default (NullifyUnityObjectReferences). The output can be deserialized with Newtonsoft.Json-based deserializer from Quantum.Json assembly. If this interoperability is needed, consider enabling IntegerEnquotingMinDigits to ensure that large integers are enquoted and not treated as floating points. More...

Inheritance diagram for Quantum.QuantumUnityJsonSerializer:
Quantum.IAssetSerializer

Classes

class  AssetObjectSurrogate
 Base class for asset object surrogates. More...
 
class  BinaryDataSurrogate
 Quantum.BinaryData surrogate. Compresses the data if it is larger than the threshold and replaces the data with a base64 encoded string. More...
 
class  EntityViewSurrogate
 Quantum.EntityView surrogate. Does not serialize the prefab reference, but only the identifier. QuantumUnityJsonSerializer.ResolvePrefab is used to resolve the prefab during deserialization. More...
 

Public Member Functions

 QuantumUnityJsonSerializer ()
 Creates a new instance of QuantumUnityJsonSerializer. More...
 
AssetObject[] DeserializeAssets (Stream stream)
 Deserializes an array of asset objects from the specified stream.
 
IRuntimeConfig DeserializeConfig (Stream stream)
 Deserializes the runtime configuration from the specified stream.
 
IRuntimePlayer DeserializePlayer (Stream stream)
 Deserializes the runtime player from the specified stream.
 
IGameResult DeserializeResult (Stream stream)
 Deserializes the game result from the specified stream.
 
string PrintObject (object obj)
 Prints the string representation of an object.
 
void RegisterSurrogate< AssetType, SurrogateType > (Func< AssetType, SurrogateType > factory)
 Registers a surrogate type for the provided asset type. Surrogates are types that are serialized and deserialized instead of the original asset type. By default, the serializer only provides surrogates for EntityView and BinaryData, for a more efficient serialization. More...
 
void SerializeAssets (Stream stream, AssetObject[] assets)
 Serializes an array of asset objects to the specified stream.
 
void SerializeConfig (Stream stream, IRuntimeConfig config)
 Serializes the runtime configuration to the specified stream.
 
void SerializePlayer (Stream stream, IRuntimePlayer player)
 Serializes the runtime player to the specified stream.
 
void SerializeResult (Stream stream, IGameResult config)
 Serializes the game result to the specified stream.
 

Protected Member Functions

virtual TextReader CreateReader (Stream stream)
 Creates a new instance of StreamReader with UTF8 encoding. The underlying stream is not closed. More...
 
virtual TextWriter CreateWriter (Stream stream)
 Creates a new instance of StreamWriter with UTF8 encoding. The underlying stream is not closed. More...
 
virtual GameObject ResolvePrefab (AssetGuid guid)
 Resolves the prefab associated with the provided AssetGuid by looking it up in the global DB. More...
 

Properties

int? CompressBinaryDataOnSerializationThreshold = 1024 [get, set]
 If set to a positive value, all uncompressed BinaryData assets with size over the value will be compressed during serialization. More...
 
bool DecompressBinaryDataOnDeserialization = false [get, set]
 If true, all BinaryData assets will be decompressed during deserialization. More...
 
Func< AssetGuid, GameObject > EntityViewPrefabResolver [get, set]
 Custom resolver for EntityView prefabs. More...
 
bool EntityViewPrefabResolvingEnabled [get, set]
 No longer used. More...
 
int? IntegerEnquotingMinDigits [get, set]
 How many digits should a number have to be enquoted. Some JSON parsers deserialize all numbers as floating points, which in case of large integers (e.g. entity ids) can lead to precision loss. If this property is set to true (default), all integers with IntegerEnquotingMinDigits or more digits are enquoted. More...
 
bool NullifyUnityObjectReferences = true [get, set]
 Should all UnityEngine.Object references be nullified in the resulting JSON? If true, all UnityEngine.Object references will be serialized as null. Otherwise, they are serialized as { "instanceId": <value> }. More...
 
bool PrettyPrintEnabled [get, set]
 No longer used. More...
 
Func< string, Type, Type > TypeResolver [get, set]
 Custom type resolver to be used during deserialization. More...
 

Detailed Description

Asset serializer implementation based on UnityEngine.JsonUtility and a set of utility methods from JsonUtilityExtensions. Can use surrogates to replace asset types with more efficient representations (RegisterSurrogate<AssetType,SurrogateType>. Additionally, any Unity-object references are serialized as null by default (NullifyUnityObjectReferences).

The output can be deserialized with Newtonsoft.Json-based deserializer from Quantum.Json assembly. If this interoperability is needed, consider enabling IntegerEnquotingMinDigits to ensure that large integers are enquoted and not treated as floating points.

Constructor & Destructor Documentation

◆ QuantumUnityJsonSerializer()

Quantum.QuantumUnityJsonSerializer.QuantumUnityJsonSerializer ( )
inline

Creates a new instance of QuantumUnityJsonSerializer.

Member Function Documentation

◆ RegisterSurrogate< AssetType, SurrogateType >()

void Quantum.QuantumUnityJsonSerializer.RegisterSurrogate< AssetType, SurrogateType > ( Func< AssetType, SurrogateType >  factory)
inline

Registers a surrogate type for the provided asset type. Surrogates are types that are serialized and deserialized instead of the original asset type. By default, the serializer only provides surrogates for EntityView and BinaryData, for a more efficient serialization.

Parameters
factoryDelegate to be used when an instance of a surrogate is needed.
Type Constraints
AssetType :AssetObject 
SurrogateType :AssetObjectSurrogate 

◆ ResolvePrefab()

virtual GameObject Quantum.QuantumUnityJsonSerializer.ResolvePrefab ( AssetGuid  guid)
inlineprotectedvirtual

Resolves the prefab associated with the provided AssetGuid by looking it up in the global DB.

Parameters
guidThe AssetGuid of the prefab to be resolved.
Returns
Returns the GameObject associated with the provided AssetGuid.
Exceptions
InvalidOperationExceptionThrown when the prefab associated with the provided AssetGuid cannot be found.

◆ CreateReader()

virtual TextReader Quantum.QuantumUnityJsonSerializer.CreateReader ( Stream  stream)
protectedvirtual

Creates a new instance of StreamReader with UTF8 encoding. The underlying stream is not closed.

Parameters
stream
Returns

◆ CreateWriter()

virtual TextWriter Quantum.QuantumUnityJsonSerializer.CreateWriter ( Stream  stream)
protectedvirtual

Creates a new instance of StreamWriter with UTF8 encoding. The underlying stream is not closed.

Parameters
stream
Returns

Property Documentation

◆ PrettyPrintEnabled

bool Quantum.QuantumUnityJsonSerializer.PrettyPrintEnabled
getset

No longer used.

◆ EntityViewPrefabResolvingEnabled

bool Quantum.QuantumUnityJsonSerializer.EntityViewPrefabResolvingEnabled
getset

No longer used.

◆ DecompressBinaryDataOnDeserialization

bool Quantum.QuantumUnityJsonSerializer.DecompressBinaryDataOnDeserialization = false
getset

If true, all BinaryData assets will be decompressed during deserialization.

◆ CompressBinaryDataOnSerializationThreshold

int? Quantum.QuantumUnityJsonSerializer.CompressBinaryDataOnSerializationThreshold = 1024
getset

If set to a positive value, all uncompressed BinaryData assets with size over the value will be compressed during serialization.

◆ IntegerEnquotingMinDigits

int? Quantum.QuantumUnityJsonSerializer.IntegerEnquotingMinDigits
getset

How many digits should a number have to be enquoted. Some JSON parsers deserialize all numbers as floating points, which in case of large integers (e.g. entity ids) can lead to precision loss. If this property is set to true (default), all integers with IntegerEnquotingMinDigits or more digits are enquoted.

◆ NullifyUnityObjectReferences

bool Quantum.QuantumUnityJsonSerializer.NullifyUnityObjectReferences = true
getset

Should all UnityEngine.Object references be nullified in the resulting JSON? If true, all UnityEngine.Object references will be serialized as null. Otherwise, they are serialized as { "instanceId": <value> }.

True by default.

◆ EntityViewPrefabResolver

Func<AssetGuid, GameObject> Quantum.QuantumUnityJsonSerializer.EntityViewPrefabResolver
getset

Custom resolver for EntityView prefabs.

EntityViews are serialized without prefab references (as they are not JSON serializable). Resolving takes place during deserialization, by looking up the prefab in the global DB.

◆ TypeResolver

Func<string, Type, Type> Quantum.QuantumUnityJsonSerializer.TypeResolver
getset

Custom type resolver to be used during deserialization.

If not set, Type.GetType(string, bool) will be used and an exception will be thrown on missing type. If set and returns null, the type object will be skipped and returned as null.