Skip to content

Conversation

def-
Copy link
Member

@def- def- commented Jan 5, 2015

Some people said it's a pain to generate JSON in Nim. Since Nim is statically typed you can't just write

var j = [{"name": "John", "age": 30}, {"name": "Susan", "age": 31}]

Instead you have to add a % everywhere to convert to JSON:

var j = %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}]

That's annoying if you just want to paste some actual JSON into your source and set a few variables. The new %* macro automatically adds % where necessary. So you can now write:

var j = %*[{"name": "John", "age": 30}, {"name": "Susan", "age": 31}]

Or even with pretty indentation of your choice:

var j2 = %*
  [
    {
      "name": "John",
      "age": 30
    },
    {
      "name": "Susan",
      "age": 31
    }
  ]
echo j2

var name = "John"
let herAge = 30
const hisAge = 31

var j3 = %*
  [ { "name": name
    , "age": herAge
    }
  , { "name": "Susan"
    , "age": hisAge
    }
  ]
echo j3

@Araq
Copy link
Member

Araq commented Jan 5, 2015

Funny, I thought we already had that!

@gradha
Copy link
Contributor

gradha commented Jan 5, 2015

Funny, I also thought we had better docs for this module!

@def-
Copy link
Member Author

def- commented Jan 5, 2015

Nice work on the documentation @gradha .

@Varriount
Copy link
Contributor

@def- You still have some debugging echo's left in the new macro.

@Araq
Copy link
Member

Araq commented Jan 5, 2015

@gradha Sorry, I had too many merge conflicts with these better docs, so I deliberately lost them. Also, IMHO they were too verbose and you copied stuff like how the 'assert' works to the description of every accessor. You should have documented that once in the module's top level comment.

@Varriount
Copy link
Contributor

So what's the final verdict on this? Does another procedure already exists that does the same thing @def- 's macro does?

@def-
Copy link
Member Author

def- commented Jan 6, 2015

@Varriount: A procedure couldn't do this and I don't see any macros or templates in the json module.

@flaviut
Copy link
Contributor

flaviut commented Jan 10, 2015

Does this need to be an operator? I'd prefer a regular word; jsonify comes to mind.

Brevity isn't really important here since only one call would be needed for an entire JSON object.

@def-
Copy link
Member Author

def- commented Jan 10, 2015

I guess it doesn't have to be. I just like %* because it applies % everywhere.

@Araq
Copy link
Member

Araq commented Jan 12, 2015

I like %* better than jsonify.

@dom96
Copy link
Contributor

dom96 commented Jan 12, 2015

👍 for %*

@dustinlacewell
Copy link
Contributor

Putting my vote in for jsonify

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The convention is to use PNimrodNode for .compileTime procs.

@def-
Copy link
Member Author

def- commented Feb 4, 2015

Speaking of which, can we not distinguish between '%*' and '%'? That makes it much easier to use IMHO. Overloading resolution should be able to handle that, right?

That might be nice, but doesn't work. The compiler complains because it tries to parse these as an array instead of resorting to the more general % macro on failure:

  var j = % [{"name": "John", "age": 30}, {"name": "Susan", "age": 31}]
lib/pure/json.nim(1133, 35) Error: type mismatch: got (tuple[string, int]) but expected 'tuple[string, string]'

Araq added a commit that referenced this pull request Feb 7, 2015
@Araq Araq merged commit b253467 into nim-lang:devel Feb 7, 2015
@def- def- deleted the json-stuff branch February 17, 2015 02:58
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

Successfully merging this pull request may close these issues.

7 participants