Class JsonSerialization
Helper class that generically writes any property container as a JSON string.
@NOTE This class makes heavy use of StringBuilder
and .ToString
on primitives, which allocates large amounts of memory. Use it sparingly.
@TODO
- Optimization
Namespace: Unity.Serialization.Json
Syntax
public static class JsonSerialization
Remarks
The deserialization methods will not construct type instances. All object fields must be initialized in the default constructor.
Methods
DeserializeFromPath<TContainer>(String)
Deserializes the given file path and returns a new instance of the container.
Declaration
public static TContainer DeserializeFromPath<TContainer>(string path)
where TContainer : new()
Parameters
Type | Name | Description |
---|---|---|
String | path | The file path to read from. |
Returns
Type | Description |
---|---|
TContainer | A new instance of the container with based on the serialized data. |
Type Parameters
Name | Description |
---|---|
TContainer | The type to deserialize. |
DeserializeFromPath<TContainer>(String, ref TContainer)
Deserializes the given file path and writes the data to the given container.
Declaration
public static void DeserializeFromPath<TContainer>(string path, ref TContainer container)
Parameters
Type | Name | Description |
---|---|---|
String | path | The file path to read from. |
TContainer | container | The container to deserialize data in to. |
Type Parameters
Name | Description |
---|---|
TContainer | The type to deserialize. |
DeserializeFromStream<TContainer>(Stream)
Deserializes the given stream and returns a new instance of the container.
Declaration
public static TContainer DeserializeFromStream<TContainer>(Stream stream)
where TContainer : new()
Parameters
Type | Name | Description |
---|---|---|
Stream | stream | The stream to read from. |
Returns
Type | Description |
---|---|
TContainer | A new instance of the container with based on the serialized data. |
Type Parameters
Name | Description |
---|---|
TContainer | The type to deserialize. |
DeserializeFromStream<TContainer>(Stream, ref TContainer)
Deserializes the given stream and writes the data to the given container.
Declaration
public static void DeserializeFromStream<TContainer>(Stream stream, ref TContainer container)
Parameters
Type | Name | Description |
---|---|---|
Stream | stream | The stream to read from. |
TContainer | container | The container to deserialize data in to. |
Type Parameters
Name | Description |
---|---|
TContainer | The type to deserialize. |
DeserializeFromString<TContainer>(String)
Deserializes the given json string and returns a new instance of the container.
Declaration
public static TContainer DeserializeFromString<TContainer>(string jsonString)
where TContainer : new()
Parameters
Type | Name | Description |
---|---|---|
String | jsonString | The json data as a string. |
Returns
Type | Description |
---|---|
TContainer | A new instance of the container with based on the serialized data. |
Type Parameters
Name | Description |
---|---|
TContainer | The type to deserialize. |
DeserializeFromString<TContainer>(String, ref TContainer)
Deserializes the given json string and writes the data to the given container.
Declaration
public static void DeserializeFromString<TContainer>(string jsonString, ref TContainer container)
Parameters
Type | Name | Description |
---|---|---|
String | jsonString | The json data as a string. |
TContainer | container | The container to deserialize data in to. |
Type Parameters
Name | Description |
---|---|
TContainer | The type to deserialize. |
Serialize<TContainer>(TContainer, JsonVisitor)
Writes a property container to a json string.
Declaration
public static string Serialize<TContainer>(TContainer container, JsonVisitor visitor = null)
Parameters
Type | Name | Description |
---|---|---|
TContainer | container | The container to write. |
JsonVisitor | visitor | The visitor to use. If none is provided, the default one is used. |
Returns
Type | Description |
---|---|
String | A json string. |
Type Parameters
Name | Description |
---|---|
TContainer | The type to serialize. |
Serialize<TContainer>(String, TContainer)
Writes a property container to a file path.
Declaration
public static void Serialize<TContainer>(string path, TContainer target)
Parameters
Type | Name | Description |
---|---|---|
String | path | The file path to write to. |
TContainer | target | The struct or class to serialize. |
Type Parameters
Name | Description |
---|---|
TContainer | The type to serialize. |