1
votes

In IBM MobileFirst 7.0, I created a SOAP adapter by pointing MobileFirst Platform Studio to a WSDL running on a local web server. When calling the SOAP adapter, which converts JSON to XML then when the response is received from XML back into JSON, it is setting number (integer, decimal) and boolean values in the JSON returned as strings i.e. surrounding them in double quotes in the response instead of leaving them unquoted. I did not change anything in the adapter that was auto generated by MFP Studio. It is my understanding that in JSON, booleans and numbers as well as nulls are not quoted. I also have a non-MobileFirst web server running via Spring Boot using WebServiceGatewaySupport to call the same SOAP service and @ResponseBody annotation to have it automatically return JSON and numbers and booleans are NOT in double quotes within the strings returned.

Is it expected that MobileFirst/Worklight returns everything as a String within the JSON body? If so, why? If not, any ideas on what I may need to do in order for this not to happen and for it to leave numbers and booleans unquoted?

Below are the relevant portions of the WSDL and the JSON request/responses to/from the Worklight SOAP adapter.

 <xs:element name="customerFoodOrderRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="customerId" type="xs:int"/>
                <xs:element name="dateTime" type="xs:dateTime"/>
                <xs:element name="item" type="xs:string"/>
                <xs:element name="amount" type="tns:money"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:simpleType name="money">
        <xs:restriction base="xs:decimal">
            <xs:fractionDigits value="2"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:element name="customerFoodOrderResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="success" type="xs:boolean"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="getRecentFoodOrdersForCustomerRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="customerId" type="xs:int"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="getRecentFoodOrdersForCustomerResponse">
        <xs:complexType>
            <xs:sequence maxOccurs="unbounded" minOccurs="0">
                <xs:element name="foodOrders" type="tns:foodOrder"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="foodOrder">
        <xs:sequence>
            <xs:element name="dateTime" type="xs:dateTime"/>
            <xs:element name="item" type="xs:string"/>
            <xs:element name="amount" type="tns:money"/>
        </xs:sequence>
    </xs:complexType>

JSON Response #1 (amount is an xs:decimal wrapped in a SimpleType named money, but it is being output to JSON as a string)

{
 "Envelope": {
      "Body": {
         "getRecentFoodOrdersForCustomerResponse": {
            "foodOrders": {
               "amount": "10",
               "dateTime": "2015-02-14T12:00:00.123-07:00",
               "item": "MyItem"
            },
            "ns2": "http:\/\/company.com\/demo\/demo-producing-web-service"
         }
      },
      "Header": "",
      "SOAP-ENV": "http:\/\/schemas.xmlsoap.org\/soap\/envelope\/"
   },
   "errors": [
   ],
   "info": [
   ],
   "isSuccessful": true,
   "responseHeaders": {
      "Accept": "text\/xml, text\/html, image\/gif, image\/jpeg, *; q=.2, *\/*; q=.2",
      "Content-Length": "444",
      "Content-Type": "text\/xml;charset=utf-8",
      "Date": "Wed, 20 May 2015 02:58:26 GMT",
      "SOAPAction": "\"\"",
      "Server": "Apache-Coyote\/1.1"
   },
   "responseTime": 328,
   "statusCode": 200,
   "statusReason": "OK",
   "totalTime": 359,
   "warnings": [
   ]
 }

JSON Response #2 with boolean value for success in double quotes:

{
   "Envelope": {
      "Body": {
         "customerFoodOrderResponse": {
            "ns2": "http:\/\/company.com\/demo\/demo-producing-web-service",
            "success": "true"
         }
      },
      "Header": "",
      "SOAP-ENV": "http:\/\/schemas.xmlsoap.org\/soap\/envelope\/"
   },
   "errors": [
   ],
   "info": [
   ],
   "isSuccessful": true,
   "responseHeaders": {
      "Accept": "text\/xml, text\/html, image\/gif, image\/jpeg, *; q=.2, *\/*; q=.2",
      "Content-Length": "304",
      "Content-Type": "text\/xml;charset=utf-8",
      "Date": "Wed, 20 May 2015 02:46:02 GMT",
      "SOAPAction": "\"\"",
      "Server": "Apache-Coyote\/1.1"
   },
   "responseTime": 764,
   "statusCode": 200,
   "statusReason": "OK",
   "totalTime": 796,
   "warnings": [
   ]
}

JSON Response #3 (Customer ID is being wrapped in double quotes even though it is defined as an xs:int)

          "customer": {
             "accountNumber": "11127174",
             "firstName": "JOHN",
             "id": "200",
             "lastName": "DOE",
             "status": "None"
          },

The customer definition from the WSDL

 <xs:complexType name="customer">
        <xs:sequence>
            <xs:element name="id" type="xs:int"/>
            <xs:element minOccurs="1" name="firstName" nillable="false" type="xs:string"/>
            <xs:element minOccurs="1" name="lastName" nillable="false" type="xs:string"/>
            <xs:element minOccurs="1" name="status" nillable="false" type="tns:status"/>
            <xs:element name="accountNumber" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
1

1 Answers

0
votes

Yes all the responses from Worklight adapter will be converted to JSON before reaching the Client device (Mobile,Web browser,etc)

To understand the structure and working of adapters more clearly just read the link below Overview of worklight Adapters

Next why Json always than XML ?

  1. JSON has several advantages over XML. Its a lot smaller and less bloated, so you will be passing much less data over the network - which in the case of a mobile device will make a considerable difference.
  2. Its also easier to use in javascript code as you can simply pass the data packet directly into a javascript array without any parsing, extracting and converting, so it is much less CPU intensive too.

These are some points to be considered and also for more details regarding this consider this link and also google yourself for more INFO.

Note

  1. You can use xsl to convert json as you wish but i would recoment you to use parseInt instead of that , when ever you read customer.accountNumber it will give you with 11127174 and not "11127174" so it will be easy to deal it with JAVASCRIPT way than using xsl
  2. We cann't change worklight behavior but you can change the xml to your own JSON format by using XSL to transform the xml to JSON format as you wished without quotes and etc