I am loading JSON data in my Unity project via Newtonsoft JsonConvert.Deserialize. This works in Editor and the json is valid. However, as soon as I make a Windows build, the serialized data is empty / filled with default values instead.
This is also shown when I serialize the deserialized data and output it:
- Json string read:
{
"galleries": [{
"id": "Acker",
"title": "Auf dem Acker",
"slides": [],
"path": "/ChangeableContentData/Auf dem Acker",
"showAllInFolder": true,
"foldersRecursive": false,
"enableSlideshow": false,
"slideshowSlideDuration": 5.0
}],
"cardMenus": [],
"urls": []
}
- reserialized:
{
"galleries": [{
"id": null,
"title": "",
"slides": null,
"path": "",
"showAllInFolder": true,
"foldersRecursive": true,
"enableSlideshow": false,
"slideshowSlideDuration": 5.0
}],
"cardMenus": []
}
Here is the relevant C# class:
[System.Serializable]
[Preserve]
public partial class UIContentData
{
[JsonProperty("galleries")]
public List<UIContentGalleryData> galleries { get; set; }
[JsonProperty("cardMenus")]
public List<UIContentCardMenuData> cardMenus { get; set; }
[JsonProperty("urls")]
public List<UIContentUrlData> urls { get; set; }
public UIContentData() { }
}
[System.Serializable]
[Preserve]
public partial class UIContentGalleryData
{
public string id { get; set; }
[JsonProperty("title")]
public string title { get; set; } = ""; // optional
[JsonProperty("slides")]
public List<UIContentGallerySlide> slides { get; set; }
[JsonProperty("path")]
public string baseFolderPath { get; set; } = "";
[JsonProperty("showAllInFolder")]
public bool showAllInFolder { get; set; } = true; // optional
[JsonProperty("foldersRecursive")]
public bool foldersRecursive { get; set; } = true; // optional
[JsonProperty("enableSlideshow")]
public bool enableSlideshow { get; set; } = false; // optional
[JsonProperty("slideshowSlideDuration")]
public float slideshowSlideDuration { get; set; } = 5f; // optional
public UIContentGalleryData() { }
}
Edit for clarity: I am using UIContentData object to deserialize.
JsonConvert.DeserializeObject<UIContentData>(json);
Any help is much appreciated!
CodePudding user response:
You are using the wrong class,should create one more. This code was tested and working properly
var data=JsonConvert.DeserializeObject<Root>(json);
public class Root
{
public List<UIContentGalleryData> galleries { get; set; }
public List<object> cardMenus { get; set; }
public List<object> urls { get; set; }
}
and remove slides property from UIContentGallerySlide
[JsonProperty("slides")]
public List<UIContentGallerySlide> slides { get; set; }
CodePudding user response:
Found the issue. Unity was stripping the class definitions I believe. Adding a link.xml with preserve="all" for the assembly containing them solved the problem!