Thanks Adam.
This how my code looks like
* fun decodeProto(protoFile: VirtualFile) { try { val fileDescriptorSet =
generateFileDescriptorSet(protoFile) val fileDescriptor:
Descriptors.FileDescriptor = parseFileDescriptorSet(fileDescriptorSet) val
dynamicMessage:DynamicMessage =
DynamicMessage.parseFrom(fileDescriptor.messageTypes[0],pbFile.inputStream.readBytes())
println(TextFormat.printToString(dynamicMessage)) }catch (ex:Exception){
ex.printStackTrace() } } private fun generateFileDescriptorSet(protoFile:
VirtualFile): ByteArray { val protocCommand = "protoc
-I=${protoFile.parent} --include_imports --descriptor_set_out=/dev/stdout
${protoFile.path}" val outputStream = ByteArrayOutputStream() val process =
Runtime.getRuntime().exec(protocCommand)
process.inputStream.copyTo(outputStream) process.waitFor() return
outputStream.toByteArray() } private fun
parseFileDescriptorSet(fileDescriptorSetBytes: ByteArray):
Descriptors.FileDescriptor { val fileDescriptorSet =
DescriptorProtos.FileDescriptorSet.parseFrom(fileDescriptorSetBytes) val
fileDescriptors = fileDescriptorSet.fileList.map {
Descriptors.FileDescriptor.buildFrom(it,
arrayOf<Descriptors.FileDescriptor>()) }.toTypedArray() return
Descriptors.FileDescriptor.buildFrom( fileDescriptorSet.fileList.first(),
fileDescriptors ) }*
I was wondering if there is an existing api, which will convert the
*DynamicMessage
*to json.
Also, in other thread you mentioned, it's better to use *cd *to fix import
issues,
probably do cd here *val protocCommand = "protoc -I=${protoFile.parent}
--include_imports --descriptor_set_out=/dev/stdout ${protoFile.path}" *and
then run the command.
Thanks a lot!
On Saturday, February 25, 2023 at 2:35:02 AM UTC+5:30 Adam Cozzette wrote:
> Probably the cleanest solution would be to cd to the directory containing
> com/ and run protoc from there: protoc -I . --include_imports
> --descriptor_set_out=/dev/stdout
> com/google/samples/apps/nowinandroid/data/user_preferences.proto
>
> On Fri, Feb 24, 2023 at 11:11 AM ritesh singh <[email protected]> wrote:
>
>> If i remove imports, it works fine.
>>
>> On Saturday, February 25, 2023 at 12:41:13 AM UTC+5:30 ritesh singh wrote:
>>
>>> It looks like this
>>>
>>> syntax = "proto3";
>>>
>>> import
>>> "com/google/samples/apps/nowinandroid/data/dark_theme_config.proto";
>>> import "com/google/samples/apps/nowinandroid/data/theme_brand.proto";
>>>
>>> option java_package =
>>> "com.google.samples.apps.nowinandroid.core.datastore";
>>> option java_multiple_files = true;
>>>
>>> message UserPreferences {
>>> reserved 2;
>>> repeated int32 deprecated_int_followed_topic_ids = 1;
>>> int32 topicChangeListVersion = 3;
>>> int32 authorChangeListVersion = 4;
>>> int32 newsResourceChangeListVersion = 6;
>>> repeated int32 deprecated_int_followed_author_ids = 7;
>>> bool has_done_int_to_string_id_migration = 8;
>>> repeated string deprecated_followed_topic_ids = 9;
>>> repeated string deprecated_followed_author_ids = 10;
>>> repeated string deprecated_bookmarked_news_resource_ids = 11;
>>> bool has_done_list_to_map_migration = 12;
>>>
>>> // Each map is used to store a set of string IDs. The bool has no
>>> meaning, but proto3 doesn't
>>> // have a Set type so this is the closest we can get to a Set.
>>> map<string, bool> followed_topic_ids = 13;
>>> map<string, bool> followed_author_ids = 14;
>>> map<string, bool> bookmarked_news_resource_ids = 15;
>>>
>>> ThemeBrandProto theme_brand = 16;
>>> DarkThemeConfigProto dark_theme_config = 17;
>>>
>>> bool should_hide_onboarding = 18;
>>>
>>> bool use_dynamic_color = 19;
>>> }
>>>
>>> On Saturday, February 25, 2023 at 12:34:51 AM UTC+5:30 Adam Cozzette
>>> wrote:
>>>
>>>> What do the import lines in user_preferences.proto look like? In your
>>>> case the import paths should point to files relative to the current
>>>> directory.
>>>>
>>>> On Fri, Feb 24, 2023 at 9:40 AM ritesh singh <[email protected]>
>>>> wrote:
>>>>
>>>>> I have 3 proto files in the current directory.
>>>>>
>>>>> user_preferences.proto imports other protos and all are available in
>>>>> the same directory.
>>>>>
>>>>> But, if i try to run this command, it throws an error. - *Imported
>>>>> file not found.*
>>>>>
>>>>>
>>>>>
>>>>> *protoc --include_imports --descriptor_set_out=/dev/stdout
>>>>> user_preferences.proto*
>>>>> Here, I am trying to invoke protoc with --descriptor_set_out to parse
>>>>> the proto file and convert it into a serialized FileDescriptorSet
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Protocol Buffers" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>> To view this discussion on the web visit
>>>>> https://groups.google.com/d/msgid/protobuf/46f16320-f15b-4c79-a998-57b30e94751cn%40googlegroups.com
>>>>>
>>>>> <https://groups.google.com/d/msgid/protobuf/46f16320-f15b-4c79-a998-57b30e94751cn%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>>
>>>> --
>> You received this message because you are subscribed to the Google Groups
>> "Protocol Buffers" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>>
> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/protobuf/93225cb4-eadc-4a71-9f89-f169043f6da7n%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/protobuf/93225cb4-eadc-4a71-9f89-f169043f6da7n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>
--
You received this message because you are subscribed to the Google Groups
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/protobuf/bc0212ca-7e5b-4e35-ab12-adf94c6d8056n%40googlegroups.com.