question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serializing custom classes should use a known type

See original GitHub issue

(title originally “[suggestion] Examples should extend this.name, not this.type”)

Description of the problem

The example components generally override the type property of custom objects/materials/etc., like Water or the PMREM Materials with types of ‘EquirectangularToCubeUV’ and ‘SphericalGaussianBlur’.

As these are still Mesh and ShaderMaterial classes respectively, the original type after serialization is lost, e.g. serializing Water shows is type as Water, and there’s no longer a reference to this object being a Mesh, other than inference. This also fails with ObjectLoader because of this, with the resulting objects being Object3Ds. While not all ‘custom classes’ can be deserialized understandably, it should be possible for simple Mesh classes that just generate geometry and material.

I think for the most part, this.type should not be overwritten, and instead this.name should be used, or perhaps another property (e.g. coreType). Happy to make the changes, but want to be sure I understand how type and name are to be used and that this change is agreeable.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
donmccurdycommented, Dec 19, 2019

The problem is that .type is not a good indicator of the traits of an object, even within “core” classes… You point out a fair problem with custom classes, but consider .type = "DodecahedronBufferGeometry", in core. There’s probably no code that really needs to know it’s anything but a BufferGeometry, On the other hand, the difference between Geometry and BufferGeometry is much more important. I don’t see an easy rule for when .type should be inherited, so I think it’s simpler to consider it a minification-safe alias of constructor.name, and to pretty much ignore it as much as possible, except perhaps as a human-readable label.

That said, I believe all existing uses of .type could be implemented nicely by other means. For toJSON, like your example above, that is pretty easy:

class Object3D {
  toJSON ( meta ) {
    // ...
    meta.object.type = 'Object3D';
    // ...
  }
}

class Mesh extends Object3D {
  toJSON ( meta ) {
    meta = super.toJSON( meta );
    meta.object.type = 'Mesh';
    // ...
  }
}

This does mean every subclass would need to override toJSON, which isn’t true now, but is certainly not out of the question. Arguably subclasses should have fromJSON methods as well, so that ObjectLoader can be given a type mapping to deserialize things it doesn’t know about yet, but that’s harder and maybe not urgent.

Also — I’m not sure why object instanceof Mesh isn’t a pattern we use… are there some prototype shenanigans that make it unreliable now? Maybe that will be fixed with ES6 classes?

0reactions
donmccurdycommented, Dec 19, 2019

Rephrasing slightly, as two parts of that are my opinion rather than fact:

this.type is should be only used for serialization, since isFoo (and instanceof maybe in the future?) is should be used internally to determine typeness, subclasses (e.g. examples/jsm/objects/Fire.js) should not set this.type unless there’s some corresponding way for ObjectLoader to handle it (which there isn’t currently).

EDIT: That said, the uses of .type in the core library do look pretty minimal already.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serializing Custom Classes [Unity Tutorial] - YouTube
In this week's Quick Tip Tuesday I'll cover how to serialize your custom classes within the Unity inspector!Join me and learn your way ......
Read more >
How to do serialization of Class having members of custom ...
I want to know the best possible implementation plan in terms of speed and memory for serialize and deserialize functions. Note : 1)...
Read more >
Everything You Need to Know About Java Serialization ...
Check out this post to learn everything you need to know about serialization in Java and what this looks like in code examples....
Read more >
How to serialize properties of derived classes with System ...
Polymorphic serialization supports derived types that have been explicitly opted in via the JsonDerivedTypeAttribute. Undeclared types will ...
Read more >
Different Serialization Approaches for Java - Baeldung
We can use the writeValue() method, which belongs to the ObjectMapper class, to serialize any Java object as JSON output. ... This code...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found