yanghuan / CSharp.lua

The C# to Lua compiler
Other
1.23k stars 201 forks source link

Serialization/Deserialization Support (error using System.Text.Json.JsonSerializer) #275

Open Qbz23 opened 4 years ago

Qbz23 commented 4 years ago

I tried to use the following...

m_UnitInputData = System.Text.Json.JsonSerializer.Deserialize<InputData.Unit[]>(jsonStr);

But when compiled to lua, it gives an error: attempt to index a nil value (upvalue 'SystemTextJson')

My understanding is that this indicates this functionality is not supported. Are there any alternate forms of serialization that are supported? Might I have more luck with the BinaryFormatter class? Or do I just need to find a different way to solve my problem than de-serializing data?

Also just for clarity, I am able to deserialize the same json str into the same types in a standalone c# console app.

yanghuan commented 4 years ago

you can use cjson in lua, i will use cjson to simple support System.Text.Json in future

Orden4 commented 3 years ago

So recently I've developed more or less this functionality. You can find it on this repo https://github.com/Orden4/WCSharp. As of this writing, it's in the development branch in the WCSharp.Json folder, but it should be on the master within a few weeks.

As said, it's still under development, as for one I'd like to add some basic error reporting, do some further robustness testing, and add some more comments. However, I believe the current version to already be feature complete to the full extent of my capabilities within CSharp.lua.

Use is essentially like Newtonsoft.Json, in which you can just call JsonConvert.Serialize(object) and JsonConvert.Deserialize<Type>(object). Currently supported types are:

Additionally, much like Newtonsoft, besides being able to do e.g. Serialize<MyClass>(object), you can also straight up (de)serialize arrays, lists and dictionaries via e.g. Serialize<List<string>>(object). Note that there is no support for attributes though, so (de)serialization simply follows your exact property definitions.

Currently, this is implemented via a combination of C# code to do the type processing, and injected Lua code using https://github.com/rxi/json.lua to convert data into strings and back. As such it's a bit hacked together, as it's not written entirely in either C# nor Lua, but this was the easiest implementation I could think of that didn't involve me writing my own stringify code or doing it entirely in Lua. Perhaps later on I'll make a complete C# implementation, but for now I'm happy with its capabilities already.

Anyway, feel free to do with this as you like. I wrote this to use in WarCraft 3, to allow custom maps to easily store data/send it between players, but I'd be happy if someone found a use for this elsewhere.

yanghuan commented 3 years ago

Thank you very much for your code, and when I looked closely, I found that there was a difference in interface requirements with System.Text.Json.JsonSerializer that could not be used directly.

ghost commented 9 months ago

503