Wednesday, June 7, 2017

Overcoming the AutoPrimitive Issue in WSO2 APIM 1.10 during Json Conversion

When we use the default application/json builder and formatter specified in axis2.xml, we will get a problem with AutoPrimitive ( Consider this problem will come only when there is a content awware mediator in the flow ), that means if the backend sends a response as below:
 {  
  "Names": [  
   {  
    "Name": [  
     {  
      "id": "850340277",  
      "username": "Ajanthan",  
      "accountLocked": false  
     }  
    ]  
   }  
  ]  
 }  

The output with default settings will be like below. YOu can observe that the string passed with numbers have converted to Integer.
 {  
  "Names": [  
   {  
    "Name": [  
     {  
      "id": 850340277,  
      "username": "Ajanthan",  
      "accountLocked": false  
     }  
    ]  
   }  
  ]  
 }  

Using adding the property synapse.commons.json.json.output.autoPrimitive=false in <APIM_HOME>/repository/conf/synapse.properties file we can overcome the issue and keep the string as it is. But the boolean values will also be converted to String. You can observe that from the below:
 {  
  "Names": [  
   {  
    "Name": [  
     {  
      "id": "850340277",  
      "username": "Ajanthan",  
      "accountLocked": "false"  
     }  
    ]  
   }  
  ]  
 }  

To overcome this problem, the recommended option is to move to JsonStreamBuilder and JsonStreamFormatter combination.

Comment the below:
 <messageBuilder contentType="application/json"  
             class="org.apache.synapse.commons.json.JsonBuilder"/>  
 <messageFormatter contentType="application/json"  
              class="org.apache.synapse.commons.json.JsonFormatter"/>  

and uncomment the below:
 <messageBuilder contentType="application/json"  
             class="org.apache.synapse.commons.json.JsonStreamBuilder"/>  
 <messageFormatter contentType="application/json"  
              class="org.apache.synapse.commons.json.JsonStreamFormatter"/>  

And now we can get the expected output after the conversion.
 {  
  "Names": [  
   {  
    "Name": [  
     {  
      "id": "850340277",  
      "username": "Ajanthan",  
      "accountLocked": false  
     }  
    ]  
   }  
  ]  
 }  

Hope this helps...



1 comment:

  1. Hi,

    synapse.commons.json.json.output.autoPrimitive=false

    This should be corrected as below.

    synapse.commons.json.output.autoPrimitive=false

    Thanks

    ReplyDelete