Home > Net >  Once again: JsonSerializationException: Unable to find a constructor to use for type "custom cl
Once again: JsonSerializationException: Unable to find a constructor to use for type "custom cl

Time:04-24

I know this quesiton has already come up a lot, but I was looking through the answers and could not really find one that fits me, therefore I am asking you for help:

I have a custom class [Loadout]:

public class Loadout                                                        // used to save a players loadout on the server
{
    public string name;
    public float loadoutID;
    public SO_Admiral admiral;
    public FM_ShipSave[] shipsInTheLoadout;
    public bool selectedLoadout;

    public Loadout(string _name, FM_ShipSave[] _ShipSavesArray)
    {
        name = _name;                                  // later this must come from an input-field
        loadoutID = Random.Range(0f, 100000000f);
        shipsInTheLoadout = _ShipSavesArray;
    }

    public Loadout(string _name)
    {
        name = _name;
        shipsInTheLoadout = new FM_ShipSave[0];
    }   
}

it also includes other custom classes like:

public class FM_ShipSave
{
    // this class saves ID and position in the array of the ship
   public int shipID;
   public int tileX;
   public int tileZ;


    public FM_ShipSave(UnitScript _unit)
    {
        shipID = _unit.SO_ofThis.getPositioninAllUnitsArray();
        tileX = _unit.getTileX;
        tileZ = _unit.getTileZ;

    }
}

that I serialize into a .Json-file to send it to a server and store it there. The serialization part works perfectly:

var request = new UpdateUserDataRequest                                                    // after what is supposed to happen with the current loadout is determined we upload it to the server
        {
            Data = new Dictionary<string, string>                                                   // create a new dicitonary
            {
                {"Loadouts", JsonConvert.SerializeObject(playerLoadouts)}                           // convert the List<Loadout> playerLoadouts into a .json file
            }
        };
        PlayFabClientAPI.UpdateUserData(request, onDataSend, one rror);                              // then send it to the server, to be stored

Link to picture of Json-editor

but as soon as I try to deserialize it:

List<Loadout> playerLoadouts = JsonConvert.DeserializeObject<List<Loadout>>(_result.Data["Loadouts"].Value);      // turn the jsonback into a List<Loadout>

I am getting the following error:

Blockquote JsonSerializationException: Unable to find a constructor to use for type Loadout. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path '[0].name', line 1, position 9.

I have also tried it with a different simpler class and there everything works fine:

public class JsonTest
{
    public string name;

        public JsonTest(string _name)
    {
        name = _name;
    }
}

Send:

// to test serialization
        JsonTest _test = new JsonTest("test");
        var request2 = new UpdateUserDataRequest                                                     // after what is supposed to happen with the current loadout is determined we upload it to the server
        {
            Data = new Dictionary<string, string>                                                   // create a new dicitonary
            {
                {"test", JsonConvert.SerializeObject(_test)}                                        // convert the List<Loadout> playerLoadouts into a .json file
            }
        };

Retrieve:

public void testDeserialization(GetUserDataResult _result)
    {
        JsonTest _retrievedTest = JsonConvert.DeserializeObject<JsonTest>(_result.Data["test"].Value);      // turn the jsonback into a List<Loadout>

        Debug.Log("retrieved test deserialization name: "   _retrievedTest.name);
    }

I therefore conclude that it has something to do with my custom classes, but I don't know how to fix it. Should I have to write a custom deserializer? Or is there maybe a different solution?

I was following this tutorial to create and upload json-files: Link to tutorial on youtube

I am coding for unity on a mac using visual studio.

Thank you for your help.

CodePudding user response:

The exception message is self explaining. You don't have a default constructor for the class Loadout. So in your Loadout class you need to add

 Loadout() {}
  • Related