-- pass a string, list, record or number, and either a path to save the result to, or missing value to have it returned as text
on
convertASToJSON:someASThing
saveTo:posixPath
--convert to JSON data
set {theData,
theError}
to current application's
NSJSONSerialization's
dataWithJSONObject:someASThing
options:0
|error|:(reference)
if
theData is
missing value then error (theError's
localizedDescription() as
text)
number -10000
if
posixPath
is missing value
then -- return string
-- convert data to a UTF8 string
set
someString
to current application's
NSString's
alloc()'s
initWithData:theData
encoding:(current application's
NSUTF8StringEncoding)
return
someString as
text
else
-- write data to file
theData's
writeToFile:posixPath
atomically:true
return
result
as boolean
-- returns false if save failed
end
if
end
convertASToJSON:saveTo: