Click or drag to resize

IPersistentField Interface

Interface for the simple types that need custom (de)serialization logic.

Namespace:  KSPDev.ConfigUtils
Assembly:  KSPDev_Utils.2.5 (in KSPDev_Utils.2.5.dll) Version: 2.5 for KSP v1
Syntax
C#
public interface IPersistentField
Request Example View Source

The IPersistentField type exposes the following members.

Methods
  NameDescription
Public methodParseFromString
Restores the object's state from a plain string.
Public methodSerializeToString
Returns the object's state as a plain string.
Top
Remarks
It's similar to IConfigNode interface in the compound types but with the following differences:
  • The value is (de)serialized from/to a simple string.
  • If the field is initialized to an instance of the type, then this instance will be used to deserialize the value. If the field is not initialized but there is a value in the config file, then a new instance will be created. For this reason the type must implement a default constructor.

Note that the types that implement this interface will never be treated as compound. I.e. ConfigAccessor will not try to persist the members of such types even though there may be fields attributed with PersistentFieldAttribute.

Examples
Here is how a simple vector serialization may look like:
public class MyVector : IPeristentField {
  float x;
  float y;

  /// <inheritdoc/>
  public string SerializeToString() {
    return string.Format("{0},{1}", x ,y);
  }
  /// <inheritdoc/>
  public void ParseFromString(string value) {
    var elements = value.Split(',');
    x = float.Parse(elements[0]);
    y = float.Parse(elements[1]);
  }
}

This example doesn't do any checking when parsing the string, but in general it's a good idea to do a sanity check of the string. It's OK to throw an exception from the parsing method when the data is invalid.

See Also