Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. example.json

    Code Block
    languagejs
    // Configuration options
    {
        // Default encoding for text
        "encoding" : "UTF-8",
        // Plug-ins loaded at start-up
        "plug-ins" : [
            "python",
            "c++",
            "ruby"
            ],
        // Tab indent size
        "indent" : { "length" : 3, "use_space": true }
    }     
  2. example.cpp

    Code Block
    languagecpp
    titleexample.cpp
    linenumberstrue
    collapsetrue
    #include <stdlib.h>
    #include <fstream>
    // This is the JSON header
    #include "json/json.h"
    using namespace std;
    int main(int argc, char **argv)
    {
        ifstream ifs("example.json");
        Json::Value root;   // will contains the root value after parsing.
        Json::Reader reader;
        bool parsingSuccessful = reader.parse(ifs, root );
        if ( !parsingSuccessful )
        {
            // report to the user the failure and their locations in the document.
            std::cout  << "Failed to parse configuration\n"
                       << reader.getFormattedErrorMessages();
            return 0;
        }
    // Get the value of the member of root named 'encoding', return 'UTF-8' if there is no
    // such member.
        std::string encoding = root.get("encoding", "UTF-8" ).asString();
    // Get the value of the member of root named 'encoding', return a 'null' value if
    // there is no such member.
        const Json::Value plugins = root["plug-ins"];
        cout << "plug-ins : ";
        for ( int index = 0; index < plugins.size(); ++index )  // Iterates over the sequence elements.
            cout << plugins[index].asString() << ","; 
        cout << endl;
        //setIndentLength( root["indent"].get("length", 3).asInt() );
        //setIndentUseSpace( root["indent"].get("use_space", true).asBool() );
    // ...
    // At application shutdown to make the new configuration document:
    // Since Json::Value has implicit constructor for all value types, it is not
    // necessary to explicitly construct the Json::Value object:
        root["encoding"] = "UTF-16";
        root["indent"]["length"] = 5;
        root["indent"]["use_space"] = 0;
        Json::StyledWriter writer;
    // Make a new JSON document for the configuration. Preserve original comments.
        std::string outputConfig = writer.write( root );
    // You can also use streams.  This will put the contents of any JSON
    // stream at a particular sub-value, if you'd like.
        std::cin >> root["subtree"];
    // And you can write to a stream, using the StyledWriter automatically.
        std::cout << root;
        return 0;
    }
  3. compile
    1. g++ -o example example.cpp jsoncpp.cpp -I. -DJSON_IS_AMALGAMATION
  4. 실행
    1. ./example
  5. jsonCPP 는 std::cin stream 에서 json 데이타를 입력받을수 있고 위 예제는 subtree 라는 value 를 요구하므로 아래와 같은 json data 를 입력하고 Ctrl-D 를 누르면 된다.

    Code Block
    ./example 
    plug-ins : python,c++,ruby,
    {     "name": "value",
        "arrary": {
            "age": 99,
            "ar": [
                1,
                2,
                3,
                4
            ]
        }
    }
    // Configuration options
    
    

     

    결과

    Code Block
    collapsetrue
    {// Default encoding for text
    
            "encoding" : "UTF-16",// Tab indent size
    
            "indent" : 
            {
                    "length" : 5,
                    "use_space" : 0
            },// Plug-ins loaded at start-up
    
            "plug-ins" : [ "python", "c++", "ruby" ],
            "subtree" : 
            {
                    "arrary" : 
                    {
                            "age" : 99,
                            "ar" : [ 1, 2, 3, 4 ]
                    },
                    "name" : "value"
            }
    }

...