XBMC研究之json使用

来源:互联网 发布:淘宝体天涯 编辑:程序博客网 时间:2024/05/26 05:53

http://blog.sina.com.cn/s/blog_6c14c17e0100lnv9.html

 

 

1. 概述
    json在XBMC中是配合webserver和libjsonrpc使用的,其json库是cppjson。它的作用是通过网络访问XBMC的Web GUI来远程管理XBMC,当你连接上Web GUI后你将看到My Music、 My Videos、 Music Playlist、 Video Playlist和playback controls等选项,类似下图:
XBMC研究之json使用
2. jsoncpp详解
    1) Json::Value 用于创建对象和给各种类型的对象赋值,如果 C 程序是用 Unicode 编码的,另需要用Adapt 类来适配。jsoncpp 支持的对象类型有nullValue = 0, intValue, uintValue, realValue, stringValue, booleanValue, arrayValue, objectValue。
    用法如下:
    Json::Value json_media;                                 // 创建一个对象,供如下代码使用
    json_media["path"] = Json::Value("/home/mp3/test.mp3"); // stringValue
    json_media["size"] = Json::Value(4000);                 // intValue
    json_media["isBool"] = Json::Value(true);               // booleanValue
    json_media["isDouble"] = Json::Value(0.25);             // realValue
    json_media["isObject"] = Json_temp;                     // objectValue
    json_media["isArray"].append("test1");                  // arrayValue
    json_media["isArray"].append("test2");                  // arrayValue
    Json::ValueType type = json_media.type();               // 获得json_media的类型
    2) Json::FastWriter用来快速输出Json对象的值。
    用法:
    Json::FastWriter writer;
    std::cout << writer.write(json_media)<< std::endl;
    结果:
    {"isArray":["test1","test2"],"isBoolean":true,"isDouble":0.25,"size":4000,"isObject":            {},"path":"/home/mp3/test.mp3"}
    3) Json::StyledWriter用来格式化输出Json对象的值。
    用法:
    Json::StyledWriter writer;
    std::cout << writer.write(json_media) << std::endl;
    结果:
    {
       "isArray" : [ "test1", "test2" ],
       "isBoolean" : true,
       "isDouble" : 0.24,
       "size" : 4000,
       "isObject" : {},
       "path" : "/home/mp3/test.mp3"
    }
    4) Json::Reader 是用于读取Json对象的值。
    用法:
    Json::Reader reader;
    Json::Value reader_object;
    const char* reader_document = "{"path" : "/home/test.mp3","size" : 4000}";
    if (!reader.parse(reader_document, reader_object))
        return 0;
    std::cout << reader_object["path"] << std::endl;
    std::cout << reader_object["size"] << std::endl;
    结果:
    "/home/test.mp3"
    4000
    3. XBMC中的libjsonrpc就是用以上函数对Web respone/request action进行处理的。
    当我们在GUI上呼叫如下函数:
    function CallPlay(player, playlistItem)
    {
        var http_request = new XMLHttpRequest();
        http_request.open( "POST", "jsonrpc", false );
        http_request.send('{"jsonrpc": "2.0", "method": "AudioPlaylist.Play", "params": ' test.mp3', "id": 1}');
    }
    这时会调用AnswerToConnection函数来应答,它定义在utils/WebServer.cpp中:
    int CWebServer::AnswerToConnection(void *cls, struct MHD_Connection *connection,
                      const char *url, const char *method,
                      const char *version, const char *upload_data,
                      unsigned int *upload_data_size, void **con_cls)
    {
        CWebServer *server = (CWebServer *)cls;
        if (!IsAuthenticated(server, connection))
            return AskForAuthentication(connection);
        CStdString strURL = url;
        bool get  = strcmp(method, "GET")  == 0;
        bool post = strcmp(method, "POST") == 0;
    #ifdef HAS_JSONRPC
        if (strURL.Equals("/jsonrpc"))
        {
            if (post)
                return JSONRPC(server, con_cls, connection, upload_data, upload_data_size);
            else if (get)
                return CreateMemoryDownloadResponse(connection, (void *)PAGE_JSONRPC_INFO, strlen(PAGE_JSONRPC_INFO));
        }
    #endif
        ...
    }
    从而通过JSONRPC函数来执行libjsonrpc中定义的AudioPlaylist.Play方法。
    接下来会呼叫
PlayListPlayerPlay函数
    void CApplicationMessenger::PlayListPlayerPlay()
    {
        ThreadMessage tMsg = {TMSG_PLAYLISTPLAYER_PLAY, (DWORD) -1};
        SendMessage(tMsg, true);
    }
    通过
SendMessage函数发消息给Window,从而达到控制XBMC的目的。有些函数不像上面那样直接CallSendMessage,而是通过SendAction函数先指定window id和action,然后再CallSendMessage来达到在指定window上做特定Action。 window和action的类型定义在guilib/key.h中。