Tensor: Add `toJSON` and `fromJSON` methods

A toJSON and fromJSON utility method on Tensor-compliant classes would be nice. The goal is to provide a simple way to store vectors, colors, matrices, and quaternions in plain text (with an obvious focus on JSON).

While using asArray and fromArray works fine, storing the data as an array has some consequences. To start with, data structure is not saved- for example, Color3 and Vector3 are saved the same. This makes reading the data difficult since a programmer needs a deeper knowledge of a code base to know what an array of numbers is.

This is not a comprehensive overhaul to serialization, it is giving programmers more options when serializing data involving Babylon’s classes.

What do you all think?

Are you suggesting to add those method to the all the classes?

I think it might add too much overhead. Every time you change an attribute or add a new class, you need to handle both methods, right?

You can always write custom serializers that would meet your needs. That’s what I usually do when I need cleaner JSON.

2 Likes

The JSON methods are more for convenience, and only add a few bytes that I think is worth the QoL.