JsonIgnore & XmlElement annotations conflict - java

I've faced issue to combine JAXB and Jackson annotation together:
public class Document {
String someField;
#JsonIgnore
#XmlElementWrapper(name = "someWrapper")
#XmlElement(name = "someElement")
List<String> someCollection;
}
I need to be able to marshall and unmarshall field 'someCollection' to xml, but to have the opportunity to serialize 'Document' object to json without such field.
But this field appears in final json string

So, if I've understood right - Jackson sees both JsSON and XML annotations. So I can not force to serialize something and do not serialize in the same moment.
It is not possible, I think

Related

Java InfluxDB: Map tags as well as fields in POJO from query

Using the influxdb library in Java I'm trying to map the results of a query formed using the QueryReactiveAPI query builder to a POJO so I can store them for processing. I get that you need to use the #Measurement annotation on the class definition and the #Column annotation on class variables to map fields but is there the equivalent for tags?
Thanks.
You can also use the #Column annotation for tags, simply include tag = true in the annotation, for example:
#Column(tag = true)
String location;
Source: https://github.com/influxdata/influxdb-client-java#writes-and-queries-in-influxdb-2x

Convert XML to JSON with different property names using Jackson

I have the next task: read XML file from some directory and convert it to JSON string.
The problem: initial XML and JSON have different names for corresponding properties, e.g. x_date in XML and j_date in JSON.
I have created the class with required field for JSON with such annotations:
public class Card {
#JacksonXmlProperty(localName = "x_date")
#JsonProperty("j_date")
private String date;
// other fields
I have tried to serialize/deserialize test XML file, and it's seems work correctly.
But I'm not sure that is ok to annotate fields with #JacksonXmlProperty and #JsonProperty annotations at the same time. Maybe it's better to create one class per XML part and one for the JSON and transfer the data between them some mapper (e.g. Orika)?
Any suggestions?
Finally solved this by splitting logic in two separate classes: Card.class for XML data with help of #JacksonXmlProperty annotation and CardDto.class which uses #JsonProperty. Mapping between these classes is handled by Orika mapper.
This split will ease further customization of both classes and will allow add new functionality (e.g. persist data to the database using new entity class).

customized property name using jaxb annotations

I have a pojo definition like following:
class Pojo<T>
{
private T entity;
//setters and getters;
}
I want to serialize this pojo into json using jackson and jaxb annotations. I want the property name in json to be the type of T not the name "entity". I tried few things with jaxb annotations but nothing worked as expected.
Any help would be much appreciated.

Validating JSON inside a POJO

What is the best/preferred way to validate JSON using annotations inside a POJO?
I would like to be able to distinguish between optional and required fields of a POJO.
I would like to be able to provide default values for required fields of a POJO.
Example:
#JsonTypeInfo(use=Id.NAME, include = As.WRAPPER_OBJECT)
#JsonTypeName("Foo")
public class MyClass{
#JsonProperty
private String someOptionalField;
#JsonProperty
private String someRequiredField;
#JsonProperty
private String someRequiredFieldThatIsNotNull;
#JsonProperty
private int someRequiredFieldThatIsGreaterThanZero;
// etc...
}
A possible approach is to deserialize JSON into an object and validate an object with validation API #MattBall linked. The advantage is that this logic is being decoupled from storage logic and you are free to change your storage logic with no need to reimplement validation.
If you want to validate JSON, you might want to have a look at JSON schema.

Tell JAXB to unmarshal <xs:dateTime> to Date class using annotations

When using JAXB with Java-First, fields/properties of type java.util.Date are marshalled and unmarshalled as xs:dateTime and everything works as expected.
But if the type of the field/property is Object, JAXB unmarshals xs:dateTimeto XMLGregorianCalendarImpl.
I need to find a way that makes JAXB unmarshal date-time values to java.util.Date by itself using annotations. Otherwise, I'll need to go through all unmarshalled values in each use case.
Even if there were some after-unmarshall-hook to define on the classes containing Object fields and convert the instances manually would be good. But I couldn't find anything that can be used this way either.
Note that I have limited access to the JAXB context, as it is used inside Apache CXF.
In addition to Blaise Doughan's answer:
I could finally figure this out, thanks for help from Blaise Doughan. Actually his answer works with just a small change: if there's several types expected to be unmarshalled as the Object property, there needs to be multiple #XmlElement annotations placed on it using #XmlElements annotation.
Here's my code now:
#XmlElements
({
#XmlElement(name = "dateValue", type = Date.class),
#XmlElement(name = "stringValue", type = String.class),
#XmlElement(name = "booleanValue", type = Boolean.class),
#XmlElement(name = "listValue", type = ArrayList.class),
#XmlElement(name = "bytesValue", type = Byte[].class)
})
public Object getFieldValue()
{
return fieldValue;
}
Note: specifying "name" is required for this to work, since there should be a way for the marshaller / unmarshaller to identify the type of the content.
There are two minor issues here:
You need to specify a list of all of the types expected (which is logical, given the case of marshalling)
There's no way to specify a single name for this property. In my case, where JAXB is used in CXF web services, code generated from WSDL in .NET names this field as "Item". If there was a way, for example, to wrap the XML elements in another one which has a single name, the generated code could be a little bit nicer.
You can set the type property on #XmlElement.
#XmlElement(type=Date.class)
public Object getGenericDateProperty() {
return date;
}
Edit:
Since you don't know the type you could use an XmlAdapter. If the unmarshalled value is an XMLGregorianCalendar convert it to a Date. For more info on XmlAdapter see:
http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html

Categories