
The goal ofdir2json is to provide a utility forconverting directories into JSON format and decoding JSON back intodirectory structures. This is particularly useful for archiving,sharing, or analyzing directory contents in a structured format. Thepackage can handle a variety of file types within the directory,including text and binary files (e.g., images, PDFs), converting them toand from JSON. It can beusedwith a Shiny app to allow users to upload files, encode them intoJSON format, and process or download the resulting JSON file. JSON, inthis manner, can be used as a format for sharing multiple files withLLMs.
Note that this JSON representation is it not an alternative to ZIPfiles, but rather a way to represent the contents of a directory in astructured format that can be easily parsed and manipulated. ZIP filesare a superior solution for compressing and archiving files, while JSONis a text-based format that is more suitable for data interchange andmanipulation.
Encode a directory: Convert a directorystructure (including its files) into a JSON object.
Decode a JSON object: Convert a JSON object backinto the original directory structure, restoring files andfolders.
Supports mixed file types: Handles both text andbinary files by encoding binary files in base64 and text files as plaintext.
JSON schema: The structure of the JSON outputfollows a defined schema, allowing for efficient storage and easyparsing.
The JSON schema used bydir2json is very simple andflat. Each file (or directory) is represented by an object with two mainfields:
name: The file or directoryname.
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.
You can install the released version of lzstring fromCRAN with:
install.packages("dir2json")You can install the development version ofdir2json fromGitHub with:
# install.packages("devtools")devtools::install_github("parmsam/dir2json-r")This is a basic example which shows you how to encode a directoryinto JSON and decode it back:
library(dir2json)# Create a temporary directory with a fileexample_dir<-tempfile()dir.create(example_dir)file.create(file.path(example_dir,"example.txt"))#> [1] TRUEwriteLines("Hello, dir2json!",file.path(example_dir,"example.txt"))# Encode the directory to JSONjson_data<-json_encode_dir(example_dir)cat(json_data)#> [{"name":"example.txt","content":"Hello, dir2json!\n"}]# Decode the JSON back to a new directorynew_dir<-tempfile()json_decode_dir(json_data, new_dir)# Verify the contentslist.files(new_dir,recursive =TRUE)#> [1] "example.txt"readLines(file.path(new_dir,"example.txt"))#> [1] "Hello, dir2json!"Encoding a Directory: Thejson_encode_dir function traverses the directoryrecursively, reading each file and determining if it is a text or binaryfile. Text files are directly stored as their contents in the JSON,while binary files are base64-encoded for safe transport within the JSONformat.
Decoding a JSON Object: Thejson_decode_dir function reads the JSON object, restoresthe directory structure, and writes files back to the file system.Binary files are decoded from base64 back to their original binaryformat, and text files are written as plain text.