新手如何在CentOS7中C++使用jsoncpp

  • JSON

    JSON(JavaScript Object Notation)

    JSON is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.

    JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python…

    These properties make JSON an ideal data-interchange language.

    JSON is built on two structures :

    A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.

    An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

  • jsoncpp

    WikiBooks : JsonCpp 其中案例部分失效,可以参考安装过程和内容结构介绍

    Github : wiki

    • Install - 官方推荐 -vcpkg

      You can download and install JsonCpp using the vcpkg dependency manager:

      git clone https://github.com/Microsoft/vcpkg.git
      cd vcpkg
      ./bootstrap-vcpkg.sh
      ./vcpkg intergrate install
      ./vcpkg install jsoncpp
      

      (So, what is vcpkg? 《理解C++中的vcpkg是什么怎么用》)

      扫描二维码关注公众号,回复: 11581552 查看本文章

      The JsonCpp port in vcpkg is kept up to date by Microsoft team members and community contributors.

      Amalgamated source

      The Meson Build System 《理解C++中Meson Build System

    • Ubuntu - apt-get
      sudo apt-get install libjsoncpp-dev
      

      The header files will be installed to /usr/include/jsoncpp/json.

      In case you are curious, the biraries will be most probably installed to /usr/lib/x86_64-linux-gun , you can find through:

      ls /usr/lib/*/*jsoncpp*
      ls /usr/lib/*jsoncpp*
      
    • With amalgamated source

      To use JsonCpp with amalgamated source, you don’t need to download or make any binary files. You will have a single cpp and two .h files which you should include into your projects. These files will be system-independent.

      1. Download and unzip the source from the official repository
      2. python amlgamate.py

      It will create three files

      dist/jsoncpp.cpp, the source file to be added to your project
      dist/json/json.h, the correspondent header file
      dist/json/json-forwards.h, which contains forward declarations of JSON types
      

      You don’t need anything except these three files.

    • With cmake
      1. Download and unzip the source from github , and cd jsoncpp
        $ git clone https://github.com/open-source-parsers/jsoncpp.git
        $ cd jsoncpp 
        
      2. Install cmake
      3. Create the build directory and enter it
        mkdir -p build
        cd build
        
      4. Run the cmake command
        cmake -DCMAKE_BUILD_TYPE=release -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF -DARCHIVE_INSTALL_DIR=. -G "Unix Makefiles" ..
        
      5. Run the make command
        make
        // or use -j flag to parallelize the building
        make -j 4
        

        make过程出错

        • */jsoncpp/src/test_lib_json/jsontest.h:87:37: 错误:‘hexfloat’不是‘std’的成员

          根据《JsonCpp Linux环境构建动态库》,问题原因时当前编译器不支持hexfloat语言特性,这是C11最新版本的特性,需要升级编译器。

          From GCC 5 Release Series, GCC5 support for the C++11 hexfloat. 因此升级gcc4.8到gcc5.

          在CentOS中升级gcc4.8到gcc5

          升级之后,从头开始,重新下载jsoncpp,重新cmake制作makefile

      6. makeinstall
        make install
        

        至此,安装过程结束,json/*.h头文件拷贝在/usr/local/include/json

      7. Use

        Under Unix, it will create the file src/lib_json/libjsoncpp.a in your build directory. The include files will be in the ../include/json. Install the files (make install might help), and use:

        #include <jsoncpp/json/json.h> 
        // compile
        -ljsoncpp
        
  • Conventions and restrictions of JsonCpp

    Everything is in the Json namespace.

    • Names of classes and other types use upper CamelCase notation (capitalize first letter of each world). Examples: Int , ArrayIndex, ValueType
    • Names of member functions, fields, enum values use lower camelCase notation (capotalize first letter of each word except the first word). Examples: stringValue, isInt, size

    JsonCpp performs extensive validity checking. If an operation is invalid, it throws the std::runtime_error exception with relevant message.

    JsonCpp stores each number as

    • 64-bit integer (long long int or __int64) || int
    • 64-bit unsigned integer(unsigned long long int or unsigned __int64) || unit
    • double || real
  • Auxiliary types

    JsonCpp provides several auxilary types.

    The following types are defined in config.h:

    • Int – defined as int
    • UInt – defined as unsigned int
    • Int64 - a 64-bit signed integer, defined as __int64 for Microsoft Visual Studio, otherwise long long int
    • UInt64 – a 64-bit unsigned intefer, defined as unsigned __int64 for Microsoft Visual Studio, otherwise unsigned long long int.
    • LargestInt – a largest possible signed integer, defined as Int64
    • LargestUInt – a largest possible unsigned integer, defined as UInt64
    • ArrayIndex
    • ValueType
  • Tutorial

    JsonCpp Github Wiki

    There are totally hundreds of libraries for parsing and generating JSON on 62 languages, including 22 different libraries for C++.

    JsonCpp is probably the most popular C++ library. Another popular library is rapidjson, which is very fast.(关于rapidjson参见《在CentOS7||C++中使用rapidjson除了JSON文件||数据》)

    • Features

      What the jsoncpp can do ?

      1. read and write JSON document
      2. attach C++ style comments to element during parsing
      3. rewrite JSON document preserving original comments
    /* 案例一:读取外部json文件
    g++ file.cpp -o file -ljsoncpp
    ./file
    */
    #include <json/json.h> // "json/json.h" is also OK
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    int main() {
        Json::Value root;   // everything is in the Json namespace 
        ifstream file("./dirpath/test.json");  // read from test.json file
        file >> root; 
        cout << root;
    }
    /*案例二:外部输入
    g++ -o file file.cpp -ljsoncpp
    echo '{"a":[1,2]}' | ./file
    */
    #include <json/json.h> // "json/json.h" is also OK
    #include <iostream>
    using namespace std;
    int main() {
        Json::Value val;
        cin >> val;
        cout << val;
    }
    /*案例三:写入json文件*/
    #include<json/josn.h>
    #include<iostream>
    using namespace std;
    int main() {
        Json::Value wcontent;
        Json::Value wlist;
        wlist.append("1\n");
        wlist.append("12\n");
        wcontent["key"] = wlist;
        ofstream wf("write.json");
        wf << wcontent;
    }
    
  • References

  1. Full example for using JSONcpp on Unix

猜你喜欢

转载自blog.csdn.net/The_Time_Runner/article/details/107295679
今日推荐