Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

% JsonNode operator cast true and false literal value to 1 and 0 #104

Closed
cn-care opened this issue Jul 10, 2019 · 2 comments
Closed

% JsonNode operator cast true and false literal value to 1 and 0 #104

cn-care opened this issue Jul 10, 2019 · 2 comments

Comments

@cn-care
Copy link

cn-care commented Jul 10, 2019

import karax / [jjson, kajax, karax]

var option* = %*{
    "backgroundColor": "#404a59",
    "title": {
        "text": "",
        "left": "center",
        "textStyle": {
            "color": "#fff"
        }
    },
    "tooltip": {
        "trigger": "item",
        "formatter": formatter,
        "textStyle": {
            "color": "#fff",
            "fontSize": 12,
            "fontWeight": "normal"
        },
        "padding": [10, 15],
    },
    "geo": {
        "map": "world",
        "label": {
            "emphasis": {
                "show": true,
                "color": "#fff"
            }
        },
        "roam": defaults["roam"],
        "zoom": defaults["zoom"],
        "center": defaults["center"],
        "itemStyle": {
            "normal": {
                "areaColor": "#323c48",
                "borderColor": "#404a59"
            },
            "emphasis": {
                "areaColor": "#2a333d"
            }
        }
    },
    "series": []
}

In many scenes, bool literal value is expected rather than 1 or 0.
Have to set bool value manually as
option["geo"]["label"]["emphasis"]["show"] = true

@moigagoo
Copy link
Contributor

Is it related to nim-lang/Nim#7375?

@ajusa
Copy link
Collaborator

ajusa commented May 2, 2021

I think that this is a solution:

import karax / [jjson, kajax, karax]
import jsffi
var option* = %*{
  "test": true
}
var option2* = js{
  "test": true
}
include karax/prelude
proc render(): VNode =
  buildHtml(tdiv):
    kout option
    kout option2
    kout option2["test"]
setRenderer render

And the output in the console looks like:

{test: 1} //from jjson
{test: true} //from jsffi
true //getting value by key from jsffi

So, use jsffi instead of jjson. Both of them should have similar performance characteristics, but jsffi is part of stdlib now (not sure if existed back when this issue was created) so I would recommend it over jjson. I've successfully used jsffi to create a similar options js object for an external library.

One thing to be careful of though when using it with karax is that

var option2* = js{
  "test": true,
  "test2": "hey now"
} # bad

var option2* = js{
  "test": true,
  "test2": kstring"hey now"
} # good

If you use the first one, then the string is interpreted as a nim string which means it becomes an array of numbers, whereas with the second one it is treated correctly on both the js and the c backends.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants