Comments (10)
I also don't like this naming convention, but it actually has nothing to do with this library. toJSON
is a special method that is used by JSON.stringify
if it is available (spec). Built-in objects, like Date
implement such a method as well.
from immutable-js.
Yeah I will probably hide toJSON and expose toJS which similarly does a deep conversion to plain JS objects, but doesn't muck with JSON. —
Sent from Mailbox
On Wed, Jul 30, 2014 at 11:32 AM, Felix Kling [email protected]
wrote:
I also don't like this naming convention, but it actually has nothing to do with this library.
toJSON
is a special method that is used byJSON.stringify
if it is available (spec). Built-in objects, likeDate
implement such a method as well.
Reply to this email directly or view it on GitHub:
#15 (comment)
from immutable-js.
@leebyron : This was actually more of a reply to @benjamingr :) Not sure what you mean by "hiding" in this case. The method should definitely exist to make JSON conversion easier.
I just dislike the choice of the method name in general ;)
from immutable-js.
Hide as in not document beyond "supports JSON.stringify"—
Sent from Mailbox
On Wed, Jul 30, 2014 at 11:40 AM, Felix Kling [email protected]
wrote:
@leebyron : This was actually more of a reply to @benjamingr :) Not sure what you mean by "hiding" in this case. The method should definitely exist to make JSON conversion easier.
I just dislike the choice of the method name in general ;)
Reply to this email directly or view it on GitHub:
#15 (comment)
from immutable-js.
@fkling but .toJSON
expects you to return a JSON string, that's what Date
does in the spec.. this is also what a lot of libraries do with it. Returning an object just happens to work since it'll recursively run the serialization function again.
from immutable-js.
@benjamingr: No, Date.prototype.toJSON
does not return a string containing JSON. It returns the value null
or the return value of toISOString
, which returns a string, but it doesn't contain JSON.
I'm relatively sure that .toJSON()
is supposed to return a JSON encodable value, especially given how the spec process the return value. If you were to return JSON, you would get undesired results:
var foo = [{
toJSON: function() { return JSON.stringify({bar: 42}); }
}];
JSON.stringify(foo); // "["{\"bar\":42}"]" instead of "[{"bar":42}]"
Again, I don't like the naming convention either, but it's not the fault of this library.
Other libraries / frameworks such as Backbone.js, do the exact same thing. I was about to create an issue about that, until I realized that .toJSON
is just messed up from start.
from immutable-js.
It appears that you are right @fkling, this is what the proposal page says too. It's a poor naming choice.
from immutable-js.
The name toJSON
is confusing, but I don't think it's so bad that we need a separate method toJS
.
from immutable-js.
toJSON is no longer described at all in the type definitions file, but continues to exist as an alias for toJS so that JSON.stringify still behaves correctly. I also changed fromJSON to fromJS to solve for the same ambiguity.
Thanks for highlighting this point of confusion, @benjamingr!
from immutable-js.
Hi, toJS is confusing to me that we are writing in JS already. A class instance with methods is still a JS object, isn't it?
from immutable-js.
Related Issues (20)
- Converting a Seq to a list causes RangeError (max call size exceeded) HOT 1
- 4.2.0: partition missing in flow HOT 3
- Question: Wiki article for upgrading to v4 HOT 3
- `toJS` breaks type compatibility between records with object values HOT 8
- Wrong typescript declarations for `.update()` method (continued) HOT 2
- Record with more keys should be assignable to Record with less keys if type is a subset of the first one
- An argument for 'reviver' was not provided. HOT 4
- ReferenceError: Immutable is not defined HOT 9
- Highlight inputs where `Set` can't maintain uniqueness HOT 4
- Flow type definition outdated, not working with last year changes HOT 1
- Urgent: Package Installation Reveals Multiple Vulnerabilities HOT 1
- Type predicate missing on isOrderedSet function type declaration
- support deno HOT 2
- immutable default import in 5.x
- version 4.3.3 breaks TS/Angular/webpack HOT 2
- Typescript: Implement `MapOf::merge `
- Extract sort function from Collection types HOT 6
- Enhancement: Custom equals HOT 8
- Strange edge case with Map.update(...) HOT 3
- Moving off deprecated dependencies HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from immutable-js.