Reputation: 590
I have the following java class
public class TabularDescriptor extends ReportDescriptor {
private String generatorClass;
private String targetClass;
private String name;
private String sublabel;
private String reportName;
private List<MappingMetadata> mappings = null;
private List<TabularColumnGroup> columnGroups = null;
private List<TabularStates> states = null;
:
:
and its getters and settere
I have entity classes for each of those List like MappingMetadata,TabularColumnGroup,TabularStates. I want to get a json data for this pojo classes. What can I do for it.
And what is the use of
public JSONObject toJSON() {
JSONObject ret = new JSONObject();
ret.put("generatorClass", this.generatorClass);
ret.put("targetClass", this.targetClass);
ret.put("name", this.name);
:
:
return ret;
}
And is there anyway I can display my json content on browser if yes how can I? Thanks.
Upvotes: 20
Views: 61639
Reputation: 2934
There are 2 libraries that deal with JSON serialization/deserialization using Java:
The go-to library for Java serialization/deserialization(docs). A default choice for the JSON interaction within Java for the majority of developers. Comes completely embedded with all dependencies in spring-boot-starter-web
and spring-boot-starter-webflux
, dependency starters of Spring Boot - popular Java IOC/DI framework.
Maven:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
Gradle:
dependencies {
implementation "com.fasterxml.jackson.core:jackson-databind:${yourVersion}"
}
TabularDescriptor tabularDescriptor = new TabularDescriptor();
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(tabularDescriptor);
Google's library for a Java serialization/deserialization(docs).
Gradle:
dependencies {
implementation "com.google.code.gson:gson:${yourVersion}"
}
Maven:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
TabularDescriptor tabularDescriptor = new TabularDescriptor();
Gson gson = new Gson();
String json = gson.toJson(obj);
Details worth noting: You have to have all getters/setters public for both complete serialization and complete deserialization of an object(in its simplest form). An empty constructor is a must in any case.
Reference Information:
Upvotes: 12
Reputation: 1681
You can use ObjectMapper or Gson for Class to JSON conversion and vice-versa.
(I would recommend ObjectMapper)
- Object Mapper
Intro to the Jackson ObjectMapper
- GSON
How to convert Java object to / from JSON
- Comparison
Upvotes: 4
Reputation: 285
I would recommend you to add Jackson to your project, it's rather easy to use.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
And in Java Code can be used as so:
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(tabularDescriptor);
TabularDescriptor newTabularDescriptor = objectMapper.readValue(json, TabularDescriptor.class);
Upvotes: 6