So I have been working with a javascript based lib...
# cfml-general
p
So I have been working with a javascript based library for some front-end UI stuff and
serializeJSON()
is super helpful to out my data for use. But my problem is that serializeJSON wraps the name and every value-set with double quotes. Is it possible to modify this or some examples for modifying the outcome of serializeJSON? I havent found much about implementing the
*useCustomSerializer*
arg.
For real use case, the JS library is expecting the name's to not be double quoted. And specific values to not have double quotes such as my
coordinates
value to not be a string but rather just
[-75.9999,36.9999]
but it serializes as
"[-75.9999,36.9999]"
c
Hmm, for the coordinates, something sounds wrong. I tried the following at trycf.com and it doesn't double quote the array of numerics.
Copy code
<cfscript>
var myStruct = {
                    'coordinates': [-75.9999,36.9999]
                };

dump(serializeJson(myStruct));
</cfscript>
p
They are selected from the DB in this manner:
CONCAT('[', al.lng, ',', al.lat, ']') as COORDS
which makes it a string
c
As for your first question, check out https://helpx.adobe.com/coldfusion/developing-applications/changes-in-coldfusion/restful-web-services-in-coldfusion.html#RESTfulWebSe[…]deserializer , search for "The below CustomSerializer code sample will help you to achieve the result for the scenario explained above (Customizing serialization /deserialization of phone directory):" in the page
g
You could try
.toJSON()
<https://docs.lucee.org/reference/objects/any/tojson.html>
The example code - would seem to show it gives you the format you're after.
c
@gavinbaumanis I believe
.toJSON()
is the same as
serializeJson()
?
👍🏼 1
g
Changing the original code "stringify" the array of numbers,
st.data = "[1,2,3,4,5]";
Only produces a single set of quotes - not a doubled-up version.
The plot thickens.... Using serializeJSON with the quoted array
writeDump(serializeJson(st.data));
On tryCF - also produces a single quote version... But I also see in my own use of serializeJSON - that I use for logging / dumping :- "double-double" quotes, too. Weird indeed!
c
@Patrick By using concat, your DB is returning a string. CAST it as an array of integers and it should solve that issue. As for your other problem, can you use
serializeJSON
, replace the double quotes with nothing, and then return the resultant string? Or does your database support native JSON conversion (though I imagine if it does, it does it to the JSON standard)?
p
So I think I just figured it out my solution
JSON.parse(mycolumnvalue)
that reads my string column that serializeJSON creates and actually parse it on the JS side no problem
I am pretty restricted on the legacy DB and query but running serializejson and then just parsing the string on the JS side with that seems to avoid rewriting a bunch shew. Thanks for the suggestions guys.
c
Ah, yes, it is the Javascript equivalent of CF
deserializeJSON()
. I assumed you had already parsed the JSON string in Javascript before handing off to your library & you were still getting the double quoting after deserialization. BTW, check out: https://www.w3schools.com/js/js_json_parse.asp and the
reviver
parameter. Looks interesting - I didn't know this parameter existed for JSON.parse.
Copy code
const text = '{"name":"John", "birth":"1986-12-14", "city":"New York"}';
const obj = JSON.parse(text, function (key, value) {
  if (key == "birth") {
    return new Date(value);
  } else {
    return value;
  }
});