java.lang.Object
edu.jiangxin.apktoolbox.convert.protobuf.supervised.ProtoToJson

public final class ProtoToJson extends Object
API to decode binary protobuf messages to readable JSON, based on protobuf descriptors given as DescriptorCache.

Instances can be created using the static factory methods fromCache(DescriptorCache) and fromEmptyCache(). In order to add additional descriptors to the cache, it can be get using getCache().

Conversion can be done using one of the various toJson overloads, for example toJson(Path). Variants accepting a String messageTypeName, like toJson(Path, String) will use a descriptor suitable for the given message type. Other variants will attempt to automatically find a best match among all available descriptors.

The conversion methods throw

  • UncheckedIOException if an I/O error occurs during conversion,
  • NoDescriptorFoundException if no suitable descriptor could be found and
  • UncheckedInvalidProtocolBufferException if a message could not be parsed with the demanded descriptor.

Usage example:


 DescriptorCache cache = DescriptorCache.fromDirectory(Path.of("descriptorCache"));
 ProtoToJson protoToJson = ProtoToJson.fromCache(cache);

 String json = protoToJson.toJson(Path.of("someProtoMessage.message"));
 
Descriptors can be obtained by applying a protoc command on the protobuf schema .proto, for example:

 protoc --descriptor_set_out foo.desc foo.proto
 
Author:
Daniel Tischner <zabuza.dev@gmail.com>
  • Method Details

    • fromCache

      public static ProtoToJson fromCache(DescriptorCache cache)
      Creates an API from a given descriptor cache.
      Parameters:
      cache - The cache to use, not null
      Returns:
      The API instance
    • fromEmptyCache

      public static ProtoToJson fromEmptyCache()
      Creates an API from an empty descriptor cache. The cache can be get using getCache().
      Returns:
      The API instance
    • getCache

      public DescriptorCache getCache()
      Gets the descriptor cache used by the API. For example, to add more descriptors to the cache.
      Returns:
      The cache used by the API
    • toJson

      public String toJson(Path messageFile)
      Converts the protobuf message given as a file to JSON.

      The method will attempt to find a suitable descriptor to parse the message from the cache.

      Parameters:
      messageFile - Path to the protobuf message file, not null
      Returns:
      The JSON decoded message
      Throws:
      NoDescriptorFoundException - If no suitable descriptor to parse the message could be found in the descriptor cache
      edu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedInvalidProtocolBufferException - If the message is in an invalid format or not in the format expected by the descriptor
      UncheckedIOException - If an I/O error occurs during reading the file
    • toJson

      public String toJson(byte[] messageRaw)
      Converts the given raw protobuf message to JSON.

      The method will attempt to find a suitable descriptor to parse the message from the cache.

      Parameters:
      messageRaw - The raw protobuf message, not null
      Returns:
      The JSON decoded message
      Throws:
      NoDescriptorFoundException - If no suitable descriptor to parse the message could be found in the descriptor cache
      edu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedInvalidProtocolBufferException - If the message is in an invalid format or not in the format expected by the descriptor
    • toJson

      public String toJson(Path messageFile, String messageTypeName)
      Converts the protobuf message given as a file to JSON.

      If messageTypeName is null, it will attempt to find a suitable descriptor from the cache. Otherwise it will use a descriptor for the given message type.

      Parameters:
      messageFile - Path to the protobuf message file, not null
      messageTypeName - The name of the messages type or null if the API should pick a suitable descriptor itself
      Returns:
      The JSON decoded message
      Throws:
      NoDescriptorFoundException - If no suitable descriptor to parse the message could be found in the descriptor cache
      edu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedInvalidProtocolBufferException - If the message is in an invalid format or not in the format expected by the descriptor
      UncheckedIOException - If an I/O error occurs during reading the file
    • toJson

      public String toJson(byte[] messageRaw, String messageTypeName)
      Converts the given raw protobuf message to JSON.

      If messageTypeName is null, it will attempt to find a suitable descriptor from the cache. Otherwise it will use a descriptor for the given message type.

      Parameters:
      messageRaw - The raw protobuf message, not null
      messageTypeName - The name of the messages type or null if the API should pick a suitable descriptor itself
      Returns:
      The JSON decoded message
      Throws:
      NoDescriptorFoundException - If no suitable descriptor to parse the message could be found in the descriptor cache
      edu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedInvalidProtocolBufferException - If the message is in an invalid format or not in the format expected by the descriptor
    • toJson

      public String toJson(byte[] messageRaw, com.google.protobuf.Descriptors.Descriptor descriptor)
      Converts the given raw protobuf message to JSON using the given descriptor.
      Parameters:
      messageRaw - The raw protobuf message, not null
      descriptor - The descriptor to use for parsing the message, not null
      Returns:
      The JSON decoded message
      Throws:
      edu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedInvalidProtocolBufferException - If the message is in an invalid format or not in the format expected by the given descriptor
    • toJson

      public String toJson(com.google.protobuf.DynamicMessage message)
      Converts the given protobuf message to JSON.
      Parameters:
      message - The protobuf message, not null
      Returns:
      The JSON decoded message
      Throws:
      edu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedInvalidProtocolBufferException - If the message is in an invalid format