[jruby-user] need to extend msgpack

38 views
Skip to first unread message

Charles Monteiro

unread,
Jun 5, 2013, 5:18:37 PM6/5/13
to Jruby Users List 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



Joel VanderWerf

unread,
Jun 5, 2013, 10:53:46 PM6/5/13
to us...@jruby.codehaus.org
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"


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Charles Oliver Nutter

unread,
Jun 6, 2013, 7:23:06 PM6/6/13
to jruby-user
On Wed, Jun 5, 2013 at 4:18 PM, Charles Monteiro <jr...@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

Charles Oliver Nutter

unread,
Jun 6, 2013, 7:23:30 PM6/6/13
to jruby-user
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

Charles Monteiro

unread,
Jun 6, 2013, 7:53:44 PM6/6/13
to us...@jruby.codehaus.org
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
jr...@smallruby.com

Charles Monteiro

unread,
Jun 6, 2013, 10:36:34 AM6/6/13
to us...@jruby.codehaus.org
my reply did not seem to have gone thru, see below

Charles Monteiro
jr...@smallruby.com



On Jun 6, 2013, at 5:31 AM, Charles Monteiro <jr...@smallruby.com> wrote:

> 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
> jr...@smallruby.com

Charles Monteiro

unread,
Jun 7, 2013, 6:17:00 PM6/7/13
to us...@jruby.codehaus.org
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
jr...@smallruby.com

Charles Monteiro

unread,
Jun 6, 2013, 5:31:00 AM6/6/13
to us...@jruby.codehaus.org
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
jr...@smallruby.com



Charles Oliver Nutter

unread,
Jun 10, 2013, 12:05:04 PM6/10/13
to jruby-user
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

Theo Hultberg

unread,
Jun 10, 2013, 4:08:44 PM6/10/13
to us...@jruby.codehaus.org
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#

Charles Monteiro

unread,
Jun 11, 2013, 6:44:39 AM6/11/13
to us...@jruby.codehaus.org
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


Theo Hultberg

unread,
Jun 11, 2013, 7:55:19 AM6/11/13
to us...@jruby.codehaus.org
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#
Reply all
Reply to author
Forward
0 new messages