ISavable

Namespace:
BonaJson

Description

Implement this interface when you want custom behaviour on you serialization and deserialization of objects. When using JObject.Value, if the type of T is a class implementing ISavable it will be allowed. When serializing ISavable.Save() is called and when deserializing Load(JObject is used).

Public Methods

Name Type Description
Save () JObject Implement to for customs creation of a JObject.
Load (JObject source) Void Called when using JObject.Value to load the class.

Examples

Sample taken from a student dance game. Have been modified from the original to better show the interface implementation.
public class Song : ISavable
{
	public string DisplayName { get; set; } 	//What the player sees as the name of the level
	public string StepListName { get; set; }	//what the steplist is called
	public string ArtistName { get; set; }      //what the artist is called
	public string SongName { get; set; }        //what the song is called
	public string MusicName { get; set; }       //what the filename for the music is called
	public SongDifficulty Difficulty { get; set; }	// External enum for the difficulty
	public int BeatsPerSecond { get; set; }
	public int SongLength { get; set; }
	
	public JObject Save()
	{
		var result = new JObjectCollection();
		result.Add("DisplayName", DisplayName);
		result.Add("StepListName", StepListName);
		result.Add("ArtistName", ArtistName);
		result.Add("SongName", SongName);
		result.Add("MusicName", MusicName);
		result.Add("Difficulty", (int)Difficulty);
		result.Add("BeatsPerSecond", BeatsPerSecond);
		result.Add("SongLength", SongLength);

		return result;
	}
	
	public void Load(Jobject source)
	{
		this.DisplayName = jObject["DisplayName"].Value <String>();
		this.StepListName = jObject["StepListName"].Value<String>();
		this.ArtistName = jObject["ArtistName"].Value<String>();
		this.SongName = jObject["SongName"].Value<String>();
		this.MusicName = jObject["MusicName"].Value<String>();
		this.Difficulty = jObject["Difficulty"].Value<SongDifficulty>();
		this.BeatsPerSecond = jObject["BeatsPerSecond"].Value<int>();
		this.SongLength = jObject["SongLength"].Value<int>();
	}
}