Once you have the DynamicMessage, you can use the JsonFormat API the same
way you would use it with ordinary messages.

On Fri, Feb 24, 2023 at 1:52 PM ritesh singh <[email protected]> wrote:

> Thanks, any leads on generating json at run time using dynamicMessage. Any
> existing api util if you can point me to.
>
> On Saturday, February 25, 2023 at 2:44:14 AM UTC+5:30 Adam Cozzette wrote:
>
>> I think DynamicMessage is the right approach since you need to work with
>> proto files that are only known at run time. This will also allow you to
>> generate JSON.
>>
>> On Fri, Feb 24, 2023 at 11:19 AM ritesh singh <[email protected]>
>> wrote:
>>
>>>
>>> Or if there's a better different approach instead of relying on
>>> FileDescriptorSet and DynamicMessage - considering i have access to only
>>> .pb (generate by proto-lite) or .proto file.
>>> On Saturday, February 25, 2023 at 12:46:44 AM UTC+5:30 ritesh singh
>>> wrote:
>>>
>>>> Thanks Adam, so basically i am writing an Intellij plugin for Android
>>>> Studio.
>>>> The plugin will just take the .pb file and .proto file, run the protoc
>>>> compiler, generate FileDescriptorSet for proto and create a DynamicMessage
>>>> using generated fileDescriptorSet and .pb.
>>>>
>>>> I was wondering if there a way to generate the json schema, as in
>>>> key-value pair. As we know, lite version doesn't include fileds, is it
>>>> possible to do it at run-time, as in extract keys in any phase?
>>>>
>>>> On Saturday, February 25, 2023 at 12:30:54 AM UTC+5:30 Adam Cozzette
>>>> wrote:
>>>>
>>>>> A serialized protocol buffer doesn't include any type information, so
>>>>> before you can parse one you have to know in advance which type you're
>>>>> expecting. If you want to be prepared to accept either A or B, then a good
>>>>> solution is to put both types inside a oneof in a parent message, and then
>>>>> just parse the parent message normally.
>>>>>
>>>>> It doesn't matter which protobuf implementation created the .pb file,
>>>>> since all implementations use the same wire format.
>>>>>
>>>>> That is true that the Java lite implementation doesn't support
>>>>> reflection and doesn't include descriptors, so it cannot serialize protos
>>>>> to JSON. If you can stick to the binary format then that would be ideal
>>>>> since that format is useable everywhere.
>>>>>
>>>>> On Thu, Feb 23, 2023 at 5:33 PM ritesh singh <[email protected]>
>>>>> wrote:
>>>>>
>>>>>> Also does it matter if .pb was created using proto lite and android
>>>>>> end.
>>>>>>
>>>>>> I believe the lite version doesn't store fields, so converting it to
>>>>>> json using proto-util won't work and i need to make use of reflection.
>>>>>>
>>>>>> On Fri, 24 Feb, 2023, 6:54 am ritesh singh, <[email protected]>
>>>>>> wrote:
>>>>>>
>>>>>>> Thanks Adam. I will give it a shot. I thought of using *protoc*
>>>>>>> compiler, but what if my proto file contains nested messages
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> *message A {  B b = 1}*
>>>>>>> *message B {*
>>>>>>> *}*
>>>>>>>
>>>>>>>
>>>>>>> After running *protoc *on the proto file, it will generate 2 java
>>>>>>> classes. I won't know which generated class to use against my .pb file, 
>>>>>>> as
>>>>>>> my plugin is only aware of .pb and .proto file.
>>>>>>>
>>>>>>> Please correct me, if am wrong.
>>>>>>> On Friday, February 24, 2023 at 6:42:05 AM UTC+5:30 Adam Cozzette
>>>>>>> wrote:
>>>>>>>
>>>>>>>> I suspect that your code is getting an error because it's trying to
>>>>>>>> parse a .proto file as a serialized protocol buffer. (This won't work 
>>>>>>>> since
>>>>>>>> .proto files use a text representation very different from the standard
>>>>>>>> protobuf binary format.) Probably the best way to fix this problem 
>>>>>>>> would be
>>>>>>>> to invoke protoc with --descriptor_set_out to parse the proto file and
>>>>>>>> convert it into a serialized FileDescriptorSet. A FileDescriptorSet is 
>>>>>>>> just
>>>>>>>> a protocol buffer, so once you have it in that form you can easily 
>>>>>>>> parse it
>>>>>>>> like you would parse any other serialized proto.
>>>>>>>>
>>>>>>>> On Thu, Feb 23, 2023 at 4:59 PM ritesh singh <[email protected]>
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>> Hello,
>>>>>>>>>
>>>>>>>>> I am working on creating a Android Studio plugin for proto data
>>>>>>>>> store.
>>>>>>>>> Android uses proto-lite, my current approach, i get the .pb file
>>>>>>>>> and .proto file and use the same in intellij plugin to deserialise it.
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> *val psiFile = PsiManager.getInstance(project).findFile(protoFile)
>>>>>>>>> val protoFileContent = psiFile!!.text val fileDescriptorProto:
>>>>>>>>> DescriptorProtos.FileDescriptorProto =
>>>>>>>>> DescriptorProtos.FileDescriptorProto.parseFrom(protoFileContent.byteInputStream())
>>>>>>>>> val fileDescriptor =
>>>>>>>>> Descriptors.FileDescriptor.buildFrom(fileDescriptorProto, 
>>>>>>>>> arrayOfNulls(0))
>>>>>>>>> val messageDescriptor = fileDescriptor.messageTypes[0] val buffer =
>>>>>>>>> pbFile.contentsToByteArray() val messageBuilder: 
>>>>>>>>> DynamicMessage.Builder =
>>>>>>>>> DynamicMessage.newBuilder(messageDescriptor)
>>>>>>>>> messageBuilder.mergeFrom(buffer) val message = messageBuilder.build() 
>>>>>>>>> val
>>>>>>>>> text: String = TextFormat.printToString(message)*
>>>>>>>>>
>>>>>>>>> Above code is throwing error in parsing step,* Protocol message
>>>>>>>>> tag had invalid wire type.*
>>>>>>>>>
>>>>>>>>> Thanks in advance.
>>>>>>>>>
>>>>>>>>> --
>>>>>>>>> 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/7fe3a019-e846-4792-8d8a-1655a2d93975n%40googlegroups.com
>>>>>>>>> <https://groups.google.com/d/msgid/protobuf/7fe3a019-e846-4792-8d8a-1655a2d93975n%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/5042e4c5-2643-4587-812a-730206f14424n%40googlegroups.com
>>>>>>> <https://groups.google.com/d/msgid/protobuf/5042e4c5-2643-4587-812a-730206f14424n%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/CABaCSzEG%2B19VhXjsYJpphbxA%2B3s7mAmUSiDF_Kmkkr5_Y0A%3Dsg%40mail.gmail.com
>>>>>> <https://groups.google.com/d/msgid/protobuf/CABaCSzEG%2B19VhXjsYJpphbxA%2B3s7mAmUSiDF_Kmkkr5_Y0A%3Dsg%40mail.gmail.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/10949542-694e-48b3-8743-7711237f9725n%40googlegroups.com
>>> <https://groups.google.com/d/msgid/protobuf/10949542-694e-48b3-8743-7711237f9725n%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/ee678da0-6fbc-4c32-ac1a-8c5499efaf66n%40googlegroups.com
> <https://groups.google.com/d/msgid/protobuf/ee678da0-6fbc-4c32-ac1a-8c5499efaf66n%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/CADqAXr49_BdFzvV%3DYVN4Y7Ng8-8AcLOipDrFau3pmKe1FQXWyg%40mail.gmail.com.

Reply via email to