使用boost :: asio读取JSON流,获取完整的字符串?

时间:2017-05-09 09:45:13

标签: c++ json boost

我正在使用boost读取TCP流。流采用以下格式:

"{\"animationValues\":{\"mouth_rightMouth_stretch\":0.0000000000000000,\"mouth_leftMouth_narrow\":0.00000000000000000,\"mouth_up\":0.0000000000000000,\"mouth_leftMouth_stretch\":0.0000000000000000,\"mouth_rightMouth_narrow\":0.00000000000000000,\"mouth_down\":0.00000000000000000,\"mouth_upperLip_left_up\":0.0000000000000000,\"mouth_upperLip_right_up\":0.0000000000000000,\"mouth_lowerLip_left_down\":0.0000000000000000,\"mouth_lowerLip_right_down\":0.0000000000000000,\"mouth_leftMouth_frown\":0.0000000000000000,\"mouth_rightMouth_frown\":0.0000000000000000,\"mouth_leftMouth_smile\":0.00000000000000000,\"mouth_rightMouth_smile\":0.00000000000000000,\"eyes_lookRight\":0.0000000000000000,\"eyes_lookLeft\":0.00000000000000000,\"eyes_lookDown\":0.0000000000000000,\"eyes_lookUp\":0.00000000000000000,\"eyes_leftEye_blink\":0.00000000000000000,\"eyes_rightEye_blink\":0.00000000000000000,\"eyes_leftEye_wide\":0.0000000000000000,\"eyes_rightEye_wide\":0.0000000000000000,\"brows_leftBrow_up\":0.0000000000000000,\"brows_leftBrow_down\":0.00000000000000000,\"brows_rightBrow_up\":0.0000000000000000,\"brows_rightBrow_down\":0.00000000000000000,\"brows_midBrows_up\":0.0000000000000000,\"brows_midBrows_down\":0.00000000000000000,\"jaw_open\":0.0000000000000000,\"jaw_left\":0.0000000000000000,\"jaw_right\":0.00000000000000000,\"mouth_phoneme_oo\":0.0000000000000000,\"mouth_right\":0.0000000000000000,\"mouth_left\":0.00000000000000000,\"mouth_phoneme_mbp\":0.0000000000000000,\"mouth_phoneme_ch\":0.0000000000000000},\"headRot\":[0.0,0.0, 0.0]}";

