nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.58k stars 1.47k forks source link

Proc Members in Json in the JS Backend #12494

Closed gogolxdong closed 2 years ago

gogolxdong commented 5 years ago
import karax/jjson
proc o2(param: JsonNode): cstring =
    result = param["data"][0].getStr & "<br/>"
    for i in param["data"][3]:
        result = result & i[1].getStr & " : " & i[2].getStr & "<br/>"

var datacenterOption* = %*{
    "backgroundColor": "#404a59",
    "tooltip": {
        "formatter": o2,
    },
    "geo": {
        "roam": true,
        "map": "world",
        "emphasis": {
            "label": {
                "color": "#fff"
        }
    },
        "scaleLimit": {
            "min": 1,
            "max": 10
        },
    },
    "itemStyle": {
        "areaColor": "#323c48",
        "borderColor": "#404a59"
    },
    "emphasis": {
        "itemStyle": {
            "areaColor": "#2a333d"
        }
    },
    "dataset": {
        "source": datacenter,
    },
    "series": [
        {
        "emphasizes": {
            "label": {
                "formatter": "{@[0]}"
            }
        },
        "type": "scatter",
        "coordinateSystem": "geo",
        "encode": {
            "lng": 1,
            "lat": 2
        }
    }
    ]
}
krux02 commented 5 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?

gogolxdong commented 5 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 works with karax/jjson. I want to use json to replace karax/jjson. output should be something like this in Javascript: 图片

andreaferretti commented 5 years ago

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

gogolxdong commented 5 years ago

Then it's possible to construct JsObject with function member and cast it to JsonNode of json module instead of jjson?

gogolxdong commented 5 years ago
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.

andreaferretti commented 5 years ago

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.

metagn commented 2 years ago

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.

bung87 commented 2 years ago

this should close, it's not part of json spec. cant'be stored to disk nor transport through internet.