Commit e70f9256 authored by Joshua Haberman's avatar Joshua Haberman

Merge pull request #1139 from haberman/rubyjsoncamel

Changed Ruby to properly camelCase its JSON by default.
parents 37663e86 f654d49d
...@@ -51,7 +51,12 @@ def do_test(request) ...@@ -51,7 +51,12 @@ def do_test(request)
end end
when :json_payload when :json_payload
test_message = Conformance::TestAllTypes.decode_json(request.json_payload) begin
test_message = Conformance::TestAllTypes.decode_json(request.json_payload)
rescue Google::Protobuf::ParseError => err
response.parse_error = err.message.encode('utf-8')
return response
end
when nil when nil
fail "Request didn't have payload" fail "Request didn't have payload"
...@@ -66,6 +71,9 @@ def do_test(request) ...@@ -66,6 +71,9 @@ def do_test(request)
when :JSON when :JSON
response.json_payload = test_message.to_json response.json_payload = test_message.to_json
when nil
fail "Request didn't have requested output format"
end end
rescue StandardError => err rescue StandardError => err
response.runtime_error = err.message.encode('utf-8') response.runtime_error = err.message.encode('utf-8')
...@@ -96,8 +104,8 @@ def do_test_io ...@@ -96,8 +104,8 @@ def do_test_io
STDOUT.flush STDOUT.flush
if $verbose if $verbose
STDERR.puts("conformance-cpp: request={request.to_json}, " \ STDERR.puts("conformance_ruby: request=#{request.to_json}, " \
"response={response.to_json}\n") "response=#{response.to_json}\n")
end end
$test_count += 1 $test_count += 1
...@@ -107,7 +115,7 @@ end ...@@ -107,7 +115,7 @@ end
loop do loop do
unless do_test_io unless do_test_io
STDERR.puts('conformance-cpp: received EOF from test runner ' \ STDERR.puts('conformance_ruby: received EOF from test runner ' \
"after #{$test_count} tests, exiting") "after #{$test_count} tests, exiting")
break break
end end
......
This diff is collapsed.
...@@ -243,6 +243,10 @@ void Descriptor_free(void* _self) { ...@@ -243,6 +243,10 @@ void Descriptor_free(void* _self) {
if (self->fill_method) { if (self->fill_method) {
upb_pbdecodermethod_unref(self->fill_method, &self->fill_method); upb_pbdecodermethod_unref(self->fill_method, &self->fill_method);
} }
if (self->json_fill_method) {
upb_json_parsermethod_unref(self->json_fill_method,
&self->json_fill_method);
}
if (self->pb_serialize_handlers) { if (self->pb_serialize_handlers) {
upb_handlers_unref(self->pb_serialize_handlers, upb_handlers_unref(self->pb_serialize_handlers,
&self->pb_serialize_handlers); &self->pb_serialize_handlers);
...@@ -271,6 +275,7 @@ VALUE Descriptor_alloc(VALUE klass) { ...@@ -271,6 +275,7 @@ VALUE Descriptor_alloc(VALUE klass) {
self->layout = NULL; self->layout = NULL;
self->fill_handlers = NULL; self->fill_handlers = NULL;
self->fill_method = NULL; self->fill_method = NULL;
self->json_fill_method = NULL;
self->pb_serialize_handlers = NULL; self->pb_serialize_handlers = NULL;
self->json_serialize_handlers = NULL; self->json_serialize_handlers = NULL;
self->typeclass_references = rb_ary_new(); self->typeclass_references = rb_ary_new();
......
...@@ -640,6 +640,14 @@ static const upb_pbdecodermethod *msgdef_decodermethod(Descriptor* desc) { ...@@ -640,6 +640,14 @@ static const upb_pbdecodermethod *msgdef_decodermethod(Descriptor* desc) {
return desc->fill_method; return desc->fill_method;
} }
static const upb_json_parsermethod *msgdef_jsonparsermethod(Descriptor* desc) {
if (desc->json_fill_method == NULL) {
desc->json_fill_method =
upb_json_parsermethod_new(desc->msgdef, &desc->json_fill_method);
}
return desc->json_fill_method;
}
// Stack-allocated context during an encode/decode operation. Contains the upb // Stack-allocated context during an encode/decode operation. Contains the upb
// environment and its stack-based allocator, an initial buffer for allocations // environment and its stack-based allocator, an initial buffer for allocations
...@@ -752,13 +760,14 @@ VALUE Message_decode_json(VALUE klass, VALUE data) { ...@@ -752,13 +760,14 @@ VALUE Message_decode_json(VALUE klass, VALUE data) {
TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg); TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
{ {
const upb_json_parsermethod* method = msgdef_jsonparsermethod(desc);
stackenv se; stackenv se;
upb_sink sink; upb_sink sink;
upb_json_parser* parser; upb_json_parser* parser;
stackenv_init(&se, "Error occurred during parsing: %s"); stackenv_init(&se, "Error occurred during parsing: %s");
upb_sink_reset(&sink, get_fill_handlers(desc), msg); upb_sink_reset(&sink, get_fill_handlers(desc), msg);
parser = upb_json_parser_create(&se.env, &sink); parser = upb_json_parser_create(&se.env, method, &sink);
upb_bufsrc_putbuf(RSTRING_PTR(data), RSTRING_LEN(data), upb_bufsrc_putbuf(RSTRING_PTR(data), RSTRING_LEN(data),
upb_json_parser_input(parser)); upb_json_parser_input(parser));
...@@ -1041,6 +1050,7 @@ static void putmsg(VALUE msg_rb, const Descriptor* desc, ...@@ -1041,6 +1050,7 @@ static void putmsg(VALUE msg_rb, const Descriptor* desc,
!upb_msg_field_done(&i); !upb_msg_field_done(&i);
upb_msg_field_next(&i)) { upb_msg_field_next(&i)) {
upb_fielddef *f = upb_msg_iter_field(&i); upb_fielddef *f = upb_msg_iter_field(&i);
bool is_matching_oneof = false;
uint32_t offset = uint32_t offset =
desc->layout->fields[upb_fielddef_index(f)].offset + desc->layout->fields[upb_fielddef_index(f)].offset +
sizeof(MessageHeader); sizeof(MessageHeader);
...@@ -1057,6 +1067,7 @@ static void putmsg(VALUE msg_rb, const Descriptor* desc, ...@@ -1057,6 +1067,7 @@ static void putmsg(VALUE msg_rb, const Descriptor* desc,
} }
// Otherwise, fall through to the appropriate singular-field handler // Otherwise, fall through to the appropriate singular-field handler
// below. // below.
is_matching_oneof = true;
} }
if (is_map_field(f)) { if (is_map_field(f)) {
...@@ -1071,7 +1082,7 @@ static void putmsg(VALUE msg_rb, const Descriptor* desc, ...@@ -1071,7 +1082,7 @@ static void putmsg(VALUE msg_rb, const Descriptor* desc,
} }
} else if (upb_fielddef_isstring(f)) { } else if (upb_fielddef_isstring(f)) {
VALUE str = DEREF(msg, offset, VALUE); VALUE str = DEREF(msg, offset, VALUE);
if (RSTRING_LEN(str) > 0) { if (is_matching_oneof || RSTRING_LEN(str) > 0) {
putstr(str, f, sink); putstr(str, f, sink);
} }
} else if (upb_fielddef_issubmsg(f)) { } else if (upb_fielddef_issubmsg(f)) {
...@@ -1082,7 +1093,7 @@ static void putmsg(VALUE msg_rb, const Descriptor* desc, ...@@ -1082,7 +1093,7 @@ static void putmsg(VALUE msg_rb, const Descriptor* desc,
#define T(upbtypeconst, upbtype, ctype, default_value) \ #define T(upbtypeconst, upbtype, ctype, default_value) \
case upbtypeconst: { \ case upbtypeconst: { \
ctype value = DEREF(msg, offset, ctype); \ ctype value = DEREF(msg, offset, ctype); \
if (value != default_value) { \ if (is_matching_oneof || value != default_value) { \
upb_sink_put##upbtype(sink, sel, value); \ upb_sink_put##upbtype(sink, sel, value); \
} \ } \
} \ } \
...@@ -1189,7 +1200,7 @@ VALUE Message_encode_json(VALUE klass, VALUE msg_rb) { ...@@ -1189,7 +1200,7 @@ VALUE Message_encode_json(VALUE klass, VALUE msg_rb) {
putmsg(msg_rb, desc, upb_json_printer_input(printer), 0); putmsg(msg_rb, desc, upb_json_printer_input(printer), 0);
ret = rb_str_new(sink.ptr, sink.len); ret = rb_enc_str_new(sink.ptr, sink.len, rb_utf8_encoding());
stackenv_uninit(&se); stackenv_uninit(&se);
stringsink_uninit(&sink); stringsink_uninit(&sink);
......
...@@ -112,6 +112,7 @@ struct Descriptor { ...@@ -112,6 +112,7 @@ struct Descriptor {
VALUE klass; // begins as nil VALUE klass; // begins as nil
const upb_handlers* fill_handlers; const upb_handlers* fill_handlers;
const upb_pbdecodermethod* fill_method; const upb_pbdecodermethod* fill_method;
const upb_json_parsermethod* json_fill_method;
const upb_handlers* pb_serialize_handlers; const upb_handlers* pb_serialize_handlers;
const upb_handlers* json_serialize_handlers; const upb_handlers* json_serialize_handlers;
// Handlers hold type class references for sub-message fields directly in some // Handlers hold type class references for sub-message fields directly in some
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
...@@ -1160,7 +1160,7 @@ module BasicTest ...@@ -1160,7 +1160,7 @@ module BasicTest
# TODO: Fix JSON in JRuby version. # TODO: Fix JSON in JRuby version.
return if RUBY_PLATFORM == "java" return if RUBY_PLATFORM == "java"
m = MapMessage.new(:map_string_int32 => {"a" => 1}) m = MapMessage.new(:map_string_int32 => {"a" => 1})
expected = '{"map_string_int32":{"a":1},"map_string_msg":{}}' expected = '{"mapStringInt32":{"a":1},"mapStringMsg":{}}'
assert MapMessage.encode_json(m) == expected assert MapMessage.encode_json(m) == expected
m2 = MapMessage.decode_json(MapMessage.encode_json(m)) m2 = MapMessage.decode_json(MapMessage.encode_json(m))
assert m == m2 assert m == m2
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment