Question by
BillDNA · Dec 06, 2020 at 08:46 PM ·
serializationfield
JsonUtility serialize custom getter setter field
So how do i get the id to be included in JSON when i call JsonUtility.ToJson(CharacterData) ?
[Serializable]
public struct CharacterData {
private string _id;
[SerializeField]
public string id {
get {
if(_id == null || _id == "") {
Guid g = Guid.NewGuid();
string GuidString = Convert.ToBase64String(g.ToByteArray());
GuidString = GuidString.Replace("=","");
GuidString = GuidString.Replace("+","");
_id = GuidString;
}
return _id;
}
set {
_id = value;
}
}
}
Comment
Answer by mgstauff · Dec 16, 2020 at 04:13 AM
AFAIK you don't. You need a public backing field that actually gets serialized and deserialized. So something like
public string _id;
public string id { get {return whatever-you-want} set { _id = whatever-you-want; } }
But I can't guarantee this is right. I only know I've been doing this for the past year or so and am pretty confident I researched it back then or otherwise wouldn't be doing it.
Your answer

Follow this Question
Related Questions
Understanding the need of serialization 1 Answer
Binary Serialization problem. 1 Answer
Why Does My Saved Data Not Load? 0 Answers
SerializationException Field not found 1 Answer
Deserialization during Unit Testing 0 Answers