ohler55 / oj_mimic_json Goto Github PK
View Code? Open in Web Editor NEWSimple gem to call Oj.mimic_JSON when using bundler or just want to limit code changes to just pulling in gems.
License: BSD 3-Clause "New" or "Revised" License
Simple gem to call Oj.mimic_JSON when using bundler or just want to limit code changes to just pulling in gems.
License: BSD 3-Clause "New" or "Revised" License
When requiring oj_mimic_json
, decoding a float with more than 18 decimal places, including more than 4 trailing zeros, results in wrong values:
[19] pry(main)> Oj.load('{"foo":0.1000000000000000000}')
=> {"foo"=>-0.11838881245526249}
[20] pry(main)> Oj.load('{"foo":0.100000000000000000}')
=> {"foo"=>0.1}
[21] pry(main)> Oj.load('{"foo":0.110000000000000000}')
=> {"foo"=>0.11}
[22] pry(main)> Oj.load('{"foo":0.1100000000000000000}')
=> {"foo"=>-0.13022769370078874}
[23] pry(main)> Oj.load('{"foo":0.1000000000000000000}')
=> {"foo"=>-0.11838881245526249}
[24] pry(main)> Oj.load('{"foo":0.1100000000000000000}')
=> {"foo"=>-0.13022769370078874}
[25] pry(main)> Oj.load('{"foo":0.1110000000000000000}')
=> {"foo"=>-0.13141158182534138}
[26] pry(main)> Oj.load('{"foo":0.1111000000000000000}')
=> {"foo"=>-0.13152997063779662}
[27] pry(main)> Oj.load('{"foo":0.1111100000000000000}')
=> {"foo"=>-0.13154180951904215}
[28] pry(main)> Oj.load('{"foo":0.1111110000000000000}')
=> {"foo"=>-0.13154299340716671}
[29] pry(main)> Oj.load('{"foo":0.1111111000000000000}')
=> {"foo"=>-0.13154311179597916}
[30] pry(main)> Oj.load('{"foo":0.1111111100000000000}')
=> {"foo"=>-0.1315431236348604}
[31] pry(main)> Oj.load('{"foo":0.1111111110000000000}')
=> {"foo"=>-0.13154312481874852}
[32] pry(main)> Oj.load('{"foo":0.1111111111000000000}')
=> {"foo"=>-0.13154312493713735}
[33] pry(main)> Oj.load('{"foo":0.1111111111100000000}')
=> {"foo"=>-0.13154312494897621}
[34] pry(main)> Oj.load('{"foo":0.1111111111110000000}')
=> {"foo"=>-0.1315431249501601}
[35] pry(main)> Oj.load('{"foo":0.1111111111111000000}')
=> {"foo"=>-0.1315431249502785}
[36] pry(main)> Oj.load('{"foo":0.1111111111111100000}')
=> {"foo"=>-0.13154312495029036}
[37] pry(main)> Oj.load('{"foo":0.1111111111111110000}')
=> {"foo"=>#<BigDecimal:1f157b0,'0.1111111111 11111E0',18(36)>}
[38] pry(main)> Oj.load('{"foo":0.1111111111111110000}')['foo'].to_f
=> 0.111111111111111
This does not happen if only oj
is being required.
using oj_mimic_json
together with rails 6 action_text
fails with JSON::ParserError: unexpected characters after the JSON document () at line 30, column 2 [sparse.c:642].
rails _6.0.2.1_ new testojmimic
add the following gems:
gem 'oj'
gem 'oj_mimic_json'
yields in:
testojmimic ry$ bundle exec rails action_text:install
rails aborted!
JSON::ParserError: unexpected characters after the JSON document () at line 30, column 2 [sparse.c:642]
/Users/ry/Sites/test/testojmimic/bin/rails:9:in `<top (required)>'
/Users/ry/Sites/test/testojmimic/bin/spring:15:in `require'
/Users/ry/Sites/test/testojmimic/bin/spring:15:in `<top (required)>'
./bin/rails:3:in `load'
./bin/rails:3:in `<main>'
Tasks: TOP => app:template
(See full trace by running task with --trace)
Copied migration 20200112140326_create_active_storage_tables.active_storage.rb from active_storage
Copied migration 20200112140327_create_action_text_tables.action_text.rb from action_text
remove gem 'oj_mimic_json'
(keep gem oj
, again bundle install
) then run bundle exec rails action_text:install
again, runs without error:
Copying actiontext.scss to app/assets/stylesheets
create app/assets/stylesheets/actiontext.scss
Copying fixtures to test/fixtures/action_text/rich_texts.yml
create test/fixtures/action_text/rich_texts.yml
Copying blob rendering partial to app/views/active_storage/blobs/_blob.html.erb
create app/views/active_storage/blobs/_blob.html.erb
Installing JavaScript dependencies
run yarn add trix@^1.0.0 @rails/actiontext@^6.0.2-1 from "."
yarn add v1.9.0-20180627.0947
[1/4] 🔍 Resolving packages...
⠁ (node:3868) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
[2/4] 🚚 Fetching packages...
[3/4] 🔗 Linking dependencies...
warning " > [email protected]" has unmet peer dependency "webpack@^4.0.0 || ^5.0.0".
warning "webpack-dev-server > [email protected]" has unmet peer dependency "webpack@^4.0.0".
[4/4] 📃 Building fresh packages...
success Saved lockfile.
success Saved 2 new dependencies.
info Direct dependencies
├─ @rails/[email protected]
└─ [email protected]
info All dependencies
├─ @rails/[email protected]
└─ [email protected]
✨ Done in 15.46s.
Adding trix to app/javascript/packs/application.js
append app/javascript/packs/application.js
Adding @rails/actiontext to app/javascript/packs/application.js
append app/javascript/packs/application.js
Tried again with a fresh rails app, found here: https://github.com/swiknaba/testojmimic
ruby 2.6.5
rails 6.0.2.1
update (July 15, 2021)
updated the repo to:
ruby 2.7.3
rails 6.1.4
-> the error is still present
update (Sept 30, 2022)
minor updates:
ruby 2.7.5
rails 6.1.7
-> and also added a docker image to test the dummy app without messing with the install on your local machine; see this comment further below: #7 (comment)
I suppose we need to add Oj.add_to_json()
.
JSON mudule supports ascii_only option in similar way Oj does, but not directly in JSON.dump.
oj_mimic_json doesn't pass this option to Oj.
2.0.0-p598 :003 > require 'json'
2.0.0-p598 :004 > JSON.dump("a😬sdf") # => "\"a😬sdf\""
2.0.0-p598 :005 > JSON.generate(["a😬sdf"], ascii_only: true) # => "[\"a\\ud83d\\ude2csdf\"]"
2.0.0-p598 :006 > JSON.dump_default_options[:ascii_only]=true
2.0.0-p598 :007 > JSON.dump(["a😬sdf"]) # => "[\"a\\ud83d\\ude2csdf\"]"
2.0.0-p598 :008 > require 'oj_mimic_json'
2.0.0-p598 :009 > JSON.generate(["a😬sdf"], ascii_only: true) # => "[\"a😬sdf\"]"
2.0.0-p598 :010 > JSON.dump(["a😬sdf"]) # => "[\"a😬sdf\"]"
I guess, this issue relates to my situation.
If I include
gem 'oj_mimic_json'
into my rails application, I could not use JSON.parse with object_class: OpenStruct option. It breakes parsing of json into OpenStruct and result is still a hash instead of struct object
Is there a workaround?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.