XML response serialization for RESTful web service

Forum to discuss the Web client version of Servoy.

XML response serialization for RESTful web service

Postby ATrinkunas » Thu Jul 27, 2023 6:38 pm

Hi

RESTful web service response return value is a JavaScript object. This JavaScript object is serialized and placed in the body of the HTTP Response.
Is there way somehow to control this serialization process?
I want the attributes of this JavaScript object to be serialized to XML attributes (Not XML Elements)

Example:

JSON response:
Code: Select all
{
    "JMF": {
        "Response": {
            "Type": "SubmissionMethods",
            "ReturnCode": "0",
            "ID": "SubmissionMethodsResponse_Q38BE5CF9-F912-4E18-8F76-C3DB8B9CEEE2",
            "refID": "Q38BE5CF9-F912-4E18-8F76-C3DB8B9CEEE2",
            "SubmissionMethods": {
                "Packaging": "MIME",
                "URLSchemes": "file http"
            }
        }
    }
}


XML Response:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<JMF>
    <Response>
        <Type>SubmissionMethods</Type>
        <ReturnCode>0</ReturnCode>
        <ID>SubmissionMethodsResponse_Q38BE5CF9-F912-4E18-8F76-C3DB8B9CEEE2</ID>
        <refID>Q38BE5CF9-F912-4E18-8F76-C3DB8B9CEEE2</refID>
        <SubmissionMethods>
            <Packaging>MIME</Packaging>
            <URLSchemes>file http</URLSchemes>
        </SubmissionMethods>
    </Response>
</JMF>


I want "<Packaging>MIME</Packaging>" to be an attribute of "SubmissionMethods" XML Element etc.

Thanks, Anton
ATrinkunas
 
Posts: 5
Joined: Mon Apr 06, 2020 7:52 pm

Re: XML response serialization for RESTful web service

Postby rgansevles » Fri Aug 04, 2023 11:45 am

Hi,

In stead of a json object you can also return an xml object where you have full control of which data is serialized as element or as attribute:

Code: Select all
return new XML('<JMF>\
    <Response>\
        <Type>SubmissionMethods</Type>\
        <ReturnCode>0</ReturnCode>\
        <ID>SubmissionMethodsResponse_Q38BE5CF9-F912-4E18-8F76-C3DB8B9CEEE2</ID>\
        <refID>Q38BE5CF9-F912-4E18-8F76-C3DB8B9CEEE2</refID>\
        <SubmissionMethods Packaging="MIME">\
            <URLSchemes>file http</URLSchemes>\
        </SubmissionMethods>\
    </Response>\
</JMF>')



Alternatively, you can create the xml programatically:

Code: Select all
var xml = new XML('<JMF></JMF>')
xml.Response.Type = 'SubmissionMethods'
xml.Response.ReturnCode = 0
xml.Response.ID = 'SubmissionMethodsResponse_Q38BE5CF9-F912-4E18-8F76-C3DB8B9CEEE2'
xml.Response.refID = 'Q38BE5CF9-F912-4E18-8F76-C3DB8B9CEEE2'
xml.Response.SubmissionMethods.@Packaging = 'MIME'
xml.Response.SubmissionMethods.URLSchemes = 'file http'
return xml

In both cases, this is what is returned to the client:

Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<JMF>
  <Response>
    <Type>SubmissionMethods</Type>
    <ReturnCode>0</ReturnCode>
    <ID>SubmissionMethodsResponse_Q38BE5CF9-F912-4E18-8F76-C3DB8B9CEEE2</ID>
    <refID>Q38BE5CF9-F912-4E18-8F76-C3DB8B9CEEE2</refID>
    <SubmissionMethods Packaging="MIME">
      <URLSchemes>file http</URLSchemes>
    </SubmissionMethods>
  </Response>
</JMF>
Rob Gansevles
Servoy
User avatar
rgansevles
 
Posts: 1929
Joined: Wed Nov 15, 2006 6:17 pm
Location: Amersfoort, NL


Return to Servoy Web Client

Who is online

Users browsing this forum: No registered users and 7 guests