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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question JsonUtility.ToJson produces blank JSON even for int data

Discussion in 'Scripting' started by m098shrh7s7r95s9, Jan 12, 2023.

  1. m098shrh7s7r95s9

    m098shrh7s7r95s9

    Joined:
    Dec 23, 2022
    Posts:
    4
    I am trying to build a simple system for saving my game state to a JSON file, but I keep getting blank JSON. I have read many posts on this forum and on Stack Overflow from people with similar issues, but in all of the issues I have seen, the issue boiled down to the player trying to serialize a complex type (e.g. a Dictionary) that Unity's JsonUtility cannot serialize. But I am getting blank JSON even with primitive types, e.g. integers.

    Here is a minimal example:

    1. Create an empty 3D project and generate two C# scripts as follows:

    GameData.cs, a class for holding the data:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]using System.Collections;
    6. using System.Collections.Generic;
    7. using UnityEngine;
    8.  
    9. [System.Serializable]
    10. public class GameData
    11. {
    12.     public int score { get; set; }
    13.     public int health { get; set; }
    14.  
    15.     public GameData(int score, int health)
    16.     {
    17.         this.score = score;
    18.         this.health = health;
    19.     }
    20. }
    21.  
    SaveLoadManager.cs, which will write the JSON to a file (in this case, we just write to the console to keep things simple):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SaveLoadManager : MonoBehaviour
    6. {
    7.     void OnMouseDown()
    8.     {
    9.         SaveJson();
    10.     }
    11.  
    12.     void SaveJson()
    13.     {
    14.         var data = new GameData(4, 5);
    15.         var serializedData = JsonUtility.ToJson(data);
    16.         Debug.Log($"Serialized data: {serializedData}");
    17.     }
    18. }
    19.  
    20.  
    2. Create an empty cube and add the SaveLoadManager.cs script to it.

    3. Play the game and click on the cube. The following is logged to the console:

    upload_2023-1-12_11-40-5.png

    ("Serialized data: {}")

    Why is my JSON empty?

    (Unity v2021.3.16f1 on Windows 10.)
     
  2. tomfulghum

    tomfulghum

    Joined:
    May 8, 2017
    Posts:
    69
    JsonUtility doesn't serialize properties, only fields that are either public or have the SerializeField attribute.

    You can tell it to serialize a property's backing field by using
    [field: SerializeField]
    but it'll be called something like
    <score>k__BackingField
    in the result.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
  4. m098shrh7s7r95s9

    m098shrh7s7r95s9

    Joined:
    Dec 23, 2022
    Posts:
    4
    Thank you, I had also read about this issue but got the concepts of fields and properties reversed in my head.