#2312 Serialising JSON functions

SlimerDude Tue 8 Jul 2014

Is there a way to serialise functions() using util::JsonOutStream?

For example, I'd like to create a map which, when written, produces:

{
  "key1": "val",
  "key2": 42,
  "func": function() { alert('wotever') }
}

It seems only possible to create string values:

map  := ["key1":"val", "key2":42, "func", "function() { alert('wotever') }"]
json := JsonOutStream.writeJsonToStr(map)

=>

{
  "key1": "val",
  "key2": 42,
  "func": "function() { alert('wotever') }"
}

?

brian Tue 8 Jul 2014

Is there a way to serialise functions() using util::JsonOutStream?

That wouldn't be JSON :) JSON is actually formally defined to only support maps, lists, and the three scalar types numbers, string literals, and booleans.

It sounds like what you might want is something to generate JavaScript source code. In compilerJs we have JsWriter, but for the most part its just hand written code using normal OutStream methods.

Login or Signup to reply.