From f9afc3e55bbc289df41606d493377318c6645817 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Wed, 13 Jan 2016 18:04:48 -0800 Subject: Changed JSON parser/printer to correctly camelCase names. --- upb/def.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'upb/def.c') diff --git a/upb/def.c b/upb/def.c index 0c694f8..a1dc192 100644 --- a/upb/def.c +++ b/upb/def.c @@ -1,6 +1,7 @@ #include "upb/def.h" +#include #include #include #include "upb/structdefs.int.h" @@ -721,6 +722,35 @@ const char *upb_fielddef_name(const upb_fielddef *f) { return upb_def_fullname(upb_fielddef_upcast(f)); } +bool upb_fielddef_getjsonname(const upb_fielddef *f, char *buf) { + const char *name = upb_fielddef_name(f); + size_t i, j; + bool ucase_next = false; + + if (!name) return false; + + /* Implement the transformation as described in the spec: + * 1. upper case all letters after an underscore. + * 2. remove all underscores. + */ + for (i = 0, j = 0; name[i]; i++) { + if (name[i] == '_') { + ucase_next = true; + continue; + } + + if (ucase_next) { + buf[j++] = toupper(name[i]); + ucase_next = false; + } else { + buf[j++] = name[i]; + } + } + + buf[j] = '\0'; + return true; +} + const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f) { return f->msg_is_symbolic ? NULL : f->msg.def; } -- cgit v1.2.3