use of com.google.protobuf.util.JsonFormat.TypeRegistry in project yamcs by yamcs.
the class HttpServer method addApi.
public void addApi(Api<Context> api) {
apis.add(api);
for (MethodDescriptor method : api.getDescriptorForType().getMethods()) {
RpcDescriptor descriptor = protobufRegistry.getRpc(method.getFullName());
if (descriptor == null) {
throw new UnsupportedOperationException("Unable to find rpc definition: " + method.getFullName());
}
if (WEBSOCKET_ROUTE.equals(descriptor.getHttpRoute())) {
topics.add(new Topic(api, descriptor.getWebSocketTopic(), descriptor));
for (WebSocketTopic topic : descriptor.getAdditionalWebSocketTopics()) {
topics.add(new Topic(api, topic, descriptor));
}
} else {
routes.add(new Route(api, descriptor.getHttpRoute(), descriptor, metricRegistry));
for (HttpRoute route : descriptor.getAdditionalHttpRoutes()) {
routes.add(new Route(api, route, descriptor, metricRegistry));
}
}
}
// Regenerate JSON converters with type support (needed for the "Any" type)
TypeRegistry.Builder typeRegistryb = TypeRegistry.newBuilder();
typeRegistryb.add(CancelOptions.getDescriptor());
typeRegistryb.add(Reply.getDescriptor());
apis.forEach(a -> typeRegistryb.add(a.getDescriptorForType().getFile().getMessageTypes()));
TypeRegistry typeRegistry = typeRegistryb.build();
jsonParser = JsonFormat.parser().usingTypeRegistry(typeRegistry);
jsonPrinter = JsonFormat.printer().usingTypeRegistry(typeRegistry);
// Sort in a way that increases chances of a good URI match
Collections.sort(routes);
}
use of com.google.protobuf.util.JsonFormat.TypeRegistry in project curiostack by curioswitch.
the class MessageMarshallerTest method assertMatchesUpstream.
private static void assertMatchesUpstream(Message message, boolean includingDefaultValueFields, boolean preservingProtoFieldNames, boolean omittingInsignificantWhitespace, boolean printingEnumsAsInts, boolean sortingMapKeys, Message... additionalTypes) throws IOException {
MessageMarshaller.Builder marshallerBuilder = MessageMarshaller.builder().register(message.getClass()).includingDefaultValueFields(includingDefaultValueFields).preservingProtoFieldNames(preservingProtoFieldNames).omittingInsignificantWhitespace(omittingInsignificantWhitespace).printingEnumsAsInts(printingEnumsAsInts).sortingMapKeys(sortingMapKeys);
for (Message m : additionalTypes) {
marshallerBuilder.register(m.getDefaultInstanceForType());
}
MessageMarshaller marshaller = marshallerBuilder.build();
TypeRegistry.Builder typeRegistry = TypeRegistry.newBuilder();
for (Message m : additionalTypes) {
typeRegistry.add(m.getDescriptorForType());
}
Printer printer = JsonFormat.printer().usingTypeRegistry(typeRegistry.build());
if (includingDefaultValueFields) {
printer = printer.includingDefaultValueFields();
}
if (preservingProtoFieldNames) {
printer = printer.preservingProtoFieldNames();
}
if (omittingInsignificantWhitespace) {
printer = printer.omittingInsignificantWhitespace();
}
if (printingEnumsAsInts) {
printer = printer.printingEnumsAsInts();
}
if (sortingMapKeys) {
printer = printer.sortingMapKeys();
}
String json = marshaller.writeValueAsString(message);
String upstreamJson = printer.print(message);
assertThat(json).isEqualTo(upstreamJson);
// whether this can be fixed.
if (!sortingMapKeys) {
String jsonFromBytes = new String(marshaller.writeValueAsBytes(message), StandardCharsets.UTF_8);
assertThat(jsonFromBytes).isEqualTo(upstreamJson);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
marshaller.writeValue(message, bos);
assertThat(bos.toString(StandardCharsets.UTF_8.toString())).isEqualTo(upstreamJson);
}
Message.Builder builder = message.newBuilderForType();
mergeFromJson(json, builder, additionalTypes);
assertThat(builder.build()).isEqualTo(message);
}
use of com.google.protobuf.util.JsonFormat.TypeRegistry in project fizz-gateway-community by wehotel.
the class GrpcProxyClient method invokeMethodAsync.
public ListenableFuture<Void> invokeMethodAsync(GrpcMethodDefinition definition, Channel channel, CallOptions callOptions, List<String> requestJsonTexts, CallResults results) {
FileDescriptorSet fileDescriptorSet = GrpcReflectionUtils.resolveService(channel, definition.getFullServiceName());
if (fileDescriptorSet == null) {
return null;
}
ServiceResolver serviceResolver = ServiceResolver.fromFileDescriptorSet(fileDescriptorSet);
MethodDescriptor methodDescriptor = serviceResolver.resolveServiceMethod(definition);
TypeRegistry registry = TypeRegistry.newBuilder().add(serviceResolver.listMessageTypes()).build();
List<DynamicMessage> requestMessages = GrpcReflectionUtils.parseToMessages(registry, methodDescriptor.getInputType(), requestJsonTexts);
// CallResults results = new CallResults();
StreamObserver<DynamicMessage> streamObserver = MessageWriter.newInstance(registry, results);
CallParams callParams = CallParams.builder().methodDescriptor(methodDescriptor).channel(channel).callOptions(callOptions).requests(requestMessages).responseObserver(streamObserver).build();
return grpcClient.call(callParams);
}
Aggregations