From ae0eb05f22ac45ab07b6e8f0ef4c3e2cc02fbb1b Mon Sep 17 00:00:00 2001 From: Joe Cheng Date: Sun, 30 Jun 2013 17:14:29 -0700 Subject: [PATCH] Implement serializers for Date and POSIXt --- R/json.R | 13 +++++++++++++ man/toJSON.Rd | 2 ++ 2 files changed, 15 insertions(+) diff --git a/R/json.R b/R/json.R index bc47de2..3e3c896 100644 --- a/R/json.R +++ b/R/json.R @@ -296,3 +296,16 @@ setMethod("toJSON", "environment", toJSON(as.list(x), container, collapse, .level = .level, .withNames = .withNames, .escapeEscapes = .escapeEscapes, asIs = asIs) }) +setMethod("toJSON", "Date", + function(x, container = isContainer(x, asIs, .level), + collapse = "\n", ..., .level = 1L, + .withNames = length(x) > 0 && length(names(x)) > 0, .na = "null", pretty = FALSE, asIs = NA) { + toJSON(as.character(x), container, collapse, ..., .level = .level, .withNames = .withNames, .na = .na, pretty = pretty, asIs = asIs) + }) + +setMethod("toJSON", "POSIXt", + function(x, container = isContainer(x, asIs, .level), + collapse = "\n", ..., .level = 1L, + .withNames = length(x) > 0 && length(names(x)) > 0, .na = "null", pretty = FALSE, asIs = NA) { + toJSON(sub("(\\d{2})(\\d{2})$", "\\1:\\2", format(x, "%Y-%m-%dT%H:%M:%OS6%z")), container, collapse, ..., .level = .level, .withNames = .withNames, .na = .na, pretty = pretty, asIs = asIs) + }) diff --git a/man/toJSON.Rd b/man/toJSON.Rd index 1aee176..56c7e55 100644 --- a/man/toJSON.Rd +++ b/man/toJSON.Rd @@ -16,6 +16,8 @@ \alias{toJSON,factor-method} \alias{toJSON,AsIs-method} \alias{toJSON,environment-method} +\alias{toJSON,Date-method} +\alias{toJSON,POSIXt-method} \alias{emptyNamedList}