Gson transform a field into multiple fields

Viewed 880

Say I have serialized the following object as json string:

class Person {
    public final String name;
    public Person(String name) { this.name = name; }
}

Person p = new Person("Bob Falaway");
JsonObject json = gson.toJsonTree(p, Person.class).getAsJsonObject();

Now I want to deserialize it, but I want to split split the name into two fields, firstName and lastName. How do I do this?

I want the end to result in a class similar to:

class RefinedPerson {
    public final String firstName;
    public final String lastName;

    public String toString() { return String.format("%s %s", firstName, lastName); }
}

Is this at all possible with Gson?

1 Answers
Related