Bona JSON

.NET library for Json serilalization and deserialization built for the Unity game engine. Is built to be RFC-7159 compliant (https://tools.ietf.org/html/rfc7159).

The project is released under the MIT software license (https://opensource.org/licenses/MIT). The source is available, no matter how you acquire the package and you are free to change it as you see fit.

This project was initiated before Unity had announced its JsonUtility class which enabled JSON for Unity's internal serialization process. However, as that serialization process is still quite strict, in situations where more control over is needed BonaJson could still come in handy. Classes can be serialized and deserialized in two different ways. Either the ISavable interface is used, that allows application specific implementation of how and what to save. The other way is automatic by using reflection. This works good for custom data classes but poorly for classes derived from MonoBehavior (or other Unity base classes) as the entire visible part of the class will be serialized.

Though the code is written to used together with Unity, there is nothing Unity specific about it, apart from the project structure, and can therefore be used in any .NET project.

The source is available at github.
Github.com/Bonahona/BonaJson

Classes

Namespace BonaJson

Name Description
ISavable Interface for custom serialization/deserialization.
JBoolObject JObject derivate class for boolean data.
JIntObject JObject derivate class for integer data.
JNullObject JObject derivate class for a null value.
JObject Base class for any JSON value. Contains static methods for serialization and deserialization.
JObjectArray JObject derivate class to contains a JSON array.
JObjectCollection JObject derivate class to contains a JSON object.
JStringObject JObject derivate class to contain a string.
JValueObject JObject derivate class to contain an int or a float.

Namespace BonaJson.Internal

Name Description
Deserializer Used to Deserialize a JObject into a class instance. Used by JObject.Deserialize
JObjectValue JObject derivate class to contain any JSON data except arrays and objects.
JsonFormatException Thrown by the JSON parser when faults are found in the JSON string.
Parser Internal JSON parser. Turns a string into JObject tree.
PrettyPrintData Used when using JObject.PrettyPrint().
Reader Underlying reader to the parser, to make it single-pass.
Serializer Used to Serialize a class instance to a JObject tree. Used by JObject.Serialize.

Namespace BonaJson.Example

Name Description
ISaveableExample Example in the package for implementation of ISavable interface.

Examples

Usage of Isavable Interface
// Example of a Monobehaviour class that can be serialized and deserialized, through the use of the method T Value<T>.
public class ISaveableExample : MonoBehaviour, ISavable
{
	// Custom fields we want to serialize and later deserialize in this object
	public string CustomName;
	public int CustomInt;

	JObject ISavable.Save()
	{
		var result = new JObjectCollection();
		result.Add("CustomName", CustomName);
		result.Add("CustomInt", CustomInt);

		return result;
	}

	public void Load(JObject source)
	{

		this.CustomName = source["CustomName"].Value<String>();
		this.CustomInt= source["CustomInt"].Value<int>();
	}
}            
Usage of reflection. Note that nothing has been done to the class.
// Class will serve as the structure to be serialized and reserialized
 public class ReflectionData
{
    public String CustomName;
    public int CustomInt;
}

public class Example
{
    public String Serialize()
    {
        var dataToSerialize = new ReflectionData();
        var serializeData = JObject.Serialize(dataToSerialize);
        
        return serializeData;
    }

    public ReflectionData Deserialize(JObject data)
    {
        var deserializedData = data.Deserialize<ReflectionData>();
        
        return deserializeData;
    }
}            

Documents

Release notes

1.0 Released

See also