<p>A new version of <a href="https://cran.r-project.org/web/packages/jsonlite/index.html">jsonlite</a> package to CRAN. This is a maintenance release with enhancements and bug fixes. A summary of changes in v1.2 from the <a href="https://cran.r-project.org/web/packages/jsonlite/NEWS">NEWS</a> file:</p>
<ul><li>Add <code>read_json</code> and <code>write_json</code> convenience wrappers, <a href="https://github.com/jeroenooms/jsonlite/issues/161">#161</a></li>
<li>Update <code>modp_numtoa</code> from upstream, fixes a rounding issue in <a href="https://github.com/jeroenooms/jsonlite/issues/148">#148</a>.</li>
<li>Ensure <code>asJSON.POSIXt</code> does not use sci notation for negative values, <a href="https://github.com/jeroenooms/jsonlite/issues/155">#155</a></li>
<li>Tweak <code>num_to_char</code> to properly print large negative numbers</li>
<li>Performance optimization for simplyfing data frames (see below)</li>
</ul><p>Use the Github compare page to see the full diff on <a href="https://github.com/cran/jsonlite/compare/1.1...1.2">metacran</a>.</p>
<h2>New read/write API</h2>
<p>The package has gained new high level functions <code>read_json</code> and <code>write_json</code>. These are <a href="https://github.com/cran/jsonlite/blob/1.2/R/read_json.R#L18-L29">wrappers</a> for <code>fromJSON</code> and <code>toJSON</code> which read/write json directly from/to disk. This API is consistent with tidyverse packages like readr, readxl and haven (see <a href="https://github.com/jeroenooms/jsonlite/issues/161">#161</a>).</p>
<p>The only thing to note is that <code>read_json</code> does not simplify by default, as is done by <code>fromJSON</code>. For example:</p>
<div><pre><code><span></span><span># Write Data frame to a temp file</span>
tmp <span>&lt;-</span> <span>tempfile</span><span>()</span>
write_json<span>(</span>iris<span>,</span> tmp<span>)</span>

<span># Nested lists</span>
read_json<span>(</span>tmp<span>)</span>

<span># A data frame</span>
read_json<span>(</span>tmp<span>,</span> simplifyVector <span>=</span> <span>TRUE</span><span>)</span>
</code></pre></div>
<p>Notice how <code>read_json</code> only returns a data frame when <code>simplifyVector</code> is explicitly set to <code>TRUE</code>.</p>
<h2>Performance enhancements</h2>
<p>We have ported a bit of C code to optimize simplification for data frame structures. This script compares performance for both versions:</p>
<div><pre><code><span></span><span># example json</span>
json <span>&lt;-</span> jsonlite<span>::</span>toJSON<span>(</span>ggplot2<span>::</span>diamonds<span>)</span>

<span># Test with jsonlite 1.1</span>
devtools<span>::</span>install_github<span>(</span><span>&quot;cran/jsonlite@1.1&quot;</span><span>)</span>
microbenchmark<span>::</span>microbenchmark<span>(</span>jsonlite<span>::</span>fromJSON<span>(</span>json<span>),</span> times <span>=</span> <span>50</span><span>)</span>

<span># Unload jsonlite 1.1 (might need restart R on windows)</span>
<span>unloadNamespace</span><span>(</span><span>&quot;jsonlite&quot;</span><span>)</span>
<span>library.dynam.unload</span><span>(</span><span>'jsonlite'</span><span>,</span> <span>find.package</span><span>(</span><span>'jsonlite'</span><span>))</span>

<span># Test with jsonlite 1.2</span>
devtools<span>::</span>install_github<span>(</span><span>&quot;cran/jsonlite@1.2&quot;</span><span>)</span>
microbenchmark<span>::</span>microbenchmark<span>(</span>jsonlite<span>::</span>fromJSON<span>(</span>json<span>),</span> times <span>=</span> <span>50</span><span>)</span>
</code></pre></div>
<p>On my Macbook this has reduced the median time from approx 0.91s to 0.76s.</p>