Movatterモバイル変換


[0]ホーム

URL:


dir2jsondir2json website

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.

Features

JSON Schema

The JSON schema used bydir2json is very simple andflat. Each file (or directory) is represented by an object with two mainfields:

Installation

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")

Example

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!"

JSON Encoding and DecodingProcess

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.


[8]ページ先頭

©2009-2025 Movatter.jp