Forum: JRuby need to extend msgpack

2d1b64aaf61046ad166db3009fa8c7dc?d=identicon&s=25 Charles Monteiro (Guest)
on 2013-06-06 03:20
(Received via mailing list)
Msgpack is not supporting date. I just need it to do what json does
which is to stringify dates  and pass them along.

So I would like to intercept however its buried in
MessagePackLibrary.java. A little of digging points to what I believe I
need which is to include the following:

private void write(BufferPacker packer, RubyDate date) throws
IOException {
    write(packer, (RubyString) date.to_s());
  }

and:

private void write(BufferPacker packer, IRubyObject o) throws
IOException {
    if (o == null || o instanceof RubyNil) {
      packer.writeNil();
    } else if (o instanceof RubyBoolean) {
      packer.write(((RubyBoolean) o).isTrue());
    } else if (o instanceof RubyBignum) {
      write(packer, (RubyBignum) o);
    } else if (o instanceof RubyInteger) {
      write(packer, (RubyInteger) o);
    } else if (o instanceof RubyFixnum) {
      write(packer, (RubyFixnum) o);
    } else if (o instanceof RubyFloat) {
      write(packer, (RubyFloat) o);
    } else if (o instanceof RubyString) {
      write(packer, (RubyString) o);
    } else if (o instanceof RubySymbol) {
      write(packer, (RubySymbol) o);
    } else if (o instanceof RubyArray) {
      write(packer, (RubyArray) o);
    } else if (o instanceof RubyHash) {
      write(packer, (RubyHash) o);
    } else if (o instanceof RubyDate) {
      write(packer, (RubyDate) o);
    }

  else {
      throw o.getRuntime().newArgumentError(String.format("Cannot pack
type: %s", o.getClass().getName()));
    }
  }



so my general question is do I really need to modify the Java code ,
create the gem and install it in or does JRuby also give one a
convenient mechanism to override from Ruby. BTW, do not know if there's
such a  beast as RubyDate but it serves to make my point.

Finally , if the developer of msgpack-jruby is listening would it not be
a reasonable implementation to do what json does so that msgpack can be
plugged in wherever json is being used? That perhaps is not faithful to
the msgpack original implementation but it would be very useful.

thanks


Charles Monteiro
jruby@smallruby.com
0e6ac58dab6125c1cd2e7ac645076b6f?d=identicon&s=25 Joel VanderWerf (Guest)
on 2013-06-06 04:55
(Received via mailing list)
On 06/05/2013 02:18 PM, Charles Monteiro wrote:
> Msgpack is not supporting date. I just need it to do what json does
> which is to stringify dates  and pass them along.

There's something about msgpack-jruby that I don't understand:

$ jirb -r msgpack
 >> MessagePack.dump 4
NoMethodError: undefined method `dump' for MessagePack:Module

But if you were using mri, this is would be a way to stringify:

class Date
   def to_msgpack pk = nil
     case pk
     when MessagePack::Packer
       pk.write to_s
       return pk

     else # nil or IO
       MessagePack.pack(to_s, pk)
     end
   end
end

d = Date.today
MessagePack.dump d # => "\xAA2013-06-05"
MessagePack.load(MessagePack.dump(d)) # => "2013-06-05"
F1d37642fdaa1662ff46e4c65731e9ab?d=identicon&s=25 Charles Nutter (headius)
on 2013-06-07 01:25
(Received via mailing list)
On Wed, Jun 5, 2013 at 4:18 PM, Charles Monteiro <jruby@smallruby.com>
wrote:
> Msgpack is not supporting date. I just need it to do what json does which is
> to stringify dates  and pass them along.
...
> so my general question is do I really need to modify the Java code , create
> the gem and install it in or does JRuby also give one a convenient mechanism
> to override from Ruby. BTW, do not know if there's such a  beast as RubyDate
> but it serves to make my point.

Yeah, you probably will have to modify the Java code. JRuby exts (like
C exts) *can* be implemented more in Ruby or in ways that allow more
customization, but that doesn't appear to be the case here.

> Finally , if the developer of msgpack-jruby is listening would it not be a
> reasonable implementation to do what json does so that msgpack can be
> plugged in wherever json is being used? That perhaps is not faithful to the
> msgpack original implementation but it would be very useful.

Have you tried filing an issue directly?

- Charlie
F1d37642fdaa1662ff46e4c65731e9ab?d=identicon&s=25 Charles Nutter (headius)
on 2013-06-07 01:27
(Received via mailing list)
If this is a behavioral difference form the C version of msgpack, then
I suggest you file a bug with them...and maybe help fix it. :-)

- Charlie

On Wed, Jun 5, 2013 at 9:53 PM, Joel VanderWerf
2d1b64aaf61046ad166db3009fa8c7dc?d=identicon&s=25 Charles Monteiro (Guest)
on 2013-06-07 01:55
(Received via mailing list)
Their site has issues , can't at the moment dig up the docs on the C
version but according  to the MRI version there is no support for Date
and
Json itself does not support Date. The MRI version of JSON will turn
dates into a String and include them in the serialization i.e. instead
of crashing.

I can add my to_msgpack , and take the path of least resistance. I"m
guessing they want to stay faithful to the JSON spec.

Charles Monteiro
jruby@smallruby.com
2d1b64aaf61046ad166db3009fa8c7dc?d=identicon&s=25 Charles Monteiro (Guest)
on 2013-06-07 20:47
(Received via mailing list)
my reply did not seem to have gone thru, see below

Charles Monteiro
jruby@smallruby.com
2d1b64aaf61046ad166db3009fa8c7dc?d=identicon&s=25 Charles Monteiro (Guest)
on 2013-06-08 00:35
(Received via mailing list)
wasn't thinking , the moment you sent the top object to MessagePack ,
the entire graph is out in Java land and they are no recursive hooks
where I could intercept with my Date implementation

So I may need to do that in Java after all

Charles Monteiro
jruby@smallruby.com
2d1b64aaf61046ad166db3009fa8c7dc?d=identicon&s=25 Charles Monteiro (Guest)
on 2013-06-08 03:41
(Received via mailing list)
right, for one in msgpack-jruby to_msgpack was not provided, I had to
use MessagePack.pack and unpack to work the examples. I'm sure a lot of
this is driven by the implementation that is entirely in Java. I don't
know enough about JRbuy magic yet to reify into Ruby land a Java
construct but now that I think about it, I should at least be able to
add to_msgpack myself to Jruby and then delegate to MessagePack.pack
taking care of stringifying object types not handled by MessagePack i.e.
date/time


Charles
jruby@smallruby.com
F1d37642fdaa1662ff46e4c65731e9ab?d=identicon&s=25 Charles Nutter (headius)
on 2013-06-10 18:43
(Received via mailing list)
So yeah, I guess your options are:

1. Get a patch into msgpack-jruby for the additional behavior.
2. Reimplement that part of msgpack-jruby from Ruby if you can.
3. Fork the gem and fix it yourself.

If this behavior is different from msgpack-ruby, I'd say #1 is the
best option, followed by #2. If it's not different, it may still be
logical to include in the JRuby version.

- Charlie
8c21306523b16ba5dd35c3549bf90994?d=identicon&s=25 Theo Hultberg (Guest)
on 2013-06-10 22:11
(Received via mailing list)
Hi,

I'm the auhor of msgpack-jruby. I made a concious decision not to
support
msgpack's #to_msgpack, too many gems pollute the global namespace with
their methods and you can so easily add this yourself if you really need
it.

It wouldn't have helped you to have it anyway, unfortunately, since it
would only make it possible to serialize dates, the deserialization side
wouldn't know when it should reconstruct a Date instance instead of
something else. You'd need to tag the dates on the serialization side,
and
then a lot of logic on the deserialization side to discover the tagged
values and deserialize them correctly. All of the the custom logic is on
the deserialization side, and outside of MessagePack.

Please report an issue in the msgpack-jruby project if you have feature
requests, suggestions or bugs you'd like to report.

T#


On Mon, Jun 10, 2013 at 6:05 PM, Charles Oliver Nutter
2d1b64aaf61046ad166db3009fa8c7dc?d=identicon&s=25 Charles Monteiro (Guest)
on 2013-06-11 12:47
(Received via mailing list)
ok, I guess my main surprise is that I would expect that I can simply
substitute msgpack where I am using json i.e. I'm y Ruby code and that's
not possible.

I"ll get around to entering the request but it seems design decisions
would prevent my wish from coming true.

thanks


Charles Monteiro
jruby@smallruby.com
8c21306523b16ba5dd35c3549bf90994?d=identicon&s=25 Theo Hultberg (Guest)
on 2013-06-11 14:08
(Received via mailing list)
there have been movement towards merging msgpack-jruby into the regular
msgpack gem, if that happens then some of these features would be
included.

T#
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.