20

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.

1
  • 2
    Have a look at github.com/google/gson. It seems to be exactly what you're looking for. You can easily serialize and deserialize Java Objects using it. Commented Jan 9, 2020 at 20:23

3 Answers 3

12

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
Sign up to request clarification or add additional context in comments.

Comments

6

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);

3 Comments

Thank you. What is value in tabularDescriptor. Is it the value I set using its setter? If yes how can I set value to pojo classes like MappingMetadata.
@Lilac 1) tabularDescriptor is your object e.g. TabularDescriptor tabularDescriptor = new TabularDescriptor(...); 2) Not sure that I get what you mean here. Are you refering to how to create objects? Easiest way is to use a Builder Pattern, especially in your case since you have a lot of inner objects in the tabular descriptor.
Thank you. I wasn't sure how to give values for the inner objects.
4

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.