https://github.com/ijlyttle/srlze
Serialize Lists into Text-Lists
https://github.com/ijlyttle/srlze
Last synced: 12 months ago
JSON representation
Serialize Lists into Text-Lists
- Host: GitHub
- URL: https://github.com/ijlyttle/srlze
- Owner: ijlyttle
- License: other
- Created: 2017-01-07T14:42:42.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-14T12:51:31.000Z (over 9 years ago)
- Last Synced: 2025-07-14T13:40:04.933Z (12 months ago)
- Language: R
- Homepage: https://ijlyttle.github.io/srlze/
- Size: 106 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- License: LICENSE
Awesome Lists containing this project
README
---
output: github_document
---
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
[](https://cran.r-project.org/package=srlze)
[](https://travis-ci.org/ijlyttle/srlze)
[](https://codecov.io/github/ijlyttle/srlze?branch=master)
# srlze
Serialzing lists into text-lists can make it easier to work with web-service APIs. This package can help you work with http-query lists in the "R world", helping you serialize it for use in the "remote world". In many cases, parameters for http queries are formatted differently from R:
- logicals are expressed as `"true"` or `"false"`
- time durations are specified in ms (R uses difftime, or **lubridate** durations or periods)
- vectors are expressed using delimited strings (often delimited by commas)
The goal of this package is to make that translation process easier.
## Installation
You can install **srlze** from github with:
```{r gh-installation, eval = FALSE}
# install.packages("devtools")
devtools::install_github("ijlyttle/srlze")
```
## Example
```{r}
library("httr")
library("lubridate")
library("srlze")
```
Let's consider a web-serice endpoint that accepts query-parameters in the format described above. You wish to work with the query-parameters in an R-sensible way. The function `serialize_list()` is used to serialize the elements of your list into the format expected by the web-service.
```{r example, message=FALSE}
query_params <- list(
delay = dseconds(3),
print = TRUE,
next_steps = c("collate", "send"),
number = 20
)
serialize_list(query_params)
```
Thus, you can use the **httr** package to build your URL, attaching the query.
```{r httr}
url <- parse_url("https://useful.site.com/service")
url$query <- serialize_list(query_params)
build_url(url)
```
If you need to change the default behavior of the serializer, the `serialize_list()` function lets you set the delimiter. It also lets you set the `locale()`, which is used to specify the format for individual types (like logical and time-difference).
There are other situations where list-serialization may be useful. For example if you are writing HTML elements for use with JavaScript, you will often have to set parameters like `data-foo = "true"`. You can use `serialize_list()` to help you think in the R world, then write to the HTML/JavaScript world.
## Code of Conduct
Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md). By participating in this project you agree to abide by its terms.