From 12a52977402146e236703e5465c6218b9f6b27b9 Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Sat, 19 Jan 2019 06:07:45 +0000 Subject: [PATCH] Fix empty FieldMask json encoding --- upb/json/printer.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/upb/json/printer.c b/upb/json/printer.c index 2283d55423..f0859a312c 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -752,7 +752,6 @@ static void *startseq_fieldmask(void *closure, const void *handler_data) { UPB_UNUSED(handler_data); p->depth_++; p->first_elem_[p->depth_] = true; - print_data(p, "\"", 1); return closure; } @@ -760,7 +759,6 @@ static bool endseq_fieldmask(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); p->depth_--; - print_data(p, "\"", 1); return true; } @@ -980,6 +978,29 @@ static bool printer_endmsg_noframe( return true; } +static bool printer_startmsg_fieldmask( + void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + if (p->depth_ == 0) { + upb_bytessink_start(p->output_, 0, &p->subc_); + } + print_data(p, "\"", 1); + return true; +} + +static bool printer_endmsg_fieldmask( + void *closure, const void *handler_data, upb_status *s) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + UPB_UNUSED(s); + print_data(p, "\"", 1); + if (p->depth_ == 0) { + upb_bytessink_end(p->output_); + } + return true; +} + static void *scalar_startstr_onlykey( void *closure, const void *handler_data, size_t size_hint) { upb_json_printer *p = closure; @@ -1033,8 +1054,8 @@ void printer_sethandlers_fieldmask(const void *closure, upb_handlers *h) { upb_handlers_setstartseq(h, f, startseq_fieldmask, &empty_attr); upb_handlers_setendseq(h, f, endseq_fieldmask, &empty_attr); - upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr); - upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr); + upb_handlers_setstartmsg(h, printer_startmsg_fieldmask, &empty_attr); + upb_handlers_setendmsg(h, printer_endmsg_fieldmask, &empty_attr); upb_handlers_setstartstr(h, f, repeated_startstr_fieldmask, &empty_attr); upb_handlers_setstring(h, f, repeated_str_fieldmask, &empty_attr);