liteserver / binn

Binary Serialization

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add binn_to_string() function

fanoI opened this issue · comments

commented

It is possible to add a function to dump the value of a binn in json like format?

The possible prototype could be:

const char *binn_to_string(void *data, size_t data_len);

I think it will be very important for debug purpose.

Hi fanol!

Check the conversions/json folder. The functions for conversions are there.

You can use this function:

char * binn_to_json(void *binn);

The return value must be freed by the caller using free().

The conversion currently depends on jansson library. I tested with the version 2.3 but you can test with newer ones.

commented

Yes I've noticed that but I think a dependency on jansson should be not needed! It should be an independent way to print the binn object to console what do you think?

Currently the conversion code supports so export as import from JSON and also to/from jansson objects, what makes it useful for serialized data modifications.

This support may not be abandoned.

If you want you can implement a function to just export data as string without any deps as an alternative.

commented

To be clear I don't intended to drop the conversion to json support but I think that should be an independent way to have have a toString() conversion without the need to:

  1. Convert the binn to a json_object
  2. Call the json_to_string() method
  3. Delete the created json
  4. Free the string itself after have printed it

The fact is that in my code we used json as Message format but now we are finding too much slow as we need to convert it in string to transmit the data and in reception to re-parse the data any time! With big json this could be really slow...

But at same time I find really useful to have the possibility for debug purposes to print the received data as string... now I'm asking myself if reconverting to string the binn to print it will defeat the purpose of binn: the string should be generated and allocated while the json library we use don't need to free the string when we do toString() onto a json_object so I imagine it retains a pointer to it will be faster or in the end nothing will change doing the conversion?

Debug does not need to be fast! The current implementation is sufficient for debug purposes.

But if you want a different way, fork the project and make the function in the conversions/string folder.

"binn_to_string" We should make this possible