Custom Class to JSON or JSON object - Forum - OpenEdge General - Progress Community

Custom Class to JSON or JSON object

 Forum

Custom Class to JSON or JSON object

This question is not answered

I have started a custom class like follows:

CLASS Cask:

DEFINE PUBLIC PROPERTY iCaskno AS INTEGER
GET.
SET.

CONSTRUCTOR PUBLIC Cask(INPUT ipCaskno AS INTEGER):
    iCaskno = ipCaskno.
END CONSTRUCTOR.

END CLASS.

and i want to be able to create a JSON array with several Cask objects. I suspect the solution would be to turn the Cask objects into JSON objects somehow, but i cannot find any documentation for the best way to do this.

Thanks

All Replies
  • There's no built-in way of doing this (yet).
     
    You will need to serialize the object yourself, into JSON (or whatever you want).
     
    The very simplest way would be to do it via a ToString() override, but there are a couple of issues with that
    1) you only have a single form of serialisation available (ie whatever you choose ther), and
    2) you may blow up the character size limit (~32k bytes)
     
    There are a number of approaches you can take for implementing your own (de)serialisation, but they're largely hamstrung by the lack of reflection (introspection) in ABL, meaning you have to know the names of the members you want to serialise in advance.
     
    IIRC there have been a number of discussions on these forums over the years about serialisation approaches.
     
    I'd also suggest adding an Idea ( at https://community.progress.com/community_groups/products_enhancements/i/openedge/default.aspx)  which would allow you to add Objects as values to JsonObject or JsonArray instances.
     
    -- peter
     
    From: Andrew Stalker [mailto:bounce-ajstalker14@community.progress.com]
    Sent: Wednesday, 01 April, 2015 11:10
    To: TU.OE.General@community.progress.com
    Subject: [Technical Users - OE General] Custom Class to JSON or JSON object
     
    Thread created by Andrew Stalker

    I have started a custom class like follows:

    CLASS Cask:

    DEFINE PUBLIC PROPERTY iCaskno AS INTEGER
    GET.
    SET.

    CONSTRUCTOR PUBLIC Cask(INPUT ipCaskno AS INTEGER):
        iCaskno = ipCaskno.
    END CONSTRUCTOR.

    END CLASS.

    and i want to be able to create a JSON array with several Cask objects. I suspect the solution would be to turn the Cask objects into JSON objects somehow, but i cannot find any documentation for the best way to do this.

    Thanks

    Stop receiving emails on this subject.

    Flag this post as spam/abuse.

  • I personally would leave the override of  ToString() for debugging/tracing/logging purposes and create a WriteJson() or Serialize() method which returns LONGCHAR.
     
    We solve the lack of reflection (introspection) by using include files to define the serializable properties:
     
    Consultingwerk/JsonSerializablePropperty.i
     
        DEFINE PUBLIC PROPERTY {1} AS {2} NO-UNDO {3}
        GET.
        SET.
       
    &IF "{&SerializableProperties}":U NE "":U &THEN
    &GLOBAL-DEFINE SerializableProperties {&SerializableProperties},{1},{2}
    &ELSE
    &GLOBAL-DEFINE SerializableProperties {1},{2}
    &ENDIF
     
    That way we create our own meta schema of serializable properties which allows for full automatic JSON serialization and deserialization including serialization of inherited classes, referenced object instances, enums, ….
     
    CLASS Consultingwerk.NumericFormat
    &IF "{&FrameworkSerializationType}" EQ "XML" OR PROVERSION BEGINS "10.2":U &THEN
        INHERITS XmlSerializable
    &ELSE
        INHERITS JsonSerializable
    &ENDIF  
        :
     
        {Consultingwerk/JsonSerializableProperty.i NumSeparator CHARACTER} .
        {Consultingwerk/JsonSerializableProperty.i DecimalPoint CHARACTER} .
     
        /*------------------------------------------------------------------------------
            Purpose: Constructor for the NumericFormat class
            Notes:  

    Architect of the SmartComponent Library and WinKit

    Consultingwerk Ltd.