# Saving Serialization Data

In this post, we will explore about serializing object with internal save support.

In one of our old post How to serialize an object we looked into the API’s used to serialize and deserialize an object. These API’s were just focusing on serializing and deserializing input object. Here user was responsible for saving serialization data that was returned after serialization. And similarly, while deserializing user had to provide serialization data to retrieve back formerly serialized object.

Even though above API’s are quite useful esp. for sharing data on network. We feel that most users will still prefer to have an option to save data locally. So keeping that in mind, we are providing additional API’s for Serialize and Deserialize which internally handles saving data based on user preferred option.

RS provide two options to locally save serialization data. They are as follows:

## Save to PlayerPrefs

On calling API with save target as PlayerPrefs, RS will serialize input object and convert binary format serialization data into Base64 string. This string is then add to PlayerPrefs with Serialization ID as key.

Following code shows the serialization with save option as PlayerPrefs.

```
// Serializing object
RSManager.Serialize<TargetObjectType>(_targetObject,"serialization-id", eSaveTarget.PLAYER_PREFS);

// Deserializing object
TargetObjectType _object = RSManager.Deserialize<TargetObjectType>("serialization-id");
```

## Save to File

When serialization of an object with save target as File System is requested, RS serializes object to binary format serialization data and saves this data as text format file. This option is ideal for larger serialization data, as PlayerPrefs has size restriction in some platforms (for eg: PlayerPrefs on WebPlayer has a size limit of 1MB).

Following code demonstrates the serialization with save option as File System.

```
// Serializing object
RSManager.Serialize<TargetObjectType>(_targetObject,"serialization-id", eSaveTarget.FILE_SYSTEM);

// Deserializing object
TargetObjectType _object = RSManager.Deserialize<TargetObjectType>("serialization-id");
```

NOTE: *You can locate these files in Application.PersistentDataPath folder with filename same as SerializationID prefixed with “RS*”.\_

NOTE: *By default, RS writes all serialization data on Application Pause as well as on Application Quit. However this might cause data loss if game crashes. You can avoid it by forcing RS to write all data by calling Save function.*
