I can not use JsonConverter on Json.Net with Unity

0
So I'm developing a visual programming system in FSM , and for serialization, I used Json.Net , so far, okay, it worked correctly, however, I needed to serialize UnityObjects and things got complicated, I wanted serialize only the reference in the editor, but it tried to serialize the object. So I created a JsonConverter and added it to JsonSettings , as it should be, but when serializing, everything worked fine, and it serialized, but to deserializar , it does not even call the method, I believe it's due to being serializing a 'int' , but I have already tried in every way thinkable, and I could not, I hope someone can give me a light on this.

Code:

public class UObjectConverter : JsonConverter
{
    public override bool CanConvert (Type objectType)
    {
        var uobj = typeof(UnityEngine.Object);
        return objectType == uobj || uobj.IsAssignableFrom(objectType);
    }
    public override void WriteJson (JsonWriter writer, object value,    JsonSerializer serializer)
    {
        var uobj = value as UnityEngine.Object;
        int id;
        if (uobj == null)
            id = 0;
        else
            id = uobj.GetInstanceID ();
        var type = typeof(Object);
        if (uobj != null)
            type = uobj.GetType ();
        writer.WriteValue(id);
    }
    public override object ReadJson (JsonReader reader, Type objectType,
                                     object existingValue, JsonSerializer serializer)
    {
        int id = (int)((long)reader.Value);
        if (id == 0)
            return null;
        else
            return MainClass.GetObject(id);
    }
}

Well, here is an example code, let me first explain, within the 'AIScriptData' class there is the list with the 'States', in these 'States' there is basically a list with 'Actions' each 'Action' is basically a type with a 'Execute ()' virtual method, and derivatives can use their own fields and etc, when you want to reference a variable or value, use the 'Value' class, which can reference a 'Variable', or have an object of its own value. The 'Variable' has an id ('string') and a value ('object'), plus a type ('Type'). The other list in 'AIScriptData' is about 'Variable', basically that's it. The type 'DarkJson' is just a type I created using the JsonConvert class.

Code:

public AIScriptData script;
public string json;
public void Save()
{
  // Até aqui tudo bem, ele serializa corretamente
  json = DarkJson.Serialize(script);
} 
public void Load()
{
  // Aqui ocorre o problema, ele simplesmente deserializa um inteiro
  // ao inves do transform, como deveria ser.
  script = DarkJson.Deserialize<AIScriptData>(json);
}
    
asked by anonymous 08.05.2018 / 04:00

0 answers