Skip to content

Conversation

@BrianHuf
Copy link
Contributor

Add support for objects with prototypes

@marcolink marcolink requested a review from Copilot October 28, 2025 18:30
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for objects with custom prototypes by updating the isJsonObject function to use a more permissive type check. Previously, the function only accepted plain objects (with Object constructor), but now it accepts any non-null object type, enabling the library to handle class instances and objects with custom prototypes.

  • Modified isJsonObject to accept objects with any prototype, not just plain objects
  • Added comprehensive test coverage for objects with custom prototypes and nested class instances

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/index.ts Updated isJsonObject to use generic object type check instead of constructor comparison
src/index.spec.ts Added test suite demonstrating JSON patch generation for class instances with nested objects

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@marcolink marcolink changed the title support objects with prototypes feat: support objects with prototypes Oct 28, 2025
@marcolink
Copy link
Owner

Thanks for this @BrianHuf 🙏

So far, this lib doesn't explicitly support any JS classes (Date, Map, Set, RegExp, Array) or custom classes. I want to first support built-ins, before supporting custom classes.

depending on what you want to achieve, you could JSON.parse(JSON.stringify(classInstance)) your input before creating a patch.

@BrianHuf
Copy link
Contributor Author

No problem. Just sharing some changes we're using in a fork.

For us, it's all about performance. We were using the JSON.parse(JSON.stringify(classInstance)) approach, but profiling showed it being our bottleneck. In our case, classInstance is a proxy (something like https://github.com/immerjs/immer) so stringify is fairy slow.

Another necessary change for performance (for us) is to do object equality checks. For example,

  function compareObjects(path: string, leftJsonValue: any, rightJsonValue: any) {
    if (leftJsonValue === rightJsonValue) {
      return;
    }
   ...

Thanks for the making this library! It's fast, small, and very easy to read.

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