c++ - 使用 Boost JSON 解析器的 JSON 格式化错误

标签 c++ json parsing boost

我正在尝试使用 Boost 从我的 Firefox 配置文件夹中读取一个名为 sessionstore.js 的 JSON 文件,其中保存了当前/上一个 Firefox session 的信息以供恢复之用。我写了一个基于 XML-based tutorial 的程序从 Boost 网站,只需将 XML 部分换成 JSON 部分,如下所示

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
#include <string>
#include <set>
#include <exception>

using boost::property_tree::ptree;
using namespace std;

const string FILENAME = "sessionstore.js";
const string WINDOW_TAG = "windows";

struct session_settings    
{
    void load (const string &FILENAME);
};

void session_settings::load (const string &FILENAME)
{       
    ptree pt;
    read_json (FILENAME, pt);
}

int main()
{
    try
    {
        session_settings Settings;
        Settings.load(FILENAME);
    }
    catch (exception &e)
    {
        cout << "Error: " << e.what() << endl;
    }
    return 0;
}

我试图读取的 JSON 文件的内容是

{"windows":[{"tabs":[{"entries":[{"url":"about:home","title":"Mozilla Firefox Start Page","ID":5,"docshellID":11,"owner_b64":"NhAra3tiRRqhyKDUVsktxQAAAAAAAAAAwAAAAAAAAEYAAQAAAAAAAS8nfAAOr03buTZBMmukiq4HoizADOUR05MxABBLoP1AAAAAAAVhYm91dAAAAARob21l4NodcC97EdOM0ABgsPwUoweiLMAM5RHTkzEAEEug/UAAAAAADm1vei1zYWZlLWFib3V0AAAABGhvbWUAAAA=","docIdentifier":5},{"url":"http://www.google.co.uk/","title":"Google","ID":6,"docshellID":11,"docIdentifier":6,"children":[{"url":"about:blank","ID":7,"docshellID":12,"owner_b64":"NhAra3tiRRqhyKDUVsktxQAAAAAAAAAAwAAAAAAAAEYAAQAAAAAAAd6UctCANBHTk5kAEEug/UAHoizADOUR05MxABBLoP1AAAAAAv////8AAABQAQAAABhodHRwOi8vd3d3Lmdvb2dsZS5jby51ay8AAAAAAAAABAAAAAcAAAAQAAAAB/////8AAAAH/////wAAAAcAAAAQAAAAFwAAAAEAAAAXAAAAAQAAABcAAAABAAAAGAAAAAAAAAAY/////wAAABf/////AAAAF/////8AAAAX/////wEAAAAAAAAAAAABAAA=","docIdentifier":7,"scroll":"0,0"}],"formdata":{"#csi":"1","#hcache":"{\"BInSTfL-EtSt8QOl24nrCg\":[[69,{}],[14,{}],[60,{}],[81,{\"persisted\":true}],[42,{}],[43,{}],[83,{}],[95,{\"kfe\":{\"kfeHost\":\"clients1.google.co.uk\",\"kfeUrlPrefix\":\"/webpagethumbnail?r=2&f=2&s=300:585&query=&hl=en&gl=uk\",\"maxPrefetchConnections\":2,\"prefetch\":90,\"slowConnection\":false},\"logging\":{\"csiFraction\":0.05,\"gen204Fraction\":0.05},\"msgs\":{\"loading\":\"Still loading...\",\"mute\":\"Mute\",\"noPreview\":\"Preview not available\",\"sound\":\"Sound:\",\"soundOff\":\"off\",\"soundOn\":\"on\",\"unmute\":\"Unmute\"},\"pb\":{\"desiredHeight\":585,\"desiredWidth\":300,\"minHeight\":200,\"minWidth\":300},\"time\":{\"hoverClose\":300,\"hoverModeTimeout\":60,\"hoverOpen\":125,\"loading\":100,\"longHoverOpen\":725,\"prefetchOnLoad\":3000,\"timeout\":2500}}],[78,{}],[25,{\"m\":{\"bks\":true,\"blg\":true,\"dsc\":true,\"evn\":true,\"frm\":true,\"isch\":true,\"klg\":true,\"mbl\":true,\"nws\":true,\"plcs\":true,\"ppl\":true,\"prc\":true,\"pts\":true,\"rcp\":true,\"shop\":true,\"vid\":true},\"t\":null}],[64,{}],[105,{}],[22,{\"m_errors\":{\"32\":\"Sorry, no more results to show.\",\"default\":\"<font color=red>Error:</font> The server could not complete your request.  Try again in 30 seconds.\"},\"m_tip\":\"Click for more information\"}],[77,{}],[84,{}],[99,{}],[29,{\"mcr\":5}],[92,{\"avgTtfc\":2000,\"fd\":1000,\"fl\":true,\"focus\":true,\"hpt\":250,\"kn\":true,\"mds\":\"clir,clue,dfn,evn,frim,klg,prc,rl,show,sp,sts,ww,mbl_he,mbl_hs,mbl_re,mbl_rs,mbl_sv,isch\",\"msg\":{\"dym\":\"Did you mean:\",\"gs\":\"Google Search\",\"kntt\":\"Use the up and down arrow keys to select each result. Press Enter to go to the selection.\",\"sif\":\"Search instead for\",\"srf\":\"Showing results for\"},\"odef\":true,\"ophe\":true,\"pq\":true,\"rpt\":41,\"tct\":\" ?\",\"tdur\":50}],[24,{}],[38,{}]]}"},"scroll":"0,0"}],"index":2,"hidden":false,"attributes":{"image":"http://www.google.co.uk/favicon.ico"},"storage":{"http://www.google.co.uk":{"web-v":"12_c9c918f0"}}}],"selected":1,"_closedTabs":[],"width":994,"height":688,"screenX":1650,"screenY":24,"sizemode":"normal","title":"Google"}],"selectedWindow":0,"_closedWindows":[{"tabs":[{"entries":[{"url":"about:home","title":"Mozilla Firefox Start Page","ID":0,"docshellID":5,"owner_b64":"NhAra3tiRRqhyKDUVsktxQAAAAAAAAAAwAAAAAAAAEYAAQAAAAAAAS8nfAAOr03buTZBMmukiq4HoizADOUR05MxABBLoP1AAAAAAAVhYm91dAAAAARob21l4NodcC97EdOM0ABgsPwUoweiLMAM5RHTkzEAEEug/UAAAAAADm1vei1zYWZlLWFib3V0AAAABGhvbWUAAAA="},{"url":"http://www.facebook.com/","title":"Welcome to Facebook - Log In, Sign Up or Learn More","ID":1,"docshellID":5,"docIdentifier":1,"formdata":{"//xhtml:div[@id='reg_form_box']/xhtml:table/xhtml:tbody/xhtml:tr[6]/xhtml:td[2]/xhtml:div/xhtml:div/xhtml:select":0,"//xhtml:div[@id='reg_form_box']/xhtml:table/xhtml:tbody/xhtml:tr[6]/xhtml:td[2]/xhtml:div/xhtml:div/xhtml:select[2]":0,"#sex":0,"#birthday_month":0,"#birthday_day":0,"#birthday_year":0},"scroll":"0,0"}],"index":2,"hidden":false,"attributes":{"image":"http://www.facebook.com/favicon.ico"}},{"entries":[{"url":"http://twitter.com/","title":"Twitter","ID":3,"docshellID":6,"docIdentifier":3,"children":[{"url":"http://api.twitter.com/receiver.html","ID":4,"docshellID":7,"referrer":"http://twitter.com/","docIdentifier":4,"scroll":"0,0"}],"formdata":{},"scroll":"0,0"}],"index":1,"hidden":false,"attributes":{"image":"http://twitter.com/phoenix/favicon.ico"}}],"selected":2,"_closedTabs":[],"width":994,"height":688,"screenX":1366,"screenY":307,"sizemode":"normal","cookies":[{"host":".facebook.com","value":"J4-69","path":"/","name":"lsd"},{"host":".facebook.com","value":"http%3A%2F%2Fwww.facebook.com%2F","path":"/","name":"reg_fb_gate"},{"host":".facebook.com","value":"http%3A%2F%2Fwww.facebook.com%2F","path":"/","name":"reg_fb_ref"},{"host":".facebook.com","value":"994x624","path":"/","name":"wd"},{"host":".twitter.com","value":"43838368","path":"/","name":"__utmc"},{"host":"twitter.com","value":"4bfz%2B%2BmebEkRkMWFCXm%2FCUOsvDoVeFTl","path":"/","name":"original_referer"},{"host":"scribe.twitter.com","value":"4bfz%2B%2BmebEkRkMWFCXm%2FCUOsvDoVeFTl","path":"/","name":"original_referer"},{"host":".twitter.com","value":"BAh7CToPY3JlYXRlZF9hdGwrCDoVZ%252F4vAToMY3NyZl9pZCIlODE2MGI1ZjJh%250AYmViNDMwODMxNDlkN2U5ZDg5Yjk4ZmU6B2lkIiU2N2I4YjdmNGExNWFkNzlk%250AODI0MDVjMGM1NmMzYjVhYSIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6%250ARmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA%253D%253D--8b0d751e9774c5cfaa61fdec567cb782aa8757dd","path":"/","name":"_twitter_sess","httponly":true},{"host":".twitter.com","value":"43838368","path":"/","name":"__utmc"},{"host":"twitter.com","value":"4bfz%2B%2BmebEkRkMWFCXm%2FCUOsvDoVeFTl","path":"/","name":"original_referer"},{"host":"scribe.twitter.com","value":"4bfz%2B%2BmebEkRkMWFCXm%2FCUOsvDoVeFTl","path":"/","name":"original_referer"},{"host":".twitter.com","value":"BAh7CToPY3JlYXRlZF9hdGwrCDoVZ%252F4vAToMY3NyZl9pZCIlODE2MGI1ZjJh%250AYmViNDMwODMxNDlkN2U5ZDg5Yjk4ZmU6B2lkIiU2N2I4YjdmNGExNWFkNzlk%250AODI0MDVjMGM1NmMzYjVhYSIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6%250ARmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA%253D%253D--8b0d751e9774c5cfaa61fdec567cb782aa8757dd","path":"/","name":"_twitter_sess","httponly":true}],"title":"Twitter"}],"session":{"state":"stopped","lastUpdate":1305658398727}}

