-
Notifications
You must be signed in to change notification settings - Fork 32
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
serialization method lookup order in serializeUnknownRubyObject #25
Comments
It is a last resort, by calling to_json, jrjackson releases control to the object author. As the method is named to_json, the author is correct to return a JSON string representing the object maybe invoking a new JSON parser session or some, perhaps, sub-optimal mechanism. So I first try to get a Time, Array, or Hash first. What we really need is a to_json_data method that returns a String, Numeric, true, false, nil, Hash or Array representation of the object and allow JRuby and Jackson to handle the data structure. Further if the object is best represented as a JSON number and the author included methods to_i and to_f, jrjackson would be wrong to choose to_i when to_f would be most accurate and vice-versa, however with to_json_data, the author would supply the best representation. For best JRuby performance the whole object graph passed to jrjackson would already be JSON compatible primitives meaning that the parser doesn't need to cross the JRuby/Java boundary repeatedly, i.e. needing to be aware of Ruby runtimes and ThreadContexts but, of course, this means that the object graph may be processed twice, once to convert in userland and once by Jackson in javaland. Choosing a particular set of trade-offs in JrJackson is unlikely to be satisfactory for all users of this library. Colin, I have always been impressed with your work and welcome any suggestions you have. |
Thanks Guy, and thanks for JrJackson, it's really nice! First: after giving more time understanding the serialization flow in JrJackson I realized my problem is not really related to the I understand your reasoning. In a performance big picture, optimizing for handling Jackson compatible primitives is ok but if it requires the traversal of the object graph in Ruby to convert to these primitives I think we loose. IIUC RubyObject obj = (RubyObject) method.call(ctx, rubyObject, meta, "to_json");
if (obj instanceof RubyString) {
jgen.writeRawValue(obj.toString());
} else {
provider.defaultSerializeValue(obj, jgen);
} If the output of Thoughts? |
On Sun, Jun 8, 2014 at 4:16 AM, Colin Surprenant [email protected]
I hope you guys are aware that you can "monkey patch" java classes in jruby just in case this is of any help for you ;) |
I think the generally understood to_json behaviour is to return a JSON encoded string. |
@mkristian, may be an interesting proposition. |
Perhaps the method |
Not to complicate things, but Rails has an |
I have no problem with Date.today.as_json ----> "2014-06-10"
{a: 33.4, t: Date.today}.as_json ----> {"a"=>33.4, "t"=>"2014-06-10"}
# I expected
{a: 33.4, t: Date.today}.as_json ----> {"a"=>33.4, "t"=> #<Date >} |
I personally don't like the |
yeah, it concerns me too. |
What's your use-case - Railsy or daemon? |
Sorry. I think you misunderstood. I wasn't suggesting that it be named |
@nirvdrum - good point. I guess the we are trying to resolve the opposing forces of Framework users with some custom classes, structs or Time-like objects ending up in the object tree and the Framework-free (daemon) users with almost all custom classes. Thing is all the Ruby Json parser authors would have to implement the call to |
@guyboertje my use-case is non-rails. I'm not sure we need to worry too much about the other json parsers and rails. The way I see it, it is an optimization you can provide by implementing |
@colinsurprenant, @nirvdrum, @mkristian - have you seen Transit https://github.com/cognitect/transit-format |
@colinsurprenant, @nirvdrum, @mkristian, @headius, @jgwmaxwell: I have: to_json_serializable |
In v 0.3.4 I have included support for to_json_data. If the Object author needs an alternative representation to be serialized then they should implement this method and return only JSON datatypes. |
Auditing old issues that mention me... Is there anything else to do here? |
Wasted a fair bit of time trying to work out why my ElasticSearch importer was breaking because of this - I have PostgreSQL array types which are behind Sequel's Array-delegating Personally I'd feel more comfortable without this This seems much safer and more predictable than blindly calling an ad-hoc grab-bag of conversion methods and hoping it produces something the user wanted - as noted, this would be a bad idea with something like |
I am not sure I understand why the method
to_json
is not the first one looked up inserializeUnknownRubyObject
- https://github.com/guyboertje/jrjackson/blob/master/src/main/java/com/jrjackson/RubyAnySerializer.java#L74IMO it should be the first method tried to control the serialization behaviour for any class by implementing, overriding or monkey-patching
to_json
.Is there a particular reason why it is the last one tried?
The text was updated successfully, but these errors were encountered: