Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is any ways to convert google protobuf message into json with indentation?

I need to save Google protobuf IMessage object into json file, using C#. here is sample code:

using (var input = File.OpenRead(protodatFile)) 
{
    string jsonString = null;
    message.MergeFrom(input); //read message from protodat file
    JsonFormatter formater = new JsonFormatter(
        new JsonFormatter.Settings(false));
    jsonString = formatter.Format(message);
    System.IO.File.WriteAllText(jsonFile, jsonString);
}

This uses the JsonFormatter from the Google Protobuf library.

The problem: all json content is stored in one line. when file is quite big(>50 MB) it is hard to open/view in text editor.

What is the best way to make indented jsonFile here?

like image 372
Dorrian Do Avatar asked Jul 22 '18 17:07

Dorrian Do


People also ask

Can protobuf be converted to JSON?

A Parser parses JSON to protobuf message. A Printer converts protobuf message to JSON format. A TypeRegistry is used to resolve Any messages in the JSON conversion.

Does protobuf use JSON?

JSON is limited to certain python objects, and it cannot serialize every python object. Protobuf supports a wider range of data types when compared to JSON. For example, enumerations and methods are supported by Protobuf and not supported by JSON. JSON supports only a subset of python data types.

Is protobuf like JSON?

Protocol buffers provide a language-neutral, platform-neutral, extensible mechanism for serializing structured data in a forward-compatible and backward-compatible way. It's like JSON, except it's smaller and faster, and it generates native language bindings.

Why protobuf is faster than JSON?

JSON is usually easier to debug (the serialized format is human-readable) and easier to work with (no need to define message types, compile them, install additional libraries, etc.). Protobuf, on the other hand, usually compresses data better and has built-in protocol documentation via the schema.


1 Answers

As an extremely inefficient workaround, one can use Json.NET to re-format the Protobuffer Json:

// Re-Parse the one-line protobuf json string into an object:
object parsed = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString);
// Format this object as JSON with indentation:
string jsonWithIndent = Newtonsoft.Json.JsonConvert.SerializeObject(parsed, Newtonsoft.Json.Formatting.Indented);

Since the original question asked about a 50MB file, this is probably a bad solution for such large-ish files, but given that I couldn't find anything on JsonFormatter.Settings, it was what I reached for.

like image 139
Martin Ba Avatar answered Oct 20 '22 00:10

Martin Ba