Can anyone help me out on this one? I believe I am successfully converting the object from one type to another - all fields are equal. But I cannot get the first criteria to pass. Any ideas?
package com.codegym.task.task33.task3304;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.StringWriter;
/*
Using JSON to convert from one class to another
*/
public class Solution {
public static void main(String[] args) throws IOException {
Second s = (Second) convertOneToAnother(new First(), Second.class);
First f = (First) convertOneToAnother(new Second(), First.class);
}
public static Object convertOneToAnother(Object one, Class resultClassObject) throws IOException {
ObjectMapper mapper = new ObjectMapper();
StringWriter writer = new StringWriter();
mapper.writeValue(writer, one);
String input = writer.toString().replace(one.getClass().getSimpleName().toLowerCase(),resultClassObject.getSimpleName().toLowerCase());
return mapper.readValue(input,resultClassObject);
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property="className")
@JsonSubTypes(@JsonSubTypes.Type(value=First.class, name="first"))
public static class First {
public int i;
public String name;
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property="className")
@JsonSubTypes(@JsonSubTypes.Type(value=Second.class, name="second"))
public static class Second {
public int i;
public String name;
}
}