RapidJSON将字符串值转换为索引数组?

时间:2016-06-06 07:30:57

标签: json c++11 rapidjson

我有一个JSON字符串,其中的值实际上是一个字符串,但我需要像JSON数组一样访问它。 {"data" : "[A,B,C]"}

有没有办法使用RapidJSON将VALUE解析为索引数组,以便我得到一个文档:{"0" : "A", "1" : "B", "2" : "C"}

1 个答案:

答案 0 :(得分:0)

  

RapidJSON应完全符合RFC7159 / ECMA-404。

没有办法,你需要手动完成。

例如使用正则表达式:

#include <iostream>
#include <iterator>
#include <string>
#include <regex>

static std::regex value_regex("[\\s]*([\\w\\s]+),?", std::regex::optimize);

std::string parse(const std::string& value) {
    if (value.at(0) != '[' || value.at(value.size() - 1) != ']') {
        throw std::invalid_argument("Error in Parse [must start with '[' and end with ']']: " + value);
    }

    std::string result("{");
    std::sregex_iterator next(++value.begin(), --value.end(), value_regex, std::regex_constants::match_continuous);
    std::sregex_iterator end;
    size_t len = 0, cont = 0;
    while (next != end) {
        if (cont) {
            result.append(", \"").append(std::to_string(cont)).append("\":\"").append(next->str(1)).append(1, '\"');
        } else {
            result.append(1, '\"').append(std::to_string(cont)).append("\":\"").append(next->str(1)).append(1, '\"');
        }
        len += next->length(0);
        ++next;
        ++cont;
    }

    if (len != value.size() - 2) {
        throw std::invalid_argument("Error in Parse [" + std::to_string(len) + "]: " + value);
    }

    result.append(1, '}');
    return result;
}

int main() {
    // Tests
    try {
        std::string value("[A,B,C,D,E]");
        std::cout << value << "  ->  " << parse(value) << std::endl;
    } catch (const std::invalid_argument& err) {
        std::cout << err.what() << std::endl;
    }
    try {
        std::string value("[  A, B X, Z]");
        std::cout << value << "  ->  " << parse(value) << std::endl;
    } catch (const std::invalid_argument& err) {
        std::cout << err.what() << std::endl;
    }
    try {
        std::string value("[A,BaaayyX, Z]");
        std::cout << value << "  ->  " << parse(value) << std::endl;
    } catch (const std::invalid_argument& err) {
        std::cout << err.what() << std::endl;
    }
    try {
        std::string value("[A,B,C,]");
        std::cout << value << "  ->  " << parse(value) << std::endl;
    } catch (const std::invalid_argument& err) {
        std::cout << err.what() << std::endl;
    }
    try {
        std::string value("[]");
        std::cout << value << "  ->  " << parse(value) << std::endl;
    } catch (const std::invalid_argument& err) {
        std::cout << err.what() << std::endl;
    }


    // Errors
    try {
        std::string value("A,B,C");
        std::cout << value << "  ->  " << parse(value) << std::endl;
    } catch (const std::invalid_argument& err) {
        std::cout << err.what() << std::endl;
    }

    try {
        std::string value("A,B,C]");
        std::cout << value << "  ->  " << parse(value) << std::endl;
    } catch (const std::invalid_argument& err) {
        std::cout << err.what() << std::endl;
    }

    try {
        std::string value("[A,B,C");
        std::cout << value << "  ->  " << parse(value) << std::endl;
    } catch (const std::invalid_argument& err) {
        std::cout << err.what() << std::endl;
    }

    try {
        std::string value("[,A,B,C]");
        std::cout << value << "  ->  " << parse(value) << std::endl;
    } catch (const std::invalid_argument& err) {
        std::cout << err.what() << std::endl;
    }

    try {
        std::string value("");
        std::cout << value << "  ->  " << parse(value) << std::endl;
    } catch (const std::exception& err) {
        std::cout << err.what() << std::endl;
    }

    try {
        std::string value("[");
        std::cout << value << "  ->  " << parse(value) << std::endl;
    } catch (const std::exception& err) {
        std::cout << err.what() << std::endl;
    }

    try {
        std::string value("]");
        std::cout << value << "  ->  " << parse(value) << std::endl;
    } catch (const std::exception& err) {
        std::cout << err.what() << std::endl;
    }
}

输出:

[A,B,C,D,E]  ->  {"0":"A", "1":"B", "2":"C", "3":"D", "4":"E"}
[  A, B X, Z]  ->  {"0":"A", "1":"B X", "2":"Z"}
[A,BaaayyX, Z]  ->  {"0":"A", "1":"BaaayyX", "2":"Z"}
[A,B,C,]  ->  {"0":"A", "1":"B", "2":"C"}
[]  ->  {}
A,B,C  ->  Error in Parse [must start with '[' and end with ']']: A,B,C
A,B,C]  ->  Error in Parse [must start with '[' and end with ']']: A,B,C]
[A,B,C  ->  Error in Parse [must start with '[' and end with ']']: [A,B,C
[,A,B,C]  ->  Error in Parse [0]: [,A,B,C]
  ->  basic_string
[  ->  Error in Parse [must start with '[' and end with ']']: [
]  ->  Error in Parse [must start with '[' and end with ']']: ]

您可以自定义正则表达式以变得更灵活或更严格。