When execeuting the code fragment at the end of this message I get some unwanted escape characters:
The output looks like:
{
"data-id":"123",
"parameters":"
{\"ttParameter\":[{\"PName\":\"foo\",\"PValue\":\"bar\"}]}"
}
Obviously the backslashes should be there in this case. Question is, how does one accomplish that?
Code:
using Progress.Json.ObjectModel.JsonObject.
define temp-table ttParameter no-undo
field PName as character
field PValue as character
.
def var jsonMessage as JsonObject no-undo.
def var ttserialized as longchar no-undo.
def var cOut as longchar no-undo.
create ttParameter.
assign
ttParameter.PName = "foo"
ttParameter.PValue = "bar"
.
jsonMessage = new JsonObject().
jsonMessage:Add("data-id", "123").
temp-table ttParameter:write-json("longchar", ttserialized).
jsonMessage:Add("parameters", ttserialized).
jsonMessage:Write(input-output cOut).
message string(cOut) view-as alert-box.
Btw, I'm aware of the REPLACE function in ABL.
bfvo wrote:
When execeuting the code fragment at the end of this message I get some unwanted escape characters:
The output looks like:
{
"data-id":"123",
"parameters":"
{\"ttParameter\":[{\"PName\":\"foo\",\"PValue\":\"bar\"}]}"
}Obviously the backslashes should be there in this case. Question is, how does one accomplish that
Your "parameters" object has one value.
{
"parameters":""
}
The was apparently a JSON object, but since it was inserted as a value the quotes have been escaped.
It is very unlikely that this is what you want.
First of all, In the last sentence you qoute I wanted to write "shouldn't" instead of "should". Indeed I don't want the qoutes to be escaped (hence the title of the message ;-))
My challenge is however that there's no AddJsonFragment (or something similar). The documentation isn't very helpfull in this case but somehow I'm under the impression that what I want is simply not possible which is, imho, an omission...
JSON is begging for interfaces like:
define input parameter cIn as longchar no-undo.
define output parameter cOut as longchar no-undo.
Hello,
I am guessing that you want the property "parameters" to be JSON instead of character (or longchar).
If you add a character (or longchar) to a JSON object, the expected behavior is that double quotes are escaped.
Perhaps, you want something like the following:
/*================= BEGIN =================*/
using Progress.Json.ObjectModel.JsonObject.
using Progress.Json.ObjectModel.JsonArray.
define temp-table ttParameter no-undo
field PName as character
field PValue as character
.
def var httParameter as handle no-undo.
def var jsonMessage as JsonObject no-undo.
def var jsonArray as JsonArray no-undo.
def var cOut as longchar no-undo.
httParameter = TEMP-TABLE ttParameter:HANDLE.
create ttParameter.
assign
ttParameter.PName = "foo"
ttParameter.PValue = "bar"
.
jsonMessage = new JsonObject().
jsonMessage:Add("data-id", "123").
jsonArray = new JsonArray().
jsonArray:Read(httParameter).
jsonMessage:Add("parameters", jsonArray).
jsonMessage:Write(input-output cOut).
message string(cOut) view-as alert-box.
/*================= END =================*/
You can add/insert complex/nested JSON objects to other JSON objects. But this code is adding a longchar to a JSON object.
Hi egarcia (sorry, I don't know your real name),
I failed to grasp that it should be done via a json array and its Read method. Thanks for pointing me in the right direction.
I still think it's hard to find in the docs btw (there's a very short mention of the Read method in the "Working with Json" manual).
Anyway, it works now. Thanks again.
Not sure if you're still considering the RESTful thing or not, but if so I just released an alpha version of a Ruby (DataMapper) adapter for OpenEdge that should make it pretty trivial. You just have to write some models like this and then you could write a small Sinatra or Rails app to do the HTTP servicing. My next blog post will be on exactly how to do this. But at this moment getting from the database model to JSON is super easy:
Customer.first.to_json
Customer.get(5).to_json
Customer.first(:country => "USA").to_json
Customer.all.to_json
etc.
Although we're investigating into all the possiblilties, some choices have already been made.
First of all, we'll be using ExtJS so my assumption is that there's no fit with Ruby on rails (again, this is an assumption).
Second, Our server side architecture is OERA. I don't know if this fit the bill in this case.
The reason for OERA is that we want to decouple our logic from the physical data model. From what I see in you sample you pretty much expose your datamodel to the client which is something we want to avoid at all cost.
This doesn't exclude REST btw.
Anyway, thanks for thinking along.