Coder Social home page Coder Social logo

JSON additions do not recurse about json HOT 4 CLOSED

mperham avatar mperham commented on June 8, 2024
JSON additions do not recurse

from json.

Comments (4)

mperham avatar mperham commented on June 8, 2024

The round trip isn't symmetric. How can I get symmetric dump/parse?

# current
> JSON.parse JSON.generate(:foo)
=> {"json_class"=>"Symbol", "s"=>"foo"}

# ideal
> JSON.parse JSON.generate(:foo)
=> :foo

from json.

mperham avatar mperham commented on June 8, 2024

Ah, ok:

> JSON.parse JSON.generate(:foo), create_additions: true
=> :foo

Still need support for Hash and recursive structures.

from json.

mperham avatar mperham commented on June 8, 2024

Looks like it's only an issue with Hash keys. Works with Arrays and Hash values.

> JSON.parse(JSON.generate([1, [:foo, "mike"]]), create_additions: true)
=> [1, [:foo, "mike"]]
> JSON.parse(JSON.generate({:foo => "mike", 123 => :bar}), create_additions: true)
=> {"foo"=>"mike", "123"=>:bar}

But unfortunately Hash keys are the main usecase for Symbols in method arguments.

from json.

mperham avatar mperham commented on June 8, 2024

I think I understand now. "JSON keys are strings, always on the left of the colon, and must be wrapped in double quotes". Converting a Symbol to a Hash is ok but it can't be used as a key in a larger Hash. The code would need to be modified to perform something similar to ActiveJob here:

https://github.com/rails/rails/blob/3ecc26981476d6a8c4f1ba5bd33bfdeef1659a0f/activejob/lib/active_job/arguments.rb#L92C1-L101C1

from json.

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.