This is what the ‘SerializeFieldAttribute’ is for. Just tested this one:
[System.Serializable]
public class Blargh
{
public string Name;
public int Value;
[SerializeField]
private float _otherValue;
public float OtherValue
{
get { return _otherValue; }
set { _otherValue = value; }
}
}
Worked just fine.
[qutoe]Unity’s implementation enforces a fixed structure. Every variable in a class that matches a field in the JSON file is filled in with that field’s value, and every other field is discarded. The loose structure of JSON can be a benefit in some cases.[/quote]
I’m not understanding what you’re getting at… what else could you do with values in JSON that don’t have a corresponding field in the object type it’s deserialized to? There’s no field/variable to put it in, it has to be discarded.
Are you asking for a dynamic type. Like it deserializes into a named dictionary so you can access fields by name?
There are 3rd party libraries that support such things. Like Json.Net:
Json.NET - Newtonsoft
Of which some people have forked to get better support in unity (since unity is on an older version of mono/.net still):
https://www.google.com/search?q=json.net+unity&oq=json.net+unity&aqs=chrome.0.0l6.2240j0j4&sourceid=chrome&ie=UTF-8
But I’m not sure if this is what you need… you’re description here is vague to me.
Also, you can get similar functionality out of the unity serializer for dynamic properties. Just like how people serialize dictionaries, you can serialize a name/value pairing into 2 arrays with the ISerializationCallbackReceiver.
…
I don’t know. My main beef with the unity serializer is that it doesn’t support polymorphic serialization. If you have a field/var of a base type, and an object of some child type in it, it’ll be serialized/deserialized as the base type.
This is my only true gripe with the thing.
(this fact is also why you can’t use interfaces as a field type and let it be serialized… grrrrr)
Oh and my other gripe is that their ISerializationCallbackReceiver forces you to bloat your class/struct with fields for the data to be serialized. A system like .Net’s SerializationInfo would be so much nicer.