I cannot use Jsonconverter in Json.Net with Unity

Asked

Viewed 77 times

0

So I’m developing a visual programming system in FSM, and for serialization, I used Json.Net, until then, all right, it worked correctly, however, I had to serialize UnityObjects And things got complicated, I wanted to serialize just the reference in the editor, but he tried to serialize the object. So I created a JsonConverter and added to the JsonSettings, as it should be, however, when serializing, everything worked well, and he serialized, but to deserializar, he doesn’t even call the method, I believe it’s due to being Serializing a 'int', but I’ve tried every conceivable way, and I can’t, I hope someone can shed some 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 of code, before let me explain, within the class 'Aiscriptdata' there is the list with the 'States', in these 'States', there is basically a list with 'Actions' and the name, each 'Action' is basically a type with a virtual method 'Execute()', and derivatives can use their own Fields and etc, when you want to reference a variable or value, you use the class 'Value', which can refer to a 'Variable', or have an 'Object' of own value. 'Variable' has an id('string') and a value('Object'), in addition to a type('Type'). The other list in Aiscriptdata is about 'Variable', basically that’s it. The type 'Darkjson' is just a type I created that uses 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);
}
  • Add an example with a Json and the call from ReadJson() that you are running.

  • Added Example Code.

1 answer

0

Unity has its own Json class which is Jsonutility, I recommend you take a look but briefly you need to know 4 things:

  • The output/input object needs to give a [Serializable] or have Unity Serializable attributes, and if you create as [Serializable] you cannot have the Monobehaviour class as hierarchy.
  • Use Jsonutility.Tojson(obj) to pick up and serialize the entire object in a Json string.
  • Use Jsonutility.Fromjsonoverwrite(obj) to load json and override any information that comes.
  • Use Jsonutility.Fromjson(json) to initialize your obj

I hope this helps

Browser other questions tagged

You are not signed in. Login or sign up in order to post.