Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question JsonUtility.FromJsonOverwrite doesn't seem to do anything?

Discussion in 'Scripting' started by GlassBee, Jun 20, 2023.

  1. GlassBee

    GlassBee

    Joined:
    Nov 1, 2021
    Posts:
    4
    I have a scriptable object that I would like to overwrite with data from a json file. I have no issue getting the data from the json as a string, and I've confirmed that it's formatted correctly. When I pass it into this method I receive no error messages, but my scriptable object isn't overwritten.

    Code (CSharp):
    1.     public void JsonInit(string jsonString)
    2.     {
    3.         JsonUtility.FromJsonOverwrite(jsonString, this);
    4.     }
    What am I missing here? I've gone digging around in the documentation, and checked multiple code examples, but as far as I can tell, this should work fine?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    Please post the code for the SO and the json.

    Note from the docs:
    The types of fields that you want to be overwritten must be supported by the serializer; unsupported fields will be ignored, as will private fields, static fields, and fields with the NonSerialized attribute applied.
    If a field of the object is not present in the JSON representation, that field will be left unchanged.
     
    Bunny83 and Ryiah like this.
  3. GlassBee

    GlassBee

    Joined:
    Nov 1, 2021
    Posts:
    4
    Sure thing, they're both quite basic

    Here's the SO:
    Code (CSharp):
    1. public class SO_GUIData: ScriptableObject
    2. {
    3.     public string FileType { get; set; }
    4.     public string LanguageCode { get; set; }
    5.     public string LanguageName { get; set; }
    6.     public string PauseText { get; set; }
    7.     public string ResumeButtonText { get; set; }
    8.     public string SettingsButtonText { get; set; }
    9.     public string QuitButtonText { get; set; }
    10.     public string TwelveHourClockName { get; set; }
    11.     public string TwentyFourHourClockName { get; set; }
    12.     public string AnteMeridiemShort { get; set; }
    13.     public string PostMeridiemShort { get; set; }
    14.     public string WeeHours { get; set; }
    15.     public string SundayLong { get; set; }
    16.     public string SundayShort { get; set; }
    17.     public string MondayLong { get; set; }
    18.     public string MondayShort { get; set; }
    19.     public string TuesdayLong { get; set; }
    20.     public string TuesdayShort { get; set; }
    21.     public string WednesdayLong { get; set; }
    22.     public string WednesdayShort { get; set; }
    23.     public string ThursdayLong { get; set; }
    24.     public string ThursdayShort { get; set; }
    25.     public string FridayLong { get; set; }
    26.     public string FridayShort { get; set; }
    27.     public string SaturdayLong { get; set; }
    28.     public string SaturdayShort { get; set; }
    29.     public string SpringLong { get; set; }
    30.     public string SpringShort { get; set; }
    31.     public string SummerLong { get; set; }
    32.     public string SummerShort { get; set; }
    33.     public string AutumnLong { get; set; }
    34.     public string AutumnShort { get; set; }
    35.     public string WinterLong { get; set; }
    36.     public string WinterShort { get; set; }
    37.     public string SeasonName { get; set; }
    38.     public string YearName { get; set; }
    39.  
    40.     public void JsonInit(string jsonString)
    41.     {
    42.         JsonUtility.FromJsonOverwrite(jsonString, this);
    43.         this.name = LanguageCode + "GUIData";
    44.     }
    45. }
    Here's the json:
    Code (CSharp):
    1. {
    2.     "FileType": "GUI",
    3.     "LanguageCode": "eng",
    4.     "LanguageName": "English",
    5.  
    6.     "PauseText": "Paused",
    7.     "ResumeButtonText": "Resume",
    8.     "SettingsButtonText": "Settings",
    9.     "QuitButtonText": "Quit Game",
    10.  
    11.     "TwelveHourClockName": "12 Hour Clock",
    12.     "TwentyFourHourClockName": "24 Hour Clock",
    13.     "AnteMeridiemShort": "A.M.",
    14.     "PostMeridiemShort": "P.M.",
    15.     "WeeHours": "Wee Hours",
    16.  
    17.     "SundayLong": "Sunday",
    18.     "SundayShort": "Sun",
    19.     "MondayLong": "Monday",
    20.     "MondayShort": "Mon",
    21.     "TuesdayLong": "Tuesday",
    22.     "TuesdayShort": "Tues",
    23.     "WednesdayLong": "Wednesday",
    24.     "WednesdayShort": "Wed",
    25.     "ThursdayLong": "Thursday",
    26.     "ThursdayShort": "Thurs",
    27.     "FridayLong": "Friday",
    28.     "FridayShort": "Fri",
    29.     "SaturdayLong": "Saturday",
    30.     "SaturdayShort": "Sat",
    31.  
    32.     "SpringLong": "Spring",
    33.     "SpringShort": "Spr",
    34.     "SummerLong": "Summer",
    35.     "SummerShort": "Sum",
    36.     "AutumnLong": "Autumn",
    37.     "AutumnShort": "Aut",
    38.     "WinterLong": "Winter",
    39.     "WinterShort": "Win",
    40.  
    41.     "SeasonName": "Season",
    42.     "YearName": "Year"
    43. }
     
  4. rdjadu

    rdjadu

    Joined:
    May 9, 2022
    Posts:
    102
    Unity serialization doesn't deal in properties. The properties have to be fields or have to have backing fields marked with [SerializeField].
     
  5. GlassBee

    GlassBee

    Joined:
    Nov 1, 2021
    Posts:
    4
    That was it, thank you!
     
    Bunny83 likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Problems with Unity "tiny lite" built-in JSON:

    In general I highly suggest staying away from Unity's JSON "tiny lite" package. It's really not very capable at all and will silently fail on very common data structures, such as bare arrays, tuples, Dictionaries and Hashes and ALL properties.

    Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window -> Package Manager).

    https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347

    Also, always be sure to leverage sites like:

    https://jsonlint.com
    https://json2csharp.com
    https://csharp2json.io