-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
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
Exception in JSON.stringify #8428
Comments
Shouldn't you be doing |
Thank you for looking at this so quickly. I apologize for the incomplete example. I have updated the jsfiddle so it more fully illustrates the issue. http://jsfiddle.net/sjpt/akmcv7Lh/40/ |
Hi @sjpt I had a look at your fiddle and the issue is not related to THREE: the only explanation I can find is that the replacer function of |
I am afraid it is related to THREE, as it is THREE that is throwing the exception and causing failure of what should be a safe call to JSON.stringify. I agree it would be more convenient if my replacer() filter were called before the THREE toJSON() method, which would be more efficient and in which case the THREE bug would not matter. However, the spec at http://www.ecma-international.org/ecma-262/6.0/#sec-serializejsonproperty indicates this is the correct behaviour. As I said at in the first post I have a workaround so it is not too important for me, however, I think the THREE bug should be fixed. |
You easily get exceptions (at best - weird behavior being the worse case) from within the JSON serializer, since it doesn't pass our extra For instance, I know that this line throws when trying to serialize a |
Oh noes! Wasn't aware... Is the solution renaming to |
Me not either until I ran into it.
Arguably. As long as our implementation is complete, there's no problem. In fact, one could see it as syntactic sugar. OTOH it avoids confusion like here. What OP wants (specifying a replacer argument to I consider serializing |
I think your renaming Object3D.toJSON is a good solution. toJSON is a reserved method name with behaviour specified in the JSON.serialize specification; and that behaviour involves calling the method without arguments. If you want to implement something similar but different, your method should have a different name. That presumably means a rename of your other toJSON methods too. I do not need your serializer to be called as I ignore its output anyway in my replacer function. I just need a serializer that does not throw an exception. The default serializer (which gets called as a result of my I could save a trivial amount of compute time by replacing my workaround with |
I could see some potential breakage. People that relied on |
One suggestion to minimize breakage is to provide a toJSON that detects whether it has an input argument. Curiously, it seems that
|
How about just letting it break and adding an IMPORTANT sentence to the release notes? |
To clarify: To make this case work, we'd have to change this line to var isRootObject = ( meta === undefined || meta === '' ); or use |
The same issue has returned with THREE.Texture(). Not at all serious for me as I am now using yaml instead of JSON. |
Description of the problem
Sample code:
mmm = new THREE.Mesh(); JSON.stringify(mmm);
throws exception
[(http://jsfiddle.net/sjpt/akmcv7Lh/32/)]
My larger scale code is stringifying at a higher level, and tries to avoid any attempt to stringify the THREE components by
JSON.stringify(o, function(key, value) { if value instanceof THREE.Object3D return undefined; ... }
This worked in older versions of THREE. However, the THREE.Object3D.prototype.toJSON code now gets called BEFORE my escape function, and the exception means my entire JSON.stringify fails, even though it didn't want to go near stringifying the THREE objects.
For now I have a nasty workaround of
delete THREE.Object3D.prototype.toJSON;
before calling the stringify, but that is obviously not a good long term or general solution.
Three.js version
Browser
OS
Hardware Requirements (graphics card, VR Device, ...)
The text was updated successfully, but these errors were encountered: