2011-05-18 102 views
3

我试图使用Boost从我的Firefox配置文件夹中读取一个名为sessionstore.js的JSON文件,其中当前/最后一个Firefox会话的信息被保存用于恢复目的。我已经写了基于该XML-based tutorial来自Boost网站程序,只需换出XML部分为JSON部分,可以在下面使用Boost JSON解析器的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 

由于文件的格式都在同一行,这意味着错误C应该在文件中的任何位置,所以我通过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。关于JSON格式,从what I've read开始,这听起来像是一个逗号或括号,但是这是我的Mozilla生成的与Firefox一起工作的文件,我不相信他们会犯错误像这样,所以我导致相信Boost中的JSON解析器存在问题。任何人都可以确认,如果是这样的话,或者如果我是一个做错事的人?

回答

6

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

编辑

在实施再来看,“一把手”的规则是用精神执行如下:

number 
      = strict_real_p 
       | int_p 
       ; 

看着精神strict_real_p使用double作为基本类型,int_p实际上使用了int

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

+0

假设对JSON的一个非常简单的改变就可以做到这一点,因为'1305658398727.0'正好由'double'表示(假设IEEE yadda yadda)。如此接近,但... – 2011-05-18 20:34:48

+0

@Steve:啊,它会适合一个'双'?那么,解决方案更容易。 – 2011-05-18 20:48:36

0

正如迭戈说的那样,是因为1305658398727不适合strict_real_pint_p生产。

我怀疑你会需要其他JSON解析器或自己修改Spirit定义。

无论是这样的:

number 
      = strict_real_p 
       | int_p 
       | int64_p 
       ; 

或正如:在JSON

number 
      = real_p; 

理想的日期/时间应在ISO格式的字符串来呈现。在这种情况下,你不会有这样的问题。我怀疑自1970-01-01以来的数据只有几毫秒(JavaScript Date.valueOf()

1

在收到迭戈塞维利亚和c-smile的回答后,我做了一些Google搜索,以了解如何将他们的建议进入Boost,因为不幸的是,改变JSON文件不是我的选择,我遇到了this ticket on the Boost bug tracker,它描述了我的确切问题。它已经被固定并随着Boost 1.45发布。但是,我使用Ubuntu存储库中的1.42版本,因此需要手动安装较新的版本。