EGL Development User Group

EGL Development User Group

EGL Development User Group

The EGL Development User Group is dedicated to sharing news, knowledge, and insights regarding the EGL language and Business Developer product. Consisting of IBMers, HCL, and users, this community collaborates to advance the EGL ecosystem.

 View Only
Expand all | Collapse all

Ignore missing properties during jackson json deserialize (gen to java)

  • 1.  Ignore missing properties during jackson json deserialize (gen to java)

    Posted Wed February 10, 2016 07:39 AM

    Is it possible in EGL to parse JSON into EGL objects when JSON has missing properties?
    (java: http://stackoverflow.com/questions/20578846/ignore-missing-properties-during-jackson-json-deserialize-in-java)

    I rather not use external types.


    Record definition:

    Record Person   name string;   age int;end

     

    JSON definition:

    { name : John}

    EGL Code to parse JSON into record:

    person Person;JSON string = "{\"name\":\"John\"}";  ServiceLib.convertFromJSON(JSON, person)

    Error message

    EGL1572E JSON parsing error occurred

     

    Niek_Vandael


  • 2.  Re: Ignore missing properties during jackson json deserialize (gen to java)

    Posted Wed February 10, 2016 10:09 AM

    Hi,

    I don't know but i've a possible workaround for you.

     

    You could create a record without the age like this:

    Record Person_name   name string;  end

    Then convert from JSON:

    ServiceLib.convertFromJSON(JSON, person_name)

    and move the content of the person_name record to your person record...:

    move person_name to person byName;

     

     

    Kind Regards!

     

    Marcel-D


  • 3.  Re: Ignore missing properties during jackson json deserialize (gen to java)

    Posted Wed February 10, 2016 10:15 AM

    I know that will work but the problem is i do not know what element is missing.

    Sometimes it's age, sometimes it name.

    (actually, my JSON is about 5 in dept and 500 in length, and quite variable since it comes from a 3rd party datasource) 

     

    Niek_Vandael


  • 4.  Re: Ignore missing properties during jackson json deserialize (gen to java)

    Posted Thu February 11, 2016 02:35 AM

    Hi Niek,

    Yes it is possible! Try this:

    program jsontest type BasicProgram() {}                function main()                JSON string = "{\"name\":\"John\", \"yearofbirth\" : 1879}";                person Person;                jsonAsDictionary dictionary{};                ServiceLib.convertFromJSON(JSON, jsonAsDictionary);                for (i int from 1 to jsonAsDictionary.getKeys().getSize())                        person[jsonAsDictionary.getKeys()[i]] = jsonAsDictionary.getValues()[i];                end                SysLib.writeStdout(person.name);                SysLib.writeStdout(person.age);                SysLib.writeStdout(person.yearofbirth);        end        endrecord Person   name string;   age int;  yearofbirth int;end

    If you can be sure of the fact that the json string represents (part of) your record you can use above code literally.

    If the json string is likely to contain other fields also, you would come to some code like this:

    program jsontest type BasicProgram() {}                function main()                JSON string = "{\"name\":\"John\", \"bornIn\" : 1879, \"age\" : 137}";                person Person;                jsonAsDictionary dictionary{};                personAsDictionary dictionary{};                ServiceLib.convertFromJSON(JSON, jsonAsDictionary);                ServiceLib.convertFromJSON(ServiceLib.convertToJSON(person), personAsDictionary);                for (i int from 1 to jsonAsDictionary.getKeys().getSize())                        if (personAsDictionary.containsKey(jsonAsDictionary.getKeys()[i]))                                person[jsonAsDictionary.getKeys()[i]] = jsonAsDictionary.getValues()[i];                        else                                // Ring some bells? Or not.                        end                end                SysLib.writeStdout(person.name);                SysLib.writeStdout(person.age);                SysLib.writeStdout(person.yearofbirth);        end        endrecord Person   name string;   age int;  yearofbirth int;end

    Regards,

    Guus

     

    gweis


  • 5.  Re: Ignore missing properties during jackson json deserialize (gen to java)

    Posted Thu February 11, 2016 10:15 AM

    Guus,

     

    nice solution.  Thanks for posting!!

     

    Mark

    markevans


  • 6.  Re: Ignore missing properties during jackson json deserialize (gen to java)

    Posted Fri February 12, 2016 05:07 AM

    Hi Guus,

    Thank you for your answer. I saw you converting the record into a dictionary,
    from there i was able to complete the code to convert JSON into the record structure on multiple layers.

    Who wants to use this code has to take in account to initialize records with sufficient space.

     

     

    program JSONTest type BasicProgram {}    JSON string =         "{" +        "  \"TransactionId\": \"abcdef\"," +        "  \"Content\": {" +        "          \"Persons\": [{" +        "                  \"Name\": \"Niek\"," +        "                  \"Age\": 28," +        "                  \"DateOfBirth\": \"1987-10-20\"," +        "                  \"Sex\": \"M\"," +        "                  \"Hobbies\": [{" +        "                          \"Name\": \"Hockey\"," +        "                          \"Day\": \"Thu\"" +        "                  }, {" +        "                          \"Name\": \"Running\"," +        "                          \"Day\": \"Fri\"" +        "                  }]" +        "          }, {" +        "                  \"Name\": \"Chrissy\"," +        "                  \"Sex\": \"V\"," +        "                  \"Hobbies\": [{" +        "                          \"Name\": \"Make-up\"" +        "                  }, {" +        "                          \"Name\": \"Horseriding\"," +        "                          \"Day\": \"Mon\"" +        "                  }]" +        "          }]" +        "  }" +        "}";        private RequestPayload requestPayload;         private jsonAsDictionary dictionary{};                function main()                try                        ServiceLib.convertFromJSON(JSON, jsonAsDictionary);                        setValuesRecursive(jsonAsDictionary, RequestPayload as any);                onException(exception AnyException)                        SysLib.writeStdout(exception.message);                end                                break boolean = true; // Put your breakpoint here        end                function setValuesRecursive(value dictionary, level any)                for (i int from 1 to value.getKeys().getSize())                        _key string = value.getKeys()[i];                _val any = value[_key];                                                        if(_val isa dictionary)                        setValuesRecursive(_val, level[_key]);                else                        if(_val isa any[])                                for(j int from 1 to (_val as any[]).getSize() by 1)                                        _arrayElement any;                                        try                                                _arrayElement  = level[_key][j] as any;                                                onException(exception IndexOutOfBoundsException)                                                        SysLib.writeStdout("Please initialize array with sufficient space");                                                        SysLib.writeStdout(exception.message);                                                        SysLib.writeStdout("----------------------------------------------");                                                        return;                                                end                                        setValuesRecursive(_val[j], _arrayElement);                                end                        else                                level[_key] = _val;                        end                end        end        endendrecord RequestPayload        TransactionId string;        Content Content;endrecord Content        Persons Person[10];endrecord Person        Name string;        Age int;        DateOfBirth string;        Sex string;        Hobbies Hobby[10];endrecord Hobby        Name string;        Day string;end

     

    Niek_Vandael