{"action":"created","issue":{"url":"https://api.github.com/repos/jeroenooms/jsonlite/issues/133","repository_url":"https://api.github.com/repos/jeroenooms/jsonlite","labels_url":"https://api.github.com/repos/jeroenooms/jsonlite/issues/133/labels{/name}","comments_url":"https://api.github.com/repos/jeroenooms/jsonlite/issues/133/comments","events_url":"https://api.github.com/repos/jeroenooms/jsonlite/issues/133/events","html_url":"https://github.com/jeroenooms/jsonlite/issues/133","id":158601261,"number":133,"title":"Need more accurate control of rendering scalar or array","user":{"login":"renkun-ken","id":4662568,"avatar_url":"https://avatars.githubusercontent.com/u/4662568?v=3","gravatar_id":"","url":"https://api.github.com/users/renkun-ken","html_url":"https://github.com/renkun-ken","followers_url":"https://api.github.com/users/renkun-ken/followers","following_url":"https://api.github.com/users/renkun-ken/following{/other_user}","gists_url":"https://api.github.com/users/renkun-ken/gists{/gist_id}","starred_url":"https://api.github.com/users/renkun-ken/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/renkun-ken/subscriptions","organizations_url":"https://api.github.com/users/renkun-ken/orgs","repos_url":"https://api.github.com/users/renkun-ken/repos","events_url":"https://api.github.com/users/renkun-ken/events{/privacy}","received_events_url":"https://api.github.com/users/renkun-ken/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-06-06T05:05:03Z","updated_at":"2016-06-06T08:19:15Z","closed_at":"2016-06-06T08:19:15Z","body":"Suppose the following json:\r\n\r\n```json\r\n{\r\n  \"x\": 1,\r\n  \"y\": [1, 2, 3],\r\n  \"z\": [ { \"a\": 1, \"b\": 2}, { \"a\": 2, \"b\": 3} ],\r\n  \"w\": {\r\n    \"p\": [ 1 ],\r\n    \"q\": {\r\n      \"m1\": [1, 2, 3],\r\n      \"m2\": \"hello\"\r\n    }\r\n  }\r\n}\r\n```\r\n\r\nIf a json is only used within R, whether `w.p` is a vector or a scalar does not matter. But if this produced json is to be read from C++ or other strong type languages, it does matter to distinguish a vector from a scalar.\r\n\r\nCurrently, `toJSON(..., auto_unbox = TRUE)` produces scalar representation from one-element vector. But in many cases, we need more accurate control. For example, we may need `x` to be scalar, but `w.p` to be vector no matter whether it is supplied one or more elements.\r\n\r\nIt would be nice to have a function to hint the json generator like\r\n\r\n```r\r\ntoJSON(list(\r\n  x = scalar(1),\r\n  y = c(1, 2, 3),\r\n  z = list(list(a = scalar(1), b = scalar(2)), list(a = scalar(2), b = scalar(2))),\r\n  w = list(p = 1, q = list(m1 = c(1, 2, 3), m2 = scalar(\"hello\")))\r\n))\r\n```\r\n\r\nwhere `scalar()` hints that the value must be rendered as scalar value, everything else produces vector;\r\n\r\nor \r\n\r\n```r\r\ntoJSON(list(\r\n  x = 1,\r\n  y = c(1, 2, 3),\r\n  z = list(list(a = 1), b = 2), list(a = 2, b = 2)),\r\n  w = list(p = json_array(1), q = list(m1 = c(1, 2, 3), m2 = \"hello\"))\r\n))\r\n```\r\n\r\nwhere `json_array()` hints that the value must produce an array representation, otherwise, one-element value should produce a scalar, and zero-element and multi-element vector should produce array.\r\n\r\nI'm not sure if it is a good design, but if it supports more accurate control over whether to produce a scalar or an array on a field basis, it would certainly make the produced json much more predictable and more easily for other strong-typed languages to work with."},"comment":{"url":"https://api.github.com/repos/jeroenooms/jsonlite/issues/comments/223895675","html_url":"https://github.com/jeroenooms/jsonlite/issues/133#issuecomment-223895675","issue_url":"https://api.github.com/repos/jeroenooms/jsonlite/issues/133","id":223895675,"user":{"login":"renkun-ken","id":4662568,"avatar_url":"https://avatars.githubusercontent.com/u/4662568?v=3","gravatar_id":"","url":"https://api.github.com/users/renkun-ken","html_url":"https://github.com/renkun-ken","followers_url":"https://api.github.com/users/renkun-ken/followers","following_url":"https://api.github.com/users/renkun-ken/following{/other_user}","gists_url":"https://api.github.com/users/renkun-ken/gists{/gist_id}","starred_url":"https://api.github.com/users/renkun-ken/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/renkun-ken/subscriptions","organizations_url":"https://api.github.com/users/renkun-ken/orgs","repos_url":"https://api.github.com/users/renkun-ken/repos","events_url":"https://api.github.com/users/renkun-ken/events{/privacy}","received_events_url":"https://api.github.com/users/renkun-ken/received_events","type":"User","site_admin":false},"created_at":"2016-06-06T08:19:15Z","updated_at":"2016-06-06T08:19:15Z","body":"That's super! Sorry for my ignorance :)"}}
{"actor":{"display_login":"renkun-ken"}}