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

devalue is too strict about what can be serialized with custom types #71

Open
Janpot opened this issue Nov 27, 2023 · 0 comments
Open

Comments

@Janpot
Copy link

Janpot commented Nov 27, 2023

Trying to add some custom types for functions but devalue seems to throw before calling the replacer. I understand that serializing functions is a non-goal, but I'm not sure it makes sense to prevent the following use-cases to be implemented with custom types:

  • Reviving a pure function using a custom type. There is no transferring of the function, we can just reference it on both sides of serialization:

    const add = (a, b) => a + b;
    
    const stringified = devalue.stringify({ myOperation: add }, { Add: (value) => value === add });
    
    const result = devalue.parse(stringified, { Add: () => add });
    
    console.log(result.myOperation(1, 2));
  • Ignoring non-serializable. While transferring user generated values, trying to do a best effort while communicating failures:

    const NONTRANSFERRABLE = Symbol('nontransferrable');
    
    const stringified = devalue.stringify(
    { foo: () => 1 },
    { Ignore: (value) => typeof value === 'function' || value === NONTRANSFERRABLE },
    );
    
    const result = devalue.parse(stringified, { Ignore: () => NONTRANSFERRABLE });
    
    console.log(result.add(1, 2));

both of these result in

Uncaught DevalueError: Cannot stringify a function

Would you consider relaxing the acceptable values that can be serialized with custom types?

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

No branches or pull requests

1 participant