本文整理汇总了Java中com.google.protobuf.util.JsonFormat.Printer方法的典型用法代码示例。如果您正苦于以下问题:Java JsonFormat.Printer方法的具体用法?Java JsonFormat.Printer怎么用?Java JsonFormat.Printer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.protobuf.util.JsonFormat
的用法示例。
在下文中一共展示了JsonFormat.Printer方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getDefaultRequestJSON
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
private JsonNode getDefaultRequestJSON(Message msg) throws JsonParseException, IOException
{
Message.Builder o2 = DefaultCustomPredictRequest.newBuilder();
TypeRegistry registry = TypeRegistry.newBuilder().add(o2.getDescriptorForType()).build();
JsonFormat.Printer jPrinter = JsonFormat.printer();
String result = jPrinter.usingTypeRegistry(registry).print(msg);
ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = mapper.getFactory();
JsonParser parser = factory.createParser(result);
JsonNode jNode = mapper.readTree(parser);
if (jNode.has(PredictionBusinessServiceImpl.REQUEST_CUSTOM_DATA_FIELD))
{
JsonNode values = jNode.get(PredictionBusinessServiceImpl.REQUEST_CUSTOM_DATA_FIELD).get("values");
((ObjectNode) jNode).set(PredictionBusinessServiceImpl.REQUEST_CUSTOM_DATA_FIELD, values);
}
return jNode;
}
示例2: get
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
@GET
@Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8" })
public Response get(@Context Response response, @QueryParam(LumongoConstants.INDEX) String index, @QueryParam(LumongoConstants.PRETTY) boolean pretty) {
try {
StringBuilder responseBuilder = new StringBuilder();
IndexConfig indexConfig = indexManager.getIndexConfig(index);
responseBuilder.append("{");
responseBuilder.append("\"indexName\": ");
responseBuilder.append("\"");
responseBuilder.append(indexConfig.getIndexName());
responseBuilder.append("\"");
responseBuilder.append(",");
responseBuilder.append("\"numberOfSegments\": ");
responseBuilder.append(indexConfig.getNumberOfSegments());
responseBuilder.append(",");
responseBuilder.append("\"indexSettings\": ");
JsonFormat.Printer printer = JsonFormat.printer();
responseBuilder.append(printer.print(indexConfig.getIndexSettings()));
responseBuilder.append("}");
String docString = responseBuilder.toString();
if (pretty) {
docString = JsonWriter.formatJson(docString);
}
return Response.status(LumongoConstants.SUCCESS).entity(docString).build();
}
catch (Exception e) {
return Response.status(LumongoConstants.INTERNAL_ERROR).entity("Failed to get index names: " + e.getMessage()).build();
}
}
示例3: getJsonPrinter
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
private JsonFormat.Printer getJsonPrinter() {
if (mJsonPrinter == null) {
TypeRegistry typeRegistry = TypeRegistry.newBuilder()
.add(Entity.getDescriptor())
.build();
mJsonPrinter = JsonFormat.printer()
.usingTypeRegistry(typeRegistry)
.omittingInsignificantWhitespace();
}
return mJsonPrinter;
}
示例4: createPrinter
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
public static JsonFormat.Printer createPrinter() {
TypeRegistry typeRegistry = TypeRegistry.newBuilder()
.add(Entity.getDescriptor())
.build();
return JsonFormat.printer()
.usingTypeRegistry(typeRegistry)
.omittingInsignificantWhitespace();
}
示例5: getJSONFromMethod
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
private JsonNode getJSONFromMethod(Method m,Message msg,String fieldname) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, JsonParseException, IOException
{
Message.Builder o2 = (Message.Builder) m.invoke(null);
TypeRegistry registry = TypeRegistry.newBuilder().add(o2.getDescriptorForType()).build();
JsonFormat.Printer jPrinter = JsonFormat.printer();
String result = jPrinter.usingTypeRegistry(registry).print(msg);
ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = mapper.getFactory();
JsonParser parser = factory.createParser(result);
JsonNode jNode = mapper.readTree(parser);
if (jNode.has(fieldname) && jNode.get(fieldname).has("@type"))
((ObjectNode) jNode.get(fieldname)).remove("@type");
return jNode;
}
示例6: getJSON
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
private JsonNode getJSON(Message msg,String fieldname) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, JsonParseException, IOException
{
JsonFormat.Printer jPrinter = JsonFormat.printer();
String result = jPrinter.print(msg);
ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = mapper.getFactory();
JsonParser parser = factory.createParser(result);
JsonNode jNode = mapper.readTree(parser);
return jNode;
}
示例7: dump
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
public static void dump(File src) throws Exception {
try(FileInputStream f = new FileInputStream(src)) {
Data data = Data.parseFrom(f);
JsonFormat.Printer printer = JsonFormat.printer().usingTypeRegistry(TypeRegistry.getEmptyTypeRegistry());
System.out.println(printer.print(data));
}
}
示例8: MessageWriter
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
@VisibleForTesting
MessageWriter(JsonFormat.Printer jsonPrinter, Output output) {
this.jsonPrinter = jsonPrinter;
this.output = output;
}
示例9: generateOutputFiles
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
private void generateOutputFiles(Service serviceConfig)
throws FileNotFoundException, IOException {
// Create normalized service proto, in binary form.
if (!Strings.isNullOrEmpty(options.get(BIN_OUT))) {
File outFileBinaryServiceConfig = new File(options.get(BIN_OUT));
OutputStream normalizedOut = new FileOutputStream(outFileBinaryServiceConfig);
serviceConfig.writeTo(normalizedOut);
normalizedOut.close();
}
// Create normalized service proto, in text form.
if (!Strings.isNullOrEmpty(options.get(TXT_OUT))) {
File outFileTxtServiceConfig = new File(options.get(TXT_OUT));
try (PrintWriter textPrintWriter =
new PrintWriter(outFileTxtServiceConfig, StandardCharsets.UTF_8.name())) {
TextFormat.print(serviceConfig, textPrintWriter);
}
}
// Create normalized service proto, in json form.
if (!Strings.isNullOrEmpty(options.get(JSON_OUT))) {
File outFileJsonServiceConfig = new File(options.get(JSON_OUT));
TypeRegistry registry =
addPlatformExtensions(TypeRegistry.newBuilder())
.add(Service.getDescriptor())
.add(com.google.protobuf.BoolValue.getDescriptor())
.add(com.google.protobuf.BytesValue.getDescriptor())
.add(com.google.protobuf.DoubleValue.getDescriptor())
.add(com.google.protobuf.FloatValue.getDescriptor())
.add(com.google.protobuf.Int32Value.getDescriptor())
.add(com.google.protobuf.Int64Value.getDescriptor())
.add(com.google.protobuf.StringValue.getDescriptor())
.add(com.google.protobuf.UInt32Value.getDescriptor())
.add(com.google.protobuf.UInt64Value.getDescriptor())
.build();
JsonFormat.Printer jsonPrinter = JsonFormat.printer().usingTypeRegistry(registry);
try (PrintWriter jsonPrintWriter =
new PrintWriter(outFileJsonServiceConfig, StandardCharsets.UTF_8.name())) {
jsonPrinter.appendTo(serviceConfig, jsonPrintWriter);
}
}
}
示例10: toJSON
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
/**
* Format a Protocol Buffers Message to a JSON string
* @param data The Message to be formatted
* @return A JSON String representing the data
* @throws InvalidProtocolBufferException Thrown in case of invalid Message data
*/
public static String toJSON(Message data) throws InvalidProtocolBufferException {
JsonFormat.Printer printer = JsonFormat.printer();
return printer.print(data);
}