Skip to contents

This function serializes R data to JSON format using a specified structure. The main purpose is to provide a type-safe way to convert R objects into JSON strings that conform to a predefined structure.

Since R is an array language, many typical serialization tasks can make interoperability with other languages difficult. It is not uncommon for an API to expect a scalar value, but R will serialize it as a length-1 vector.

This function is designed to handle such cases by providing a structure that defines the expected types and formats of the data. The structure is defined using the build_structure function, which allows you to specify the types of each field in the data. The function will then serialize the data according to this structure, ensuring that the output is in the correct format.

Usage

serialize_json(data, structure, pretty = FALSE)

Arguments

data

The data to serialize

structure

The structure to use for serialization

pretty

Whether to pretty-print the JSON

Value

A JSON string

Examples

# Example usage
data <- list(name = "Test", value = 123L, active = TRUE)
structure <- build_structure(s_map(
  name = s_string(),
  value = s_integer(),
  active = s_logical()
))
json_string <- serialize_json(data, structure)
print(json_string)
#> {"value":123,"name":"Test","active":true}