i'm using this function and need to pass it an Aeson Value:
{ logLevel : vega.Debug }
this is supposed to refer to an enum in a javascript package that the binding doesn't export.
afaict i'm supposed to use Data.Aeson.QQ.Simple for this, but everything i try that compiles puts quotes around "vega.Debug", which i can't have.
[aesonQQ| { logLevel : "vega.Debug" } |]
what am i missing? is there a way to use encode for this?
In general, Aeson
Values represent JSON objects only, so they don’t support embedded JavaScript expressions, or any other extensions.If this API only accepts
Values, you’re stuck. I think the best solution is to just duplicate the integer value ofvega.Debugand serialise that.Otherwise, a straightforward solution is to make a modified version of
toHtmlWiththat accepts a more flexible input type, such as a string:Then you can call
encodeToLazyTexton your own Aeson values, or include arbitraryTextstrings as needed.If you really want to avoid duplicating the page contents, then you could also call the existing
toHtmlWithwith aValuecontaining a special delimiter that you control, such asString "<user1441998>vega.Debug</user1441998>", and then use that delimiter to postprocess the result:As yet another hack, you could make a
ToJSONinstance for your type that implementstoEncodingbut nottoJSON, and have the encoded value be a JavaScript expression (i.e. invalid JSON). You would want to maketoJSONraise an error so you don’t use it inadvertently.If you want to generate JavaScript code in general, I would have a look at
language-javascript. Instead of producing aValue, produce aJSExpressionand then use one of the pretty-printing functions likerenderToTextto render it. Here’s a sketch of the structure of a possible solution:Your expression would have the form:
The
JSAnnottype would also allow you to include comments in the generated result. Bear in mind that thelanguage-javascriptpretty-printing is likely less well optimised than Aeson’s JSON serialisation.