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

feat: add isCustomJsonable and isJsonable #128

Merged
merged 2 commits into from
Aug 24, 2024
Merged

Conversation

lambdalisue
Copy link
Member

@lambdalisue lambdalisue commented Aug 20, 2024

If we strictly adhere to the description of JSON.stringify, the isJsonable function would be implemented like this.

However, I’m not sure if this aligns with what @Milly originally intended. What we might actually need is a predicate function to determine if an object implements a toJSON function.

@Milly What do you think?

@lambdalisue lambdalisue marked this pull request as draft August 20, 2024 17:26
@Milly
Copy link
Collaborator

Milly commented Aug 21, 2024

My isJsonable implementation in vim-denops/deno-denops-std#260 is intended to have a toJSON method, so names like hasToJSON or isJsonableObject would be appropriate.

@Milly
Copy link
Collaborator

Milly commented Aug 21, 2024

About this implementation

This implementation is deficient for JSON.stringify.

  1. Not all typeof x === "object" are jsonable.
const objBigInt = Object.assign(1n, { a: 1 });
assertEquals(typeof objBigInt, "object");
assertThrows(
  () => JSON.stringify(objBigInt),
  TypeError,
);
  1. Primitive BigInt is jsonbale if prototype has toJSON method.
BigInt.prototype.toJSON = function() { return Number(this) };
const primitiveBigInt = 42n;
assertEquals(typeof primitiveBigInt, "bigint");
assertEquals(JSON.stringify(primitiveBigInt), "42");
  1. Function is jsonable if prototype or own has toJSON method.
const hasown = Object.assign(() => {}, { toJSON: () => "own" });
assertEquals(typeof hasown, "function")
assertEquals(JSON.stringify(hasown), '"own"');
Function.prototype.toJSON = () => "proto";
const hasproto = () => {};
assertEquals(typeof hasproto, "function")
assertEquals(JSON.stringify(hasproto), '"proto"');

@lambdalisue
Copy link
Member Author

lambdalisue commented Aug 21, 2024

Do you think we need isJsonable, isJsonableObject, or both?

P.S.

Probably I'll implement isCustomJsonable (that has own toJSON method) and isJsonable (that follows JSON.stringify).

  • isCustomJsonable
  • isUserJsonable
  • isJsonableObject

@lambdalisue lambdalisue force-pushed the add-is-jsonable branch 2 times, most recently from f308e9a to 499b465 Compare August 21, 2024 19:19
@lambdalisue lambdalisue changed the title WIP: add isJsonable feat: add isCustomJsonable and isJsonable Aug 21, 2024
@lambdalisue lambdalisue marked this pull request as ready for review August 21, 2024 19:20
Copy link

codecov bot commented Aug 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.89%. Comparing base (d2075df) to head (39e4eb1).
Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #128   +/-   ##
=======================================
  Coverage   99.88%   99.89%           
=======================================
  Files          50       52    +2     
  Lines         909      954   +45     
  Branches      104      120   +16     
=======================================
+ Hits          908      953   +45     
  Misses          1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lambdalisue lambdalisue force-pushed the add-is-jsonable branch 2 times, most recently from 142f99b to 3227c4a Compare August 21, 2024 19:23
@lambdalisue lambdalisue requested a review from Milly August 21, 2024 19:24
is/custom_jsonable_test.ts Outdated Show resolved Hide resolved
is/custom_jsonable_test.ts Outdated Show resolved Hide resolved
is/jsonable_test.ts Outdated Show resolved Hide resolved
is/jsonable_test.ts Outdated Show resolved Hide resolved
is/jsonable_test.ts Outdated Show resolved Hide resolved
is/jsonable.ts Show resolved Hide resolved
is/jsonable.ts Outdated Show resolved Hide resolved
@lambdalisue lambdalisue force-pushed the add-is-jsonable branch 3 times, most recently from d0b46d2 to dcc7d6d Compare August 22, 2024 19:53
@lambdalisue lambdalisue requested a review from Milly August 22, 2024 19:53
is/custom_jsonable.ts Outdated Show resolved Hide resolved
is/jsonable_bench.ts Show resolved Hide resolved
is/jsonable_test.ts Outdated Show resolved Hide resolved
is/jsonable_test.ts Outdated Show resolved Hide resolved
is/jsonable.ts Outdated Show resolved Hide resolved
is/custom_jsonable_test.ts Outdated Show resolved Hide resolved
Copy link
Collaborator

@Milly Milly left a comment

Choose a reason for hiding this comment

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

LGTM!

@lambdalisue lambdalisue merged commit 6f1acaa into main Aug 24, 2024
5 checks passed
@lambdalisue lambdalisue deleted the add-is-jsonable branch August 24, 2024 00:09
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