-
-
Notifications
You must be signed in to change notification settings - Fork 187
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
Generate typed equivalents of Json.hx for other languages #238
Comments
Hey there! I am kind of needing this right now so that I can use this in my Rust project. Could you point me to the code that generates the current JSON doc so that I could take a shot at generating JSON schema with it? Edit: Nevermind, I found it. I'll open a PR if I do anything useful. 🙂 |
Started some experiments on |
The generated C# code has this : So it's returning a plain object instead of the correct typed object. When testing on https://app.quicktype.io/ with some dummy JSON, it does add the correct return types, so something seems to be going wrong when you generate the file. Additional note : For Unity, it would be easier if the file extension was .json instead of .ldtk (or maybe .ldtk.json). Might be too late to change that now, but it would also have the benefit of enabling syntax highlighting in code editors by default. |
For the Rust version, I haven't tried the verbatim generated code form Quicktype, but I did have to make some of the non-optional types optional, such as
Counter to that point, there certain things like setting your desktop to open LDtk files, where it is better to have the |
I can definitely understand that having a custom extension has its benefits. I guess having it as an option might be the best route? |
@Cammin @Mudloop |
These should be marked as optional in latest schema version :) |
Using the latest schema & API sample test from master branch, I can easily get a simple example working in Rust. I don't know if this is what I am supposed to be doing with this, but here's what I did:
[dependencies]
serde = {version = "1.0", features = ["derive"]}
serde_json = "1.0"
use std::fs::File;
mod ldtk_json;
fn main() {
let file = File::open("assets/test.ldtk").expect("file not found");
let data: ldtk_json::LdtkJsonClass =
serde_json::from_reader(file).expect("error while reading");
println!("default bg_color is {}!", data.bg_color);
}
I think this means that "it works" and the entire JSON file is being parsed successfully. Otherwise I think I'd generate an error reading in the file. Will try and use it in an actual game example next. |
Yep, that means that it works. There are a few things that bugged me about the generated Rust types, even though they do technically work:
In short, those issues will make you write a lot more code to access the values, because it assumes a level of variation that isn't actually going to be in the schema. I'm pretty sure that is all either because of a fundamental limitation of JSON schema, or because QuickType doesn't generate the best Rust code, or a little of both. I think the JSON schema itself is accurate. That said, with just a little bit of extra Regex on top of the Quicktype generated schema, we can easily get it much more Rusty, and I have published a work-in-progress Rust crate for LDTK with the manual changes to clean it up a bit: |
Yep, seeing that as I try to replace my ldtk_rust crate with this. Looks like we took similar approaches and are wrestling with the same complications. I was hoping that the schema was not totally fleshed out yet and some of these optional values would clean up over time, but I'm not familiar enough with schemas or QuickType. I'll keep going with this auto-generated code to learn more, and also check out your crate. Maybe this auto-generated code can be wrapped in a lib to make it more accessible while at the same time easy to keep up to date. |
@estivate that's funny, I didn't realize when I started my crate that there was already your crate and somebody else also had this one. 😄 Being as I took the On the topic of auto-generation, it is worth noting that we might try generating Rust directly from the DocGenerator.hx, which would probably allow us to get it exactly how we want it, instead of depending on QuickType's interpretation of the JSON Schema. QuickType was just an easy way to instantly get type generation for a much larger collection of languages in one go. It was a reallly simple script to generate the JSON Schema, so it wouldn't be hard to port it to generating Rust instead. I think that's probably our best bet. Still, to get niceties like converting the |
Well actually, it's a better approach to rely on the JSON schema, because it's purpose is specifically to describe the file format while being language agnostic. And this one is guaranteed to be always up to date in future updates as it'll be the backbone of importers. Quicktype is just a "JSON Schema to whatever" converter. |
Before we get too deep into the details, can we first look at the LdtkJson.rs file posted on the site and see if anyone can get it to compile and give access to anything? It may not work well, but I'm having two problems getting it to work at all:
I'm curious what @zicklag and other Rust folks think because I may not be using the right techniques. There is a serde_derive crate, but I usually use their other crates with a If indeed these issues need to be corrected, we might be able to do so in the generation process, or by commit to the QuickType project. Or if we can't fix I think a quick note to new users would be helpful, especially since it takes less than a minute to fix! For completeness, I'll also note that the commented instructions at the top also don't work for me. They of course don't cause compile issues, but they slowed me down the first time. If they are misleading we should remove or replace if possible, or at least document for first time users. |
True. Then we should just write a JSON Schema → Rust converter that we can put into the @estivate to cover your questions:
Yes, that should be changed, apparently quicktype uses the older rust import strategy. It should be
There is a Rust specific option to pass to the quicktype CLI or some option in the web interface that adds
Yep, all your guesses are correct. Those fields should be public and you should be able to use I think a PR to Quicktype would be reasonable to fix crate imports and comments at the top of the file, and otherwise we just need to pass in the |
I've converted ldtk_rust to use the QuickType generated code and updated it to work with the 0.6.3-preview JSON Schema, including combined or separate level files (all in the qtype branch here). I added an example loading a single external level at a time. The Bevy example needs some work but it does compile, so I think the QuickType auto-generated code is going to work well. Could we add defs to the list of required properties in the top level LdtkJsonRoot object in the JSON Schema? Every |
@estivate did you have to modify the types generated from QuickType? It looks like those are different than the ones I got when I tried QuickType out earlier. It doesn't have those annoying |
@zicklag I used the JSON Schema from the
It's almost to the point where we don't really need our libraries any more. I only have a |
@estivate very cool. 🙂 Once I get some time for this, I might try to make a Rust version of the QuickType Rust generator that it could be included in the But anyway, it looks like you've got it going pretty well with the QuickType one so anything else is just a bonus. 👍 |
Latest JSON schema is now fully typed, with the following exceptions:
See this issue for a downloadable ZIP: #319 |
Thanks @deepnight ! The Schema is looking really good and this will makes it even easier for Rust consumers. |
I just published a new alpha release of my ldtk Rust crate which is now 100% automatically generated using a custom JSON schema → Rust generator that is built into the build process for the crate. You can also optionally have the crate automatically download the latest schema from GitHub during the build so there is absolutely no manual process necessary to get up-to-date bindings. There are a couple of modifications automatically applied to the generated code such as adding a You can see the generated API docs here. |
Feel free to re-open is more info are needed 👍 |
Sounds good. 👍 Would it be possible to have my LDtk crate added as an alternative Rust implementation to the website? Also, if you do, could you put it as "by Katharos Technology" since it's a project under my organization. |
QuickType:
https://github.com/quicktype/quicktype
Json schema + quickType:
https://github.com/elnabo/json2object/#using-the-jsonschema-writer
The text was updated successfully, but these errors were encountered: