diff options
author | Adam Greene <adam.greene@gmail.com> | 2015-05-03 10:55:10 -0700 |
---|---|---|
committer | Adam Greene <adam.greene@gmail.com> | 2015-05-13 10:03:56 -0700 |
commit | d1b52a00e002fd7a4adbcdeafa0634de6088a88f (patch) | |
tree | fe21dea8131562f7ff974edaf3c92edb4a5b465f /ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java | |
parent | 23bb79d4a32a77e8349d6c49052be1e56a8b8153 (diff) |
adding and simplifying encoders/decoders
* make consistent between mri and jruby
* create a #to_h and have it use symbols for keys
* add #to_json and #to_proto helpers on the Google::Protobuf message classes
Diffstat (limited to 'ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java')
-rw-r--r-- | ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java b/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java index c7fd7aa7..547ab22c 100644 --- a/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java +++ b/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java @@ -338,16 +338,20 @@ public class RubyMessage extends RubyObject { return ret; } - @JRubyMethod(name = "to_h") + @JRubyMethod(name = {"to_h", "to_hash"}) public IRubyObject toHash(ThreadContext context) { Ruby runtime = context.runtime; RubyHash ret = RubyHash.newHash(runtime); for (Descriptors.FieldDescriptor fdef : this.descriptor.getFields()) { IRubyObject value = getField(context, fdef); - if (value.respondsTo("to_h")) { - value = Helpers.invoke(context, value, "to_h"); + if (!value.isNil()) { + if (value.respondsTo("to_h")) { + value = Helpers.invoke(context, value, "to_h"); + } else if (value.respondsTo("to_a")) { + value = Helpers.invoke(context, value, "to_a"); + } } - ret.fastASet(runtime.newString(fdef.getName()), value); + ret.fastASet(runtime.newSymbol(fdef.getName()), value); } return ret; } |