https://github.com/parmsam/dir2json-r
Package for converting directories into JSON format and decoding JSON back into directory structures with R
https://github.com/parmsam/dir2json-r
Last synced: 7 months ago
JSON representation
Package for converting directories into JSON format and decoding JSON back into directory structures with R
- Host: GitHub
- URL: https://github.com/parmsam/dir2json-r
- Owner: parmsam
- License: other
- Created: 2025-05-08T02:19:38.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-23T20:44:06.000Z (12 months ago)
- Last Synced: 2025-10-17T23:43:55.328Z (7 months ago)
- Language: R
- Homepage: https://parmsam.github.io/dir2json-r/
- Size: 4.57 MB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
README
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
The goal of `dir2json` is to provide a utility for converting directories into JSON format and decoding JSON back into directory structures. This is particularly useful for archiving, sharing, or analyzing directory contents in a structured format. The package can handle a variety of file types within the directory, including text and binary files (e.g., images, PDFs), converting them to and from JSON. It can be [used with a Shiny app](https://parmsam.github.io/dir2json-r/articles/shiny.html) to allow users to upload files, encode them into JSON format, and process or download the resulting JSON file. JSON, in this manner, can be used as a format for sharing multiple files with LLMs.
Note that this JSON representation is it not an alternative to ZIP files, but rather a way to represent the contents of a directory in a structured format that can be easily parsed and manipulated. ZIP files are a superior solution for compressing and archiving files, while JSON is a text-based format that is more suitable for data interchange and manipulation.
## Features
- **Encode a directory**: Convert a directory structure (including its files) into a JSON object.
- **Decode a JSON object**: Convert a JSON object back into the original directory structure, restoring files and folders.
- **Supports mixed file types**: Handles both text and binary files by encoding binary files in base64 and text files as plain text.
- **JSON schema**: The structure of the JSON output follows a defined schema, allowing for efficient storage and easy parsing.
### JSON Schema
The JSON schema used by `dir2json` is very simple and flat. Each file (or directory) is represented by an object with two main fields:
- **`name`**: The file or directory name.
- **`content`**: The content of the file. For text files, this is the raw content as a string. For binary files, it is a base64-encoded string.
## Installation
You can install the released version of lzstring from [CRAN](https://CRAN.R-project.org/package=dir2json) with:
```r
install.packages("dir2json")
```
You can install the development version of `dir2json` from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("parmsam/dir2json-r")
```
## Example
This is a basic example which shows you how to encode a directory into JSON and decode it back:
```{r example}
library(dir2json)
# Create a temporary directory with a file
example_dir <- tempfile()
dir.create(example_dir)
file.create(file.path(example_dir, "example.txt"))
writeLines("Hello, dir2json!", file.path(example_dir, "example.txt"))
# Encode the directory to JSON
json_data <- json_encode_dir(example_dir)
cat(json_data)
# Decode the JSON back to a new directory
new_dir <- tempfile()
json_decode_dir(json_data, new_dir)
# Verify the contents
list.files(new_dir, recursive = TRUE)
readLines(file.path(new_dir, "example.txt"))
```
## JSON Encoding and Decoding Process
**Encoding a Directory**: The `json_encode_dir` function traverses the directory recursively, reading each file and determining if it is a text or binary file. Text files are directly stored as their contents in the JSON, while binary files are base64-encoded for safe transport within the JSON format.
**Decoding a JSON Object**: The `json_decode_dir` function reads the JSON object, restores the directory structure, and writes files back to the file system. Binary files are decoded from base64 back to their original binary format, and text files are written as plain text.
