-1

My Class

   [System.Serializable]
   public class StallData
   {
    public StallData(ulong stallID, stallSurfaceTextureData)
    {
      StallID = stallID;
      StallSurfaceTextureData = stallSurfaceTextureData;
    }
    
    [field: SerializeField]
    public ulong StallID{ get; private set; }

    [field: SerializeField]
    public StallSurfaceTextureData StallSurfaceTextureData { get; private set; }
}


[System.Serializable]
public class StallSurfaceTextureData
{
    public StallSurfaceTextureData( int floorTextureID, int leftTextureID, int right, int back)
    {
        Debug.LogFormat($" f {floorTextureID} l {leftTextureID} r {right} b {back}");

        BackWallTextureID = back;
        LeftWallTextureID = leftTextureID;
        RightWallTextureID = right;
        FloorTextureID = floorTextureID;
    }
    
    [field: SerializeField]
    public int BackWallTextureID { get; private set; }
    
    [field: SerializeField]
    public int LeftWallTextureID { get; private set; }
    
    [field: SerializeField]
    public int RightWallTextureID { get;private  set; }
    
    [field: SerializeField]
    public int FloorTextureID { get; private set; }
}

using newtonsoft-json when I am converting my JSON to this class I am getting back, left & right value 0 and for floor, I am getting the correct value.

I am unable to find the reason for it.

Unity version 2021.3.12f1

this is my JSON

{
  "StallID": 889448,
  "StallSurfaceTextureData": 
  {
    "BackWallWallTextureID": 0,
    "LeftWallWallTextureID": 1,
    "RightWallWallTextureID": 2,
    "FloorTextureID": 3
  }
}

I renamed the parameter name, and one time it worked then again I am getting the same issue.

Edit: I have changed the parameter name in JSon but still the issue persists. The only way it is working is if I am keeping the parameter name and the variable name inside the class is same. I am not getting how the Parameter name is affecting the value assignment in the class? for ex: If I keep my class like this it will work

    [System.Serializable]
     public class StallSurfaceTextureData
     {
    public StallSurfaceTextureData( int floorTextureID, int leftWallTextureID, int rightWallTextureID, int backWallTextureID)
    {
        Debug.LogFormat($" f {floorTextureID} l {leftWallTextureID} r {rightWallTextureID} b {backWallTextureID}".ToGreen());

        BackWallTextureID = backWallTextureID;
        LeftWallTextureID = leftWallTextureID;
        RightWallTextureID = rightWallTextureID;
        FloorTextureID = floorTextureID;
    }
    
    [field: SerializeField]
    public int BackWallTextureID { get; private set; }
    
    [field: SerializeField]
    public int LeftWallTextureID { get; private set; }
    
    [field: SerializeField]
    public int RightWallTextureID { get;private  set; }
    
    [field: SerializeField]
    public int FloorTextureID { get; private set; }
}
  • Could it be Id and ID not being the same in the json and the class? While FloorTextureID is same on both. – Everts Feb 06 '23 at 09:22
  • That's simply a typo: you fields in JSON are `...Id` you fields in c# are `...ID` => they are not found and keep their default values – derHugo Feb 06 '23 at 09:40
  • I tried JsonConvert.DeserializeObject(json) - no any problem, I guess because you constructor input parameters are ignored since they are not exist – Serge Feb 06 '23 at 17:54
  • In the json you posted, all your "wall"-related variables are named "xxxWallWallxxx" while your variables in C# are named just "xxxWallxxx". Could this be your issue? – Max Play Feb 06 '23 at 23:36
  • 1) Your properties `BackWallTextureID` and etc. have **private setters** so Json.NET won't call the setters unless you mark your properties with `[JsonProperty]`. See [Private setters in Json.Net](https://stackoverflow.com/q/4066947). (`[SerializeField]` has no effect on Json.NET unless you are using some custom contract resolver that checks for the attribute and interprets it as equivalent to `[JsonProperty]`.) – dbc Feb 07 '23 at 01:54
  • 2) The reason the 2nd version of your `StallSurfaceTextureData` works is that it has a fully compatible constructor. When your class has a single constructor which is parameterized, Json.NET it will call the constructor, **matching the constructor arguments to JSON properties by case-invariant name**. See [JSON.net: how to deserialize without using the default constructor?](https://stackoverflow.com/q/23017716) and [Json.net `JsonConstructor` constructor parameter names](https://stackoverflow.com/q/43032552). Do those three questions answer yours sufficiently, or do you need – dbc Feb 07 '23 at 01:55

2 Answers2

1

Unity need to have the same to the letter variables name in JSON and class. You have BackWallTextureID in JSON and BackWallTextureId in class.

0

I tried JsonConvert.DeserializeObject(json) - no any problem, I guess because you constructor input parameters are ignored since they are not exist for Newtonsoft.Json. If you want to use a constructor, input parameters should be the same as json ( case insensitive by default).

Serge
  • 40,935
  • 4
  • 18
  • 45