Giter Site home page Giter Site logo

JSON about immutable-js HOT 10 CLOSED

immutable-js avatar immutable-js commented on May 20, 2024
JSON

from immutable-js.

Comments (10)

fkling avatar fkling commented on May 20, 2024

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.

leebyron avatar leebyron commented on May 20, 2024

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 by JSON.stringify if it is available (spec). Built-in objects, like Date implement such a method as well.


Reply to this email directly or view it on GitHub:
#15 (comment)

from immutable-js.

fkling avatar fkling commented on May 20, 2024

@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.

leebyron avatar leebyron commented on May 20, 2024

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.

benjamingr avatar benjamingr commented on May 20, 2024

@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.

fkling avatar fkling commented on May 20, 2024

@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.

benjamingr avatar benjamingr commented on May 20, 2024

It appears that you are right @fkling, this is what the proposal page says too. It's a poor naming choice.

from immutable-js.

sophiebits avatar sophiebits commented on May 20, 2024

The name toJSON is confusing, but I don't think it's so bad that we need a separate method toJS.

from immutable-js.

leebyron avatar leebyron commented on May 20, 2024

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.

yavuzmester avatar yavuzmester commented on May 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.