Reputation: 1263
I want to convert Java object to JSON string. Lets say the object looks like this:
public class User{
private Long id;
private String firstName;
}
And the json should look like this:
{
"inputFields":[
{
"name":"id",
"value":"123"
},
{
"name":"firstName",
"value":"George"
}
]
}
Tried to use Jackson, but it looks like it doesn't provide such kind of serialization.
Upvotes: 2
Views: 3162
Reputation: 2108
Jackson can use custom serializer where you can have control in generating the output. The following are the step to do this:
Annotate your POJO to use a custom serializer
@JsonSerialize(using = CustomSerializer.class)
static class User{
public Long id;
public String firstName;
public User(Long id, String firstName) {
this.id = id;
this.firstName = firstName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
Declare the serialzer
static class CustomSerializer extends JsonSerializer{
@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider serializers) throws IOException, JsonProcessingException {
jgen.writeStartObject();
jgen.writeArrayFieldStart("inputFields");
Field[] fields = value.getClass().getDeclaredFields();
for (Field field : fields) {
try {
field.setAccessible(true);
jgen.writeStartObject();
jgen.writeObjectField("name", field.getName());
jgen.writeObjectField("value", field.get(value));
jgen.writeEndObject();
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
jgen.writeEndArray();
jgen.writeEndObject();
}
}
A simple test
public static void main(String[] args) throws JsonProcessingException {
User user = new User(1L, "Mike");
ObjectMapper om = new ObjectMapper();
om.writeValueAsString(user);
System.out.println(om.writeValueAsString(user));
}
And the output will be
{"inputFields":[{"name":"id","value":1},{"name":"firstName","value":"Mike"}]}
Upvotes: 2
Reputation: 1
Just a example of stepanian's answer, but i prefer dumitru's way to implement a CustomSerializer.
create a InputField class
public class InputField {
private String name;
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
parse User
public static List<InputField> parseInputFields(Object model) {
List<InputField> inputFields = new ArrayList<InputField>();
Field[] field = model.getClass().getDeclaredFields();
try {
for (int j = 0; j < field.length; j++) {
InputField inputField = new InputField();
inputField.setName(field[j].getName());
inputField.setValue(String.valueOf(field[j].get(model));
inputFields.add(inputField);
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return inputFields;
}
test
public static void main(String[] args) throws JsonProcessingException {
User user = new User(1L, "Mike");
Map<String, Object> tmpMap=new HashedMap();
tmpMap.put("inputFields", parseInputFields(user));
ObjectMapper om = new ObjectMapper();
System.out.println(om.writeValueAsString(tmpMap));
}
Upvotes: 0
Reputation: 11433
Create an InputFields
class and move the User
object data to it before converting to json.
Edit: The InputFields class would obviously have the same structure as the desired json result.
Upvotes: 0