How do I get Jackson to treat 'name' as if it had a #JsonProperty annotation?
public class SimpleClass {
private String name;
private String doNotSerialize;
public SimpleClass( #JsonProperty("name") String name ) {
this.name = name;
}
public String getName() {
return name;
}
public int getSum() {
return 1+1;
}
}
The way it is now, I get an error, Unrecognized field "sum", because it treats every getter as a serializable property.
If I add a class annotation:
#JsonAutoDetect( getterVisibility = JsonAutoDetect.Visibility.NONE )
I get an empty string when serializing. I was hoping that Jackson would see the #JsonProperty on the constructor parameter and figure it out.
If I change the class annotation to:
#JsonAutoDetect( getterVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.ANY )
Then I get the 'doNotSerialize' field included.
If I set a #JsonCreator on the constructor, and change my autodetect, I still get a blank string:
#JsonAutoDetect( getterVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE, creatorVisibility = JsonAutoDetect.Visibility.ANY )
public class SimpleClass {
private String name;
private String doNotSerialize;
#JsonCreator
public SimpleClass( #JsonProperty("name") String name ) {
this.name = name;
}
public String getName() {
return name;
}
public int getSum() {
return 1+1;
}
}
What I'm hoping is that somehow I can tell Jackson to treat all the constructor parameters as serializable fields, and all other fields / setters as non-serializable.
You can use a filter to only serialise getters which have a matching field, e.g.
package org.example;
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.PropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import java.io.IOException;
import java.io.StringWriter;
public class App {
#JsonFilter("test")
public static class SimpleClass {
private String name;
private String doNotSerialize;
public SimpleClass(String name ) {
this.name = name;
}
public String getName() {
return name;
}
public int getSum() {
return 1+1;
}
}
public static void main( String[] args ) throws IOException {
SimpleFilterProvider filterProvider = new SimpleFilterProvider();
filterProvider.addFilter("test", new SimpleBeanPropertyFilter() {
#Override
protected boolean include(BeanPropertyWriter writer) {
return super.include(writer);
}
#Override
protected boolean include(PropertyWriter writer) {
String name = writer.getName();
Class clazz = writer.getMember().getDeclaringClass();
try {
clazz.getDeclaredField(name);
return super.include(writer);
} catch (NoSuchFieldException e) {
// ignore
return false;
}
}
});
ObjectMapper mapper = new ObjectMapper();
mapper.setFilterProvider(filterProvider);
StringWriter sw = new StringWriter();
mapper.createGenerator(sw).writeObject(new SimpleClass("foo"));
System.out.println(sw.toString());
}
}
I don't know your full requirements, but this should be a start.
I haven't tried to do what you actually, asked, that is, look at constructor parameters, but that should be possible too.
If you want "sum" to be included in the serializad json but want to ignore it when deserializing you can do:
#JsonIgnoreProperties(ignoreUnknown=true)
public class SimpleClass {
// properties/getters
public int getSum() { return 1+1; }
}
If you want to remove "sum" entirely from the json you can do
#JsonIgnoreProperties({"sum"})
public class SimpleClass {
// properties/getters
public int getSum() { return 1+1; }
}
or
public class SimpleClass {
// properties/getters
#JsonIgnore
public int getSum() { return 1+1; }
}
Related
I have a problem related to:
De-serializing JSON to polymorphic object model using Spring and JsonTypeInfo annotation
com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id '[' as a subtype
The solutions provided there didn't work for me.
I have the following DTO:
public class QuestionaireAnswersDTO {
#JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = As.EXISTING_PROPERTY)
#JsonSubTypes({
#JsonSubTypes.Type(name = "single", value = SingleChoiceAnswerDTO.class),
#JsonSubTypes.Type(name = "multi", value = MultipleChoiceAnswerDTO.class)
})
public static abstract class QuestionaireAnswerDTO {
String answerId;
String name;
public String getAnswerId() {
return answerId;
}
public void setAnswerId(String answerId) {
this.answerId = answerId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
String questionnaireId;
List<QuestionaireAnswerDTO> answers;
public String getQuestionnaireId() {
return questionnaireId;
}
public void setQuestionnaireId(String questionnaireId) {
this.questionnaireId = questionnaireId;
}
public List<QuestionaireAnswerDTO> getAnswers() {
return answers;
}
public void setAnswers(List<QuestionaireAnswerDTO> answers) {
this.answers = answers;
}
with those subclasses:
public static class SingleChoiceAnswerDTO extends QuestionaireAnswerDTO {
#Nullable
String selectedOption;
public String getSelectedOption() {
return selectedOption;
}
public void setSelectedOption(String selectedOption) {
this.selectedOption = selectedOption;
}
}
public static class MultipleChoiceAnswerDTO extends QuestionaireAnswerDTO {
List<String> selectedOptions;
public List<String> getSelectedOptions() {
return selectedOptions;
}
public void setSelectedOptions(List<String> selectedOptions) {
this.selectedOptions = selectedOptions;
}
}
Now I wanted to write a test using this json object:
{
"questionnaireId":"questionnaire1",
"answers":[
{
"name":"single",
"answerId":"Question1",
"selectedOption":"Yes"
},
{
"name":"multi",
"answerId":"Question3",
"selectedOptions":[
"yes",
"no"
]
}
]
}
Using this test:
JsonFactory factory = new JsonFactory();
factory.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
ObjectMapper mapper = new ObjectMapper(factory);
mapper.registerSubtypes(QuestionaireAnswersDTO.SingleChoiceAnswerDTO.class, QuestionaireAnswersDTO.MultipleChoiceAnswerDTO.class);
QuestionaireAnswersDTO result = mapper.readValue(testData, QuestionaireAnswersDTO.class);
String resultAsString = mapper.writeValueAsString(result);
System.out.println(resultAsString);
Which results in the following Error:
com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve subtype of [simple type, (...)
missing type id property '#class' (for POJO property 'answers')
Using the .registerSubtypes() method instead of JsonSubtypes didn't work here instead of JsonSubtypes. Same error occurs.
I am working with a java example using annotations, I created a simple POJO (java bean) using annotations to its attributes. I want to have the ability to create new objects of this type and retrieve the values of its attributes using the annotations created.
My POJO :
import java.io.Serializable;
import annotations.BusinessObject;
import annotations.BusinessObjectAttribute;
import annotations.BusinessObjectName;
import annotations.BusinessObjectPolicy;
import annotations.BusinessObjectRevision;
import annotations.BusinessObjectVault;
#BusinessObject
public class IndusTask implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
// Mandatory to create new object !
#BusinessObjectName
private String taskName;
#BusinessObjectRevision
private String taskRevision;
#BusinessObjectVault
private String vault;
// Mandatory to invoke iTask.create(context, policy) in Database
#BusinessObjectPolicy
private String policy;
//Specific attributes
#BusinessObjectAttribute
private String taskDescription;
#BusinessObjectAttribute
private String creationDate;
#BusinessObjectAttribute
private Integer weight;
public IndusTask() {
}
public IndusTask(String taskName, String taskRevision, String vault, String policy, String taskDescription,
String creationDate, Integer weight) {
super();
this.taskName = taskName;
this.taskRevision = taskRevision;
this.vault = vault;
this.policy = policy;
this.taskDescription = taskDescription;
this.creationDate = creationDate;
this.weight = weight;
}
public String getTaskName() {
return taskName;
}
public void setTaskName(String taskName) {
this.taskName = taskName;
}
public String getTaskRevision() {
return taskRevision;
}
public void setTaskRevision(String taskRevision) {
this.taskRevision = taskRevision;
}
public String getVault() {
return vault;
}
public void setVault(String vault) {
this.vault = vault;
}
public String getTaskDescription() {
return taskDescription;
}
public void setTaskDescription(String taskDescription) {
this.taskDescription = taskDescription;
}
public String getCreationDate() {
return this.creationDate;
}
public void setCreationDate(String creationDate) {
this.creationDate = creationDate;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
public String getPolicy() {
return policy;
}
public void setPolicy(String policy) {
this.policy = policy;
}
}
Example of attributes' declaration:
*Business Object Type declaration
package annotations;
import java.lang.annotation.*;
//#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
public #interface BusinessObject {
}
*Business Object Name Attribute:
package annotations;
import java.lang.annotation.*;
//#Target(ElementType.FIELD)
#Retention(RetentionPolicy.RUNTIME)
public #interface BusinessObjectName {
}
I Created a main to test if all the annotations are detected:
public class MainImpl {
public static void main(String[] args) {
// TODO Auto-generated method stub
IndusTask myTask = new IndusTask("mytstTask", "001", "eService Production", "TstTask Process",
"myTstTask Description", "2018/02/16#15:30:10:GMT", 200);
System.out.println(myTask.getClass().getAnnotations().length);
}
}
Output is displaying 1 ! so only the first annotation is detected !
I was told also that the object attributes values can be accessed using these annotation (something similar to) :
object.getClass().getAnnotations()
How can i do ?
You need to iterate through the fields, get their annotations and set the value wherever the annotation matches (it can match multiple fields):
#Retention(RetentionPolicy.RUNTIME)
public #interface Field1 {}
#Retention(RetentionPolicy.RUNTIME)
public #interface Field2 {}
public static class UnderTest {
#Field1
private String field1;
#Field2
private int field2;
public UnderTest(String field1, int field2) {
this.field1 = field1;
this.field2 = field2;
}
#Override
public String toString() {
return field1 + "=" + field2;
}
}
public static void setter(Object obj, Class<? extends Annotation> fieldAnnotation, Object fieldValue) throws IllegalAccessException {
for (Field field: obj.getClass().getDeclaredFields()) {
for (Annotation annot: field.getDeclaredAnnotations()) {
if (annot.annotationType().isAssignableFrom(fieldAnnotation)) {
if (!field.isAccessible()) {
field.setAccessible(true);
}
field.set(obj, fieldValue);
}
}
}
}
public static void main(String[] argv) throws IllegalAccessException {
UnderTest underTest = new UnderTest("A", 1);
System.out.println(underTest);
setter(underTest, Field1.class, "B");
setter(underTest, Field2.class, 2);
System.out.println(underTest);
}
Running this prints
A=1
B=2
Sounds like you're after the annotations on the fields too?
E.g. for the first private field:
myTask.getClass().getDeclaredFields()[0].getAnnotations()
Note depending how you're accessing a private field, you will sometimes also need to first ensure it is accessible:
...getDeclaredFields()[0].setAccessible(true);
[edit]
The values are reachable too from the fields. A basic worked example:
for (Field f : myTask.getClass().getDeclaredFields()) {
f.setAccessible(true);
System.out.println(f.getName() + "=" + f.get(myTask));
System.out.println(" annotations=" + java.util.Arrays.toString(f.getAnnotations()));
}
When I serialize/deserialize any object, all field names are converted to lower case.
Is there any configuration to set that makes Jackson keep the field names exactly as they are? Both for serializing and deserializing?
(I know about #JsonProperty, but this does not seems to be right, since what I need is just for Jackson to respect what already exists)
My test code:
import java.io.Serializable;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
public class Test {
static class Example implements Serializable {
private String Test;
private String ABC;
private String XyZ;
public String getTest() { return Test; }
public void setTest(String test) { Test = test; }
public String getABC() { return ABC; }
public void setABC(String abc) { ABC = abc; }
public String getXyZ() { return XyZ; }
public void setXyZ(String xyz) { XyZ = xyz; }
}
static class MyPropertyNamingStrategy extends PropertyNamingStrategy {
#Override
public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
return convert(defaultName);
}
#Override
public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
return convert(defaultName);
}
#Override
public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
return convert(defaultName);
}
private String convert(String input) {
return input;
}
}
public static void main(String[] args) throws Exception {
ObjectMapper objectMapper = new ObjectMapper()
.setPropertyNamingStrategy(new MyPropertyNamingStrategy())
.enable(SerializationFeature.INDENT_OUTPUT)
.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
//From OBJECT to JSON
Example ex = new Example();
ex.setTest("1");
ex.setABC("2");
ex.setXyZ("3");
System.out.println(objectMapper.writeValueAsString(ex));
//FROM JSON to OBJECT
String jsonString = "{ \"Test\":\"0\", \"ABC\":\"1\", \"XyZ\":\"2\" }";
Example fEx = objectMapper.readValue(jsonString, Example.class);
}
}
Thanks to #BlueLettuce16, I have managed to build an 'improved' version of the PropertyNamingStrategy. Here it is:
import java.lang.reflect.Modifier;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
public class CustomPropertyNamingStrategy extends PropertyNamingStrategy {
#Override
public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
return convertForField(defaultName);
}
#Override
public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
return convertForMethod(method, defaultName);
}
#Override
public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
return convertForMethod(method, defaultName);
}
private String convertForField(String defaultName) {
return defaultName;
}
private String convertForMethod(AnnotatedMethod method, String defaultName) {
if (isGetter(method)) {
return method.getName().substring(3);
}
if (isSetter(method)) {
return method.getName().substring(3);
}
return defaultName;
}
private boolean isGetter(AnnotatedMethod method) {
if (Modifier.isPublic(method.getModifiers()) && method.getGenericParameterTypes().length == 0) {
if (method.getName().matches("^get[A-Z].*") && !method.getGenericReturnType().equals(void.class))
return true;
if (method.getName().matches("^is[A-Z].*") && method.getGenericReturnType().equals(boolean.class))
return true;
}
return false;
}
private boolean isSetter(AnnotatedMethod method) {
return Modifier.isPublic(method.getModifiers()) && method.getGenericReturnType().equals(void.class) && method.getGenericParameterTypes().length == 1
&& method.getName().matches("^set[A-Z].*");
}
}
Even though #JsonProperty doesn't work, I was able to use #JsonSetter and #JsonGetter to map capitalized json field names.
#JsonSetter("ABC")
public void setABC(String ABC) {
this.ABC= ABC;
}
Spring will now serialize the object field as "ABC" and not "abc".
I've had the same problem.
This is my solution:
public class MyNamingStrategy extends PropertyNamingStrategy {
#Override
public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
return field.getName();
}
#Override
public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
return convert(method, defaultName);
}
#Override
public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
return convert(method, defaultName);
}
private String convert(AnnotatedMethod method, String defaultName) {
Class<?> clazz = method.getDeclaringClass();
List<Field> flds = FieldUtils.getAllFieldsList(clazz);
for (Field fld : flds) {
if (fld.getName().equalsIgnoreCase(defaultName)) {
return fld.getName();
}
}
return defaultName;
}
}
In this case you will get the exact name of the property, and will not have to depend on the correct names of the methods.
I think that this is the solution (using custom PropertyNamingStrategy):
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
public class MyPropertyNamingStrategy extends PropertyNamingStrategy {
#Override
public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
return convert(field.getName());
}
#Override
public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
return convert(method.getName().toString());
}
#Override
public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
return convert(method.getName().toString());
}
private String convert(String input) {
return input.substring(3);
}
}
Test
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.IOException;
import java.io.StringWriter;
public class MyPropertyNamingStrategyTest {
public static void main(String[] args) {
PrivatePerson privatePerson = new PrivatePerson();
privatePerson.setFirstName("John");
privatePerson.setLastName("Smith");
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(new MyPropertyNamingStrategy());
mapper.enable(SerializationFeature.INDENT_OUTPUT);
StringWriter sw = new StringWriter();
try {
mapper.writeValue(sw, privatePerson);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(sw.toString());
}
}
PrivatePerson
public class PrivatePerson {
private String firstName;
private String lastName;
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
}
You can configure Jackson to be case sensitivity tolerant:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
Cudos to https://stackoverflow.com/a/32842962/1639556
Using #JsonProperty annotation. It work well
Example
#JsonProperty("Code")
private String Code;
#JsonProperty("Message")
private String Message;
Created a own class for PropertyNamingStrategy- As per Answer 7 Working fine
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
public class MyPropertyNamingStrategy extends PropertyNamingStrategy {
#Override
public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
return convert(field.getName());
}
#Override
public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
return convert(method.getName().toString());
}
#Override
public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
return convert(method.getName().toString());
}
private String convert(String input) {
return input.substring(3);
}
}
And I have my POJO class - the Payload Class:
package orderCreateAPI;
import java.util.ArrayList;
public class Payload {
OrderInfo OrderInfo;
ArrayList<orderCreateAPI.ShipmentInfo> ShipmentInfo;
public Payload(OrderInfo order, ArrayList<orderCreateAPI.ShipmentInfo> shipInfo){
this.OrderInfo =order;
this.ShipmentInfo = shipInfo;
}
public OrderInfo getOrderInfo() {
return OrderInfo;
}
public void setOrderInfo(OrderInfo orderInfo) {
OrderInfo = orderInfo;
}
public ArrayList<orderCreateAPI.ShipmentInfo> getShipmentInfo() {
return ShipmentInfo;
}
public void setShipmentInfo(ArrayList<orderCreateAPI.ShipmentInfo> shipmentInfo) {
ShipmentInfo = shipmentInfo;
}
}
The execution class:
public class TC1_CreateOrder extends orderCreateRequest{
#Test
public static void TC1_CreateOrder() throws JsonProcessingException,JsonMappingException,IOException {
//throws JsonParseException,JsonMappingException,IOException
//Data fetch
ArrayList<OrderReferences> orRef = new ArrayList<OrderReferences>();
orRef.add(new OrderReferences("BM","IFC"));
ArrayList<OrderItem> orItem = new ArrayList<OrderItem>();
orItem.add(new OrderItem("AOTEST1001","60111"));
ShipperInfo ship = new ShipperInfo("URBN","URBN PA DC");
ArrayList<ShipmentInfo> ShipInfo = new ArrayList<ShipmentInfo>();
ShipInfo.add(new ShipmentInfo("ASTEST1001","RCVD"),ship, orItem));
ConsigneeInfo ConsigneeInfo = new ConsigneeInfo("Green Mile","133 Avenue");
OrderInfo OrderInfo = new OrderInfo("AOTEST1001", "2021-09-03T",orRef, ConsigneeInfo);
Payload p = new Payload(OrderInfo,ShipInfo);
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(new MyPropertyNamingStrategy());
StringWriter s = new StringWriter();
try {
mapper.writeValue(s, p);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Response body before: -Wrong properties
{
"orderInfo": {
"orderNumber": "AOTEST1010",
"orderCreatedDate": "2021-09-03T00:00:00.000Z"
}
}
Response body after: -correct properties
{
"OrderInfo": {
"OrderNumber": "AOTEST1010",
"OrderCreatedDate": "2021-09-03T00:00:00.000Z"
}
}
I'm being given a Json file with the form:
{
"descriptions": {
"desc1": "someString",
"desc2": {"name":"someName", "val": 7.0}
}
}
I have the POJO:
public class CustomClass {
Map<String, Object> descriptions;
public static class NameVal{
String name;
double val;
public NameVal(String name, double val){...}
}
}
I can recreate the json file with the code:
CustomClass a = new CustomClass();
a.descriptions = new HashMap<String, Object>();
a.descriptions.put("desc1", "someString");
a.descriptions.put("desc2", new CustomClass.NameVal("someName", 7.0));
new ObjectMapper().writeValue(new File("testfile"), a);
But, when I read the object back in using:
CustomClass fromFile = new ObjectMapper().readValue(new File("testfile"), CustomClass.class);
then fromFile.descriptions.get("desc2") is of type LinkedHashMap instead of type CustomClass.NameVal.
How can I get Jackson to properly parse the type of the CustomClass.NameVal descriptors (other than making some class that wraps the parsing and explicitly converts the LinkedHashMap after Jackson reads the file)?
Try this. Create a class Description with name and value attributes:
public class Description {
private String name;
private double val;
}
Now in your CustomClass do this:
public class CustomClass {
List<Description> descriptions;
}
And that's it. Remember to create getters and setters because Jackson needs it.
You could try something like this:
public class DescriptionWrapper {
private Description descriptions;
public Description getDescriptions() {
return descriptions;
}
public void setDescriptions(Description descriptions) {
this.descriptions = descriptions;
}
}
public class Description {
private String desc1;
private NameValue desc2;
public String getDesc1() {
return desc1;
}
public void setDesc1(String desc1) {
this.desc1 = desc1;
}
public NameValue getDesc2() {
return desc2;
}
public void setDesc2(NameValue desc2) {
this.desc2 = desc2;
}
}
public class NameValue {
private String name;
private double val;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getVal() {
return val;
}
public void setVal(double val) {
this.val = val;
}
}
I have an issue. I want to convert an object into another object using JAXB. As in, I have a class com.home.Student, and another class com.school.Student, both have same arguments, in fact both are same (copy paste), but different package. I want to perform the conversion between them using JAXB.
How to do that, please help me.
It would be nice if you included some code that explains your problem.
JAXB 101 says you should place the right annotations, then you can serialize and deserialize correctly. You should properly annotate your classes with #XmlRootElement, #XmlElement, #XmlAttribute, etc
For example:
#XmlRootElement(name="student")
#XmlAccessorType(XmlAccessType.NONE)
class Student {
#XmlElement(name="name")
private String name;
#XmlElement(name="age")
private int age;
public Student() {
}
public String getName() { return name; }
public int getAge() { return age; }
}
Then you can use serialize it using JAXB Marshaller:
StringWriter writer = new StringWriter();
JAXBContext context = JAXBContext.newInstance(Student.class);
Marshaller m = context.createMarshaller();
m.marshal(student, writer);
And deserialize it as well by Unmarshelling the input ..
JAXBContext context = JAXBContext.newInstance(Student.class);
Unmarshaller m = context.createUnmarshaller();
return (Student)m.unmarshal(new StringReader(input));
Make sure you look at the JavaDoc I mentioned above since there are many ways to do so.
If you cannot modify your classes, you can still use JAXB (or you can use XStream) Assuming your class is the following:
class Student {
private String name;
private int age;
public Student() {
}
public void setName(String name) { this.name = name; }
public String getName() { return name; }
public void setAge(int age) { this.age = age; }
public int getAge() { return age; }
}
You can serialize it by doing:
Student student = new Student();
student.setAge(25);
student.setName('FooBar');
StringWriter writer = new StringWriter();
JAXBContext context = JAXBContext.newInstance(Student.class);
Marshaller m = context.createMarshaller();
m.marshal(new JAXBElement(new QName(Student.class.getSimpleName()), Student.class, student), writer);
System.out.println(writer.toString());
If you are using XStream, you can do the serialization without Annotations too (and it is more controllable).
http://x-stream.github.io/tutorial.html
You could do the following.
Note:
It does not require that you ever materialize the data as XML, by leveraging JAXBSource.
It does not require any annotations on your object model.
com.home.Student
package com.home;
public class Student {
private String name;
private Status status;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
}
com.home.Status
package com.home;
public enum Status {
FULL_TIME("F"),
PART_TIME("P");
private final String code;
Status(String code) {
this.code = code;
}
public String getCode() {
return code;
}
}
com.school.Student
package com.school;
public class Student {
private String name;
private Status status;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
}
com.school.Status
package com.school;
public enum Status {
FULL_TIME("F"),
PART_TIME("P");
private final String code;
Status(String code) {
this.code = code;
}
public String getCode() {
return code;
}
}
com.example.Demo;
package com.example;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.util.JAXBSource;
import javax.xml.namespace.QName;
public class Demo {
public static void main(String[] args) throws Exception {
com.home.Student studentA = new com.home.Student();
studentA.setName("Jane Doe");
studentA.setStatus(com.home.Status.FULL_TIME);
JAXBContext contextA = JAXBContext.newInstance(com.home.Student.class);
JAXBElement<com.home.Student> jaxbElementA = new JAXBElement(new QName("student"), com.home.Student.class, studentA);
JAXBSource sourceA = new JAXBSource(contextA, jaxbElementA);
JAXBContext contextB = JAXBContext.newInstance(com.school.Student.class);
Unmarshaller unmarshallerB = contextB.createUnmarshaller();
JAXBElement<com.school.Student> jaxbElementB = unmarshallerB.unmarshal(sourceA, com.school.Student.class);
com.school.Student studentB = jaxbElementB.getValue();
System.out.println(studentB.getName());
System.out.println(studentB.getStatus().getCode());
}
}
If your goal is simply to convert (assign actually) between the two, and they're identical except package name, I would think you could use simple reflection. Just iterate over the fields of source object, and assign to the field of the same name in the target object. Vaguely, like this:
import java.lang.reflect.Field;
public class Converter {
public void convert (com.home.Student src, com.school.Student dst) throws Exception {
for (Field f : src.getFields()) {
// src field name
String name = f.getName();
// get corresponding field in dst
Field dstField = dst.getDeclaredField(name);
dstField.set(dst, f.get());
}
}
}
Note: I didn't compile this, which is why I say "vaguely". You'll need to use the Field.isAccessible()/Field.setAccessible(true) if the fields are private, so that you can temporarily change accessibility while you're assigning values. Or, you an write slightly more complex code that uses public setters/getters rather than directly using field access.