Search code examples
javajsongson

JSON conversion does not work as expected


The program stops when it encounters the command new Gson().toJson(emailPOJO); Simple strings work so probably the problem is the Object emailPOJO, but I searching online I did not find anything different with what I wrote.

I need to convert this EmailPOJO object into a JSON and I'm using com.google.code.gson version 2.10.1


import java.util.List;

public class EmailPOJO {
    private Integer ID;
    private String sender;
    private List<String> receivers;
    private String subject;
    private String content;
    private String sendDate;
    private Boolean deleted;


    public Integer getID () {
        return ID;
    }

    public void setID (Integer ID) {
        this.ID = ID;
    }

    public String getSender () {
        return sender;
    }

    public void setSender (String sender) {
        this.sender = sender;
    }

    public List<String> getReceivers () {
        return receivers;
    }

    public void setReceivers (List<String> receivers) {
        this.receivers = receivers;
    }

    public String getSubject () {
        return subject;
    }

    public void setSubject (String subject) {
        this.subject = subject;
    }

    public String getContent () {
        return content;
    }

    public void setContent (String content) {
        this.content = content;
    }

    public String getSendDate () {
        return sendDate;
    }

    public void setSendDate (String sendDate) {
        this.sendDate = sendDate;
    }

    public Boolean getDeleted () {
        return deleted;
    }

    public void setDeleted (Boolean deleted) {
        this.deleted = deleted;
    }

    @Override
    public String toString () {
        return "EmailPOJO{" +
                "ID=" + ID +
                ", sender='" + sender + '\'' +
                ", receivers=" + receivers +
                ", subject='" + subject + '\'' +
                ", content='" + content + '\'' +
                ", sendDate='" + sendDate + '\'' +
                ", deleted=" + deleted +
                '}';
    }
}

I properly set each variable in it using getX() (so no null variables) and call these line of code

System.out.println("trasforming into json");
String emailJson = new Gson().toJson(emailPOJO);
System.out.println("json: "+emailJson);

but as I reach the second line the execution stops and never goes on. So I just get "trasforming into json" and that's it, no exception, no errors even using try catch.

I have no idea what's causing this, I've searched a lot online but no answers. Note that converting primitive object like a String work properly. I also tried making the class serializable, but nothing changed.


Solution

  • This is a problem in your environment/IDE. I copied your code and it worked perfectly:

    import java.util.Arrays;
    import java.util.List;
    
    import com.google.gson.Gson;
    
    public class EmailPOJO {
        private Integer ID;
        private String sender;
        private List<String> receivers;
        private String subject;
        private String content;
        private String sendDate;
        private Boolean deleted;
    
        
        public static void main(String[] args) {
    
            EmailPOJO emailPOJO = new EmailPOJO();
            emailPOJO.setID(5);
            emailPOJO.setContent("content, more content");
            emailPOJO.setDeleted(false);
            emailPOJO.setReceivers(Arrays.asList(new String[]{"receiver1", "receiver2"}));
            emailPOJO.setSendDate("2023-03-14");;
            emailPOJO.setSender("sender");;
            emailPOJO.setSubject("subject");;
            System.out.println("trasforming into json");
            String emailJson = new Gson().toJson(emailPOJO);
            System.out.println("json: "+emailJson);
        }
    
        public Integer getID () {
            return ID;
        }
    
        public void setID (Integer ID) {
            this.ID = ID;
        }
    
        public String getSender () {
            return sender;
        }
    
        public void setSender (String sender) {
            this.sender = sender;
        }
    
        public List<String> getReceivers () {
            return receivers;
        }
    
        public void setReceivers (List<String> receivers) {
            this.receivers = receivers;
        }
    
        public String getSubject () {
            return subject;
        }
    
        public void setSubject (String subject) {
            this.subject = subject;
        }
    
        public String getContent () {
            return content;
        }
    
        public void setContent (String content) {
            this.content = content;
        }
    
        public String getSendDate () {
            return sendDate;
        }
    
        public void setSendDate (String sendDate) {
            this.sendDate = sendDate;
        }
    
        public Boolean getDeleted () {
            return deleted;
        }
    
        public void setDeleted (Boolean deleted) {
            this.deleted = deleted;
        }
    
        @Override
        public String toString () {
            return "EmailPOJO{" +
                    "ID=" + ID +
                    ", sender='" + sender + '\'' +
                    ", receivers=" + receivers +
                    ", subject='" + subject + '\'' +
                    ", content='" + content + '\'' +
                    ", sendDate='" + sendDate + '\'' +
                    ", deleted=" + deleted +
                    '}';
        }
    }
    

    Gson version:

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.10.1</version> 
    </dependency>
    

    Output:

    trasforming into json
    json: {"ID":5,"sender":"sender","receivers":["receiver1","receiver2"],"subject":"subject","content":"content, more content","sendDate":"2023-03-14","deleted":false}