From 1019f76fc550bebafed0cfb65e6a04b9ff945185 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Thu, 14 Apr 2016 10:40:11 -0700 Subject: Added migration flag for users using old JSON format. --- upb/json/parser.c | 12 +++++++++++- upb/json/parser.rl | 8 ++++++++ upb/json/printer.c | 19 +++++++++++++------ 3 files changed, 32 insertions(+), 7 deletions(-) (limited to 'upb/json') diff --git a/upb/json/parser.c b/upb/json/parser.c index 3731478..e1a334d 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -1627,14 +1627,24 @@ static void add_jsonname_table(upb_json_parsermethod *m, const upb_msgdef* md) { !upb_msg_field_done(&i); upb_msg_field_next(&i)) { const upb_fielddef *f = upb_msg_iter_field(&i); + size_t field_len = upb_fielddef_getjsonname(f, buf, len); if (field_len > len) { + size_t len2; buf = realloc(buf, field_len); len = field_len; - upb_fielddef_getjsonname(f, buf, len); + len2 = upb_fielddef_getjsonname(f, buf, len); + UPB_ASSERT_VAR(len2, len == len2); } upb_strtable_insert(t, buf, upb_value_constptr(f)); + if (getenv("UPB_JSON_ACCEPT_LEGACY_FIELD_NAMES")) { + /* Temporary code to help people migrate if they were depending on the + * old, non-proto3-json-compliant field names. In this case we + * recognize both old names and new names. */ + upb_strtable_insert(t, upb_fielddef_name(f), upb_value_constptr(f)); + } + if (upb_fielddef_issubmsg(f)) { add_jsonname_table(m, upb_fielddef_msgsubdef(f)); } diff --git a/upb/json/parser.rl b/upb/json/parser.rl index fbc2d88..eecb3b2 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -1362,6 +1362,7 @@ static void add_jsonname_table(upb_json_parsermethod *m, const upb_msgdef* md) { !upb_msg_field_done(&i); upb_msg_field_next(&i)) { const upb_fielddef *f = upb_msg_iter_field(&i); + size_t field_len = upb_fielddef_getjsonname(f, buf, len); if (field_len > len) { size_t len2; @@ -1372,6 +1373,13 @@ static void add_jsonname_table(upb_json_parsermethod *m, const upb_msgdef* md) { } upb_strtable_insert(t, buf, upb_value_constptr(f)); + if (getenv("UPB_JSON_ACCEPT_LEGACY_FIELD_NAMES")) { + /* Temporary code to help people migrate if they were depending on the + * old, non-proto3-json-compliant field names. In this case we + * recognize both old names and new names. */ + upb_strtable_insert(t, upb_fielddef_name(f), upb_value_constptr(f)); + } + if (upb_fielddef_issubmsg(f)) { add_jsonname_table(m, upb_fielddef_msgsubdef(f)); } diff --git a/upb/json/printer.c b/upb/json/printer.c index c290efe..e6135bb 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -47,12 +47,19 @@ void freestrpc(void *ptr) { strpc *newstrpc(upb_handlers *h, const upb_fielddef *f) { /* TODO(haberman): handle malloc failure. */ strpc *ret = malloc(sizeof(*ret)); - size_t len; - ret->len = upb_fielddef_getjsonname(f, NULL, 0); - ret->ptr = malloc(ret->len); - len = upb_fielddef_getjsonname(f, ret->ptr, ret->len); - UPB_ASSERT_VAR(len, len == ret->len); - ret->len--; /* NULL */ + if (getenv("UPB_JSON_WRITE_LEGACY_FIELD_NAMES")) { + /* Temporary code to help people migrate if they were depending on the + * old, non-proto3-json-compliant field names. */ + ret->ptr = upb_strdup(upb_fielddef_name(f)); + ret->len = strlen(ret->ptr); + } else { + size_t len; + ret->len = upb_fielddef_getjsonname(f, NULL, 0); + ret->ptr = malloc(ret->len); + len = upb_fielddef_getjsonname(f, ret->ptr, ret->len); + UPB_ASSERT_VAR(len, len == ret->len); + ret->len--; /* NULL */ + } upb_handlers_addcleanup(h, ret, freestrpc); return ret; -- cgit v1.2.3