Class ProtoToJson
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 andUncheckedInvalidProtocolBufferException
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 Summary
Modifier and TypeMethodDescriptionstatic ProtoToJson
fromCache
(DescriptorCache cache) Creates an API from a given descriptor cache.static ProtoToJson
Creates an API from an empty descriptor cache.getCache()
Gets the descriptor cache used by the API.toJson
(byte[] messageRaw) Converts the given raw protobuf message to JSON.toJson
(byte[] messageRaw, com.google.protobuf.Descriptors.Descriptor descriptor) Converts the given raw protobuf message to JSON using the given descriptor.Converts the given raw protobuf message to JSON.toJson
(com.google.protobuf.DynamicMessage message) Converts the given protobuf message to JSON.Converts the protobuf message given as a file to JSON.Converts the protobuf message given as a file to JSON.
-
Method Details
-
fromCache
Creates an API from a given descriptor cache.- Parameters:
cache
- The cache to use, not null- Returns:
- The API instance
-
fromEmptyCache
Creates an API from an empty descriptor cache. The cache can be get usinggetCache()
.- Returns:
- The API instance
-
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
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 cacheedu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedInvalidProtocolBufferException
- If the message is in an invalid format or not in the format expected by the descriptorUncheckedIOException
- If an I/O error occurs during reading the file
-
toJson
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 cacheedu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedInvalidProtocolBufferException
- If the message is in an invalid format or not in the format expected by the descriptor
-
toJson
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 nullmessageTypeName
- 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 cacheedu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedInvalidProtocolBufferException
- If the message is in an invalid format or not in the format expected by the descriptorUncheckedIOException
- If an I/O error occurs during reading the file
-
toJson
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 nullmessageTypeName
- 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 cacheedu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedInvalidProtocolBufferException
- If the message is in an invalid format or not in the format expected by the descriptor
-
toJson
Converts the given raw protobuf message to JSON using the given descriptor.- Parameters:
messageRaw
- The raw protobuf message, not nulldescriptor
- 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
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
-