Tried the Unity JSON… doesn’t work on lists or arrays
Tried the Newton JSON… can handle null items
I updated to Unity 6 hoping to use .NET System.Text.Json; because Unity 6 uses the right version. Doesn’t have it.
How can I get it on it? I have a data file load for my game that takes 35s to load through a binary formatter. I need something faster.
It’s a large save file for a wargame I am working on. A massive map.
Load it once, convert it, save it to a format of your liking. You can do that with a C# command line app.
But as far as Newton not handling null items I’m pretty sure this isn’t a dead-end. Possibly requires subscribing to events in order to create a proxy object for a null entry or something like that.
Though your best option is to use the Serialization package. You can bend it to your will.
Frankly, I suck as a coder. I am guessing at complex things half the time. It’s a miracle I put out 3 successful games. I read that page link and it is all gibbery goop to me. I also don’t know what you mean with Newton.
I need something simple for my simple mind.
using Newtonsoft.Json;
string json = JsonConvert.SerializeObject(gameData);
using (var stream2 = File.Open(fileName, FileMode.Create)) {
using (var writer = new BinaryWriter(stream2, Encoding.UTF8, false)) {
writer.Write(json);
}
}
using Newtonsoft.Json;
string json = "";
using (var stream = File.Open(fileName, FileMode.Open)) {
using (var reader = new BinaryReader(stream, Encoding.UTF8, false)) {
json = reader.ReadString();
}
}
gameData = JsonConvert.DeserializeObject<Data>(json);
I assume you meant to write “can’t” here, but I believe it can with the right settings. Unity’s serializer can handle null as well with fields serialized with [SerializeReference].
Yeah I can see the miracles at work. You must have a guiding angel of sorts!
Like seriously, the above is better written as:
var json = JsonConvert.SerializeObject(gameData);
File.WriteAllText(fileName, json);
Not sure why you would want to write a string as binary. It will end up being text regardless.
There is an equivalent ReadAllText as well.
Also, be sure to try catch! (maybe it’s been omitted in the sample code but just to be sure)
try
{
var json = JsonConvert.SerializeObject(gameData);
File.WriteAllText(fileName, json);
}
catch (Exception e)
{
// handle e, log it, show it to the user, etc.
}
Any I/O operation may fail at any time for some users, be it because the disk is full or the path is invalid or the user doesn’t have permission or a virus scanner interferes and considers your json as malicious code (highly unlikely but these things do happen).
You don’t want the user experience a crash while saving, losing all progress, and possibly leaving a negative review because of it.
Thanks for the suggestions and the additions on the safe catch for the writer.
I tried everything everyone listed that I could understand. But each one had their own errors.
[SerializeReference] fix was an error
Setting the null value in Newton didn’t do anything, same errors.
Odin JSON has errors in the code of conflicting methods. It also has to allow unsafe code activated. I couldn’t even try to use it. It has conflicting code with itself.
ERROR
Assets\Scripts\odin-serializer-master\OdinSerializer\Utilities\Misc\UnsafeUtilities.cs(91,37): error CS0227: Unsafe code may only appear if compiling with /unsafe. Enable “Allow ‘unsafe’ code” in Player Settings to fix this error.
So I set to accept unsafe code. I get 336 errors similar to…
Assets\Scripts\odin-serializer-master\OdinSerializer\Unity Integration\UnitySerializationUtility.cs(387,27): error CS0121: The call is ambiguous between the following methods or properties: ‘OdinSerializer.Utilities.MemberInfoExtensions.IsDefined(System.Reflection.ICustomAttributeProvider)’ and ‘OdinSerializer.Utilities.MemberInfoExtensions.IsDefined(System.Reflection.ICustomAttributeProvider)’
I am an old coder than learned procedural programing. I can write assembler language. But it it really difficult for me to understand how to use all the newer coding.
Holy crap I got Odin working apparently. I had to go to their website and click a unity link that didn’t work. So I took a guess at the right link and got it right!!
It saved the data and looks like it loaded it.
More testing is required.