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(dia.Cell): new toJSON options #2728

Merged
merged 9 commits into from
Aug 12, 2024

Conversation

MartinKanera
Copy link
Contributor

@MartinKanera MartinKanera commented Jul 31, 2024

Description

This PR includes changes to the dia.Cell.toJSON(), now accepting two new options.

  • opt.ignoreDefaults (default ['attrs']) - accepts an array of attribute names to be compared to the defaults, additionally setting this option to false will result in returning all of the attributes without comparing them, while setting it to true results in a comparison of all the attributes with defaults
  • opt.ignoreEmptyAttributes (default false) - determines if the result should include empty attributes

These two options are also applicable to dia.Graph.toJSON() method.

This PR also includes a new util.objectDifference function used to determine the difference between two provided objects.

Documentation

https://docs.jointjs.com/api/dia/Cell/#tojson

toJSON()

cell.toJSON([opt])

Return a copy of the cell's attributes for JSON serialization. This can be used for persistance or serialization. Note that this method doesn't return a JSON string but rather an object that can be then serialized to JSON with JSON.stringify().

By default, opt.ignoreDefaults is set to ['attrs']. This setting ensures that only non-default attributes in the attrs object are returned, as it compares the attrs object to its defaults. To include all attributes of dia.Cell without filtering out defaults, set opt.ignoreDefaults to false. Alternatively, setting opt.ignoreDefaults to true will compare all attributes to their defaults, returning only those that differ. You can also provide an array of strings for opt.ignoreDefaults, specifying a list of attributes to compare against their defaults.

Additionally, opt.ignoreEmptyAttributes defaults to false, which may include empty objects in the JSON output. To exclude empty objects, set opt.ignoreEmptyAttributes to true.

const rectangle = new shapes.standard.Rectangle();

rectangle.toJSON();
// {
//     "type": "standard.Rectangle",
//     "attrs": {},
//     "position": {
//         "x": 0,
//         "y": 0
//     },
//     "size": {
//         "width": 1,
//         "height": 1
//     },
//     "angle": 0,
//     "id": "..."
// }

rectangle.toJSON({ ignoreDefaults: true });
// {
//     "type": "standard.Rectangle",
//     "attrs": {},
//     "position": {},
//     "size": {},
//     "id": "..."
// }

rectangle.toJSON({ ignoreDefaults: true, ignoreEmptyAttributes: true });
// {
//     "type": "standard.Rectangle",
//     "id": "..."
// }

rectangle.toJSON({ ignoreDefaults: ['attrs', 'size'] });
// {
//     "type": "standard.Rectangle",
//     "attrs": {},
//     "position": {
//         "x": 0,
//         "y": 0
//     },
//     "size": {},
//     "angle": 0,
//     "id": "..."
// }

https://docs.jointjs.com/api/dia/Graph/#tojson

toJSON()

graph.toJSON([opt])

This method accepts an optional opt object, the opt.cellAttributes accepts same options as the dia.Cell.toJSON() method, which will impact the result of the toJSON() method.


https://docs.jointjs.com/api/util/

objectDifference()

util.objectDifference(object, base, [opt])

Return an object that represents the difference between object and base.

object Object The object to compare
base Object The object to compare with
[opt.maxDepth] number The maximum depth to compare

@MartinKanera MartinKanera changed the title To json options feat: new toJSON options Jul 31, 2024
@MartinKanera MartinKanera changed the title feat: new toJSON options feat(dia.Cell): new toJSON options Jul 31, 2024
packages/joint-core/types/joint.d.ts Outdated Show resolved Hide resolved
packages/joint-core/src/util/util.mjs Outdated Show resolved Hide resolved
packages/joint-core/src/dia/Cell.mjs Outdated Show resolved Hide resolved
packages/joint-core/src/dia/Cell.mjs Outdated Show resolved Hide resolved
@kumilingus kumilingus closed this Aug 1, 2024
@kumilingus kumilingus reopened this Aug 1, 2024
@kumilingus kumilingus merged commit bf753a1 into clientIO:master Aug 12, 2024
3 checks passed
@MartinKanera MartinKanera deleted the to-json-options branch August 14, 2024 16:26
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