static class DataObject {
private String access;
private String foo;
private Int age;
}
to {"access" : "value","foo":"value","age":"value"}
without using Gson or jackson. just vanilla java
Is there a more simpler way ?
static class DataObject {
private String access;
private String foo;
private Int age;
}
to {"access" : "value","foo":"value","age":"value"}
without using Gson or jackson. just vanilla java
Is there a more simpler way ?
2
Answers
As of Java 20, there is still no included JSON API in the JRE.
So, your answer would be to write the String yourself, using
String.format
, for exampleRegarding your class structure, you can use records.
To do this dynamically would most-likely require recursion and, I guess reflection, also.
A simple implementation, for static content, would be to utilize the toString method.
Here is an example, given the data you provided within the comments.
The Formatter class, here, is simply an extenstion to the String#format methods.
It has no correlation to JSON parsing, in-particularly.
Then, if we populate the data, and use a println call, we’ll get a valid JSON value.
Output