我正在尝试阅读这个并解析每个字符串。所以我需要做的是将流分成如上所示的部分。我试过了:

  boost::system::error_code error;
    boost::asio::streambuf buffer;
    boost::asio::read_until(sock, buffer, "]}"", error);
    std::istream str(&buffer);

但这只能给我一半我需要的字符串。我试过了:

boost::array<char, 2046> buf; 
size_t len = sock.read_some(boost::asio::buffer(buf), error); 
std::string data(buf.begin(), buf.end());

但这让我更少。如何一次阅读流的一部分?谢谢。

1 个答案:

答案 0 :(得分:0)

是的,我使用read_until或任何boost::asio::read_*系列,因为它们组成了读取操作(与read_some一致)。

这是这项工作的POC:

<强> Live On Coliru

#include <boost/asio.hpp>
#include <iostream>

int main()
{
    using namespace boost::asio;
    using ip::tcp;

    io_service io;
    tcp::acceptor a(io, { {}, 6768 });
    a.listen(5);

    tcp::socket s(io);
    a.accept(s);

    streambuf sb;
    boost::system::error_code ec;
    read_until(s, sb, "]}", ec);

    std::cout << "Response: " << ec.message() << "\n";
    std::cout << "'" << &sb << "'\n";
}

如您所见,发送时

{"animationValues": {"mouth_rightMouth_stretch": 0,"mouth_leftMouth_narrow": 0,"mouth_up": 0,"mouth_leftMouth_stretch": 0,"mouth_rightMouth_narrow": 0,"mouth_down": 0,"mouth_upperLip_left_up": 0,"mouth_upperLip_right_up": 0,"mouth_lowerLip_left_down": 0,"mouth_lowerLip_right_down": 0,"mouth_leftMouth_frown": 0,"mouth_rightMouth_frown": 0,"mouth_leftMouth_smile": 0,"mouth_rightMouth_smile": 0,"eyes_lookRight": 0,"eyes_lookLeft": 0,"eyes_lookDown": 0,"eyes_lookUp": 0,"eyes_leftEye_blink": 0,"eyes_rightEye_blink": 0,"eyes_leftEye_wide": 0,"eyes_rightEye_wide": 0,"brows_leftBrow_up": 0,"brows_leftBrow_down": 0,"brows_rightBrow_up": 0,"brows_rightBrow_down": 0,"brows_midBrows_up": 0,"brows_midBrows_down": 0,"jaw_open": 0,"jaw_left": 0,"jaw_right": 0,"mouth_phoneme_oo": 0,"mouth_right": 0,"mouth_left": 0,"mouth_phoneme_mbp": 0,"mouth_phoneme_ch": 0},"headRot": [0, 0, 0]}
{"animationValues": {"mouth_rightMouth_stretch": 1,"mouth_leftMouth_narrow": 1,"mouth_up": 1,"mouth_leftMouth_stretch": 1,"mouth_rightMouth_narrow": 1,"mouth_down": 1,"mouth_upperLip_left_up": 1,"mouth_upperLip_right_up": 1,"mouth_lowerLip_left_down": 1,"mouth_lowerLip_right_down": 1,"mouth_leftMouth_frown": 1,"mouth_rightMouth_frown": 1,"mouth_leftMouth_smile": 1,"mouth_rightMouth_smile": 1,"eyes_lookRight": 1,"eyes_lookLeft": 1,"eyes_lookDown": 1,"eyes_lookUp": 1,"eyes_leftEye_blink": 1,"eyes_rightEye_blink": 1,"eyes_leftEye_wide": 1,"eyes_rightEye_wide": 1,"brows_leftBrow_up": 1,"brows_leftBrow_down": 1,"brows_rightBrow_up": 1,"brows_rightBrow_down": 1,"brows_midBrows_up": 1,"brows_midBrows_down": 1,"jaw_open": 1,"jaw_left": 1,"jaw_right": 1,"mouth_phoneme_oo": 1,"mouth_right": 1,"mouth_left": 1,"mouth_phoneme_mbp": 1,"mouth_phoneme_ch": 1},"headRot": [1, 1, 1]}
{"animationValues": {"mouth_rightMouth_stretch": 2,"mouth_leftMouth_narrow": 2,"mouth_up": 2,"mouth_leftMouth_stretch": 2,"mouth_rightMouth_narrow": 2,"mouth_down": 2,"mouth_upperLip_left_up": 2,"mouth_upperLip_right_up": 2,"mouth_lowerLip_left_down": 2,"mouth_lowerLip_right_down": 2,"mouth_leftMouth_frown": 2,"mouth_rightMouth_frown": 2,"mouth_leftMouth_smile": 2,"mouth_rightMouth_smile": 2,"eyes_lookRight": 2,"eyes_lookLeft": 2,"eyes_lookDown": 2,"eyes_lookUp": 2,"eyes_leftEye_blink": 2,"eyes_rightEye_blink": 2,"eyes_leftEye_wide": 2,"eyes_rightEye_wide": 2,"brows_leftBrow_up": 2,"brows_leftBrow_down": 2,"brows_rightBrow_up": 2,"brows_rightBrow_down": 2,"brows_midBrows_up": 2,"brows_midBrows_down": 2,"jaw_open": 2,"jaw_left": 2,"jaw_right": 2,"mouth_phoneme_oo": 2,"mouth_right": 2,"mouth_left": 2,"mouth_phoneme_mbp": 2,"mouth_phoneme_ch": 2},"headRot": [2, 2, 2]}

打印:

Response: Success
'{"animationValues": {"mouth_rightMouth_stretch": 0,"mouth_leftMouth_narrow": 0,"mouth_up": 0,"mouth_leftMouth_stretch": 0,"mouth_rightMouth_narrow": 0,"mouth_down": 0,"mouth_upperLip_left_up": 0,"mouth_upperLip_right_up": 0,"mouth_lowerLip_left_down": 0,"mouth_lowerLip_right_down": 0,"mouth_leftMouth_frown": 0,"mouth_rightMouth_frown": 0,"mouth_leftMouth_smile": 0,"mouth_rightMouth_smile": 0,"eyes_lookRight": 0,"eyes_lookLeft": 0,"eyes_lookDown": 0,"eyes_lookUp": 0,"eyes_leftEye_blink": 0,"eyes_rightEye_blink": 0,"eyes_leftEye_wide": 0,"eyes_rightEye_wide": 0,"brows_leftBrow_up": 0,"brows_leftBrow_down": 0,"brows_rightBrow_up": 0,"brows_rightBrow_down": 0,"brows_midBrows_up": 0,"brows_midBrows_down": 0,"jaw_open": 0,"jaw_left": 0,"jaw_right": 0,"mouth_phoneme_oo": 0,"mouth_right": 0,"mouth_left": 0,"mouth_phoneme_mbp": 0,"mouth_phoneme_ch": 0},"headRot": [0, 0, 0]}
{"animationValues": {"mouth_rightMouth_stretch": 1,"mouth_leftMouth_narrow": 1,"mouth_up": 1,"mouth_leftMouth_stretch": 1,"mouth_rightMouth_narrow'

需要注意的事项:

  1. 确保格式化实际上是]}发生的。这意味着无法识别漂亮打印的json:

            "mouth_phoneme_mbp": 2,
            "mouth_phoneme_ch": 2
        },
        "headRot": [2, 2, 2]
    }
    
  2. 还要确保您知道read_until可能会返回包含更多数据的缓冲区(它会在分隔符满足后立即返回,但更多数据可能已被阅读)