Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Qbz23 opened this issue Jan 20, 2020 · 4 comments

Comments

@Qbz23
Copy link

Qbz23 commented Jan 20, 2020

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.

@Qbz23 Qbz23 changed the title Serialization/Deserialization Support Serialization/Deserialization Support (error using System.Text.Json.JsonSerializer) Jan 20, 2020
@yanghuan
Copy link
Owner

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

@Orden4
Copy link
Contributor

Orden4 commented Mar 17, 2021

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:

  • All primitive value types
  • Strings
  • Enums
  • Arrays, of which the element type must be a supported format
  • Classes which implement IList<>, of which the generic type must be a supported format
  • Classes which implement IDictionary<,>, of which the generic key must be a primitive value type, string or enum, and the value must be a supported format.
  • All other classes will be (de)serialized according to public properties (note that all public properties must have both a getter and a setter).

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
Copy link
Owner

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
Copy link

ghost commented Feb 8, 2024

#503

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants