Forum: JRuby How best to convert Java Map to json in JRuby?

B5e99386259230dd2bd86fc5e6544a69?d=identicon&s=25 Kimpton, C (Chris) (Guest)
on 2013-04-25 08:43
(Received via mailing list)
Hi,

I raised this on stackoverflow - but wondering if its perhaps a jruby
bug (or feature)

http://stackoverflow.com/questions/16174722/how-to...

I have a Java HashMap that I have in JRuby, I am trying to convert it to
JSON, but its not converting correctly, I think this example shows the
problem:


    $ irb
    irb(main):001:0> require 'java'
    => false
    irb(main):003:0> require 'json'
    => true
    irb(main):005:0> h = java.util.HashMap.new()
    => {}
    irb(main):006:0> x = {}
    => {}
    irb(main):007:0> JSON.parse JSON.dump x
    => {}

    irb(main):008:0> JSON.parse JSON.dump h
    JSON::ParserError: unexpected token at '"{}"'
          from json/ext/Parser.java:251:in `parse'
          from
/Users/kimptoc/.rvm/rubies/jruby-1.7.3/lib/ruby/1.9/json/common.rb:155:in
`parse'
          from (irb):9:in `evaluate'
          from org/jruby/RubyKernel.java:1066:in `eval'
          from org/jruby/RubyKernel.java:1409:in `loop'
          from org/jruby/RubyKernel.java:1174:in `catch'
          from org/jruby/RubyKernel.java:1174:in `catch'
          from /Users/kimptoc/.rvm/rubies/jruby-1.7.3/bin/irb:13:in
`(root)'
    irb(main):010:0> JSON.dump h
    => "\"{}\""

Any ideas on how to handle this - do I need to turn the map into a Ruby
map?

There are some more subtleties around this too

-      The map is actually a synchronizedMap, which Jruby doesnt seem to
think is a Hash - so currently I wrap it in a HashMap to make it more
Jruby friendly

-      Sub maps also have the issue (perhaps obviously, I guess)

Should I raise a bug, or is this one of those, thats how it is features
:)

Thanks,
Chris


_____________________________________________________________

This email (including any attachments to it) is confidential, legally
privileged, subject to copyright and is sent for the personal attention
of the intended recipient only. If you have received this email in
error, please advise us immediately and delete it. You are notified that
disclosing, copying, distributing or taking any action in reliance on
the contents of this information is strictly prohibited. Although we
have taken reasonable precautions to ensure no viruses are present in
this email, we cannot accept responsibility for any loss or damage
arising from the viruses in this email or attachments. We exclude any
liability for the content of this email, or for the consequences of any
actions taken on the basis of the information provided in this email or
its attachments, unless that information is subsequently confirmed in
writing.
B5e99386259230dd2bd86fc5e6544a69?d=identicon&s=25 Kimpton, C (Chris) (Guest)
on 2013-07-12 05:22
(Received via mailing list)
Hi,

This is also true of Java List's too.

I guess I have to do a .to_a/.to_hash as appropriate - or is this a
json/Jruby bug?

Thanks,
Chris

From: Kimpton, C (Chris)
Sent: 25 April 2013 07:42
To: 'user@jruby.codehaus.org'
Subject: How best to convert Java Map to json in JRuby?

Hi,

I raised this on stackoverflow - but wondering if its perhaps a jruby
bug (or feature)

http://stackoverflow.com/questions/16174722/how-to...

I have a Java HashMap that I have in JRuby, I am trying to convert it to
JSON, but its not converting correctly, I think this example shows the
problem:


    $ irb
    irb(main):001:0> require 'java'
    => false
    irb(main):003:0> require 'json'
    => true
    irb(main):005:0> h = java.util.HashMap.new()
    => {}
    irb(main):006:0> x = {}
    => {}
    irb(main):007:0> JSON.parse JSON.dump x
    => {}

    irb(main):008:0> JSON.parse JSON.dump h
    JSON::ParserError: unexpected token at '"{}"'
          from json/ext/Parser.java:251:in `parse'
          from
/Users/kimptoc/.rvm/rubies/jruby-1.7.3/lib/ruby/1.9/json/common.rb:155:in
`parse'
          from (irb):9:in `evaluate'
          from org/jruby/RubyKernel.java:1066:in `eval'
          from org/jruby/RubyKernel.java:1409:in `loop'
          from org/jruby/RubyKernel.java:1174:in `catch'
          from org/jruby/RubyKernel.java:1174:in `catch'
          from /Users/kimptoc/.rvm/rubies/jruby-1.7.3/bin/irb:13:in
`(root)'
    irb(main):010:0> JSON.dump h
    => "\"{}\""

Any ideas on how to handle this - do I need to turn the map into a Ruby
map?

There are some more subtleties around this too

-      The map is actually a synchronizedMap, which Jruby doesnt seem to
think is a Hash - so currently I wrap it in a HashMap to make it more
Jruby friendly

-      Sub maps also have the issue (perhaps obviously, I guess)

Should I raise a bug, or is this one of those, thats how it is features
:)

Thanks,
Chris


_____________________________________________________________

This email (including any attachments to it) is confidential, legally
privileged, subject to copyright and is sent for the personal attention
of the intended recipient only. If you have received this email in
error, please advise us immediately and delete it. You are notified that
disclosing, copying, distributing or taking any action in reliance on
the contents of this information is strictly prohibited. Although we
have taken reasonable precautions to ensure no viruses are present in
this email, we cannot accept responsibility for any loss or damage
arising from the viruses in this email or attachments. We exclude any
liability for the content of this email, or for the consequences of any
actions taken on the basis of the information provided in this email or
its attachments, unless that information is subsequently confirmed in
writing.
B05d3cbc64b0031a24c2887fb6ddc173?d=identicon&s=25 kristian (Guest)
on 2013-07-12 05:39
(Received via mailing list)
JSON.parse JSON.dump h.to_hash

should do the trick

-christian
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.