• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Any way to parse JSON in lua?

mdl

mdl

Level 3
Joined
Sep 19, 2024
Messages
12
I'm looking to use programmable data which doesn't output to lua, wondering if anyone knows of any way to parse raw data in lua? I'm aware that it's possible in lua outside of the world editor environment, but I couldn't imagine you could import just any lua library?
 

Uncle

Warcraft Moderator
Level 73
Joined
Aug 10, 2018
Messages
7,866
No, you can't import some outside library. Lua is very limited to the constraints of "what's allowed" in Warcraft 3, which isn't much.

Your best bet would be to make a little app on your computer to automate the process, or just use one online. You could probably have something that rebuilds the master .lua file using your JSON files + existing Lua code, that way you don't have to deal with the hassle of copying and pasting code constantly or have to worry about overwriting the wrong thing. You can save your map as a folder to make this easier to manage.

Edit: Also, what Cheshire said. WCSharp allows for a much better workflow than using (standard) Lua, in my opinion. Also, C# is simple and easy to use.
 
Last edited:

mdl

mdl

Level 3
Joined
Sep 19, 2024
Messages
12
No, you can't import some outside library. Lua is very limited to the constraints of "what's allowed" in Warcraft 3, which isn't much.

Your best bet would be to make a little app on your computer to automate the process, or just use one online. You could probably have something that rebuilds the master .lua file using your JSON files + existing Lua code, that way you don't have to deal with the hassle of copying and pasting code constantly or have to worry about overwriting the wrong thing. You can save your map as a folder to make this easier to manage.

Edit: Also, what Cheshire said. WCSharp allows for a much better workflow than using (standard) Lua, in my opinion. Also, C# is simple and easy to use.
I did essentially this, wrote a simple transpiler for json to lua and using npm to transpile on save.

join the dark side

not exactly what you asked, but this will let you parse json in your code with no problems.
I was looking at these. Is it still possible to use all the nice lua libraries around when using C# or typescript?
 
Level 5
Joined
Mar 8, 2012
Messages
50
Woops, don't check here very often.

Yes, WCSharp has support for parsing JSON files, though I'm guessing that mdl has already found that out since he's found his way to the C# mapping discord. For more info, see: WCSharp.JsonConvert

As for using JSON in other languages, I'm fairly certain that it will never be quite as powerful as that of C#, since CSharp.lua's metadata output allows you to do static analysis of the structure, which means that the converter can do all of the hard stuff for you. But with a bit of manual labour and ignoring type safety, you should be able to do something with any given json library. WCSharp itself uses rxi.json in the background, a lua library (since writing my own parser was a hassle).

Aside from that, you may also want to look into MessagePack if you just want maximum size efficiency. There's libraries for that in basically every language. I intend to add support for it to WCSharp at some point.
 
  • Like
Reactions: mdl

mdl

mdl

Level 3
Joined
Sep 19, 2024
Messages
12
Woops, don't check here very often.

Yes, WCSharp has support for parsing JSON files, though I'm guessing that mdl has already found that out since he's found his way to the C# mapping discord. For more info, see: WCSharp.JsonConvert

As for using JSON in other languages, I'm fairly certain that it will never be quite as powerful as that of C#, since CSharp.lua's metadata output allows you to do static analysis of the structure, which means that the converter can do all of the hard stuff for you. But with a bit of manual labour and ignoring type safety, you should be able to do something with any given json library. WCSharp itself uses rxi.json in the background, a lua library (since writing my own parser was a hassle).

Aside from that, you may also want to look into MessagePack if you just want maximum size efficiency. There's libraries for that in basically every language. I intend to add support for it to WCSharp at some point.
Hey, thanks a ton for the detailed response!

As you pointed out, I've switched over to WCSharp, which means I don't need to parse JSON anymore because of the static typing. That you support it is awesome nevertheless! I'm a big fan of Apple's pkl format, and since it can output JSON, I might find myself needing this in the future. Even if I don't, I'm sure your answer will be super helpful for others who are trying to do the same thing.
 
Top