Creating case class structure from a Json example file - json

I've a JSON representation (for a Web Service). The JSON has quite a large number of fields. I want to deserialize(serialize) Json to a case class (case to Json) so that I can use it inside Spray/Play framework.
Before I start writing my case classes I was wondering if there is something that allows the creation a set of case classes from an example Json. Something similar to how you can create Java classes for an XML/SOAP schema.

Someone has made this useful application http://json2caseclass.cleverapps.io.
Link to Github repo

Related

Django - serialize complex context structure with models and data

In django view I want to be able to serialize whole context, that is usually used to send to template (typically by calling render and passing locals).
I want to experiment with SPA+API and possibilities to go forward with and I'd like to create function, that would serialize locals to json and return it as json response.
Now problem is, that locals is typically mix of lists, dists and querysets of models.
I can serialize models using django.core.serializers or using django-rest-framework. I can serialize dict with primitive types using json library, but I don't know any simple way how to do mix of those.
Ideal would be way to go through locals dictionary and replace all found models with their serialized representations and then put it all together, maybe even specify before what serializer (in sense of drf) to use for which model. But I really don't want to reinvent wheel in case it already exists.
Anoher question is - is this even a good idea to try to do this? Return json context as alternative to server side rendering? I am in prototyping stage so I am still thinking of how to move forward and any input in the area is appreciated.
I would recommand to go with DRF
ModelSerializer will return a Json encoded array of model
Serializer with DictField will return a Json encoded dict
Serializer with ListField will return a Json encoded list
You can create Serializer with field is another Serializer for nesting purpose.
https://www.django-rest-framework.org/api-guide/fields/#composite-fields
https://www.django-rest-framework.org/api-guide/serializers/#dealing-with-nested-objects
For your' question is this a good idea, i would said :
If you push data to an external source (not django) it's fine
If you push data to django template it's a bad idea, you loose a lot of django power :(

JSON to Scala code generation

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 ?
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/
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/

Snake case json when using default Grails JSON renderer

I am building an app with Groovy/Grails (long-time Java developer), and have had some difficulty with making the JSON renderer do what I want. I would like to change the default name rendering scheme to snake case instead of camel case.
The controller code is very simple right now:
Fund show(String id) {
respond fundService.getFund(id)
}
With Java I would use Jackson and a custom naming strategy, like so:
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
Any help would be appreciated! If there is no way of doing this, and I just need to switch out the renderer wholesale, I would welcome knowing that as well. Thank you.
Create a custom marshaller - you can control anything from there
see here how to create one:
http://compiledammit.com/2012/08/16/custom-json-marshalling-in-grails-done-right/

Single serialization layer to Json with Casbah/Salat

I am trying to create a serialization layer which allows me to:
Store my classes in a MongoDB data source
Convert them to JSON to use them in a REST API.
Some classes are clearly not case classes (because they are inherited from a Java codebase) and I would have to write ad-hoc code for that. Is registering a BSON Hook for my non standard type the correct approach, and does it provide Json serialization?
Salat maintainer here.
You might prefer to create a Salat custom transformer instead of registering a BSON hook with Casbah.
See simple example and spec.
If you run into any issues, feel free to ping the mailing list with a small sample Github project that demonstrates what isn't working.

Why should I use JavaScriptObject overlay classes instead of native Java classes when processing JSON data?

In my GWT project I need to process json data retrieved from a database via PHP. I have seen the Google examples using JavaScriptObject overlay classes. What I don't understand is why this seems to be the prefered method of processing the json data. Why shouldn't I use all native Java code to pull in the data?
Think about it the other way around: what does it mean to use POJOs? (or native Java classes as you name them)
You have to:
parse the JSON into some Java-accessible structure (e.g. com.google.gwt.json.client.JSONObject, or elemental.json.JsonObject)
create POJOs
fill the POJOs with the data from the parsed JSON structure
now you can forget the parsed JSON structure from step 1
On the other hand, with JavaScriptObject, you use JsonUtil.safeEval and TA-DA! you get your JSON parsed right into a typed Java object!
Now, to deal with JSON, there's also AutoBeans.
Choose your poison.