3

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 ?

8
  • 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? Commented Jun 19, 2017 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 json2caseclass.cleverapps.io Commented Jun 20, 2017 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. Commented Jun 20, 2017 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. Commented Jun 20, 2017 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? Commented Jun 20, 2017 at 13:32

2 Answers 2

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/

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

2 Comments

Looks like a web application.. I want an API which is compatible with my existing Scala code.
json2caseclass.cleverapps.io is not answering this specific question, but it's a nice tool!
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.

http://yefremov.net/blog/scala-code-generation/

Comments

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.