PASOE WebHandler escaping slashes in the JSON request conten

Posted by davez on 07-Dec-2016 13:58

0 down vote favorite

I'm using Progress OpenEdge 11.6.1 on Windows Server 2012. I'm converting some APIs that used the REST Adapter to now use a PASOE WebHandler. The same JSON request content is giving different results between the two. In the request I pass a Progress-formatted date, such as:

"FieldValue": "11/30/2016"

The REST adapter API gets that exact value for the date and processes it successfully. However the WebHandler API gets:

11\/30\/2016

The slashes appear to be escaped with backslashes, which causes an invalid date error. How can I avoid this? Do I have to write a function to strip the backslashes or is there a setting I can change to prevent the escaping?

Posted by Matt Baker on 07-Dec-2016 14:08

Those are both valid. Escaping forward slashes in JSON is optional, but not required.  Escaping backslashes is required.  

See the sidebar for characters that can be escaped.

http://www.json.org/

What are you using to decode the returned data?  Are you treating it as a string or are you decoding it using the ABL JSON handling?

Posted by Peter Judge on 07-Dec-2016 14:30

This is the result of the JSON library we use in ABL (which is based on YAJL https://lloyd.github.io/yajl/ ).  I’m not sure what the REST adapter uses but clearly it handles these ‘better’.
 
You can see the issue in ABL with the code below.
 
using progress.json.objectmodel.*.
 
def var o1 as Progress.Json.ObjectModel.JsonObject.
 
o1 = new jsonobject().
 
o1:add('f1', string(today)).
 
message
o1:getCharacter('f1') skip       // 12/07/16
string(o1:getjsontext())         // {"f1":"12\/07\/16"}
view-as alert-box.
 
Does the client have a problem consuming it?
 
JSON doesn’t have native dates so you have to use strings.  See http://json.org and  http://stackoverflow.com/questions/10286204/the-right-json-date-format#15952652 .
 
 

Posted by onnodehaan on 07-Dec-2016 15:22

Hi Davez,

Could you try:

GetCharacter()

I believe that might fix your problem.

All Replies

Posted by Matt Baker on 07-Dec-2016 14:08

Those are both valid. Escaping forward slashes in JSON is optional, but not required.  Escaping backslashes is required.  

See the sidebar for characters that can be escaped.

http://www.json.org/

What are you using to decode the returned data?  Are you treating it as a string or are you decoding it using the ABL JSON handling?

Posted by Peter Judge on 07-Dec-2016 14:30

This is the result of the JSON library we use in ABL (which is based on YAJL https://lloyd.github.io/yajl/ ).  I’m not sure what the REST adapter uses but clearly it handles these ‘better’.
 
You can see the issue in ABL with the code below.
 
using progress.json.objectmodel.*.
 
def var o1 as Progress.Json.ObjectModel.JsonObject.
 
o1 = new jsonobject().
 
o1:add('f1', string(today)).
 
message
o1:getCharacter('f1') skip       // 12/07/16
string(o1:getjsontext())         // {"f1":"12\/07\/16"}
view-as alert-box.
 
Does the client have a problem consuming it?
 
JSON doesn’t have native dates so you have to use strings.  See http://json.org and  http://stackoverflow.com/questions/10286204/the-right-json-date-format#15952652 .
 
 

Posted by davez on 07-Dec-2016 15:13

At the moment I'm not doing any decoding of the data. The request content is in a JSON object and I'm using the GetJsonText() method to extract the data I need. Is there a better way to do this?

Posted by davez on 07-Dec-2016 15:19

Peter, that's exactly what's happening! I thought I might be doing something wrong but it's probably that the webhandler is not manipulating the request like the REST adapter does. I've done a simple REPLACE to get past the problem for now:

val = REPLACE(val, "\/", "/").

I'm guessing this will happen in other non-date fields too, but the date field was the only one to blow up because of the changes to the value.

Posted by onnodehaan on 07-Dec-2016 15:22

Hi Davez,

Could you try:

GetCharacter()

I believe that might fix your problem.

Posted by onnodehaan on 07-Dec-2016 15:26

Hi Dave

There are more "escaped characters" in Json, for example: \r voor carriage return, etc

you might end up having a pretty complex replace. Take a look at my comment abount GetCharacter(). That might get you out of the woods

Posted by davez on 07-Dec-2016 15:47

Hi onnodehaan,

I switched to GetCharacter() and that fixed it. It looks like it will handle the other escaped characters too. Thanks!

Posted by Peter Judge on 07-Dec-2016 19:36

The best approach when using the JsonObject is to use the Write() methods.
 
Def var lcData as longchar.
myJson:Write(input-output lcdata).
This should do the right thing. You can even Write() to the web-stream which will return the data via http.
 
I misunderstood your email and that you were writing the JSON that way. GetJsonText() will always ‘stringify’ the json (ie escape things).
 
 
 
 

Posted by davez on 08-Dec-2016 09:45

Peter, I'm actually reading data. I have the webhandler request content in a JSON object and I'm reading data values with the GetJsonText() (and now GetCharacter() ) methods. If the request has this content:

   {"FieldValue": "11/30/2016"}

then reading it like this:

   val = oRequest:GetJsonText("FieldValue").

gives me "11\/30\/2016" with escaped slashes. But reading it like this:

   val = oRequest:GetCharacter("FieldValue").

gives me the correct "11/30/2016". I'm using a WebResponseWriter to send back data, and that uses the Write() method.

This thread is closed