I've got some data I need to serialize/deserialize, but JsonUtility is just not doing what it's supposed to. Here's the objects I'm working with:
public class SpriteData {
public string sprite_name;
public Vector2 sprite_size;
public List<Vector2> subimage;
}
public class SpriteDataCollection
{
public SpriteData[] sprites;
}
If I create a SpriteDataCollection, and attempt to serialize it with JsonUtility, I just get an empty object {}. Here's how it's being built:
SpriteData data = new SpriteData();
data.sprite_name = "idle";
data.sprite_size = new Vector2(64.0f, 64.0f);
data.subimage = new List<Vector2> { new Vector2(0.0f, 0.0f) };
SpriteDataCollection col = new SpriteDataCollection();
col.sprites = new SpriteData[] { data };
Debug.Log(JsonUtility.ToJson(col));
The debug log only prints "{}". Why isn't it serializing anything? I've tested it out, and serializing a single SpriteData does exactly what it's supposed to do, but it won't work in the SpriteDataCollection.
Both Unity and UnrealEngine utilize C++ in their source code: Unity is partially written using C++ and C#, whereas Unreal Engine is written in C++ entirely. C++ is widely used to develop high-tier game engines and critical service applications where optimal resource utilization and performance are a priority.
The language that's used in Unity is called C# (pronounced C-sharp). All the languages that Unity operates with are object-oriented scripting languages. Like any language, scripting languages have syntax, or parts of speech, and the primary parts are called variables, functions, and classes.
The Unity runtime is written in C/C++. This runtime is used in any build you create using the editor - for webplayers and plugins it is installed separate from your build, whereas it is included in it for stand-alones and other platforms such as iPhone and Wii.
C# is the primary language of . NET, and all of Unity's libraries are built using C# code. To say that C# is the language of Unity is accurate. Unity has made it clear that it considers C# to be the only truly canon language for Unity development.
There are 4 known possible reasons why you may get empty Json in Unity.
1.Not including [Serializable]
. You get empty json if you don't include this.
2.Using property (get/set) as your variable. JsonUtility does not support this.
3.Trying to serializing a collection other than List
.
4.Your json is multi array which JsonUtility
does not support and needs a wrapper to work.
The problem looks like #1. You are missing [Serializable]
on the the classes. You must add using System;
in order to use that.
[Serializable]
public class SpriteData {
public string sprite_name;
public Vector2 sprite_size;
public List<Vector2> subimage;
}
and
[Serializable]
public class SpriteDataCollection
{
public SpriteData[] sprites;
}
5.Like the example, given above in the SpriteData
class, the variable must be a public variable. If it is a private variable, add [SerializeField]
at the top of it.
[Serializable]
public class SpriteDataCollection
{
[SerializeField]
private SpriteData[] sprites;
}
If still not working then your json is probably invalid. Read "4.TROUBLESHOOTING JsonUtility" from the answer in the "Serialize and Deserialize Json and Json Array in Unity" post. That should give you inside on how to fix this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With