当我试图用我的程序加载它时,我得到了错误

Error: sessionstore.js(1): expected value

由于文件的格式全部在一行中,这意味着错误可能出现在文件中的任何位置,因此我通过 Javascript beautifier 运行它,保留默认选项,并将结果粘贴回原始文件并执行程序。

格式化的JSON是

{
    "windows": [{
        "tabs": [{
            "entries": [{
                "url": "about:home",
                "title": "Mozilla Firefox Start Page",
                "ID": 5,
                "docshellID": 11,
                "owner_b64": "NhAra3tiRRqhyKDUVsktxQAAAAAAAAAAwAAAAAAAAEYAAQAAAAAAAS8nfAAOr03buTZBMmukiq4HoizADOUR05MxABBLoP1AAAAAAAVhYm91dAAAAARob21l4NodcC97EdOM0ABgsPwUoweiLMAM5RHTkzEAEEug/UAAAAAADm1vei1zYWZlLWFib3V0AAAABGhvbWUAAAA=",
                "docIdentifier": 5
            }, {
                "url": "http://www.google.co.uk/",
                "title": "Google",
                "ID": 6,
                "docshellID": 11,
                "docIdentifier": 6,
                "children": [{
                    "url": "about:blank",
                    "ID": 7,
                    "docshellID": 12,
                    "owner_b64": "NhAra3tiRRqhyKDUVsktxQAAAAAAAAAAwAAAAAAAAEYAAQAAAAAAAd6UctCANBHTk5kAEEug/UAHoizADOUR05MxABBLoP1AAAAAAv////8AAABQAQAAABhodHRwOi8vd3d3Lmdvb2dsZS5jby51ay8AAAAAAAAABAAAAAcAAAAQAAAAB/////8AAAAH/////wAAAAcAAAAQAAAAFwAAAAEAAAAXAAAAAQAAABcAAAABAAAAGAAAAAAAAAAY/////wAAABf/////AAAAF/////8AAAAX/////wEAAAAAAAAAAAABAAA=",
                    "docIdentifier": 7,
                    "scroll": "0,0"
                }],
                "formdata": {
                    "#csi": "1",
                    "#hcache": "{\"BInSTfL-EtSt8QOl24nrCg\":[[69,{}],[14,{}],[60,{}],[81,{\"persisted\":true}],[42,{}],[43,{}],[83,{}],[95,{\"kfe\":{\"kfeHost\":\"clients1.google.co.uk\",\"kfeUrlPrefix\":\"/webpagethumbnail?r=2&f=2&s=300:585&query=&hl=en&gl=uk\",\"maxPrefetchConnections\":2,\"prefetch\":90,\"slowConnection\":false},\"logging\":{\"csiFraction\":0.05,\"gen204Fraction\":0.05},\"msgs\":{\"loading\":\"Still loading...\",\"mute\":\"Mute\",\"noPreview\":\"Preview not available\",\"sound\":\"Sound:\",\"soundOff\":\"off\",\"soundOn\":\"on\",\"unmute\":\"Unmute\"},\"pb\":{\"desiredHeight\":585,\"desiredWidth\":300,\"minHeight\":200,\"minWidth\":300},\"time\":{\"hoverClose\":300,\"hoverModeTimeout\":60,\"hoverOpen\":125,\"loading\":100,\"longHoverOpen\":725,\"prefetchOnLoad\":3000,\"timeout\":2500}}],[78,{}],[25,{\"m\":{\"bks\":true,\"blg\":true,\"dsc\":true,\"evn\":true,\"frm\":true,\"isch\":true,\"klg\":true,\"mbl\":true,\"nws\":true,\"plcs\":true,\"ppl\":true,\"prc\":true,\"pts\":true,\"rcp\":true,\"shop\":true,\"vid\":true},\"t\":null}],[64,{}],[105,{}],[22,{\"m_errors\":{\"32\":\"Sorry, no more results to show.\",\"default\":\"<font color=red>Error:</font> The server could not complete your request.  Try again in 30 seconds.\"},\"m_tip\":\"Click for more information\"}],[77,{}],[84,{}],[99,{}],[29,{\"mcr\":5}],[92,{\"avgTtfc\":2000,\"fd\":1000,\"fl\":true,\"focus\":true,\"hpt\":250,\"kn\":true,\"mds\":\"clir,clue,dfn,evn,frim,klg,prc,rl,show,sp,sts,ww,mbl_he,mbl_hs,mbl_re,mbl_rs,mbl_sv,isch\",\"msg\":{\"dym\":\"Did you mean:\",\"gs\":\"Google Search\",\"kntt\":\"Use the up and down arrow keys to select each result. Press Enter to go to the selection.\",\"sif\":\"Search instead for\",\"srf\":\"Showing results for\"},\"odef\":true,\"ophe\":true,\"pq\":true,\"rpt\":41,\"tct\":\" ?\",\"tdur\":50}],[24,{}],[38,{}]]}"
                },
                "scroll": "0,0"
            }],
            "index": 2,
            "hidden": false,
            "attributes": {
                "image": "http://www.google.co.uk/favicon.ico"
            },
            "storage": {
                "http://www.google.co.uk": {
                    "web-v": "12_c9c918f0"
                }
            }
        }],
        "selected": 1,
        "_closedTabs": [],
        "width": 994,
        "height": 688,
        "screenX": 1650,
        "screenY": 24,
        "sizemode": "normal",
        "title": "Google"
    }],
    "selectedWindow": 0,
    "_closedWindows": [{
        "tabs": [{
            "entries": [{
                "url": "about:home",
                "title": "Mozilla Firefox Start Page",
                "ID": 0,
                "docshellID": 5,
                "owner_b64": "NhAra3tiRRqhyKDUVsktxQAAAAAAAAAAwAAAAAAAAEYAAQAAAAAAAS8nfAAOr03buTZBMmukiq4HoizADOUR05MxABBLoP1AAAAAAAVhYm91dAAAAARob21l4NodcC97EdOM0ABgsPwUoweiLMAM5RHTkzEAEEug/UAAAAAADm1vei1zYWZlLWFib3V0AAAABGhvbWUAAAA="
            }, {
                "url": "http://www.facebook.com/",
                "title": "Welcome to Facebook - Log In, Sign Up or Learn More",
                "ID": 1,
                "docshellID": 5,
                "docIdentifier": 1,
                "formdata": {
                    "//xhtml:div[@id='reg_form_box']/xhtml:table/xhtml:tbody/xhtml:tr[6]/xhtml:td[2]/xhtml:div/xhtml:div/xhtml:select": 0,
                    "//xhtml:div[@id='reg_form_box']/xhtml:table/xhtml:tbody/xhtml:tr[6]/xhtml:td[2]/xhtml:div/xhtml:div/xhtml:select[2]": 0,
                    "#sex": 0,
                    "#birthday_month": 0,
                    "#birthday_day": 0,
                    "#birthday_year": 0
                },
                "scroll": "0,0"
            }],
            "index": 2,
            "hidden": false,
            "attributes": {
                "image": "http://www.facebook.com/favicon.ico"
            }
        }, {
            "entries": [{
                "url": "http://twitter.com/",
                "title": "Twitter",
                "ID": 3,
                "docshellID": 6,
                "docIdentifier": 3,
                "children": [{
                    "url": "http://api.twitter.com/receiver.html",
                    "ID": 4,
                    "docshellID": 7,
                    "referrer": "http://twitter.com/",
                    "docIdentifier": 4,
                    "scroll": "0,0"
                }],
                "formdata": {},
                "scroll": "0,0"
            }],
            "index": 1,
            "hidden": false,
            "attributes": {
                "image": "http://twitter.com/phoenix/favicon.ico"
            }
        }],
        "selected": 2,
        "_closedTabs": [],
        "width": 994,
        "height": 688,
        "screenX": 1366,
        "screenY": 307,
        "sizemode": "normal",
        "cookies": [{
            "host": ".facebook.com",
            "value": "J4-69",
            "path": "/",
            "name": "lsd"
        }, {
            "host": ".facebook.com",
            "value": "http%3A%2F%2Fwww.facebook.com%2F",
            "path": "/",
            "name": "reg_fb_gate"
        }, {
            "host": ".facebook.com",
            "value": "http%3A%2F%2Fwww.facebook.com%2F",
            "path": "/",
            "name": "reg_fb_ref"
        }, {
            "host": ".facebook.com",
            "value": "994x624",
            "path": "/",
            "name": "wd"
        }, {
            "host": ".twitter.com",
            "value": "43838368",
            "path": "/",
            "name": "__utmc"
        }, {
            "host": "twitter.com",
            "value": "4bfz%2B%2BmebEkRkMWFCXm%2FCUOsvDoVeFTl",
            "path": "/",
            "name": "original_referer"
        }, {
            "host": "scribe.twitter.com",
            "value": "4bfz%2B%2BmebEkRkMWFCXm%2FCUOsvDoVeFTl",
            "path": "/",
            "name": "original_referer"
        }, {
            "host": ".twitter.com",
            "value": "BAh7CToPY3JlYXRlZF9hdGwrCDoVZ%252F4vAToMY3NyZl9pZCIlODE2MGI1ZjJh%250AYmViNDMwODMxNDlkN2U5ZDg5Yjk4ZmU6B2lkIiU2N2I4YjdmNGExNWFkNzlk%250AODI0MDVjMGM1NmMzYjVhYSIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6%250ARmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA%253D%253D--8b0d751e9774c5cfaa61fdec567cb782aa8757dd",
            "path": "/",
            "name": "_twitter_sess",
            "httponly": true
        }, {
            "host": ".twitter.com",
            "value": "43838368",
            "path": "/",
            "name": "__utmc"
        }, {
            "host": "twitter.com",
            "value": "4bfz%2B%2BmebEkRkMWFCXm%2FCUOsvDoVeFTl",
            "path": "/",
            "name": "original_referer"
        }, {
            "host": "scribe.twitter.com",
            "value": "4bfz%2B%2BmebEkRkMWFCXm%2FCUOsvDoVeFTl",
            "path": "/",
            "name": "original_referer"
        }, {
            "host": ".twitter.com",
            "value": "BAh7CToPY3JlYXRlZF9hdGwrCDoVZ%252F4vAToMY3NyZl9pZCIlODE2MGI1ZjJh%250AYmViNDMwODMxNDlkN2U5ZDg5Yjk4ZmU6B2lkIiU2N2I4YjdmNGExNWFkNzlk%250AODI0MDVjMGM1NmMzYjVhYSIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6%250ARmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA%253D%253D--8b0d751e9774c5cfaa61fdec567cb782aa8757dd",
            "path": "/",
            "name": "_twitter_sess",
            "httponly": true
        }],
        "title": "Twitter"
    }],
    "session": {
        "state": "stopped",
        "lastUpdate": 1305658398727
    }
}

