Closed gogolxdong closed 2 years ago
This code doesn't even work on the C backend because it is incorrect. What is the output? What is the expected output? What you you think is the cause of the problem? Can you please use the issue template that encourages you to fill in these important questions?
This code doesn't even work on the C backend because it is incorrect. What is the output? What is the expected output? What you you think is the cause of the problem? Can you please use the issue template that encourages you to fill in these important questions?
This works with karax/jjson. I want to use json to replace karax/jjson. output should be something like this in Javascript:
JSON does not allow functions. JSON objects are (almost always) valid javascript object literals, but not every javascript object is representable in JSON (having a member function precludes this, as you noticed). Have a look at the JSON spec, it is pretty simple https://www.json.org/
JSON was born in order to standardize a kind of structure that was common in many, many programming languages, so that we could have a common serialization format. Douglas Crockford decided to standardize the object literal notation used in Javascript (JSON stands for JavaScript Object Notation). But this does not mean that function members, while valid in Javascript, are part of JSON. JSON is a format to represent data, and even if one wanted to enhance it with behaviour, there would be no simple way to serialize functions across programming languages
Then it's possible to construct JsObject with function member and cast it to JsonNode of json module instead of jjson?
import karax/jjson
import jsffi
proc o2() = discard
proc setOption(option:js) =
var console {.importc, nodecl.}: js
console.log(option)
var a = jsFromAst(%*{"formatter": o2,"a":"b"})
echo a.formatter.to(cstring)
echo a.a.to(cstring)
setOption(a)
This is a workaround, to use %* from karax/jjson and convert to JsObject and change every proc takes JsonNode parameters to JsObject type. It seems that jsffi lacks a JsObject literal constructor.
It seems that jsffi lacks a JsObject literal constructor.
Agreed. Usually one defines typed object in Nim and generates literals using the {}
macro https://nim-lang.org/docs/jsffi.html#%7B%7D.m%2Ctypedesc%2Cvarargs%5Buntyped%5D
it's possible to construct JsObject with function member and cast it to JsonNode of json module instead of jjson?
No, this is not possible with any library, in any programming language. JSON just does not store functions, by design.
Usually one defines typed object in Nim and generates literals using the {} macro https://nim-lang.org/docs/jsffi.html#%7B%7D.m%2Ctypedesc%2Cvarargs%5Buntyped%5D
This works for JsObject. It should also support any Nim value including procs. I can't think of a reasonable way that proc members can be included as sugar though.
this should close, it's not part of json spec. cant'be stored to disk nor transport through internet.