0

I'm trying to generate POJO from Json and for that I've found jsonschema2pojo.

But I'd like to use the generated class from my code without having to change my code everytime a new class is generated. I'll receive many different Json's and I'd like to use them as POJO's without interrupting the program.

I also need to access at least a variable from each POJO that might have a different name, for that I was thinking of using Java reflection.

I'd like to know if this is doable, or if there's better ways to implement this.

3
  • Why do you need it or want to do that? If it is an interface use Open Api Specification and generate client, api and server interfaces. If you really want to load json into your application (if it's not a specified interface) then I don't see the why you would do all the complex stuf you suggest. It's better to handle it simple as json content in your application. Commented May 20, 2021 at 19:50
  • @JasperHuzen I need it because of another API I'm using that needs POJOs as an argument and won't accept Json. I'm trying to generalize what was built with that framework so that there's no need to create the POJO everytime there's a need for a new one. Commented May 20, 2021 at 20:57
  • If an API "needs POJOs", then doesn't that mean it's a java interface? In that case that API already defines the POJOs and you don't have to generate them. Perhaps you could specify what the other API is? Commented May 20, 2021 at 21:36

1 Answer 1

0

It sounds like you want all this to happen at runtime. I.e. you don't want to stop the program, change the code and restart the program when a new json comes in.

If that's the case generating classes (POJOs) just to do reflection on them doesn't make sense. Just work with Hashmaps, GSON-datatypes, or whatever way you can find of dynamically handle the data.

There is probably a better way to implement it, but you'll have to be more specific about what you're trying to do.

Sign up to request clarification or add additional context in comments.

6 Comments

Yes, I want these changes to happen at runtime, the API I'm using is Flink Datastreams, I need to create a Datastream<Pojo>. I'm trying to join 2 datastreams on a variable that will be indicated by the user in Json format, for example given the following JSON: {"id": 2, "name":"Zebra","dob": 1621463843900680, "gender" : "male"} The user will say it wants to join this Json stream on the variable ID with another Json stream on another variable name. So I have to generate these classes and be able to access the generated variables somehow, if you have any advice, it'd much appreciated.
It sounds like you want to invoke Flink dynamically. I don't know anything about Flink. That may or may not be possible. You might consider asking a specific question about that where you show a flask code example that works with a pair of known hard coded data types and ask how you can do the same if the class/fields change during runtime.
It might be possible to generate a class from you json at runtime, load it into a class loader, then use reflection to invoke the Flink API, but you're in really deep reflection waters at that point.
I don't think the framework affects this, if I'm able to create classes and access it's variables then that's all I need. Do you know if your previous comment Hashmaps GSON-datatypes would work for that? Thank you
No, Hashmaps or GSON-datatypes would be to handle the data without creating classes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.