How to deserialize blaze persistence entity view from JSON programatically? - java

Is there a way to programatically deserialize JSON to blaze-persistence entity view?
When I'm using standard jackson object mapper, and trying to do
mapper.readValue(serializedJSON, EntityViewImpl.class);
where serializedJSON is string representation (serialized from the same class of entity view), and EntityViewImpl.class is code-generated implementation of entity view interface, I'm getting an exception:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of (although at least one Creator exists): cannot deserialize from Object value (no delegate or property based Creator)
at [Source: (String) "{JSON representation of EntityView}"; line: 1, column: 2]
.
Same JSON correctly deserializes into entity that was basis for used entity view.
I noticed that there is a custom deserialization functionality as part of the blaze-persistance, although was not able to find any related documentation.
So any answer, eighther with standard jackson mapper or with custom blaze-persistence functionality would work for me.

You can use the Jackson integration which allows you to deserialize creatable/updatable entity views. There are integrations for JAX-RS and Spring WebMvc/WebFlux and even GraphQL though, so chances are, you don't have to do this plumbing yourself. If you share some more details like the entity view and the are where you want to do deserialization, I can give you a more extensive answer.

Related

Spring Data Elasticsearch 4 - Override Object Mapper?

Previously in version 3 of Spring Data Elasticsearch, the Jackson mapper was used by default, but could be overridden to use the Metamodel object mapper, as documented here:
https://docs.spring.io/spring-data/elasticsearch/docs/3.2.0.RC1/reference/html/#elasticsearch.mapping.meta-model
I understand the Jackson mapper has been removed in version 4 and replaced with the Metamodel object mapper, as documented here:
https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.mapping
But it appears the ability to override the object mapper was removed as well. Is there indeed no way to configure the Elasticsearch global object mapper to use Jackson (or any other mapper) again? It seems like a shame to lose the flexibility that option provided.
No. The MappingConverter is not only used and needed for converting an entity to and from JSON, but also for converting and mapping fieldnames, dateformats and other stuff for example when CriteriaQuerys are created or when search resukts like highlights are processed. There are man places in Spring Data Elasticsearch where the mapping information for an entity is needed and Jackson cannot be used there.
So in versions before 4.0 it was necessary to customize Jackson with jackson-annotations on the entity and the other stuff with different annotations, this has been consolidated.
What functionality do you need that the MappingConverter (implementation of the meta model mapper) does not offer in combination with custom converters?
Edit 05.12.2020:
valid point from the comments: It should be possible to define a FieldNamingStrategy for an entity. I created an issue for that.

Jersey XML into Map

Due to old project decisions, I work on a project that uses Jersey to connect to services that return XML data. Sometimes I could create the bean/pojo/whatever annotated with XmlRootElement and use webTarget.get(MyPojo.class)
However, if I try to do what I would do with, say, Jackson, and do webTarget.get(Map.class) I get the following error:
MessageBodyReader not found for media type=text/xml, type=interface java.util.Map, genericType=interface java.util.Map.
My case is, I have a XML that can have arbitrary fields whithin, so the ideal way for me to read it is to read a Map. Is there any way I can do that without having to rely in other libraries? I don't need to serialize data, only deserialize responses from the web services I connect to.
Found the answer, and it's kinda depressing.
According to the Unofficial JAXB Guide, you can't use a Map as a root element, unless you do a very ugly hack, described there. So, the solutions are:
Read as a String, and use Jackson to read a Map from the string.
Do the ugly hack.
Describe part of the schema in a bean, but the part that can have variable fields can be deserialized to a Map.

Jackson mapper to persistence object

I writing JUNIT for a very complex hierarchy application
I need to test a service that his input is a very large and complicated persistence object.
As it so big it we decided to initialize it with JSONN string.
The problem is that because it has a large Inheritance tree its getting very difficult to Jackson to de serializing it.
is there a why to instruct Jackson by annotation that he will not do automatic serializing and just will do explicit.
I want to ignore all filed and getters/setters except the one that with #JsonProperty annotation
Solved it by using the following config
getMapper().setVisibilityChecker(getMapper().getDeserializationConfig().getDefaultVisibilityChecker()
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE)
.withFieldVisibility(JsonAutoDetect.Visibility.NONE)
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withIsGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withSetterVisibility(JsonAutoDetect.Visibility.NONE));

Deep JSON Serialization of JPA Entities

Using jersey-json POJO serialization feature on a jersey service function to return a JPA entity or a List<?> of that entity, how can I "tell" the serializer to dig deeper when serializing?
In other words, how do I tell the POJO Serializer to do a "Deep Serialization" (or specify the depth of each property) which returns a more detailed JSON with deeper data of the JPA object including it's mappings and relationships with other objects, those annotated with #ManyToOne and #OneToMany?
Without having to use that kind of solution: https://stackoverflow.com/a/10615608/230637
(My problem with the linked solution is the necessity to declare a bloated copy-constructor).
Thanks.

Extra/dupe members in SOAP response prevents serialization (Axis, Java, Gson)

I am using Axis to call a SOAP-based web service. I'm then trying to serialize the returned remote object as JSON, using the Google Gson library.
The serialization to JSON fails, with Gson complaining that "there are multiple elements with the name __equalsCalc()).
When I inspect the returned object in my IDE (Eclipse), I can see that this is true -- the returned object has three members called __equalsCalc() and another three called __hashCode.
I know from looking around that these are added by WSDL2Java (I think) in order to avoid recursion. My question is, why are there THREE of each? And how can I get the serializer to ignore these? They're not actually part of the object's definition (it's called a RemoteProject, for reference). Can I do something hackish like cast the RemoteProject to a RemoteProject to get it to drop those members?
This turns out to be not too hard to solve. I have multiple copies of the same instance var because the class being serialized is shadowing fields of the same name from superclasses. Since I don't care about these fields, the best approach in Gson is to write a custom ExckusionStrategy which can selectively ignore fields based on their name:
http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/ExclusionStrategy.html
I don't know GSon.
With Jackson, you can annotate properties (i.e - fields that have getters/setters according to Java bean convention) with #JsonIgnore.
This way you can prevent issues like recursion/no matching setter or getter and so on...
Try to find out if you have the same at GSon or use Jackson.

Categories