Lilac
Lilac

Reputation: 590

Convert Java pojo to json String

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

Answers (3)

improbable
improbable

Reputation: 2934

There are 2 libraries that deal with JSON serialization/deserialization using Java:

  1. Jackson

    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.

    Dependencies (databind is the main dependency, for annotations and additional features you will need more Jackson dependencies):

    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}"
    }
    

    Serialization Snippet:

    TabularDescriptor tabularDescriptor = new TabularDescriptor();
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(tabularDescriptor);
    
  2. Gson

    Google's library for a Java serialization/deserialization(docs).

    Dependencies:

    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>
    

    Serialization Snippet:

    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:

  1. JSON in Java by Baeldung
  2. Jackson vs Gson by Baeldung

Upvotes: 12

Mehrdad Yami
Mehrdad Yami

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

Jackson(ObjectMapper) vs Gson

Upvotes: 4

Vladucu Voican
Vladucu Voican

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

Related Questions