错误

Error: sessionstore.js(179): expected value

现在将故障识别为倒数第三行,即读取 "lastUpdate": 1305658398727 的那一行。来自 what I've read关于 JSON 格式,这对我来说听起来好像这一行中缺少逗号或括号,但这是我的 Mozilla 生成的用于 Firefox 的文件,我不相信他们会犯这样的错误,所以我相信 Boost 中的 JSON 解析器有问题。任何人都可以确认是否是这种情况,或者我是否做错了什么?

最佳答案

我认为问题是这个值大于 intdouble。我不知道什么数据类型使用 BOOST JSON 来读取数字。要对此进行测试,只需将数字更改为字符串并再次解析它。在标准中,数字是不受限制的,但是你必须选择一种数据类型来表示它们,也许他们选择了double,显然这个数字是不够的。我会看看您是否可以配置用于数字的类型。

编辑:

再来看实现,“number”规则使用Spirit实现如下:

number
            =   strict_real_p
                | int_p
                ;

看 Spirit strict_real_p 使用 double 作为底层类型,而 int_p 实际上使用了一个 int

坏消息是,对于我在代码中看到的,这是不可配置的,因此您必须更改要解释的 JSON。

关于c++ - 使用 Boost JSON 解析器的 JSON 格式化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6050225/

相关文章:

c++ - 带迭代器和 const_iterator 的取消引用运算符

java - 使用自定义类型适配器添加对象,gson 中的 jsonwriter

c++ - 如何使用 yaml-cpp 解析文件

java - 如何根据光标位置分割线?

c# - 在 C# 字符串对象之间共享字符缓冲区

c++ - 你如何覆盖 operator+=

c++ - 在 C++ std::string 中解析(替换)

c++ - 返回枚举数组的函数

json - 如何使用 jsonpb 将 JSON 解码为包含一个定义的 protobuf?

javascript - 按键名过滤 JSON 数据