# Custom Serialized Field Solution for Scriptable Objects in Asset Bundles
###### tags: `framework`
## The Problem
Unity **can not deserialize** correctly serialized fields of custom types in Scriptable Objects when we load them from asset bundles.
Let's say we have created these custom types.
```csharp
[System.Serializable]
public class ClassA
{
public ClassB[] m_ClassB;
}
[System.Serializable]
public class ClassB
{
public string m_SomeText;
public int m_SomeInt;
}
```
The below `m_ObjectA` will be empty when will be loaded from an asset bundle.
```csharp
public class ExampleAsset : ScriptableObject
{
[SerializeField] private ClassA m_ObjectA;
}
```
## The Solution
We developed a custom scriptable object type (`SerializableScriptableObject`) and a custom attribute in order to serialize a field of a custom type to a JSON string. Then Unity will serialize/deserialize the JSON string fine.
### Namespace
**CNDG.Framework**
### SerializableScriptableObject
class in CNDGFramewok.dll / Inherits UnityEngine.ScriptableObject
| Public Methods | |
| - | - |
| Serialise():void | Serialise all fields with JSONSerializeField attribute to a JSON string defined in JSONSerializeField attribute. |
| Deserialise():void | Gets all the fields with JSONSerializeField attribute and tries to deserialize the JSON string defined in their JSONSerializeField attributes. |
### Example
```csharp
public class ExampleAsset : SerializableScriptableObject
{
// create a SerializeField private string (HideInInspector if we like).
// we will use this field to save the json string and Unity will serialize/deserialize it fine.
[SerializeField, HideInInspector] private string m_ObjectAJson;
// in the field with the custom type we have to add the JSONSerializeField attribute
// and define the above string field which will use to save the serialized json string.
[JSONSerializeField(jsonStringName = "m_ObjectAJson"), SerializeField] private ClassA m_ObjectA;
}
```
### Restriction
This method works only for:
* Value types
* Arrays
* Lists
Will not work with reference types.
Also, keep in mind that we are using Unity's `JsonUtility` for serialization and deserialization. So our solution inherits any restriction `JsonUtility` has.