aboutsummaryrefslogtreecommitdiffhomepage
path: root/ruby/src
diff options
context:
space:
mode:
authorGravatar Adam Greene <adam.greene@gmail.com>2015-05-03 10:55:10 -0700
committerGravatar Adam Greene <adam.greene@gmail.com>2015-05-13 10:03:56 -0700
commitd1b52a00e002fd7a4adbcdeafa0634de6088a88f (patch)
treefe21dea8131562f7ff974edaf3c92edb4a5b465f /ruby/src
parent23bb79d4a32a77e8349d6c49052be1e56a8b8153 (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')
-rw-r--r--ruby/src/main/java/com/google/protobuf/jruby/RubyDescriptor.java2
-rw-r--r--ruby/src/main/java/com/google/protobuf/jruby/RubyMap.java2
-rw-r--r--ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java12
-rw-r--r--ruby/src/main/java/com/google/protobuf/jruby/RubyProtobuf.java50
4 files changed, 11 insertions, 55 deletions
diff --git a/ruby/src/main/java/com/google/protobuf/jruby/RubyDescriptor.java b/ruby/src/main/java/com/google/protobuf/jruby/RubyDescriptor.java
index 51c50be8..dd9179b0 100644
--- a/ruby/src/main/java/com/google/protobuf/jruby/RubyDescriptor.java
+++ b/ruby/src/main/java/com/google/protobuf/jruby/RubyDescriptor.java
@@ -248,6 +248,8 @@ public class RubyDescriptor extends RubyObject {
klass.setAllocator(allocator);
klass.makeMetaClass(runtime.getObject().getMetaClass());
klass.inherit(runtime.getObject());
+ RubyModule messageExts = runtime.getClassFromPath("Google::Protobuf::MessageExts");
+ klass.include(new IRubyObject[] {messageExts});
klass.instance_variable_set(runtime.newString(Utils.DESCRIPTOR_INSTANCE_VAR), this);
klass.defineAnnotatedMethods(RubyMessage.class);
return klass;
diff --git a/ruby/src/main/java/com/google/protobuf/jruby/RubyMap.java b/ruby/src/main/java/com/google/protobuf/jruby/RubyMap.java
index b25dc6e1..2d4c03b5 100644
--- a/ruby/src/main/java/com/google/protobuf/jruby/RubyMap.java
+++ b/ruby/src/main/java/com/google/protobuf/jruby/RubyMap.java
@@ -338,7 +338,7 @@ public class RubyMap extends RubyObject {
return newMap;
}
- @JRubyMethod(name = "to_h")
+ @JRubyMethod(name = {"to_h", "to_hash"})
public RubyHash toHash(ThreadContext context) {
return RubyHash.newHash(context.runtime, table, context.runtime.getNil());
}
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;
}
diff --git a/ruby/src/main/java/com/google/protobuf/jruby/RubyProtobuf.java b/ruby/src/main/java/com/google/protobuf/jruby/RubyProtobuf.java
index cb3fcd48..2cf210d2 100644
--- a/ruby/src/main/java/com/google/protobuf/jruby/RubyProtobuf.java
+++ b/ruby/src/main/java/com/google/protobuf/jruby/RubyProtobuf.java
@@ -50,56 +50,6 @@ public class RubyProtobuf {
/*
* call-seq:
- * Google::Protobuf.encode(msg) => bytes
- *
- * Encodes the given message object to protocol buffers wire format. This is an
- * alternative to the #encode method on msg's class.
- */
- @JRubyMethod(meta = true)
- public static IRubyObject encode(ThreadContext context, IRubyObject self, IRubyObject message) {
- return RubyMessage.encode(context, message.getMetaClass(), message);
- }
-
- /*
- * call-seq:
- * Google::Protobuf.decode(class, bytes) => msg
- *
- * Decodes the given bytes as protocol buffers wire format under the
- * interpretation given by the given class's message definition. This is an
- * alternative to the #decode method on the given class.
- */
- @JRubyMethod(meta = true)
- public static IRubyObject decode(ThreadContext context, IRubyObject self, IRubyObject klazz, IRubyObject message) {
- return RubyMessage.decode(context, klazz, message);
- }
-
- /*
- * call-seq:
- * Google::Protobuf.encode_json(msg) => json_string
- *
- * Encodes the given message object to its JSON representation. This is an
- * alternative to the #encode_json method on msg's class.
- */
- @JRubyMethod(name = "encode_json", meta = true)
- public static IRubyObject encodeJson(ThreadContext context, IRubyObject self, IRubyObject message) {
- return RubyMessage.encodeJson(context, message.getMetaClass(), message);
- }
-
- /*
- * call-seq:
- * Google::Protobuf.decode_json(class, json_string) => msg
- *
- * Decodes the given JSON string under the interpretation given by the given
- * class's message definition. This is an alternative to the #decode_json method
- * on the given class.
- */
- @JRubyMethod(name = "decode_json", meta = true)
- public static IRubyObject decodeJson(ThreadContext context, IRubyObject self, IRubyObject klazz, IRubyObject message) {
- return RubyMessage.decodeJson(context, klazz, message);
- }
-
- /*
- * call-seq:
* Google::Protobuf.deep_copy(obj) => copy_of_obj
*
* Performs a deep copy of either a RepeatedField instance or a message object,