I'd like to save my custom instance as simple primitive value. I'm implementing a class which has a single int value below:
Code: Select all
public class BigInteger {
public float val;
}
public class SaveData {
public List<BigInteger> values;
}
But when an instance of this class is serialized to json, the serialized json file becomes bellow:
Code: Select all
"values" : [{"value" : 31131},{"value" : 9343}]
I'd like to save this simply:
I implemented custom ES3 Type which has ES3Type_double as a parent. And I modified ES3Writer(Line. 174) and ES3Reader(Line 233 and 250) as below:
Code: Select all
if (type.isPrimitive)
{
type.Write(value, this);
}
else
{
StartWriteObject(null);
WriteType(valueType);
type.Write(value, this);
EndWriteObject(null);
}
Code: Select all
protected virtual void ReadObject<T>(object obj, ES3Type type)
{
if (type.isPrimitive)
{
type.ReadInto<T>(this, obj);
return;
}
// Check for null.
if(StartReadObject())
return;
type.ReadInto<T>(this, obj);
EndReadObject();
}
Code: Select all
protected virtual T ReadObject<T>(ES3Type type)
{
if (type.isPrimitive)
{
object pobj = type.Read<T>(this);
return (T) pobj;
}
if(StartReadObject())
return default(T);
object obj = type.Read<T>(this);
EndReadObject();
return (T)obj;
}
My ES3UserType_BigInteger class is below:
Code: Select all
namespace ES3Types
{
[UnityEngine.Scripting.Preserve]
[ES3PropertiesAttribute("value")]
public class ES3UserType_BigInteger : ES3Type_double
{
public new static ES3Type Instance = null;
public ES3UserType_BigInteger() : base(typeof(BigInteger))
{
isPrimitive = true;
Instance = this;
priority = 1;
}
public override void Write(object obj, ES3Writer writer)
{
base.Write((obj as BigInteger).value, writer);
}
void ReadObject<T>(ES3Reader reader, object obj)
{
var instance = (BigInteger) obj;
instance.value = (double) base.Read<double>(reader);
}
public override object Read<T>(ES3Reader reader)
{
var instance = new BigInteger();
ReadObject<T>(reader, instance);
return instance;
}
}
public class ES3UserType_BigIntegerArray : ES3ArrayType
{
public static ES3Type Instance;
public ES3UserType_BigIntegerArray() : base(typeof(BigInteger[]), ES3UserType_BigInteger.Instance)
{
Instance = this;
}
}
}
This changes work successfully for me. I would be happy if you take in this changes. I'm sorry for my poor English.