JSON Serialization

Posted by simon_barker on 29-May-2018 09:40

I've been using the standard JSON (Progress.ObjectModel.*.) objects to build responses for consumption via REST and have recently been looking at the Progress.IO.JsonSerializer object. 

When I serialize JSON from an object using the Serialize() method the output contains properties that aren't required, see below:

{
    "prods:version": 1,
    "prods:objId": 1,
    "CustomScripts.OperationalSettings": {
        "WebAccess": true,
        "PhoneNumber": "12345 987654",
        "prods:tempTables": {
            "Settings": [{
                "a": "b"
            }]
        }
    }

}

Is there anyway to remove prods:version, prods:tempTables etc. without all the legwork i.e.decorating the JsonSerializer object or similar?

All Replies

Posted by benBuckley on 29-May-2018 09:52

The JsonObject and JsonArray objects both have write methods that do exactly this (Abstracted in JsonConstruct). If you use web handlers for your REST endpoints, simply making the response body the JsonObject/Array will have the web handler deal with serialization for you.

Posted by simon_barker on 29-May-2018 13:08

That's how I'm currently handling the response. What I was wanting to do was leverage some of the business logic elsewhere in the application and use this to drive the response e.g

Find first customer no-lock.

Assign oCustomer = new Customer(buffer Customer:handle).

WebReponse:entity = oCustomer.

The Serialize() method on JsonSerializer doesn't quite give me as lightweight an object as I'd like.

Posted by benBuckley on 29-May-2018 13:39

The JsonSerializer appears to be intended for deserializing ProgressObjects for exchanging Progress objects, not data. The JsonObject has a Read method that can take a dataset, temp-table, or temp-table buffer handle as input. The JsonArray has a Read method that can take a temp-table handle as input, so If you can get your response data into a temp-table or dataset, you can serialize to proper JSON:

USING Progress.Json.ObjectModel.JsonObject.

DEFINE VARIABLE oJson AS JsonObject NO-UNDO.

/* quick-and-ugly to get a valid temp-table buffer */

DEFINE TEMP-TABLE ttCustomer LIKE Customer.

FIND FIRST Customer NO-LOCK.

CREATE ttCustomer.

BUFFER-COPY Customer TO ttCustomer.

oJson = NEW JsonObject().

oJson:Read(BUFFER ttCustomer:HANDLE).

WebReponse:entity = oJson

Posted by simon_barker on 29-May-2018 14:02

That's what I've got at the minute using a mixture of the read method and building the object / array manually. The Serialize method should be for data too; for example using active record to represent a row in the database.

I'm happy with all the other ways of creating Json output it was specifically if it could be done from an object directly that I was wanting.

Posted by Kim Ward on 30-May-2018 03:04

I would try just writing your own serializer and perhaps try to avoid serializing whole objects including internal temp-tables  / Dataset for web responses.

This thread is closed