sannis / protobuf2json-c Goto Github PK
View Code? Open in Web Editor NEWPure C implementation of Protobuf to/from JSON converter
License: MIT License
Pure C implementation of Protobuf to/from JSON converter
License: MIT License
Google's spec for converting between proto3 and JSON[1] specifies that 64 bit integers should be encoded as JSON strings instead of JSON numbers. I think it would be a good idea to follow that because:
However, the spec also states that 64 bit integers encoded as JSON numbers should be accepted (i.e. be permissive). Following that may not be a good idea because it could appear to work when testing with small integers but fail with larger integers that Jansson cannot handle.
Would you be interested in a pull request for this? Do you have any other insight I should be aware of?
[1] https://developers.google.com/protocol-buffers/docs/proto3#json
https://developers.google.com/protocol-buffers/docs/proto3#json specifies that values of protobuf type “bytes” are encoded as base64 strings in the JSON representation. protobuf2json-c encodes/decodes them as plain strings. Who is right?
(I suspect it must be the former, because in a strict implementation the latter would make it impossible to encode byte sequences that are not valid UTF-8.)
Considering the following use case:
int result;
char error_string[256] = {0};
const char *initial_json_string = \
"{\n"
" \"name\": \"John Doe\",\n"
" \"id\": 42\n"
"}";
Foo__Person* pPerson = NULL;
result = json2protobuf_string((char *)initial_json_string, JSON_REJECT_DUPLICATES,
&foo__person__descriptor, (ProtobufCMessage**)&pPerson,
error_string, sizeof(error_string));
CPPUNIT_ASSERT_EQUAL(result, 0);
CPPUNIT_ASSERT(pPerson);
CPPUNIT_ASSERT_EQUAL(pPerson->id, (int32)42);
CPPUNIT_ASSERT_STREQUAL(pPerson->name, "John Doe");
free(pPerson);
Why does the message output buffer require to be passed as (ProtobufCMessage**)
? In my opinion it should be either (ProtobufCMessage*)
or (void*)
for the following reasons:
calloc()
in json2protobuf_process_message()
for performance reasons. The caller needs to know the exact type anyway, as he is required to pass the message descriptor as well.In section PROTOBUF_C_LABEL_REPEATED (func protobuf2json_process_message) protobuf_values_count is offset for value, not size!
const size_t *protobuf_values_count = (const size_t *)protobuf_value_quantifier;
then you use it as size
for (j = 0; j < *protobuf_values_count; j++) {
const char *protobuf_value_repeated = (*(char * const *)protobuf_value) + j * value_size;
...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.