Json stuff #1869

Merged
merged 4 commits into from Feb 7, 2015

Conversation

Projects
None yet
7 participants
@def-
Member

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

This comment has been minimized.

Show comment
Hide comment
@Araq

Araq Jan 5, 2015

Member

Funny, I thought we already had that!

Member

Araq commented Jan 5, 2015

Funny, I thought we already had that!

@gradha

This comment has been minimized.

Show comment
Hide comment
@gradha

gradha Jan 5, 2015

Contributor

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

Contributor

gradha commented Jan 5, 2015

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

@def-

This comment has been minimized.

Show comment
Hide comment
@def-

def- Jan 5, 2015

Member

Nice work on the documentation @gradha .

Member

def- commented Jan 5, 2015

Nice work on the documentation @gradha .

@Varriount

This comment has been minimized.

Show comment
Hide comment
@Varriount

Varriount Jan 5, 2015

Contributor

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

Contributor

Varriount commented Jan 5, 2015

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

@Araq

This comment has been minimized.

Show comment
Hide comment
@Araq

Araq Jan 5, 2015

Member

@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.

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

This comment has been minimized.

Show comment
Hide comment
@Varriount

Varriount Jan 6, 2015

Contributor

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

Contributor

Varriount commented Jan 6, 2015

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

@def-

This comment has been minimized.

Show comment
Hide comment
@def-

def- Jan 6, 2015

Member

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

Member

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

This comment has been minimized.

Show comment
Hide comment
@flaviut

flaviut Jan 10, 2015

Contributor

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.

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-

This comment has been minimized.

Show comment
Hide comment
@def-

def- Jan 10, 2015

Member

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

Member

def- commented Jan 10, 2015

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

@Araq

This comment has been minimized.

Show comment
Hide comment
@Araq

Araq Jan 12, 2015

Member

I like %* better than jsonify.

Member

Araq commented Jan 12, 2015

I like %* better than jsonify.

@dom96

This comment has been minimized.

Show comment
Hide comment
@dom96

dom96 Jan 12, 2015

Member

👍 for %*

Member

dom96 commented Jan 12, 2015

👍 for %*

@dustinlacewell

This comment has been minimized.

Show comment
Hide comment
@dustinlacewell

dustinlacewell Jan 12, 2015

Contributor

Putting my vote in for jsonify

Contributor

dustinlacewell commented Jan 12, 2015

Putting my vote in for jsonify

lib/pure/json.nim
@@ -625,6 +644,29 @@ proc `%`*(elements: openArray[JsonNode]): JsonNode =
newSeq(result.elems, elements.len)
for i, p in pairs(elements): result.elems[i] = p
+proc toJson(x: expr): expr {.compiletime.} =

This comment has been minimized.

@Araq

Araq Feb 4, 2015

Member

The convention is to use PNimrodNode for .compileTime procs.

@Araq

Araq Feb 4, 2015

Member

The convention is to use PNimrodNode for .compileTime procs.

lib/pure/json.nim
+ result = prefix(result, "%")
+
+macro `%*`*(x: expr): expr =
+ ## Convert an expression to a JsonParser directly, without having to specify

This comment has been minimized.

@Araq

Araq Feb 4, 2015

Member

What? to a JsonParser?

@Araq

Araq Feb 4, 2015

Member

What? to a JsonParser?

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

This comment has been minimized.

@Araq

Araq Feb 4, 2015

Member

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?

@Araq

Araq Feb 4, 2015

Member

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?

@def-

This comment has been minimized.

Show comment
Hide comment
@def-

def- Feb 4, 2015

Member

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]'
Member

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 def-:json-stuff branch Feb 17, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment