Skip to content
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

Case classes to json automatically #55

Closed
visox opened this issue Nov 3, 2016 · 3 comments
Closed

Case classes to json automatically #55

visox opened this issue Nov 3, 2016 · 3 comments

Comments

@visox
Copy link

visox commented Nov 3, 2016

I don't like to rewrite case classes into json like this:

val jsonData = JSONObject(
  "answer" -> 42,
  "platform" -> "node"
)

It smells and i am also loosing some typesafety (lets say i have some structure that i share with backend and frontend)

There are many json parsers/solutions that do that, so its hopefully not too hard.

@hmil
Copy link
Owner

hmil commented Nov 7, 2016

Hi @visox,

Thank you for your feedback. Could you elaborate what you don't like about the code above and what you'd like to be able to do instead?

Also when you say loosing some typesafety are you talking about typing constraints within your JSON structure (in your example answer: number, platform: string)? The JSON AST provided with RosHTTP is extremely basic and does not support this kind of internal constraints.

If you wish to use some more advanced JSON library, I'd advise you to create a subclass of BulkBodyPart, return the string you obtain from the JSON library of your choice as a UTF-8 encoded ByteBuffer and define the Content-Type member as application/json; charset=utf-8 (see the implementation of JSONBody for example.

Issue #29 might be relevant to your case.

@visox
Copy link
Author

visox commented Nov 7, 2016

hi

what you'd like to be able to do instead?

val jsonData = someCaseClassInstance.toJs

would be best. Including some default implicit write functions that one could still override

you talking about typing constraints within your JSON structure

yes

RosHTTP is extremely basic and does not support this kind of internal constraints

i see :/

Thx for the suggestions.

@hmil
Copy link
Owner

hmil commented Nov 7, 2016

AFAIK transforming class structures to program data always involves reflectivity in Java which goes way beyond the scope of this library. I'm interested to know if there is a clean way to achieve this in Scala as #29 should keep the door open for a solution to your use case, if not solve it directly.

@hmil hmil closed this as completed Nov 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants