diff options
author | Joshua Haberman <jhaberman@gmail.com> | 2016-02-17 22:01:34 -0800 |
---|---|---|
committer | Joshua Haberman <jhaberman@gmail.com> | 2016-02-17 22:01:34 -0800 |
commit | 32236c9cbc5880024a4532030a21b4f396b30b63 (patch) | |
tree | ea224e80be970cf03a705cd40aadd0f2f83ef2b7 /upb/json/printer.c | |
parent | ff6fe32744b75df52c98d8eb67dcea53d1572b68 (diff) | |
parent | 41506406506332675016916280693a8d94e1b3ac (diff) |
Merge pull request #46 from haberman/jsoncamel
Changed JSON parser/printer to correctly camelCase names.
Diffstat (limited to 'upb/json/printer.c')
-rw-r--r-- | upb/json/printer.c | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/upb/json/printer.c b/upb/json/printer.c index 4a6f1ad..0db7b80 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -33,15 +33,28 @@ struct upb_json_printer { /* StringPiece; a pointer plus a length. */ typedef struct { - const char *ptr; + char *ptr; size_t len; } strpc; +void freestrpc(void *ptr) { + strpc *pc = ptr; + free(pc->ptr); + free(pc); +} + +/* Convert fielddef name to JSON name and return as a string piece. */ strpc *newstrpc(upb_handlers *h, const upb_fielddef *f) { + /* TODO(haberman): handle malloc failure. */ strpc *ret = malloc(sizeof(*ret)); - ret->ptr = upb_fielddef_name(f); - ret->len = strlen(ret->ptr); - upb_handlers_addcleanup(h, ret, free); + 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; } |