Merge pull request #55 from haberman/jsonflag

Added migration flag for users using old JSON format.
pull/13171/head
Joshua Haberman 9 years ago
commit 534b5c98ba
  1. 12
      upb/json/parser.c
  2. 8
      upb/json/parser.rl
  3. 19
      upb/json/printer.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));
}

@ -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));
}

@ -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;

Loading…
Cancel
Save