Cpp: JSON parser in Cpp that provide support Serialize/Deserialize feature, converting JSON objects to user-defined classes?

前端 未结 2 467
走了就别回头了
走了就别回头了 2021-01-27 02:32

I\'m working on native C++ development and looking for JSON parser that can handle complex JSON files and convert into class objects.

  1. I\'ve looked at native be

2条回答
  •  我在风中等你
    2021-01-27 03:11

    jsoncons, nlohmann and ThorsSerializer all support conversion between JSON and C++ objects. Examples with jsoncons and nlohmann are shown below, Martin York has one for his ThorsSerializer in a separate posting. The latter in my opinion is very nice, and certainly wins the prize for brevity. In the spirit of Oscar Wilde's quote that "imitation is the sincerest form of flattery", I've introduced the macro JSONCONS_ALL_MEMBER_TRAITS to jsoncons, and modified that example accordingly.

    Consider

    const std::string s = R"(
    [
        {
            "author" : "Haruki Murakami",
            "title" : "Kafka on the Shore",
            "price" : 25.17
        },
        {
            "author" : "Charles Bukowski",
            "title" : "Pulp",
            "price" : 22.48
        }
    ]
    )";
    
    namespace ns {
        struct book
        {
            std::string author;
            std::string title;
            double price;
        };
    } // namespace ns
    

    Using jsoncons to convert between s and an std::vector:

    #include 
    
    namespace jc = jsoncons;
    
    // Declare the traits. Specify which data members need to be serialized.
    JSONCONS_ALL_MEMBER_TRAITS(ns::book,author,title,price);
    
    int main()
    {
        std::vector book_list = jc::decode_json>(s);
    
        std::cout << "(1)\n";
        for (const auto& item : book_list)
        {
            std::cout << item.author << ", " 
                      << item.title << ", " 
                      << item.price << "\n";
        }
    
        std::cout << "\n(2)\n";
        jc::encode_json(book_list, std::cout, jc::indenting::indent);
        std::cout << "\n\n";
    }
    

    Output:

    (1)
    Haruki Murakami, Kafka on the Shore, 25.17
    Charles Bukowski, Pulp, 22.48
    
    (2)
    [
        {
            "author": "Haruki Murakami",
            "price": 25.17,
            "title": "Kafka on the Shore"
        },
        {
            "author": "Charles Bukowski",
            "price": 22.48,
            "title": "Pulp"
        }
    ]
    

    Using nlohmann to convert between s and an std::vector:

    #include 
    #include 
    
    namespace nh = nlohmann;
    
    // Provide from_json and to_json functions in the same namespace as your type   
    namespace ns {
        void from_json(const nh::json& j, ns::book& val) 
        {
            j.at("author").get_to(val.author);
            j.at("title").get_to(val.title);
            j.at("price").get_to(val.price);
        }
    
        void to_json(nh::json& j, const ns::book& val) 
        {
            j["author"] = val.author;
            j["title"] = val.title;
            j["price"] = val.price;
        }
    } // namespace ns
    
    int main()
    {
        nh::json j = nh::json::parse(s);
    
        std::vector book_list = j.get>();
        std::cout << "\n(1)\n";
        for (const auto& item : book_list)
        {
            std::cout << item.author << ", " 
                      << item.title << ", " 
                      << item.price << "\n";
        }
    
        std::cout << "\n(2)\n";
        nh::json j2 = book_list;
        std::cout << std::setw(4) << j2 << "\n\n";
    }
    

    Output:

    (1)
    Haruki Murakami, Kafka on the Shore, 25.17
    Charles Bukowski, Pulp, 22.48
    
    (2)
    [
        {
            "author": "Haruki Murakami",
            "price": 25.17,
            "title": "Kafka on the Shore"
        },
        {
            "author": "Charles Bukowski",
            "price": 22.48,
            "title": "Pulp"
        }
    ]
    

提交回复
热议问题