Git Product home page Git Product logo

Comments (7)

patoncrispy avatar patoncrispy commented on August 15, 2024 3

I would have expected a JSON string... It's semantics really and not that big a deal. :) I use the Immutable.js library and that has .toJS(), which I guess I'm used to so when I saw .toJSON() I was expecting a different result.

from dynogels.

clarkie avatar clarkie commented on August 15, 2024 3

Following up on this I found this in the MDN docs:

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON()_behavior

This means that there is a legitimate use for toJSON() that I didn't know about before.

from dynogels.

clarkie avatar clarkie commented on August 15, 2024

I'm not sure I understand. JSON stands for JavaScript Object Notation. What would you expect a function called .toJSON() to return?

from dynogels.

adrianblynch avatar adrianblynch commented on August 15, 2024

I'm with @patoncrispy on this. This isn't right:

const myJson = {hello: 'peeps'} // I'm JS not JSON

Where as this is:

const myJs = {hello: 'peeps'} // JS
const myJson = JSON.stringify(myJs) // JSON, a string that can be parsed
const backToJs = JSON.parse(myJson) // We have JS

from dynogels.

clarkie avatar clarkie commented on August 15, 2024

How would you recommend transitioning to this? Also, toJS doesn't tell me what the function does.

What about toPlainObject()?

from dynogels.

patoncrispy avatar patoncrispy commented on August 15, 2024

Two possible suggestions:

  1. Change the function name from .toJSON() to .toJS().
  2. Introduce another function called .toJS(), which has the current functionality of .toJSON() and the make the original .toJSON() function return a JSON string.

To keep up with semver, you'd need to make a major release, as both of these suggestions are breaking changes.

Other than ImmutableJS (Facebook), the .toJS() syntax is also used by Knockout and I feel it makes a lot more sense when trying to retrieve an object than .toJSON() does. For instance, when I first found that function I used it like this:

const user = JSON.parse(User.toJSON()); 

This is the only reason I brought this up.

Mobx made this change to their API as well, so it could be worth looking at how they did it.

from dynogels.

patoncrispy avatar patoncrispy commented on August 15, 2024

That's actually quite interesting. I didn't know that!

from dynogels.

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.