Skip to content
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
wants to merge 12 commits into
base: master
Choose a base branch
from
Open

Conversation

eaton
Copy link

@eaton eaton commented Nov 16, 2023

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() and stringily() functions to be assigned as the serializer for a particular file extension.

  • setSerializer(extension, serializer), deleteSerializer(extension), and listSerializers() 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 or false; 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.
  • The existing serializeToJsonMaybe() function has been renamed to serializeMaybe(), and its safety checks to ensure the incoming data is serializable have been moved to the default JSON Serializer object. Any serializer that implements a validate() 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.
  • A simple test that adds an NdJson serializer, then roundtrips data, has been added to serializers.spec.ts.

Usage example:

const NdJson: Serializer<any[], any[]> = {
  validate: (input: unknown) => Array.isArray(input),
  parse: function (data: string) {
    const lines = data.split('\n');
    return lines.map((line) => JSON.parse(line));
  },
  stringify: function (data: any[]): string {
    return data.map((item) => JSON.stringify(item, undefined, 0)).join('\n');
  },
};


const obj = [
  { utf8: "ąćłźż" },
  { utf8: "☃" }
];

jetpack.setSerializer('.ndjson', NdJson);
jetpack.write("file.ndjson", obj);
const raw = jetpack.read("file.ndjson"); // '{"utf8":"ąćłźż"}\n{"utf8":"☃"}'
const output = jetpack.read("file.ndjson", "auto"); // [{ utf8: "ąćłźż" }, { utf8: "☃" }]

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:

  • Treating the read() method's "auto" as a default mode might make sense given the fact that readFile() allows users to bypass its conveniences. I'm pretty sure that would break existing behaviors, however.
  • Adding the NdJson serializer as an actual builtin options might make sense, as it doesn't require any additional libraries.
  • Provide a mechanism for passing serializer-specific parsing and stringification options, rather than just jsonIndent.
  • More tests to ensure overrides and "don't serialize" settings work in all scenarios

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.

@szwacz
Copy link
Owner

szwacz commented Nov 22, 2023

Thank you @eaton! I'm pretty busy now. Promise to review it soon.

@eaton
Copy link
Author

eaton commented Nov 22, 2023

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants