I'm pretty new to Jackson
, but I stumbled upon the following problem:
I'd like to serialize a simple object to an array of its fields. So considering the following class:
public class UserModel {
private String id;
private String firstName;
private String lastName;
private String email;
private String company;
}
I get the following json:
{
"id":"cec34b58",
"firstName":"foo",
"lastName":"bar",
"email":"[email protected]",
"company":"FooBar"
}
But what I'd like is the following:
[
"cec34b58",
"foo",
"bar",
"[email protected]",
"FooBar"
]
I'd like to avoid using a custom serializer if there's an easier way. Reading the Jackson Annotations, I don't immediately see something that allows immediate conversion of the model. Google only advises serialization of Java's Collections
to json but nothing to go from a Java Object
to a json array.
This does NOT require custom serializers but simple annotation:
@JsonFormat(shape=JsonFormat.Shape.ARRAY)
public class UserModel {
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With