本文介绍了Java中用于解析文本格式的协议缓冲区"ParseFromString"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ParseFromString在Java中是否可用于协议缓冲区?

Is ParseFromString available in Java for protocol buffers?

C ++版本具有它:此处

The C++ version has it: here

推荐答案

的实现(请注意,TextFormat::ParseFromString只是在新的Parser对象上调用TextFormat::Parser::ParseFromString):

Here is the implementation of ParseFromString (note that TextFormat::ParseFromString simply calls TextFormat::Parser::ParseFromString on a new Parser object):

bool TextFormat::Parser::Parse(io::ZeroCopyInputStream* input,
                               Message* output) {
  output->Clear();
  return Merge(input, output);
}

bool TextFormat::Parser::ParseFromString(const string& input,
                                         Message* output) {
  io::ArrayInputStream input_stream(input.data(), input.size());
  return Parse(&input_stream, output);
}

您会看到Parse只是清除了消息,然后调用了Merge.尽管协议缓冲区的Java实现没有Parse方法,但是您可以轻松实现它:

You can see that Parse simply clears the message, then calls Merge. Although the Java implementation of Protocol Buffers doesn't have a Parse method, you can easily implement it:

public static void parseFromString(CharSequence input, ExtensionRegistry extensionRegistry, Message.Builder builder) throws ParseException {
    builder.clear();
    TextFormat.merge(input, extensionRegistry, builder);
}

这篇关于Java中用于解析文本格式的协议缓冲区"ParseFromString"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!