type_url should be an identifier of the type of the protobuf message you're 
transcoding. By default the url of a specific message type looks like 
*type.googleapis.com/<package_name>.<message_name>. 
*I'd imagine the prefix might be configurable to something other than 
*type.googleapis.com*, but can't say for sure, haven't tried changing it.

A TypeResolver instance can be created with the function 
*NewTypeResolverForDescriptorPool 
*declared in type_resolver_util.h:
https://github.com/google/protobuf/blob/master/src/google/protobuf/util/type_resolver_util.h

*NewTypeResolverForDescriptorPool *takes a pointer to a DescriptorPool. If 
the generated code for the relevant type of message has been compiled as 
part of your binary then its descriptor should be in the generated 
descriptor pool so you should just use that. Otherwise, you can build the 
descriptor and the pool from a FileDescriptorProto.


Usage might look something like this:
...


#include <google/protobuf/message.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/util/json_util.h>
#include <google/protobuf/util/type_resolver_util.h>


using namespace google::protobuf;
using namespace google::protobuf::util;

...


void foo(const Message& msg)
{
 ...

 std::string json_output;
 TypeResolver* resolver = NewTypeResolverForDescriptorPool(
"type.googleapis.com", &DescriptorPool::generated_pool());
 
 Status status = BinaryToJsonString(resolver, "type.googleapis.com/" + msg.
GetTypeName(), msg.SerializeAsString(), &json_output);
 
 std::cout << json_output;


 delete resolver;
 ...
}


...


I hope this helps.


Ron



On Tuesday, April 5, 2016 at 9:23:17 PM UTC+3, Zachary Deretsky wrote:

> Ron,
> could you post and example and some explanation on how to (de)serialize 
> proto3 to JSON using
> LIBPROTOBUF_EXPORT util::Status BinaryToJsonString(
>     TypeResolver* resolver,
>     const string& type_url,
>     const string& binary_input,
>     string* json_output,
>     const JsonOptions& options);
>
> How to create TypeResolver and what is type_url?
>
> I am asking because you seem to be the only one with expertise on the 
> subject.
> Thank you, Zach. 
>
>
> On Thursday, November 26, 2015 at 12:51:07 AM UTC-8, Ron Ben-Yosef wrote:
>>
>>
>> On Wednesday, November 25, 2015 at 8:56:51 PM UTC+2, Feng Xiao wrote:
>>>
>>> Thanks for the explanation. Could you help file a bug for this on 
>>> protobuf github site? If you know of an solution to this, you are also 
>>> welcomed to send us a pull request.
>>>
>>
>> Sure, no problem.
>>
>> https://github.com/google/protobuf/issues/1010 
>>
>

-- 
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 protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to