-
-
Save onionmk2/d2e3e4cca27a37a89796e084e05de212 to your computer and use it in GitHub Desktop.
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using Newtonsoft.Json; | |
using UnityEngine; | |
public class UsingJsonDotNetInUnity : MonoBehaviour | |
{ | |
private void Awake() | |
{ | |
var accountJames = new Account | |
{ | |
Email = "[email protected]", | |
Active = true, | |
CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc), | |
Roles = new List<string> | |
{ | |
"User", | |
"Admin" | |
}, | |
Ve = new Vector3(10, 3, 1), | |
StrVector3Dictionary = new Dictionary<string, Vector3> | |
{ | |
{"start", new Vector3(0, 0, 1)}, | |
{"end", new Vector3(9, 0, 1)} | |
} | |
}; | |
var accountOnion = new Account | |
{ | |
Email = "[email protected]", | |
Active = true, | |
CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc), | |
Roles = new List<string> | |
{ | |
"User", | |
"Admin" | |
}, | |
Ve = new Vector3(0, 3, 1), | |
StrVector3Dictionary = new Dictionary<string, Vector3> | |
{ | |
{"vr", new Vector3(0, 0, 1)}, | |
{"pc", new Vector3(9, 9, 1)} | |
} | |
}; | |
var setting = new JsonSerializerSettings(); | |
setting.Formatting = Formatting.Indented; | |
setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; | |
// write | |
var accountsFromCode = new List<Account> {accountJames, accountOnion}; | |
var json = JsonConvert.SerializeObject(accountsFromCode, setting); | |
var path = Path.Combine(Application.dataPath, "hi.json"); | |
File.WriteAllText(path, json); | |
// read | |
var fileContent = File.ReadAllText(path); | |
var accountsFromFile = JsonConvert.DeserializeObject<List<Account>>(fileContent); | |
var reSerializedJson = JsonConvert.SerializeObject(accountsFromFile, setting); | |
print(reSerializedJson); | |
print("json == reSerializedJson is" + (json == reSerializedJson)); | |
} | |
public class Account | |
{ | |
public string Email { get; set; } | |
public bool Active { get; set; } | |
public DateTime CreatedDate { get; set; } | |
public IList<string> Roles { get; set; } | |
public Vector3 Ve { get; set; } | |
public Dictionary<string, Vector3> StrVector3Dictionary { get; set; } | |
} | |
} |
Why Json.Net ?
According to https://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity,
- Unity's JsonUtility does not support array as it is still new.
- Unity's JsonUtility can not serialize property.
- Unity's JsonUtility can not serialize Dictonary.
so, Json.Net may be better.
it doesn't work on publish just work on Editor
For support in built versions, recommended to use one of following
Json.NET v7
nuget.org/packages/Unity.Newtonsoft.Json/Json.NET v8
parentelement.com/assets/json_net_unityJson.NET v9
github.com/SaladLab/Json.Net.Unity3DJson.NET v12
github.com/jilleJr/Newtonsoft.Json-for-Unity
Or if you dont require Newtonsoft.Json (Json.NET), here's some other alternatives (all works in Unity, I've omitted libraries that don't, and there's a lot of 'em):
OdinSerializer
github.com/TeamSirenix/odin-serializerUTF8Json
github.com/neuecc/Utf8JsonFastJSON
assetstore.unity.com/packages/tools/input-management/fastjson-27220UltimateJson
assetstore.unity.com/packages/tools/integration/ultimate-json-60845SimpleJson
wiki.unity3d.com/index.php/SimpleJSONLitJson
github.com/Mervill/UnityLitJsonFullSerializer
github.com/jacobdufault/fullserializerprotobuf-net
github.com/protobuf-net/protobuf-net
There is quite the table of candy to select from. Choose well young padawans~
Hello! Do you know if this library works on Android, my app stops at JsonConvert.DeserializeObject without error or crashing it just skips the rest of the method, on editor everything works fine using the library.
Thanks in advance.
@helixen I know for a fact mine does https://github.com/jilleJr/Newtonsoft.Json-for-Unity
It could be that your code is throwing. That would quit the function but not the game. You could try doing a development build, then uncatched exceptions are viewed on screen.
I could catch the problem, the error is that the JsonConvert.DeserializeObject cannot deserialize an array to IEnumrable, but it only happens when it's running on Android device, I'm in a hurry for delivery so I had to use another library. I'm testing on Samsung Galaxy S9 Android Version 9.
Thanks a lot now we managed it working in built version. <3
How To Use
\Bin\net35\Newtonsoft.Json.dll
toAsset\