本文整理汇总了Java中com.google.protobuf.util.JsonFormat.Parser方法的典型用法代码示例。如果您正苦于以下问题:Java JsonFormat.Parser方法的具体用法?Java JsonFormat.Parser怎么用?Java JsonFormat.Parser使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.protobuf.util.JsonFormat
的用法示例。
在下文中一共展示了JsonFormat.Parser方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: upsertProduct
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
@Test
public void upsertProduct() throws Exception {
Product product = Product.newBuilder()
.setProductId(faker.number().randomNumber())
.setProductName(faker.company().name())
.setProductPrice(faker.number().randomDouble(2, 10, 100))
.setProductStatus(ProductStatus.InStock)
.build();
productDao.upsertProduct(product);
esClient.admin().indices().flush(Requests.flushRequest(INDEX)).actionGet();
GetResponse getResponse = esClient.prepareGet(INDEX, TYPE, String.valueOf(product.getProductId())).get();
JsonFormat.Parser jsonParser = injector.getInstance(JsonFormat.Parser.class);
Product.Builder builder = Product.newBuilder();
jsonParser.merge(getResponse.getSourceAsString(), builder);
assertThat(builder.build()).isEqualTo(product);
}
示例2: getPredictRequestWithCustomDefaultFromJSON
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
private ClassificationRequest getPredictRequestWithCustomDefaultFromJSON(JsonNode json) throws InvalidProtocolBufferException
{
ObjectMapper mapper = new ObjectMapper();
ObjectNode data = mapper.createObjectNode();
data.put("@type", "type.googleapis.com/" + DefaultCustomPredictRequest.class.getName());
data.put("values", json.get(PredictionBusinessServiceImpl.REQUEST_CUSTOM_DATA_FIELD));
((ObjectNode) json).put(PredictionBusinessServiceImpl.REQUEST_CUSTOM_DATA_FIELD, data);
Message.Builder o = DefaultCustomPredictRequest.newBuilder();
TypeRegistry registry = TypeRegistry.newBuilder().add(o.getDescriptorForType()).build();
ClassificationRequest.Builder builder = ClassificationRequest.newBuilder();
JsonFormat.Parser jFormatter = JsonFormat.parser();
if (registry != null)
jFormatter = jFormatter.usingTypeRegistry(registry);
jFormatter.merge(json.toString(), builder);
ClassificationRequest request = builder.build();
return request;
}
示例3: getPredictReplyFromJson
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
public ClassificationReply getPredictReplyFromJson(String client,JsonNode json)
{
RPCConfig config = services.get(client);
try
{
TypeRegistry registry = null;
if (config != null && config.replyClass != null && json.has(PredictionBusinessServiceImpl.REPLY_CUSTOM_DATA_FIELD))
{
if (!json.get(PredictionBusinessServiceImpl.REPLY_CUSTOM_DATA_FIELD).has("@type"))
((ObjectNode) json.get(PredictionBusinessServiceImpl.REPLY_CUSTOM_DATA_FIELD)).put("@type", "type.googleapis.com/" + config.replyClass.getName());
Method m = config.replyBuilder;
Message.Builder o = (Message.Builder) m.invoke(null);
registry = TypeRegistry.newBuilder().add(o.getDescriptorForType()).build();
}
ClassificationReply.Builder builder = ClassificationReply.newBuilder();
JsonFormat.Parser jFormatter = JsonFormat.parser();
if (registry != null)
jFormatter = jFormatter.usingTypeRegistry(registry);
jFormatter.merge(json.toString(), builder);
ClassificationReply reply = builder.build();
return reply;
} catch (Exception e) {
logger.error("Failed to convert json "+json.toString()+" to PredictReply",e);
return null;
}
}
示例4: testCreatesFileAndWritesProtoJsonFormat
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
@Test
public void testCreatesFileAndWritesProtoJsonFormat() throws Exception {
File output = tmp.newFile();
BuildEventStreamProtos.BuildEvent started =
BuildEventStreamProtos.BuildEvent.newBuilder()
.setStarted(BuildStarted.newBuilder().setCommand("build"))
.build();
when(buildEvent.asStreamProto(Matchers.<BuildEventConverters>any())).thenReturn(started);
JsonFormatFileTransport transport =
new JsonFormatFileTransport(output.getAbsolutePath(), pathConverter);
transport.sendBuildEvent(buildEvent, artifactGroupNamer);
transport.close().get();
try (InputStream in = new FileInputStream(output)) {
Reader reader = new InputStreamReader(in);
JsonFormat.Parser parser = JsonFormat.parser();
BuildEventStreamProtos.BuildEvent.Builder builder =
BuildEventStreamProtos.BuildEvent.newBuilder();
parser.merge(reader, builder);
assertThat(builder.build()).isEqualTo(started);
}
}
示例5: getJsonParser
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
private JsonFormat.Parser getJsonParser() {
if (mJsonParser == null) {
TypeRegistry typeRegistry = TypeRegistry.newBuilder()
.add(Entity.getDescriptor())
.build();
mJsonParser = JsonFormat.parser()
.usingTypeRegistry(typeRegistry);
}
return mJsonParser;
}
示例6: MessageReader
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
@VisibleForTesting
MessageReader(
JsonFormat.Parser jsonParser,
Descriptor descriptor,
BufferedReader bufferedReader,
String source) {
this.jsonParser = jsonParser;
this.descriptor = descriptor;
this.bufferedReader = bufferedReader;
this.source = source;
}
示例7: createMoves
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
public Moves createMoves(RawData rawData) {
final Moves.Builder allBuilder = Moves.newBuilder();
final JsonFormat.Parser parser = JsonFormat.parser();
final List<Move> moves = rawData.items.stream().filter(item -> (item != null && item.getMove() != null))
.map(item -> item.getMove()).map(move -> {
final Move.Builder builder = Move.newBuilder();
String moveString = null;
try {
// different names
builder.setMoveId(move.getMovementId());
move.setMovementId(null);
builder.setType(move.getPokemonType());
move.setPokemonType(null);
moveString = mapper.writeValueAsString(move);
parser.merge(moveString, builder);
if (builder.getMoveId().name().contains("_FAST")) {
quickMoves.add(builder.getMoveId());
} else {
// Struggle has 0 energy delta for some reason wtf
if (builder.getEnergyDelta() == 0) {
builder.setEnergyDelta(-33);
}
cinematicMoves.add(builder.getMoveId());
}
} catch (final Exception e) {
throw new IllegalArgumentException("Could not parse " + move.getMovementId() + ":" + moveString,
e);
}
return builder.build();
}).collect(Collectors.toList());
allBuilder.addAllMove(moves);
allBuilder.addMove(DODGE_MOVE);
return allBuilder.build();
}
示例8: createParser
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
public static JsonFormat.Parser createParser() {
TypeRegistry typeRegistry = TypeRegistry.newBuilder()
.add(Entity.getDescriptor())
.build();
return JsonFormat.parser()
.usingTypeRegistry(typeRegistry);
}
示例9: testParseFromJSON
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
@Test
public void testParseFromJSON() throws InvalidProtocolBufferException
{
String json = "{\"meta\":{\"modelName\":\"some-name\"},\"custom\":{\"@type\":\"type.googleapis.com/io.seldon.api.rpc.example.CustomPredictReply\",\"data\":\"some custom data\"}}";
ClassificationReply.Builder builder = ClassificationReply.newBuilder();
CustomPredictReply.Builder customBuilder = CustomPredictReply.newBuilder();
TypeRegistry registry = TypeRegistry.newBuilder().add(customBuilder.getDescriptorForType()).build();
JsonFormat.Parser jFormatter = JsonFormat.parser().usingTypeRegistry(registry);
jFormatter.merge(json, builder);
ClassificationReply reply = builder.build();
System.out.println(reply);
}
示例10: testParseFromJSONDefault
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
@Test
public void testParseFromJSONDefault() throws InvalidProtocolBufferException
{
String json = "{\"data\":{\"@type\":\"type.googleapis.com/io.seldon.api.rpc.DefaultCustomPredictRequest\",\"values\":[1.2,2.1]}}";
ClassificationRequest.Builder builder = ClassificationRequest.newBuilder();
DefaultCustomPredictRequest.Builder customBuilder = DefaultCustomPredictRequest.newBuilder();
TypeRegistry registry = TypeRegistry.newBuilder().add(customBuilder.getDescriptorForType()).build();
JsonFormat.Parser jFormatter = JsonFormat.parser().usingTypeRegistry(registry);
jFormatter.merge(json, builder);
ClassificationRequest request = builder.build();
System.out.println(request);
}
示例11: fromJSON
import com.google.protobuf.util.JsonFormat; //导入方法依赖的package包/类
/**
* Extract data from a JSON String and use them to construct a Protocol Buffers Message.
* @param jsonReader A reader providing the JSON data to parse
* @param builder A Message builder to use to construct the resulting Message
* @return the constructed Message
* @throws InvalidProtocolBufferException Thrown in case of invalid Message data
*/
public static Message fromJSON(Reader jsonReader, Message.Builder builder) throws IOException {
JsonFormat.Parser parser = JsonFormat.parser();
parser.merge(jsonReader, builder);
return builder.build();
}