I have a JSON which is an output of another application, I want to make dynamic Scala code (*.scala file ) from this JSON (can not pre define class/bean as the JSON structure depends many other parameters). Is there any API available for this ?
Asked
Active
Viewed 916 times
3
-
if the class is not defined at the compile time, how do you plan on using it in your other (non-generated) code? Can you show and example, how you would use such tool? – Dima Jun 19 '17 at 20:03
-
There are ways to load this newly generated class using classloader and instantiate. I tried to manage create case classes by reading JSON, but wondering if any API already does this . You could also check this http://json2caseclass.cleverapps.io/ – Isabel Jinson Jun 20 '17 at 13:04
-
I understand, how you can load them and instantiate. But how are you going to be _using_ those instances? The link you gave is _static_ generation - you give it sample json, it generates case classes, and you then compile them with your static code. That's not what you are talking about at all if I understand your question correctly. – Dima Jun 20 '17 at 13:19
-
I do not use that link , I gave you to understand case class generation . I will treat the generated class (from JSON) as string then load using class loader . Hope this makes sense. – Isabel Jinson Jun 20 '17 at 13:29
-
I understand the class generation, thank you very much. I don't understand what _you_ are trying to do with it, because what you are asking doesn't seem to make any sense. You load it using the classloader ... _then what_? `val loadedInstance: AnyRef = generateAndLoad(json)` ... Now what? How will you be using this variable? – Dima Jun 20 '17 at 13:32
-
I have working example , I will share when time permits. The idea is create dynamic class from JSON ( do not worry about class name - I have class template ) and the generated class loaded and instantiated ( this is like bean now ). – Isabel Jinson Jun 20 '17 at 13:48
-
You keep talking about "loading and instantiating" it. _This is not at all what I am asking_. I understand how you can load and instantiate it. What I don't understand is _why_ you want to load and instantiate it. Without having any type information about this generated class _at compile time_, pretty much, the only way you can access it's data is using reflection anyway. So, what's the benefit of having it generated in the first place, over just using a `Map[String, Any]` to begin with, which is not only simpler, but also _ways_ more efficient than reflection? – Dima Jun 20 '17 at 14:05
-
There's this UI tool that can convert complex JSON schemas to case classes: https://cchandurkar.github.io/case-class-generator/ – Chaitanya Chandurkar Jun 16 '20 at 06:52
2 Answers
4
You can use something like below:
http://json2caseclass.cleverapps.io/
https://www.reddit.com/r/scala/comments/36m951/json2caseclass_a_tool_to_generate_scala_case/

ashwinsakthi
- 1,856
- 4
- 27
- 56
-
Looks like a web application.. I want an API which is compatible with my existing Scala code. – Isabel Jinson Jun 19 '17 at 14:57
-
http://json2caseclass.cleverapps.io/ is not answering this specific question, but it's a nice tool! – mathieu Feb 04 '20 at 17:31
1
Please have a look at this one: https://github.com/julianpeeters/case-class-generator
Allows runtime data to serve as Scala case class definitions:
Case classes defined and loaded at runtime Pseudo Type-Provider via type alias
And also this one: Maybe this one is better, you need to generate some code first, and then call it.

HHH
- 311
- 2
- 7