| Title: | Convert Directory to JSON |
|---|---|
| Description: | Convert a directory structure into a JSON format. This package lets you recursively traverse a directory and convert its contents into a JSON object, making it easier to import code base from file systems into large language models. |
| Authors: | Sam Parmar [aut, cre, cph] |
| Maintainer: | Sam Parmar <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-05-24 09:23:09 UTC |
| Source: | https://github.com/parmsam/dir2json-r |
This function converts a file into a structured list for encoding.
as_file_list(path, name = fs::path_file(path), type = NULL)as_file_list(path, name = fs::path_file(path), type = NULL)
path |
A character string specifying the file path. |
name |
An optional character string specifying the file name. |
type |
An optional character string specifying the file type ("text" or "binary"). |
A structured list representing the file.
## Not run: # Convert a text file to a list as_file_list("file.txt") # Convert a binary file to a list as_file_list("image.jpg", type = "binary") ## End(Not run)## Not run: # Convert a text file to a list as_file_list("file.txt") # Convert a binary file to a list as_file_list("image.jpg", type = "binary") ## End(Not run)
This function decodes a JSON string into a directory structure.
json_decode_dir(json_data, dir)json_decode_dir(json_data, dir)
json_data |
A JSON string representing the directory's contents. |
dir |
A character string specifying the target directory. |
None. Creates files in the specified directory.
## Not run: # Decode JSON back into a directory json_decode_dir(json_str, "output_dir") ## End(Not run)## Not run: # Decode JSON back into a directory json_decode_dir(json_str, "output_dir") ## End(Not run)
This function encodes all files in a directory into a JSON format.
json_encode_dir( dir, type = c("text", "binary"), metadata = NULL, ignore = NULL )json_encode_dir( dir, type = c("text", "binary"), metadata = NULL, ignore = NULL )
dir |
A character string specifying the directory to encode. |
type |
A character vector specifying the file types to include ('text', 'binary', or both). One or both of the following options: * 'text': Text files (e.g., '.txt', '.csv', '.json'). * 'binary': Binary files (e.g., '.bin', '.exe', '.jpg'). Defaults to both. |
metadata |
A character vector specifying additional metadata to include in the JSON ('file_size', 'creation_time','last_modified_time'). One or both of the following options: * 'file_size': Size of the file in bytes. * 'creation_time': Creation time of the file. * 'last_modified_time': Last modified time of the file. Defaults to 'NULL.' |
ignore |
A character vector specifying file names to exclude from encoding. Defaults to 'NULL.' |
A JSON string representing the directory's contents.
## Not run: # Encode all files in a directory to JSON json_str <- json_encode_dir("mydir") # Encode only text files, including file size metadata json_str <- json_encode_dir("mydir", type = "text", metadata = "file_size") # Ignore specific files json_str <- json_encode_dir("mydir", ignore = c("ignore.txt")) ## End(Not run)## Not run: # Encode all files in a directory to JSON json_str <- json_encode_dir("mydir") # Encode only text files, including file size metadata json_str <- json_encode_dir("mydir", type = "text", metadata = "file_size") # Ignore specific files json_str <- json_encode_dir("mydir", ignore = c("ignore.txt")) ## End(Not run)
This function returns a vector of common text file extensions.
text_file_extensions()text_file_extensions()
A character vector of text file extensions.
text_file_extensions()text_file_extensions()
Checks if a JSON string is compliant with the expected schema for a directory structure.
validate_dir_json(json_data)validate_dir_json(json_data)
json_data |
A JSON string representing the directory's contents. |
TRUE if valid, otherwise throws an error.
## Not run: # Validate a JSON string for directory structure validate_dir_json(json_str) ## End(Not run)## Not run: # Validate a JSON string for directory structure validate_dir_json(json_str) ## End(Not run)
This function writes files to a specified directory based on a structured list.
write_files(sl_app, dest)write_files(sl_app, dest)
sl_app |
A structured list representing files and their contents. |
dest |
A character string specifying the target directory. |
None. Writes files to the specified directory.
## Not run: # Write files from a structured list to a directory write_files(sl_app, "output_dir") ## End(Not run)## Not run: # Write files from a structured list to a directory write_files(sl_app, "output_dir") ## End(Not run)