- Home /
Serializing Properties (JSON saving and loading system)
Hello, I have a few scripts: https://pastebin.com/5fVjYpWN and I'm trying to save and load some things (floats, ints, and something Called BigDouble which is made up of 2 properties, mantissa and exponent in the BreakInfinity namespace used in the scripts). Everything saves and loads except for BigDoubles since it's not serializable, I tried adding the attribute in the namespace but that wouldn't work either. Any ways to do this? Thanks.
Answer by Bunny83 · Jun 26, 2019 at 09:42 PM
Well you have several possibilities. Since you use Unity's JsonUtility you can do one of the following:
Edit your copy of the BigDouble struct and make it Serializable and replace the two auto properties with actual fields. That way it should serialize just fine.
Since editing a third-party library / class is not always a good idea you can also implement a wrapper struct that implements the ISerializationCallbackReceiver interface and use the callbacks to copy the values over to actual fields.
Another way would be to use a different serializer (not Unity's JsonUtility) however since you probably have most of it already setup it would make more sense to stick to the JsonUtility unless you stumble into more issues..
The wrapper could look like this:
[System.Serializable]
public struct BigDoubleSerializable : ISerializationCallbackReceiver
{
public double mantissa;
public long exponent;
public BigDouble value;
public void OnBeforeSerialize()
{
mantissa = value.Mantissa;
exponent = value.Exponent;
}
public void OnAfterDeserialize()
{
value.Mantissa = mantissa;
value.Exponent = exponent;
}
}
In your PlayerData2 class you would replace all the BigDouble variables with BigDoubleSerializable. Just assign the actual BigDouble value to the "value" field.
I never thought converting the properties: public double $$anonymous$$antissa {get;} public long Exponent{get;} to public double mantissa; public long exponent; would be that easy! It works perfectly now!
BreakInfinity.cs: https://pastebin.com/F$$anonymous$$2W3VnZ
Answer by Morhem · Aug 26, 2020 at 11:26 PM
Sorry for necroposting, but I was just solving the very same problem, and initially used answer from here, but later discovered, that you can just force unity to serialize private fields with [SerializeField], without having to have a wrapper.
No, you can not. What you have missed is that the original BigDouble struct was not marked as Serializable and its fields were private (auto properties) which are also not serialized. That's why I suggested to edit the BigDouble struct. However as you can see here someone filed an issue at the same day and the developer added the Serializable attribute and changed the fields to serialized fields for newer versions of Unity. So the current version of the BigDouble struct is already serializable and you don't have to do anything.
Of course if your own variables are private you have to use SerializeField just the same way you have to do with any private variable, or make it public. Especially when it comes to saving to json using Unity's JsonUtility you usually want to make the fields of your data class public.
Your answer
Follow this Question
Related Questions
How do i change my save "Total Coin" everytime i get more coins. 0 Answers
Bool array to binaryformatter method 1 Answer
Serializing System.object or System.Type doesnt work. Data is lost. 1 Answer
Xml Serialization of "sub classes" 0 Answers
JSON Error : ArgumentException: JSON must represent an object type. 1 Answer