Search code examples
javajsonapi

How do I convert a java object toJSON format without using any external libraries or dependencies for java?


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 ?


Solution

  • 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.

    class DataObject {
        String aPid;
        String location;
        String senderDomainId;
        String timeZone;
        String senderDomainAgentId;
        String recipientDomainName;
        BusinessMsg businessMsg;
    
        @Override
        public String toString() {
            StringBuilder string = new StringBuilder();
            Formatter formatter = new Formatter(string);
            formatter.format("{%n");
            formatter.format("\"sendParams\": {".indent(4), aPid);
            formatter.format("\"aPid\": \"%s\",".indent(8), aPid);
            formatter.format("\"location\": \"%s\",".indent(8), location);
            formatter.format("\"senderDomainId\": \"%s\",".indent(8), senderDomainId);
            formatter.format("\"timeZone\": \"%s\",".indent(8), timeZone);
            formatter.format("\"senderDomainAgentId\": \"%s\",".indent(8), senderDomainAgentId);
            formatter.format("\"recipientDomainName\": \"%s\",".indent(8), recipientDomainName);
            formatter.format("\"businessMsg\": %s".indent(8), businessMsg);
            formatter.format("}".indent(4));
            formatter.format("}");
            formatter.flush();
            return string.toString();
        }
    
        static class BusinessMsg {
            String id, to, from;
    
            @Override
            public String toString() {
                StringBuilder string = new StringBuilder();
                Formatter formatter = new Formatter(string);
                formatter.format("{%n");
                formatter.format("\"id\": \"%s\",".indent(12), id);
                formatter.format("\"to\": \"%s\",".indent(12), to);
                formatter.format("\"from\": \"%s\"".indent(12), from);
                formatter.format("}".indent(8).stripTrailing());
                formatter.flush();
                return string.toString();
            }
        }
    }
    

    Then, if we populate the data, and use a println call, we'll get a valid JSON value.

    DataObject dataObject = new DataObject();
    dataObject.aPid = "AGNT8935i";
    dataObject.location = "New York";
    dataObject.senderDomainId = "DOMN67";
    dataObject.timeZone = "Eastern Standard Time";
    dataObject.senderDomainAgentId = "DMNAGNT94-eoi7436ur-334kjery3-oe73573ne";
    dataObject.recipientDomainName = "tic.com";
    DataObject.BusinessMsg businessMsg = new DataObject.BusinessMsg();
    businessMsg.id = "BSMG227e0e9f-2af9-4f49-8886-39eadda4d8f3";
    businessMsg.to = "tic.com";
    businessMsg.from = "tic.com";
    dataObject.businessMsg = businessMsg;
    
    System.out.println(dataObject);
    

    Output

    {
        "sendParams": {
            "aPid": "AGNT8935i",
            "location": "New York",
            "senderDomainId": "DOMN67",
            "timeZone": "Eastern Standard Time",
            "senderDomainAgentId": "DMNAGNT94-eoi7436ur-334kjery3-oe73573ne",
            "recipientDomainName": "tic.com",
            "businessMsg": {
                "id": "BSMG227e0e9f-2af9-4f49-8886-39eadda4d8f3",
                "to": "tic.com",
                "from": "tic.com"
            }
        }
    }