tests/testthat/test-toJSON-logical.R

context("toJSON Logical")

test_that("Encoding Logical", {
  expect_that(toJSON(TRUE), equals("[true]"));
  expect_that(toJSON(FALSE), equals("[false]"));
  expect_that(toJSON(as.logical(NA)), equals("[null]"))
  expect_that(toJSON(as.logical(NA), na="string"), equals("[\"NA\"]"))
  expect_that(toJSON(c(TRUE, NA, FALSE)), equals("[true,null,false]"));
  expect_that(toJSON(c(TRUE, NA, FALSE), na="string"), equals("[true,\"NA\",false]"));
  expect_that(toJSON(logical()), equals("[]"));
});

test_that("Encoding Logical in Data Frame", {
  expect_that(toJSON(data.frame(foo=TRUE)), equals("[{\"foo\":true}]"));
  expect_that(toJSON(data.frame(foo=FALSE)), equals("[{\"foo\":false}]"));
  expect_that(toJSON(data.frame(foo=as.logical(NA))), equals("[{}]"));
  expect_that(toJSON(data.frame(foo=as.logical(NA)), na="null"), equals("[{\"foo\":null}]"));
  expect_that(toJSON(data.frame(foo=as.logical(NA)), na="string"), equals("[{\"foo\":\"NA\"}]"));
  expect_that(toJSON(data.frame(foo=c(TRUE, NA, FALSE))), equals("[{\"foo\":true},{},{\"foo\":false}]"));
  expect_that(toJSON(data.frame(foo=logical())), equals("[]"));
});

Try the jsonlite package in your browser

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

jsonlite documentation built on July 9, 2023, 6:11 p.m.