Live data from Hacker News

Using Protobuf instead of JSON to communicate with a front end

blog.wearewizards.io

21–30 of 99 posts

Re: Using Protobuf instead of JSON to communicate with a front end

#21
post #14

"While I see the need for Protobuf and Thrift for services communication, I don't really see the point of using it instead of JSON for the frontend." Ah Ok whew, so the title was wrong or designed for click bait.

Has the title been updated? It currently is "Using Protobuf instead of JSON to communicate with a front end", which is not click bait at all. The author used Protobuf instead of JSON as an experiment, and concluded that there is no reason to use it.

Re: Using Protobuf instead of JSON to communicate with a front end

#23
post #13

I always wondered why google decided to build Protocol Buffers. ASN.1 seemed like it worked well, and it covered all the corners.

Here was Kenton Varda's response: https://groups.google.com/forum/#!topic/protobuf/eNAZlnPKVW4 My understanding of ASN.1 is that it has no affordance for forwards- and backwards-compatibility, which is critical in distributed systems where the components are constantly changing. ... OK, I looked into this again (something I do once every few years when someone points it out). ASN.1 _by default_ has no extensibility,…

So, CADT?

Re: Using Protobuf instead of JSON to communicate with a front end

#24
post #7

I like to use Protobuf in my server code, but then support JSON _or_ Protobuf as the encoding. So browsers can continue to use JSON, but the server gets strongly-typed Protobuf structures.

Yeah,

    if you are using a statically typed language,  binary formats like Protobuf are a big win,  but if you are going to have the dynamic language overheard that comes with JS,  there isn't much gain to be had from binary formats.

Re: Using Protobuf instead of JSON to communicate with a front end

#26

It's possible to encode a protobuf as JSON and we do it all the time at Google. In browsers, native JSON parsing is very fast and the data is compressed, so going to a binary format doesn't seem worthwhile. The .proto file is used basically as an IDL from which we generate code.

Personally I've found JSON encoded protobufs to be almost universally awful.

The most common method is to use an array indexed by the field number. I've seen protobufs with hundreds of fields so that's hundreds of nulls as the string "null".

The alternative is to have JSON objects with attributes named after the protobufs field name. This isn't without warts either and seems to be less prevelant in my experience.

Another problem is JavaScript doesn't support all the data types you can get in protobufs, most notably int64s.

Protobufs are relatively space efficient (eg variable width int types). JSON encoded protobufs much less so.

Perhaps the rise of browser support for raw binary data will make this less awful.

Many consider it a virtue to use the same code on the client and server. It explains things like this and GWT. Personally opi think this is horribly misguided and a fools errand. You want to decouple your client and server as much as possible (IMHO).

Disclaimer: I work for Google

Re: Using Protobuf instead of JSON to communicate with a front end

#27

One thing, where protobuf (at least protobuf-net) really shines, is serialization of data into a binary format which is incredibly fast. In .NET, all inbuilt alternatives are slower by a large margin. https://code.google.com/p/protobuf-net/wiki/Performance

I agree. I recently converted some large files that were previously stored using XmlSerializer to use protobuf-net, and I found an 8x increase in space efficiency, and 6-7x increase in (de)serialization efficiency. It really is a fantastic library, and if your classes are already marked up for serialization, there is very minimal work required to make the switch. For files that need not be human-readable, protobuf is definitely the way to go.

Re: Using Protobuf instead of JSON to communicate with a front end

#28
post #7

I like to use Protobuf in my server code, but then support JSON _or_ Protobuf as the encoding. So browsers can continue to use JSON, but the server gets strongly-typed Protobuf structures.

What you describe is exactly how proto3, the latest version of protobuf, will work!

proto3 supports both binary protobuf encoding and JSON natively, so you can switch between them as desired. https://developers.google.com/protocol-buffers/docs/proto3

proto3 is currently in alpha, but we are working to bring it closer to release (I work on the protobuf team at Google).

Re: Using Protobuf instead of JSON to communicate with a front end

#30
post #7

I like to use Protobuf in my server code, but then support JSON _or_ Protobuf as the encoding. So browsers can continue to use JSON, but the server gets strongly-typed Protobuf structures.

What you describe is exactly how proto3, the latest version of protobuf, will work! proto3 supports both binary protobuf encoding and JSON natively, so you can switch between them as desired. https://developers.google.com/protocol-buffers/docs/proto3 proto3 is currently in alpha, but we are working to bring it closer to release (I work on the protobuf team at Google).

Oh cool, didn't know there was a new version of protocol buffers. I ended up choosing Thrift for my current project due to wider language support, but I have been frustrated with some the limitations of the IDL (primarily no recursive data structures, so no generic storage of JSON-like objects).
Post reply on HN