I'm using Jackson CSV to parse a CSV file into POJOs, like this.
List<SomeThing> something = csvMapper.readerFor(SomeThing.class).with(emptySchema().withHeader()).readValue(filecsv);
My issue is that I would like wrap my SomeThing list on a root Object, like this.
#JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use =
JsonTypeInfo.Id.NAME)
public class SomeThingWrapper extends BaseDto {
#JsonProperty("something")
private List<something> somethings;
}
So the previous code can be write like this :
SomeThingWrapper somethingWrapper = csvMapper.readerFor(somethingWrapper.class).with(emptySchema().withHeader()).readValue(filecsv);
My SomeThing POJO
#JsonRootName("SomeThing")
public class SomeThing {
#JsonProperty("ID")
private String id;
}
How would you suggest to be the best way to do this?
Related
I'm using jackson csv to parse a csv as pojo
ID,requestId,requestSource,documentIds,trackingIds
documentIds,123456,CLAIMS,123456;223456,
->
#Data
#NoArgsConstructor
#JacksonXmlRootElement(localName = "triggerDocumentExport",namespace = Connection.NAMESPACE_CAS)
public class TriggerDocumentExport {
private Wrapper in;
public TriggerDocumentExport(Wrapper in){this.in = in;}
#Data
public static class Wrapper{
private String requestId,
requestSource;
private List<String> documentIds,
trackingIds;
}
}
I want documentIds to be parsed as a List<String>, with default ArrayElementSeparator(which is ;)
but it keeps parsing it as a single string "123456;223456"
without DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, I got an exception
with the configuration, I got a List with only one element "123456;223456"
If I don't make mistake, should jackson csv support semicolon-seperated list by default?Or there's something I've misunderstood.
I have the following Object:
public class Class_a{
private List<class_b> someList;
}
public class Class_b{
private Map<String,String> someMap;
}
My json will look like this:
"someList":[{"someMap":{"strKey1":"strValue1"}},{"someMap":{"strKey2":"strValue2"}}]
Is it possible to serialize a Json that will look like this, without changing my Objects (and I will have the option to deserialize the Object):
"someList":[{"strKey1":"strValue1"},{"strKey2":"strValue2"}]
*I know that if will defined my object like this:
public class Class_a{
private List<Map<Strung,String>> someList;
}
i will get a Json like I want - but I am trying to find more elegant solution then 'list' that contain a 'map'
My project use spring framework and Jackson.
This worked for me I only had to add getters and setters to your classes and I was able to parse with jackson:
#Test
public void t() throws IOException {
String json = "{\"someList\":[{\"someMap\":{\"strKey1\":\"strValue1\"}},{\"someMap\":{\"strKey2\":\"strValue2\"}}]}";
Class_a a = new ObjectMapper().readValue(json, Class_a.class);
System.out.println(a);
}
#Getter
#Setter
public class Class_a{
private List<Class_b> someList;
}
#Getter
#Setter
public class Class_b{
private Map<String,String> someMap;
}
I'm using lombok but that's nothing special, you can create the getters/setters manually too and will work
I have a POJO with JsonPropertyOrder defined, in other class I need to retrieve that order for that POJO, how can I do do this?
Example POJO:
#JsonPropertyOrder({"field1", "field2", "field3"})
public class ReportRow extends Row {
private String field1;
private String field2;
private String field3;
}
Can I try something like this: ReportRow.getJsonPropertyOrder()?
You're on the right track, you can get annotation information for the class and then access it's properties:
JsonPropertyOrder jpo = ReportRow.class.getAnnotation(JsonPropertyOrder.class);
String[] propertyOrder = jpo.value();
In this case you can find the taget attribute in the Jackson JsonPropertyOrder.java source.
Adding to the answer provided, you can get to the annotation through Jackson:
ObjectMapper mapper = new ObjectMapper();
JavaType type = mapper.getTypeFactory().constructType(ReportRow.class);
BeanDescription desc = mapper.getSerializationConfig().introspect(type);
JsonPropertyOrder jpo = desc.getClassAnnotations().get(JsonPropertyOrder.class);
I have my class MyClass that can be seralized/deserialized to/from string.
I looking for a simplest way use it as a hibernate property and serialize using jadira.
class MyClass {
#ToString
public String toString() {}
#fromString
public static MyClass fromString(String encoded) {}
}
And then, use it in an entity
#Entity
class MyEntity {
#Type(type="org.jadira.usertype....")
private MyClass field;
}
My question is what should I write inside #Type?
Thank you.
Try something like:
#TypeDef(name="BoundType", typeClass="org.jadira.usertype.bindings.PersistentBoundClass")
....
#Type(type = "BoundType", parameters = { #Parameter(name="javaClass", value="MyClass"), #Parameter(name="hibernateClass", value="java.lang.String.class")})
I have this specific problem with JSON deserialization. Let's have this JSON structure:
{
"header":{
"objects":[
{"field":"value1"},
{"field":"value2"}
]
}
}
The JSON structure can't be altered as it comes from a 3rd party system.
Now let's have this simple POJO:
#JsonDeserialize(using=PojoDeserializer.class)
public class Pojo {
private string field;
//...getter, setter
}
The mentioned PojoDeserializer takes {"field": "value"} json string and deserializes it to the Pojo instance. So I can simply do the deserialization like this
Pojo instance = new
ObjectMapper().readValue("{\"field\":
\"value\"}", Pojo.class);
And here's my problem. Let's have another deserializer PojosCollectionDeserializer which takes the mentioned structure and deserializes it to a Collection of Pojo instances. I'd like to use it in a similar fashion as in the previous example:
Collection<Pojo> pojos = new ObjectMapper().readValue("{...}", Collection.class);
But this doesn't work as there is not defined that Collection should be created using the PojosCollectionDeserializer. Is there any way to achieve it?
I am not sure why are trying to explicitly specify deserializers, as it would all work just fine with something like:
public class Message {
public Header header; // or, if you prefer, getter and setter
}
public class Header {
public List<Pojo> objects;
}
public class Pojo {
public String field;
}
Message msg = objectMapper.readValue(json, Message.class);
without any additional configuration or annotations. There is no need to construct custom serializers or deserializers for simple cases like this.