-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pluggable serializers #120
Open
eaton
wants to merge
12
commits into
szwacz:master
Choose a base branch
from
eaton:pluggable-serializers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Thank you @eaton! I'm pretty busy now. Promise to review it soon. |
No worries — as you said, you're not working on this project as part of your job now, so no expectations. Just wanted to be sure I got this tidied up as an official PR rather than just playing in my own fork! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
As briefly discussed in #116, this pull request adds the concept of pluggable serializers to fs-jetpack's read and write operations. Currently, JSON and JSON with dates are treated as special conveniences. This patch allows any object with
parse()
andstringily()
functions to be assigned as the serializer for a particular file extension.setSerializer(extension, serializer)
,deleteSerializer(extension)
, andlistSerializers()
functions have been added to the jetpack api.read(path, returnAs)
now supports an "auto" returnAs mode; this will check for file's extension in the serializers list, and attempt to parse the incoming data if a matching one is found. A custom parsing function can also be passed in, in lieu of the already-set serializers.write(path, data, options)
now supports a "serializer" option; it can contain a custom serializer object orfalse
; if set to false, no auto-serialization will occur. If the option is unset, the file's extension will be used to find a matching serializer in the current serializers map.serializeToJsonMaybe()
function has been renamed toserializeMaybe()
, and its safety checks to ensure the incoming data is serializable have been moved to the default JSON Serializer object. Any serializer that implements avalidate()
function can use the opportunity to check if the incoming data is serializable before proceeding. If it is not, serialization is skipped, just like the current JSON behavior.serializers.spec.ts
.Usage example:
While I'm relatively new to the fs-jetpack codebase, I've tried to follow its conventions as closely as possible and avoid any unnecessary side effects in how other functions work. A handful of updates to the read() and write() tests have been made to account for the new option properties, but other than that all other tests are unchanged and passing.
Potential areas for improvement:
read()
method's "auto" as a default mode might make sense given the fact thatreadFile()
allows users to bypass its conveniences. I'm pretty sure that would break existing behaviors, however.jsonIndent
.I've been using the code in this patch on my own projects for a bit to simplify reading/writing NDJSON, JSON5, and yaml configuration files. Adding other formats like TOML, INI, and even goofy stuff like MacOS .plist files is pretty easy. I'm curious if the general approach is appealing as a potential addition to fsJetpack, if a different way of solving the problem would be better, or if it feels altogether out of scope for the library. In any case, thanks for the great work you've done on fsJetpack! It's been a great time saver.