tests/testthat/test-toJSON-NULL-values.R

test_that("Test NULL values", {
  namedlist <- structure(list(), .Names = character(0))
  x <- NULL
  y <- list(a = NULL, b = NA)
  z <- list(a = 1, b = character(0))

  expect_true(validate(toJSON(x)))
  expect_equal(fromJSON(toJSON(x)), namedlist)
  expect_equal(toJSON(x), "{}")
  expect_equal(toJSON(x, null = "list"), "{}")

  expect_true(validate(toJSON(y)))
  expect_equal(toJSON(y, null = "list"), "{\"a\":{},\"b\":[null]}")
  expect_equal(toJSON(y, null = "null"), "{\"a\":null,\"b\":[null]}")
  expect_equal(fromJSON(toJSON(y, null = "null")), y)
  expect_equal(fromJSON(toJSON(y, null = "list")), list(a = namedlist, b = NA))

  expect_true(validate(toJSON(z)))
  expect_equal(toJSON(z), "{\"a\":[1],\"b\":[]}")
  expect_equal(fromJSON(toJSON(z)), list(a = 1, b = list()))
})

Try the jsonlite package in your browser

Any scripts or data that you put into this service are public.

jsonlite documentation built on April 4, 2025, 5:24 a.m.