How can we convert the Java's toString() Object representation into a JSON Object
- For better viewing experience, ie. indentation, collapsibility features
An Example Java Custom Object (Of Type Person)
Person(firstName=Duke, lastName=Java, age=19, streetAddress=B'`5 CISF "Mujeri Mor" Faridabad, city=JavaTown, state=JA, postalCode=12345, phoneNumbers=[MobilePhoneNumber(mobile=111-111-1111), HomePhoneNumber(home=222-222-2222)], familyMembers=[Person(firstName=Sister, lastName=Young, age=13, streetAddress=Same Address, city=Same, state=Same, postalCode=Same, phoneNumbers=[MobilePhoneNumber(mobile=111-111-1111), HomePhoneNumber(home=222-222-2222)], familyMembers=null, attributes=null, partner=null), Person(firstName=brother, lastName=Old, age=21, streetAddress=Same Address, city=Same, state=Same, postalCode=Same, phoneNumbers=[MobilePhoneNumber(mobile=111-111-1111), HomePhoneNumber(home=222-222-2222)], familyMembers=null, attributes=null, partner=null)], attributes={key_1=value, key 2=123, brother details=Person(firstName=brother, lastName=Old, age=21, streetAddress=Same Address, city=Same, state=Same, postalCode=Same, phoneNumbers=[MobilePhoneNumber(mobile=111-111-1111), HomePhoneNumber(home=222-222-2222)], familyMembers=null, attributes=null, partner=null)}, partner=Person(firstName=Partner, lastName=Same Age, age=21, streetAddress=Same Address, city=Same, state=Same, postalCode=Same, phoneNumbers=[MobilePhoneNumber(mobile=111-111-1111), HomePhoneNumber(home=222-222-2222)], familyMembers=null, attributes=null, partner=null))
Same Above Java Object, Just Indented Manually for better readability
Person(
firstName=Duke,
lastName=Java,
age=19,
streetAddress=B'`5 CISF, 'Mujeri Mor', Faridabad,
city=JavaTown,
state=JA,
postalCode=12345,
phoneNumbers=[
MobilePhoneNumber(
mobile=111-111-1111
),
HomePhoneNumber(
home=222-222-2222
)
],
familyMembers=[
Person(
firstName=Sister,
lastName=Young,
age=13,
streetAddress=Same Address,
city=Same,
state=Same,
postalCode=Same,
phoneNumbers=[
MobilePhoneNumber(
mobile=111-111-1111
),
HomePhoneNumber(
home=222-222-2222
)
],
familyMembers=null,
attributes=null,
partner=null
),
Person(
firstName=brother,
lastName=Old,
age=21,
streetAddress=Same Address,
city=Same,
state=Same,
postalCode=Same,
phoneNumbers=[
MobilePhoneNumber(
mobile=111-111-1111
),
HomePhoneNumber(
home=222-222-2222
)
],
familyMembers=null,
attributes=null,
partner=null
)
],
attributes={
key_1=value,
key 2=123, brother
details=Person(
firstName=brother,
lastName=Old,
age=21,
streetAddress=Same Address,
city=Same,
state=Same,
postalCode=Same,
phoneNumbers=[
MobilePhoneNumber(
mobile=111-111-1111
),
HomePhoneNumber(
home=222-222-2222
)
],
familyMembers=null,
attributes=null,
partner=null
)
},
partner=Person(
firstName=Partner,
lastName=Same Age,
age=21,
streetAddress=Same Address,
city=Same,
state=Same,
postalCode=Same,
phoneNumbers=[
MobilePhoneNumber(
mobile=111-111-1111
),
HomePhoneNumber(
home=222-222-2222
)
],
familyMembers=null,
attributes=null,
partner=null
)
)
How can we convert the above Java representation of Person Object into a JSON format?
- I have tried recursion to solve it, but the number of edge cases that I would need to handle, and it does not give me full confidence that the code will parse all the Java Objects correctly
.
Is there a way, I don't have to write a custom parser for this?
And if I have to write a parser, what could be a good approach to take (either by doing some pre-processing or post-processing) that I don't have to handle lots of corner cases due to which the code becomes hard to understand and maintain for longer run
CodePudding user response:
convert the Java's toString() Object representation into a JSON Object
You wouldn't.
Just convert the object as is (using Jackson, as an example)
ObjectMapper mapper = new ObjectMapper();
Person p =...;
String json = mapper.writeValueAsString(p);
CodePudding user response:
Google's gson has a pretty similar idea. You can modify the default class to string converter to have a pretty print and specify custom type adapters for fields in the class you would want to process further.
Person duke = new Person("Duke", "Java", 19, " \"name\": \"Bon\", \"streetAddress\": \"355A\" ".getBytes(StandardCharsets.UTF_8));
Gson PrettyGson = new GsonBuilder()
.registerTypeAdapter(byte[].class,
new JsonSerializer() {
@Override
public JsonElement serialize(Object src, Type typeOfSrc, JsonSerializationContext context) {
String byteFieldVal=new String((byte[])src);
//......specify additional transformations including more TypeConverters
return new JsonPrimitive(finalByteFieldVal);
}
})
.setPrettyPrinting().create();
String donaldJson = PrettyGson.toJson(duke);
System.out.println(donaldJson);