Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java::JavaLang::NegativeArraySizeException when generating JSON #1609

Closed
kwando opened this issue Apr 4, 2014 · 10 comments
Closed

Java::JavaLang::NegativeArraySizeException when generating JSON #1609

kwando opened this issue Apr 4, 2014 · 10 comments

Comments

@kwando
Copy link

kwando commented Apr 4, 2014

I got a java exception when crafting a JSON response.

https://gist.github.com/kwando/9971023

@mkristian
Copy link
Member

with
$ jruby --version -rjson -e
'h={"api_key"=>"fb5b2a908125868a6d08122a51066dfe3ed54272"};p h.to_json'
I get
"{"api_key":"fb5b2a908125868a6d08122a51066dfe3ed54272"}"
and using jruby-1.7.11

the output from the gist look like rails ? a small 'demo' setup which
produces the error would be very helpful.

@kwando
Copy link
Author

kwando commented Apr 4, 2014

You are correct in your observation =)
I am digging around in my code now.

JSON.pretty_generate together with a custom type implementing to_json(opts = {}) seems to be the problem.

@kwando
Copy link
Author

kwando commented Apr 4, 2014

https://github.com/kwando/json_error

Here is testapp, jruby-1.7.11, rails 3.2.17

[1] $ ruby --version
jruby 1.7.11 (2.0.0p195) 2014-02-24 86339bb on Java HotSpot(TM) 64-Bit Server VM 1.8.0-b132 +indy [darwin-x86_64]

rails s

Same app on rails 4 works without that error.

@mkristian
Copy link
Member

thanx, that helped - I can reproduce it now ;)

and grabbing the code from app/controllers/application_controller.rb works
without the rails framework loaded.

let's see if I can find something . . .

@headius
Copy link
Member

headius commented Apr 7, 2014

@mkristian Any progress here?

@mkristian
Copy link
Member

only that you need rails to reproduce it.

I do have time right now . . .

On Mon, Apr 7, 2014 at 5:12 PM, Charles Oliver Nutter <
notifications@github.com> wrote:

@mkristian https://github.com/mkristian Any progress here?


Reply to this email directly or view it on GitHubhttps://github.com//issues/1609#issuecomment-39750167
.

@atambo
Copy link
Member

atambo commented Apr 7, 2014

Could this be related to ruby/json#181?

@mkristian
Copy link
Member

yes, indeed - copied the required from ruby/json#181 and then I see the error with:

require 'set'
require "active_support/json"
require "active_support/core_ext/object/to_json"
require "active_support/core_ext/hash/indifferent_access"
class TagSet
  def initialize(*args)
    @tags = Set.new(args)
  end       
  def to_json(opts = {})
    @tags.to_a.to_json(opts)
  end
end
p JSON.pretty_generate(
  name: 'Hello World',
  tags: TagSet.new('tag1', 'tag2') )

@atambo
Copy link
Member

atambo commented Apr 14, 2014

So there is already a pull request (ruby/json#195) with a fix we just need to convince @flori to merge it.

@kares
Copy link
Member

kares commented May 23, 2017

seems to be working on 9.1.8.0 (using Rails 4.2.8) :

jruby-9.1.8.0 :001 > require 'set'
 => true 
jruby-9.1.8.0 :002 > require "active_support/json"
 => true 
jruby-9.1.8.0 :003 > require "active_support/core_ext/object/to_json"
LoadError: no such file to load -- active_support/core_ext/object/to_json
	from org/jruby/RubyKernel.java:961:in `require'
	from /opt/local/rvm/rubies/jruby-9.1.8.0/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb:120:in `require'
	from (irb):3:in `<eval>'
	from org/jruby/RubyKernel.java:1000:in `eval'
	from org/jruby/RubyKernel.java:1298:in `loop'
	from org/jruby/RubyKernel.java:1120:in `catch'
	from org/jruby/RubyKernel.java:1120:in `catch'
	from /opt/local/rvm/rubies/jruby-9.1.8.0/bin/irb:13:in `<main>'
jruby-9.1.8.0 :004 > require "active_support/core_ext/hash/indifferent_access"
 => true 
jruby-9.1.8.0 :005 > class TagSet
jruby-9.1.8.0 :006?>     def initialize(*args)
jruby-9.1.8.0 :007?>         @tags = Set.new(args)
jruby-9.1.8.0 :008?>       end       
jruby-9.1.8.0 :009?>     def to_json(opts = {})
jruby-9.1.8.0 :010?>         @tags.to_a.to_json(opts)
jruby-9.1.8.0 :011?>       end
jruby-9.1.8.0 :012?>   end
 => :to_json 
jruby-9.1.8.0 :013 > p JSON.pretty_generate(
jruby-9.1.8.0 :014 >       name: 'Hello World',
jruby-9.1.8.0 :015 >       tags: TagSet.new('tag1', 'tag2') )
"{\n  \"name\": \"Hello World\",\n  \"tags\": [\n    \"tag1\",\n    \"tag2\"\n  ]\n}"
 => "{\n  \"name\": \"Hello World\",\n  \"tags\": [\n    \"tag1\",\n    \"tag2\"\n  ]\n}" 

@kares kares closed this as completed May 23, 2017
@kares kares added this to the Invalid or Duplicate milestone May 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants