Protobuf

Skip to first unread message

anandsekar

unread,
Mar 3, 2011, 7:35:24 PM3/3/11
to google-gson
Does GSON support serialization of generated protobuf classes ?

inde...@gmail.com

unread,
Mar 3, 2011, 7:53:33 PM3/3/11
to google-gson
Yes, it does to some extent. Look at the proto project under Gson:
http://code.google.com/p/google-gson/source/browse/#svn%2Ftrunk%2Fproto

Abhishek Rai

unread,
Apr 28, 2013, 2:29:09 AM4/28/13
to googl...@googlegroups.com
Reviving an old thread.

Is there any plan to export this code through maven central, like rest of gson?  I find the proto adapter quite useful, and although it's not much code, exporting it on maven central will make it easier to depend on it.

Thanks,
Abhishek

Inderjeet Singh

unread,
Apr 28, 2013, 4:23:38 AM4/28/13
to googl...@googlegroups.com
At the moment, this adapter hasn't got enough usage or testing. Hence, we are not yet ready to publish it in a released format.
Can you share your experience with this adapter? Does it have any missing features that you may want?

Inder

Abhishek Rai

unread,
Apr 28, 2013, 11:51:09 AM4/28/13
to googl...@googlegroups.com
It seems to work fine for me, except that I had to make a change to
convert field names with underscores into camel case. I've attached a
diff below.

Thanks,
Abhishek

92,98c92
< /**
< * Name of the field could be in the form abc_def_ghi,
which would be the name
< * declared in the proto file. However, the generated
code in Java for this field
< * would be in camel case with the name abcDefGhi. So,
we must transform field
< * names here to camel case before setting them inside
the generated Java class.
< */
< String fieldName = toCamelCase(name) + "_";
---
> String fieldName = name + "_";
107c101
<
---
>
144,183d137
<
< /**
< * Converts {@code in} to camel case with first character of the
string left as is. For example,
< * "hello" -> "hello", "abc_def" -> "abcDef".
< * @param in is the input string
< * @return camel case output.
< */
< private static String toCamelCase(final String in) {
< final String[] pieces = in.split("_");
< if (pieces.length <= 1) {
< return in;
< }
< final ArrayList<String> list = new ArrayList<String>();
< list.add(pieces[0]);
< for (int i = 1; i < pieces.length; ++i) {
< String piece = pieces[i];
< if (piece.length() > 0) {
< char[] ch = piece.toCharArray();
< ch[0] = Character.toUpperCase(ch[0]);
< list.add(new String(ch));
< }
< }
< return joinStrings(list, "");
< }
< /**
< * Joins list of Strings using the given delimiter.
< * @param list to join
< * @param delimiter to use
< * @return comma separated list of Strings
< */
< private static String joinStrings(final List<String> list, final
String delimiter) {
< String out = new String();
< for (final String str : list) {
< if (false == out.isEmpty()) {
< out += delimiter;
< }
< out += str;
< }
< return out;
> --
> You received this message because you are subscribed to the Google Groups
> "google-gson" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to google-gson...@googlegroups.com.
> To post to this group, send email to googl...@googlegroups.com.
> Visit this group at http://groups.google.com/group/google-gson?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Peter Tillotson

unread,
Sep 20, 2013, 9:26:36 AM9/20/13
to googl...@googlegroups.com
"Can you share your experience with this adapter?"

Took me a while to track it down, but doesn't handle protocol buffer Enum. Enum come through as EnumValueDescriptor which are themselves instances of GeneratedMessage. It StackOverflows trying to serialize the whole of the Descriptor object model. 

if( desc.getType() == Descriptors.FieldDescriptor.Type.ENUM )
{
EnumValueDescriptor eval = (EnumValueDescriptor)src.getField(desc);
ret.add(desc.getName(), context.serialize(eval.getName()));
}
else if (desc.isRepeated())
{

I added the above to my version. 

Peter

Travis Crawford

unread,
Oct 18, 2015, 5:16:44 AM10/18/15
to google-gson
[reviving an old thread]

Hi all,

Similar to others, I came across Gson while looking for a way to serialize protocol buffers to JSON.

Adding https://github.com/google/gson/blob/master/proto/src/main/java/com/google/gson/protobuf/ProtoTypeAdapter.java to my project I was able to use the adapter, and have not had any issues thus-far while testing. Enum values show up in the JSON strings as well.

Reading through the mailing list history, it sounds like the main Gson jar does not include protocol buffer support because the adapter is not ready for prime time. Is that still the case? Are there any known ProtoTypeAdapter issues?

Thanks!
Travis

Inderjeet Singh

unread,
Nov 12, 2015, 12:30:09 PM11/12/15
to google-gson
The reason it is NOT included in the main Gson jar is because Protos are not widely used.

ProtoTypeAdapter is good enough, file any bugs (or better, send pull requests with fixes) if you find any.
Reply all
Reply to author
Forward
0 new messages