NorthwoodsSoftware / GoJS

JavaScript diagramming library for interactive flowcharts, org charts, design tools, planning tools, visual languages.
http://gojs.net
Other
7.7k stars 2.86k forks source link

go.Model.fromJson corrupts nodeDataArray if nodeDataArray contains a property "class" with value "Size" #159

Closed dominic-simplan closed 2 years ago

dominic-simplan commented 2 years ago

Parsing a model such as the following with go.Model.fromJson

go.Model.fromJson({
        "class": "GraphLinksModel",
        "nodeDataArray": [{
            "class": "Size",
            "customProp": "customValue",
            "key": -4,
            "loc": "450 900",
          }]
}).nodeDataArray[0]

returns an Object with missing properties but additional height and width properties: grafik

It looks like the nodeData is converted to a go.Size object.

WalterNorthwoods commented 2 years ago

That is intentional. From the API documentation, https://gojs.net/latest/api/symbols/Model.html#static-fromJson :

Most object classes cannot be serialized into JSON without special knowledge and processing at both ends. The toJson and Model.fromJson methods automatically do such processing for numbers that are NaN and for objects that are of class Point, Size, Rect, Margin, Spot, Brush (but not for brush patterns), and for Geometry. However, we recommend that you use Binding converters (static functions named "parse" and "stringify") to represent Points, Sizes, Rects, Margins, Spots, and Geometries as string values in your data, rather than as Objects. This makes the JSON text smaller and simpler and easier to read.

dominic-simplan commented 2 years ago

Hello @WalterNorthwoods , it is not really clear from the documentation that we shouldn't use a property called "class" with a value "Point". When reading the documentation I am thinking about JavaScript classes, not property names. Maybe you could add a list of reserved keywords to the documentation? As far as I can see I shouldn't use

Any other "reserved keywords" I shouldn't use?

This is really a problem because it doesn't throw any error any just corrupts the model when such a keyword is used. So at least there should be some visible list of words to avoid in the documentation.

WalterNorthwoods commented 2 years ago

OK, I'll treat this as a bug, in that Model.fromJson was never intended to substitute for example an instance of the Size class for an Object of the form { class: "Size", . . . }. Instead models have always written and read instances of Size in the form { class: "go.Size", . . . }.

I have also updated the documentation for both Model.toJson and Model.fromJson.

This will be in 2.1.51. Thanks for taking the trouble to report this problem.