ããã¹ãããŒã¿äº€æãã©ãŒãããã®åé¡ã¯äœã§ããïŒ åœŒãã¯é ãã§ãã ãããŠãé ãã ãã§ãªããéæ¹ããªãé ãã§ãã ã¯ãããããã¯ãã€ããªãããã³ã«ãšæ¯èŒããŠåé·ã§ãããçè«çã«ã¯ãããã¹ãã·ãªã¢ã©ã€ã¶ã¯åé·ãšã»ãŒåããããé ãã¯ãã§ãã ããããå®éã«ã¯ãããã¹ãã·ãªã¢ã©ã€ã¶ãŒã¯ããã€ããªã«ãŠã³ã¿ãŒãããæ¡éãã«å£ãããšããããŸãã
ãã€ããªåœ¢åŒã«å¯ŸããJSONã®å©ç¹ã«ã€ããŠã¯èª¬æããŸãããå圢åŒã«ã¯ãããããã«é©ããå¿çšåéããããŸãã ããããå€ãã®å Žåãæåã®å£æ» çãªéå¹çæ§ã®ããã«éåžžã«å¿«é©ã§ã¯ãªãããã䟿å©ãªäœããæŸæ£ããããåŸãŸããã éçºè ã¯JSONãæåŠããŸããJSONãåé¡ã®è§£æ±ºã«æé©ã§ãã£ãŠãããããã·ã¹ãã ã®ããã«ããã¯ã§ããããšãå€æããããã§ãã ãã¡ããã責任ãããã®ã¯JSONã ãã§ã¯ãªãã察å¿ããã©ã€ãã©ãªã®å®è£ ã§ãã
ãã®èšäºã§ã¯ãäžè¬çãªããã¹ã圢åŒããŒãµãŒãç¹ã«JSONã®åé¡ã ãã§ãªããæãè² è·ã®é«ããããžã§ã¯ãã§é·å¹Žäœ¿çšããŠããã©ã€ãã©ãªãŒã«ã€ããŠã説æããŸãã é床ãšäœ¿ããããã®äž¡æ¹ã®ç¹ã§éåžžã«é©ããŠãããããããé©åãªå Žåã¯ãã€ããªåœ¢åŒãæåŠããããšããããŸãã ãã¡ãããç§ã¯ããã€ãã®å¢çæ¡ä»¶ãæå³ããŸãã
æ²åã®èŠæš¡ãè©äŸ¡ããããã«ãããç¥ãããŠããããã€ãã®JSONã©ã€ãã©ãªgoogle :: protobufããæåãJSONã·ãªã¢ã«åãããã³wjsonã©ã€ãã©ãªã®ããŸãè€éã§ã¯ãªããªããžã§ã¯ãã®ã·ãªã¢ã«åæéã枬å®ããŸããã
çµæãå³ã«ç€ºããŸãã
ç§ã¯ããããã®çµæããããŠãæ§ããã«èšã£ãŠããå°ãé©ããããšãèªããŸãã
jsoncppãšjson_spiritïŒboost :: spiritã«åºã¥ãïŒã®ããã©ãŒãã³ã¹ã¯ãå£æ» çã«google :: protobufã«è² ããŸãã sprintf / sscanfãŸãã¯std :: stringstreamã䜿çšãããæåãã·ãªã¢ã«åã®ç¶æ³ã¯ãã¯ããã«åªããŠããŸãã ãã ããæåã®2ã€ã®ããŒã«ã䜿çšããå Žåã¯ãæ¥ãã§ãã¹ãŠãããããããããèªåã§äœãããå¿ èŠãããããäŒããŸããïŒã-ãããžã§ã¯ããããçŽããŠãã ããã ã°ã©ãã§ã¯ã枬å®ã¯sprintf / sscanfã®1åã®åŒã³åºãã«å¯Ÿãããã®ã§ãããJSONãªããžã§ã¯ãã®ãã£ãŒã«ãã䞊ã¹æ¿ãããã¹ããããããããããšãªããã·ãªã¢ã«åå¯èœãªãªããžã§ã¯ããè©°ã蟌ã¿ãŸããã ãªããžã§ã¯ãã®ã·ãªã¢ã«åã«é¢ããã»ã¯ã·ã§ã³ã§ããã詳现ãªæ°å€ã瀺ããŸãã
ãã®èšäºã§ã¯ãJSONãããã©ãŒãã³ã¹ã«çŠç¹ãåœãŠãã¡ãã»ãŒãžã³ã°åœ¢åŒãšããŠèŠãŠããŸãã ãããã£ãŠããã®ã³ã³ããã¹ãã§ç¹å®ã®ãã¯ãããžãŒãæ¯èŒããŸãã ãŸããéçºïŒã³ã³ãã€ã«ïŒæ®µéã§ã®ã¡ãã»ãŒãžæ§é ãããã£ãŠããããšãæå³ããŸãã ææ¡ãããŠããwjsonã©ã€ãã©ãªãããããã®ã¿ã¹ã¯å°çšã«éçºãããŠããŸãã ãã¡ãããæªç¥ã®JSONããã¥ã¡ã³ãã䜿çšããŠèª¿ã¹ãããšãã§ããŸãããããããwjsonã¯å€ãã®ã©ã€ãã©ãªïŒãããã®å Žåãjsoncppãšjson_spiritïŒãããå¹æçã§ãã
å®éãwjsonã¯ãããšãã°äžèšã®ã©ã€ãã©ãªãããæŠå¿µçã«protobufã«è¿ãã§ãã åæ§ã«ãã¡ã¿èšè¿°ã«åŸã£ãŠã·ãªã¢ã«å/éã·ãªã¢ã«åã³ãŒããçæããŸãã ããããprotobufãšã¯ç°ãªããå€éšã¢ããªã±ãŒã·ã§ã³ã§ã¯ãªããC ++ã³ã³ãã€ã©ã䜿çšããŸãã 以åã®èšäºã§ã tic-tac-toeãåçããããã³ã³ãã€ã©ãŒã«æããæ¹æ³ã瀺ããŸããããŸããã·ãªã¢ã«åã³ãŒããçæããæ¹æ³ãæããããšã¯ãã¯ããã¯ã®åé¡ã§ãã
ããããç§ãæãæ°ã«å ¥ã£ãŠããã®ã¯ãã·ãªã¢ã©ã€ãºå¯èœãªããŒã¿æ§é ã«è¿œå æ©èœãå®è£ ããå¿ èŠããªãããšã§ããåå¥ã«ãã©ã€ãåå¥ã«ã«ãã¬ããã ããã«ãå€éç¶æ¿ããã¹ãã®éçŽãåæåã®ã·ãªã¢ã«åãªã©ã®ããã€ãã®å©ç¹ãå«ããç¶æ¿ããµããŒããããŠããŸãã
åœåãwjsonã¯ãããã°ã©ããå€ãã®å¿ èŠãªãã§ãã¯ã䌎ãå®è¡æã³ãŒããèšè¿°ããªãããã«ããããã«ãc ++ãã³ãã¬ãŒãã®JSONæ§é ã®å®£èšçãªèšè¿°ã®ããã ãã«èæ¡ãããŸããã ããããã³ã³ãã€ã©ãŒãç©æ¥µçã«ã€ã³ã©ã€ã³ã»ã€ããã®ãããªæ§æã§ããããšãããã«æããã«ãªããŸããã ãããŠããããã®èšèšãååã«å¹ççã«åäœãããŠã蚱容å¯èœãªããã©ãŒãã³ã¹ã¬ãã«ã«å°éããã«ã¯ãããªãã®åŽåãå¿ èŠã§ããã
ã§ã¯ããªãJSONã¯ãããªã«é
ãã®ã§ããããïŒ
XMLã䜿çšããå Žåããã·ãªã¢ã©ã€ãºã«ã¯2ã€ã®ã¢ãããŒããããããšã«æ°ã¥ããŸã-ãããã¯DOMïŒDocument Object ModelïŒãšSAXïŒSimple API for XMLïŒã§ãã DOMã®å Žåãããã¹ãã¯ããŒãã®ããªãŒã«å€æããã察å¿ããAPIã䜿çšããŠèª¿ã¹ãããšãã§ããŸãã ãŸããSAXããŒãµãŒã®åäœã¯ç°ãªããŸããããã¥ã¡ã³ããã¹ãã£ã³ãããŠãŒã¶ãŒã³ãŒãã«ãã£ãŠåŠçãããç¹å®ã®ã€ãã³ããçæããŸããããã¯éåžžãã³ãŒã«ããã¯é¢æ°ãšããŠå®è£ ãããŸãã äœããã®æ¹æ³ã§ãã»ãšãã©ã®ããã¹ããã·ãªã¢ã©ã€ã¶ãŒã¯ãããã®ã¢ãããŒãã®ããããã䜿çšãããããŸãã¯çµã¿åãããŸãã
DOMã®äž»ãªæ¬ ç¹ã¯ãåžžã«é«äŸ¡ãªããªãŒãæ§ç¯ããå¿ èŠãããããšã§ãã éã·ãªã¢ã«åã®æ®µéã§ãã®ãããªããªãŒãæ§ç¯ããããã»ã¹ã¯ãé©çšãããã¢ã«ãŽãªãºã ã®å®è¡ãããããªãæéããããå ŽåããããŸãã ãã ããããã«å ããŠãå¿ èŠãªãã£ãŒã«ããæ€çŽ¢ããããããå éšããŒã¿æ§é ã«å€æããå¿ èŠããããŸãã ãããŠããã®ã¿ã¹ã¯ã¯ãã§ã«ããã°ã©ããŒã®è©ã«ããã£ãŠãããéåžžã«éå¹ççã«å®è£ ã§ããŸãã å®éãããã¯äž»èŠãªãªãœãŒã¹ãé£ãå°œããã®ã¯DOMããªãŒã®æ§ç¯ã§ãããããããã»ã©éèŠã§ã¯ãããŸããã
SAXããŒãµãŒã¯ã¯ããã«é«éã§ãã æŠããŠã圌ãã¯å¯Ÿå¿ãããã³ãã©ãŒãåŒã³åºããŠãããã¹ããäžåºŠééããŸãã JSONã«ãã®ãããªããŒãµãŒãå®è£ ããã®ã¯ç°¡åãªäœæ¥ã§ããJSONèªäœã¯äžåèªã«ç°¡åã§ãããããããã®é åã§ãã ããããããŒã¿ãæœåºããããã«ããã䜿çšããããã°ã©ããŒããã®ããå€ãã®åªåãå¿ èŠã§ãã ãããŠãããã¯ããã°ã©ãã«ãšã£ãŠããå€ãã®äœæ¥ã§ãããããå€ãã®ãšã©ãŒãšéå¹ççãªã³ãŒãã§ãããSAXã®æå¹æ§ãç¡å¹ã«ããå¯èœæ§ããããŸãã
äžè¬çã«ãå¹æçãªã·ãªã¢ã«åã®ãããã¯ã¯ãæ®éçãªãœãªã¥ãŒã·ã§ã³ãéçºãããšãã芳ç¹ããéåžžã«èå³æ·±ããã®ã§ãã ããããå¿çšããã°ã©ãã³ã°ã®èŠ³ç¹ããèŠããšãããã¯éåžžã«éå±ã§éå±ã§ããããã®çµæãèšå€§ãªéã®å¹æã®ãªãããããšã§ãã ããã°ã©ããã¢ããªã±ãŒã·ã§ã³ã³ãŒããæé©åããããšã¯éåžžã«èå³æ·±ãããšã§ããéã·ãªã¢ã«åã¯1æ¡é ãããã®ã³ã³ããã¹ãã§ã®æé©åã¯ããŸãæå³ããªããããŸãæ°ã«ããŸããã
ããããäœããã®çç±ã§æ¢åã®ãœãªã¥ãŒã·ã§ã³ã®ã»ãšãã©ãç§ãã¡ã«åããªãå Žåã¯ãæåã§ãã£ãŠã¿ãŸããããJSONã®å©ç¹ã¯éåžžã«ã·ã³ãã«ãªãã©ãŒãããã§ãã ãããŠãããã§ç§ã¯éåžžã«èå³æ·±ã話ãæ°å芳å¯ããŸããã ããã°ã©ãããã€ããªãããã³ã«ã䜿çšããå Žåãããããæ éã«ã·ããããã³ãŒããæé©åããŠæ¥œããã§ããŸãã ãããã圌ãåãã³ã³ããã¹ãã§ããã¹ã圢åŒã§äœæ¥ããããã«ç³ãåºãããå Žåãäœãã圌ã®è³ã§åæãããŠããããã§ãã ããæ£ç¢ºã«ã¯ãéã«ã圌ã¯é«æ¬¡ã®èªè»¢è»ã®æ§ç¯ã«å¯Ÿããä¿è·ããªã³ã«ããŸããããã«ããããã€ããªåœ¢åŒã§äœæ¥ãããšãã«ç¹ã«èŠåŽããããšã¯ãããŸããã§ããã 圌ã¯ãJSONã§åäœããã³ãŒããäœããã®æ¹æ³ã§é«è²Žã«ããããã«ãã©ã€ãã©ãªã®ããŒããæ¥ç¶ããŠãããã€ãã®çŸããïŒãã ããååãšããŠããŸãå¹æçã§ã¯ãªãïŒé¢æ°ã®ããã¹ããæäœããŸãã ãããŠãçµæã¯ãŸã åããããã§ãã
atoiã®å®è£ ãæžãããšãèããŠãã人ã¯ã»ãšãã©ããŸããããç§ãã¡ã¯ãŸã è©Šã¿ãŸãïŒ
å®éããã¹ãŠã¯ã·ã³ãã«ã§ãããå€å žçãªatoiãããæ®éçã§äŸ¿å©ã§ãïŒç§ã®æèŠã§ã¯ïŒã ããããæãèå³æ·±ãã®ã¯ã2åã®é床ã§åäœããããšã§ãã ã¯ãããã¡ããã倧éšåã¯ã€ã³ã©ã€ã³çœ®æã®ããã§ãããããã¯ãã€ã³ãã§ã¯ãããŸããã ã¡ãªã¿ã«ãsscanf / sprintfã¯ãïŒ dãããéãïŒ sãã©ã¡ãŒã¿ãŒãå®è¡ããåçã®è¡ã®é·ããæã¡ãŸãã
sscanf / sprintfã®å±éºæ§ã«ã€ããŠã¯ä»ããã話ãããŸãããæ¢ã«äœåºŠãæžããŠãããããã«å®å šãªä»£æ¿æ段ãããšãã°std :: stringstreamãŸãã¯boost :: lexical_cast <>ããããŸãã æ®å¿µãªãããC ++ãå«ãå€ãã®ããã°ã©ããŒã¯ãçã®Cã¯ããé«éã§ãthatæã®æ°žç¶æ§ãåããsscanf / sprintfã䜿ãå§ãããšããç¥è©±ã«å°ãããŠããŸãã ãããããã®æèã§ã¯ãåé¡ã¯èšèªã«ããã®ã§ã¯ãªãã1ã€ãŸãã¯å¥ã®æ©èœã®å®è£ ã«ãããŸãã ããšãã°ãstd :: stringstreamãæ£ãã䜿çšãããšãCã®éžæè¢ããæªããªãããšã¯ãããŸããããããšãã°ãboost :: lexical_cast <>ã¯ãã®ç¹ã§èããå£ãããšããããŸãã
ãããã£ãŠããµãŒãããŒãã£ã®ã©ã€ãã©ãªã ãã§ãªãã䜿ãæ £ããããŒã«ã®ããã©ãŒãã³ã¹ã«ã€ããŠãæ éã«ãã¹ãããå¿ èŠããããŸãã ãã ããã€ã³ã¿ãŒãããäžã®å¿ èŠãªå®è£ ã確èªããããšã«ãããå€ãã®å Žåããµã€ã¯ã«ãéããªããŸãã
my_atoiã®ã³ãŒãã¯wjsonããã»ãŒå®å šã«å€æŽãããŠããªãããã誰ãã圹ã«ç«ã€ãããããŸããã ã·ãªã¢ã«åã®ã³ãŒãã¯ããå°ãæ··ä¹±ããŠããŸãã
JSONã®æ®ãã®éšåããã³ã€ã³ã©ã€ã³çœ®æã®æ§æèŠçŽ ã«å¯Ÿãããã®ãããªãã€ãããšã®äžŠã¹æ¿ãã«ãããããé«éãªéã·ãªã¢ã«åãå®çŸã§ããŸãã äœããã®æ¹æ³ã§ãããã1ã€ã®ãã¶ã€ã³ã«ã¢ã»ã³ãã«ãããšãäžçš®ã®SAXããŒãµãŒãåŸãããŸããããããéåžžã«é«éã§ãã
XMLã䜿çšããå Žåããã·ãªã¢ã©ã€ãºã«ã¯2ã€ã®ã¢ãããŒããããããšã«æ°ã¥ããŸã-ãããã¯DOMïŒDocument Object ModelïŒãšSAXïŒSimple API for XMLïŒã§ãã DOMã®å Žåãããã¹ãã¯ããŒãã®ããªãŒã«å€æããã察å¿ããAPIã䜿çšããŠèª¿ã¹ãããšãã§ããŸãã ãŸããSAXããŒãµãŒã®åäœã¯ç°ãªããŸããããã¥ã¡ã³ããã¹ãã£ã³ãããŠãŒã¶ãŒã³ãŒãã«ãã£ãŠåŠçãããç¹å®ã®ã€ãã³ããçæããŸããããã¯éåžžãã³ãŒã«ããã¯é¢æ°ãšããŠå®è£ ãããŸãã äœããã®æ¹æ³ã§ãã»ãšãã©ã®ããã¹ããã·ãªã¢ã©ã€ã¶ãŒã¯ãããã®ã¢ãããŒãã®ããããã䜿çšãããããŸãã¯çµã¿åãããŸãã
DOMã®äž»ãªæ¬ ç¹ã¯ãåžžã«é«äŸ¡ãªããªãŒãæ§ç¯ããå¿ èŠãããããšã§ãã éã·ãªã¢ã«åã®æ®µéã§ãã®ãããªããªãŒãæ§ç¯ããããã»ã¹ã¯ãé©çšãããã¢ã«ãŽãªãºã ã®å®è¡ãããããªãæéããããå ŽåããããŸãã ãã ããããã«å ããŠãå¿ èŠãªãã£ãŒã«ããæ€çŽ¢ããããããå éšããŒã¿æ§é ã«å€æããå¿ èŠããããŸãã ãããŠããã®ã¿ã¹ã¯ã¯ãã§ã«ããã°ã©ããŒã®è©ã«ããã£ãŠãããéåžžã«éå¹ççã«å®è£ ã§ããŸãã å®éãããã¯äž»èŠãªãªãœãŒã¹ãé£ãå°œããã®ã¯DOMããªãŒã®æ§ç¯ã§ãããããããã»ã©éèŠã§ã¯ãããŸããã
SAXããŒãµãŒã¯ã¯ããã«é«éã§ãã æŠããŠã圌ãã¯å¯Ÿå¿ãããã³ãã©ãŒãåŒã³åºããŠãããã¹ããäžåºŠééããŸãã JSONã«ãã®ãããªããŒãµãŒãå®è£ ããã®ã¯ç°¡åãªäœæ¥ã§ããJSONèªäœã¯äžåèªã«ç°¡åã§ãããããããã®é åã§ãã ããããããŒã¿ãæœåºããããã«ããã䜿çšããããã°ã©ããŒããã®ããå€ãã®åªåãå¿ èŠã§ãã ãããŠãããã¯ããã°ã©ãã«ãšã£ãŠããå€ãã®äœæ¥ã§ãããããå€ãã®ãšã©ãŒãšéå¹ççãªã³ãŒãã§ãããSAXã®æå¹æ§ãç¡å¹ã«ããå¯èœæ§ããããŸãã
äžè¬çã«ãå¹æçãªã·ãªã¢ã«åã®ãããã¯ã¯ãæ®éçãªãœãªã¥ãŒã·ã§ã³ãéçºãããšãã芳ç¹ããéåžžã«èå³æ·±ããã®ã§ãã ããããå¿çšããã°ã©ãã³ã°ã®èŠ³ç¹ããèŠããšãããã¯éåžžã«éå±ã§éå±ã§ããããã®çµæãèšå€§ãªéã®å¹æã®ãªãããããšã§ãã ããã°ã©ããã¢ããªã±ãŒã·ã§ã³ã³ãŒããæé©åããããšã¯éåžžã«èå³æ·±ãããšã§ããéã·ãªã¢ã«åã¯1æ¡é ãããã®ã³ã³ããã¹ãã§ã®æé©åã¯ããŸãæå³ããªããããŸãæ°ã«ããŸããã
ããããäœããã®çç±ã§æ¢åã®ãœãªã¥ãŒã·ã§ã³ã®ã»ãšãã©ãç§ãã¡ã«åããªãå Žåã¯ãæåã§ãã£ãŠã¿ãŸããããJSONã®å©ç¹ã¯éåžžã«ã·ã³ãã«ãªãã©ãŒãããã§ãã ãããŠãããã§ç§ã¯éåžžã«èå³æ·±ã話ãæ°å芳å¯ããŸããã ããã°ã©ãããã€ããªãããã³ã«ã䜿çšããå Žåãããããæ éã«ã·ããããã³ãŒããæé©åããŠæ¥œããã§ããŸãã ãããã圌ãåãã³ã³ããã¹ãã§ããã¹ã圢åŒã§äœæ¥ããããã«ç³ãåºãããå Žåãäœãã圌ã®è³ã§åæãããŠããããã§ãã ããæ£ç¢ºã«ã¯ãéã«ã圌ã¯é«æ¬¡ã®èªè»¢è»ã®æ§ç¯ã«å¯Ÿããä¿è·ããªã³ã«ããŸããããã«ããããã€ããªåœ¢åŒã§äœæ¥ãããšãã«ç¹ã«èŠåŽããããšã¯ãããŸããã§ããã 圌ã¯ãJSONã§åäœããã³ãŒããäœããã®æ¹æ³ã§é«è²Žã«ããããã«ãã©ã€ãã©ãªã®ããŒããæ¥ç¶ããŠãããã€ãã®çŸããïŒãã ããååãšããŠããŸãå¹æçã§ã¯ãªãïŒé¢æ°ã®ããã¹ããæäœããŸãã ãããŠãçµæã¯ãŸã åããããã§ãã
atoiã®å®è£ ãæžãããšãèããŠãã人ã¯ã»ãšãã©ããŸããããç§ãã¡ã¯ãŸã è©Šã¿ãŸãïŒ
template<typename T, typename P> P my_atoi( T& v, P beg, P end) { if( beg==end) return end; bool neg = ( *beg=='-' ); if ( neg ) ++beg; if ( beg == end || *beg < '0' || *beg > '9') return 0; if (*beg=='0') return ++beg; v = 0; for ( ;beg!=end; ++beg ) { if (*beg < '0' || *beg > '9') break; v = v*10 + (*beg - '0'); } if (neg) v = static_cast<T>(-v); return beg; }
å®éããã¹ãŠã¯ã·ã³ãã«ã§ãããå€å žçãªatoiãããæ®éçã§äŸ¿å©ã§ãïŒç§ã®æèŠã§ã¯ïŒã ããããæãèå³æ·±ãã®ã¯ã2åã®é床ã§åäœããããšã§ãã ã¯ãããã¡ããã倧éšåã¯ã€ã³ã©ã€ã³çœ®æã®ããã§ãããããã¯ãã€ã³ãã§ã¯ãããŸããã ã¡ãªã¿ã«ãsscanf / sprintfã¯ãïŒ dãããéãïŒ sãã©ã¡ãŒã¿ãŒãå®è¡ããåçã®è¡ã®é·ããæã¡ãŸãã
sscanf / sprintfã®å±éºæ§ã«ã€ããŠã¯ä»ããã話ãããŸãããæ¢ã«äœåºŠãæžããŠãããããã«å®å šãªä»£æ¿æ段ãããšãã°std :: stringstreamãŸãã¯boost :: lexical_cast <>ããããŸãã æ®å¿µãªãããC ++ãå«ãå€ãã®ããã°ã©ããŒã¯ãçã®Cã¯ããé«éã§ãthatæã®æ°žç¶æ§ãåããsscanf / sprintfã䜿ãå§ãããšããç¥è©±ã«å°ãããŠããŸãã ãããããã®æèã§ã¯ãåé¡ã¯èšèªã«ããã®ã§ã¯ãªãã1ã€ãŸãã¯å¥ã®æ©èœã®å®è£ ã«ãããŸãã ããšãã°ãstd :: stringstreamãæ£ãã䜿çšãããšãCã®éžæè¢ããæªããªãããšã¯ãããŸããããããšãã°ãboost :: lexical_cast <>ã¯ãã®ç¹ã§èããå£ãããšããããŸãã
ãããã£ãŠããµãŒãããŒãã£ã®ã©ã€ãã©ãªã ãã§ãªãã䜿ãæ £ããããŒã«ã®ããã©ãŒãã³ã¹ã«ã€ããŠãæ éã«ãã¹ãããå¿ èŠããããŸãã ãã ããã€ã³ã¿ãŒãããäžã®å¿ èŠãªå®è£ ã確èªããããšã«ãããå€ãã®å Žåããµã€ã¯ã«ãéããªããŸãã
my_atoiã®ã³ãŒãã¯wjsonããã»ãŒå®å šã«å€æŽãããŠããªãããã誰ãã圹ã«ç«ã€ãããããŸããã ã·ãªã¢ã«åã®ã³ãŒãã¯ããå°ãæ··ä¹±ããŠããŸãã
ã€ãã¢
// template<typename T> struct integer_buffer_size { enum { value = sizeof(T)*2 + sizeof(T)/2 + sizeof(T)%2 + is_signed_integer<T>::value }; }; // template<typename T, int > struct is_signed_integer_base { enum { value = 1 }; static bool is_less_zero(T v) { return v < 0; } }; // , false template<typename T> struct is_signed_integer_base<T, false> { enum { value = 0 }; static bool is_less_zero(T ) { return false; } }; template<typename T> struct is_signed_integer: is_signed_integer_base< T, ( T(-1) < T(1) ) > { }; template<typename T, typename P> P my_itoa(T v, P itr) { char buf[integer_buffer_size<T>::value]; char *beg = buf; char *end = buf; if (v==0) *(end++) = '0'; else { // if (false) // . // if ( is_signed_integer<T>::is_less_zero(v) ) { for( ; v!=0 ; ++end, v/=10) *end = '0' - v%10; *(end++)='-'; } else { for( ; v!=0 ; ++end, v/=10) *end = '0' + v%10; } } do { *(itr++)=*(--end); } while( end != beg ); return itr; }
JSONã®æ®ãã®éšåããã³ã€ã³ã©ã€ã³çœ®æã®æ§æèŠçŽ ã«å¯Ÿãããã®ãããªãã€ãããšã®äžŠã¹æ¿ãã«ãããããé«éãªéã·ãªã¢ã«åãå®çŸã§ããŸãã äœããã®æ¹æ³ã§ãããã1ã€ã®ãã¶ã€ã³ã«ã¢ã»ã³ãã«ãããšãäžçš®ã®SAXããŒãµãŒãåŸãããŸããããããéåžžã«é«éã§ãã
åçŽå
ã·ãªã¢ã«åã®äŸãããã«èŠãŠã¿ãŸãããã
int value = 12345; char bufjson[100]; char* ptr = wjson::value<int>::serializer()(value, bufjson); *ptr = '\0'; std::cout << bufjson << std::endl;
ããã§ãwjson :: value <int>ã¯ããã®åã®ã·ãªã¢ã©ã€ã¶ãŒå®çŸ©ãå«ãæŽæ°åã®JSONèšè¿°ã§ãã 次ã«ãã·ãªã¢ã©ã€ã¶ãŒãªããžã§ã¯ããäœæãããªãŒããŒããŒããããæŒç®åïŒïŒãåŒã³åºããŸãã äžéšã®äººã«ãšã£ãŠã¯ããã®ãããªãšã³ããªã¯å¥åŠã«èŠãããããããŸããããJSONã·ãªã¢ã©ã€ã¶ãªããžã§ã¯ãã«ã¯ç¶æ ããªãããã®ã€ã³ã¹ã¿ã³ã¹ãäœæããæå³ããªãããšã匷調ããããã«äœ¿çšããŸãã
ã·ãªã¢ã©ã€ã¶ãéçé¢æ°ã§ã¯ãªãçç±ã®è³ªåã«ããã«çããŸãã 第äžã«ãã³ã³ãã€ã©ãŒã¯ã³ã³ãã€ã«æéã®ç¹ã§éçèŠçŽ ãæ¬åœã«å¥œãã§ã¯ãããŸããã第äºã«ããšã«ãããç§ã«ãšã£ãŠãã䟿å©ã§ãã å®éãäžèšã®ãã¿ãã¬ã®äžã«ç€ºããã³ãŒãã¯ãmy_itoaãäŸã«äœ¿çšããŠå®å šã«çœ®ãæããããŸãã
å€<>ã³ã³ã¹ãã©ã¯ãã¯ãæŽæ°ã ãã§ãªããå®éã®ãã®ãæååãããã³ããŒã«å€ã«ã䜿çšãããŸãã å®çŸ©ïŒ
template<typename T, int R = -1> struct value { typedef T target; typedef implementation_defined serializer; };
ããŒã«åããã³æŽæ°åã®å ŽåãåŒæ°Rã¯äœ¿çšãããŸããã std :: stringãŸãã¯std :: vector <char>ãªã©ã®æååã®å Žåãããã¯äºçŽã®ãµã€ãºã§ãããå®éã®æååã®å Žåã¯è¡šç€ºåœ¢åŒã§ãã
ã·ãªã¢ã©ã€ã¶ãŒã¯ã©ã¹ã¯ãã·ãªã¢ã«åã«å ããŠãéã·ãªã¢ã«åæ©èœãæäŸããŸãã 2ã€ã®ãªãŒããŒããŒãæŒç®åïŒïŒïŒ
template<typename T> class implementation_defined { public: template<typename P> P operator()( const T& v, P itr); template<typename P> P operator() ( T& v, P beg, P end, json_error* e ); };
ã·ãªã¢ã«åé¢æ°ã¯ãã·ãªã¢ã«åå¯èœãªåãžã®åç §ã«å ããŠãããšãã°æ¬¡ã®ãããªåºåå埩åãåãå ¥ããŸãã
int value = 12345; std::string strjson; wjson::value<int>::serializer()(value, std::back_inserter(strjson)); std::cout << strjson << std::endl; std::stringstream ssjson; wjson::value<int>::serializer()(value, std::ostreambuf_iterator<char>(ssjson)); std::cout << ssjson.str() << std::endl; wjson::value<int>::serializer()(value, std::ostreambuf_iterator<char>(std::cout)); std::cout << std::endl;
ãã·ãªã¢ã©ã€ã¶ã¯ãå ¥åãžã®ã©ã³ãã ã¢ã¯ã»ã¹ã€ãã¬ãŒã¿ãåãå ¥ãããããã¡ã®éå§ãšçµäºã瀺ããšãšãã«ããšã©ãŒãªããžã§ã¯ããžã®ãã€ã³ã¿ïŒãŒãã®å ŽåãããïŒã瀺ããŸãã
value = 0; char bufjson[100]=â12345â; wjson::value<int>::serializer()(value, bufjson, bufjson + strlen(bufjson), 0 ); std::cout << value << std::endl; value = 0; std::string strjson=â12345â; wjson::value<int>::serializer()(value, strjson.begin(), strjson.end(), 0 ); std::cout << value << std::endl;
ã·ãªã¢ã©ã€ã¶ãŒãšãã·ãªã¢ã©ã€ã¶ãŒã®äž¡æ¹ãã€ãã¬ãŒã¿ãŒãè¿ããŸãã æåã®ã±ãŒã¹ã§ã¯ãã·ãªã¢ã«åãçµäºããå Žæã瀺ãã2çªç®ã®ã±ãŒã¹ã§ã¯ããšã©ãŒããªããã°ãã·ãªã¢ã«åãçµäºããå ¥åãããã¡å ã®å Žæã瀺ããŸãã ãšã©ãŒãçºçããå Žåããããã¡ã®æ«å°Ÿãžã®ãã€ã³ã¿ãè¿ããŸããnull以å€ã®ãã€ã³ã¿ãæž¡ãããå Žåã¯ãšã©ãŒã³ãŒããè¿ããŸãã å°ãåŸã§ãšã©ãŒåŠçã«ã€ããŠèª¬æããŸãããããŠãåçŽãªåãå®æãããŸãã
ãµããŒããããæŽæ°ïŒcharãunsigned charãshortãunsigned shortãintãunsigned intãlong intãunsigned longãlong longãunsigned long longã ããŒã«å€ïŒboolïŒã䜿çšãããšããã¹ãŠãåãã§ããããtrueããŸãã¯ãfalseãã«ã·ãªã¢ã«åãããŸãã ãã·ãªã¢ã©ã€ãºäžã®ä»ã®ã¿ã€ãããã®èªåå€æã¯ãµããŒããããŠããŸããã
ããã©ãŒãã³ã¹ã«é¢ããŠç¹ã«æ°ã«ããªãã£ãå¯äžã®ã¿ã€ãã¯å®éã®ã¿ã€ãïŒfloatãdoubleãlong doubleïŒã§ãéåžžã®std :: stringstreamããããŸãã ããã¯äž»ã«ãå®éã«äœæ¥ãããããžã§ã¯ãã§ãæŽæ°åïŒããªã¡ãŒãã«åäœã®è»¢éã¡ãŒã¿ãŒãªã©ïŒããŸãã¯CPUã³ã¢ããšã«10K以å ã®è² è·ã«çœ®ãæããããšãã§ãããšããäºå®ã«ãããã®ã§ããããã¯éèŠã§ã¯ãããŸããã ãã©ãã£ãã¯ã®å€§éšåãããå Žå-ãããçŸå®ã§ãããã©ã®ãããªæ¹æ³ã§ãããããéããããšãã§ããªãå Žåãæé©åãšæ··åããããšã¯çã«ããªã£ãŠããŸãã ããã©ã«ãã§ã¯ãå®éã®ãã®ã¯ä»®æ°ã§ã·ãªã¢ã«åãããŸãã R> = 0ã®å Žåãåºå®ã³ã³ãã®å ŽåïŒ
double value = 12345.12345; std::string json; wjson::value<double>::serializer()(value, std::back_inserter(json)); std::cout << json << std::endl; json.clear(); wjson::value<double, 4>::serializer()(value, std::back_inserter(json)); std::cout << json << std::endl;
çµæïŒ
1.234512e+04 12345.1234
utf-8ã䜿çšããå ŽåãäžèŠãããšãã¹ãŠãæååã§ã·ã³ãã«ã«ãªããŸããã次ã®ç¹ã«æ³šæããå¿ èŠããããŸãã
- é£èŒ
- 32ïŒã¹ããŒã¹ïŒã®ã³ãŒããæã€ãã¹ãŠã®utf-8æåã¯ãã®ãŸãŸã³ããŒãããŸã
- æå '' 'ã' \ 'ã' / 'ã' \ t 'ã' \ b 'ã' \ r 'ã' \ n 'ã' \ f 'ã¯JSONä»æ§ã«åŸã£ãŠãšã¹ã±ãŒãããã' \ '
- ã³ãŒãã32æªæºã®æ®ãã®æåã¯ã16é²åœ¢åŒïŒ\ uXXXXïŒã§ã·ãªã¢ã«åãããŸã
- utf-8ã¯\ xXX圢åŒã§ã·ãªã¢ã«åããããã€ãã§ã¯ãªããutf-8ã§ã®ã¿åäœããJSONä»æ§ã«æºæ ããŠããŸããããwjsonãã·ãªã¢ã©ã€ã¶ãŒã¯ãã®åœ¢åŒãç解ããŸã
- ãã·ãªã¢ã©ã€ãŒãŒã·ã§ã³
- ãšã¹ã±ãŒããããæåã¯ãšã¹ã±ãŒããããŸãã
- \ uXXXX圢åŒã®çµã¿åããã¯ã32æªæºã®å€ãé€ããutf-8ã«å€æãããŸãïŒXXXXã '\ t'ã '\ b'ã '\ r'ã '\ n'ã '\ f'ããšã³ã³ãŒãããªãå Žåãå€æãªãïŒ
- \ xXXã®åœ¢åŒã®çµã¿åããã¯ããã§ãã¯ãªãã§é衚瀺ã«ãªããŸã
- ä»ã®ãã¹ãŠã®utf-8æåã¯ãã®ãŸãŸã³ããŒãããŸã
äžéšã®ãµãŒãããŒãã£ã©ã€ãã©ãªã¯ãç¹ã«è² æ ããããããšãªããASCIIç¯å²ïŒã³ãŒã> 127ïŒä»¥å€ã®ãã¹ãŠã\ uXXXX圢åŒã§ã·ãªã¢ã«åããŸãã ãã ããwjsonã䜿çšããŠåæ§ã®æååãéã·ãªã¢ã«åãããšãutf-8ã§ãã³ãŒããããŸãã wjsonãåã·ãªã¢ã«åããããšããã®ãšã¹ã±ãŒãåŠçã¯ãªããªããŸãã
éåžžããœãããŠã§ã¢ãšã©ãŒãåå ã§ãè¡ã®äžå€®ã '\ 0'ã§ãããwjsonãå«ãã»ãšãã©ã®ã·ãªã¢ã©ã€ã¶ãŒã«ãã£ãŠ\ u0000ã«å€æãããŸãããéã·ãªã¢ã«åãããå Žåã\ 0ã«å€æãããããã®ãŸãŸæ®ããŸãã
\ xXX圢åŒã®ãµããŒãã¯ãç¡å¹ãªããŒã¿ïŒã·ãªã¢ã«åãŸãã¯ã³ã³ãã€ã«ãããŠããªãïŒãæå³ããªãwjsonã·ãªã¢ã«åã®æŠå¿µã®å¶éã«ãã£ãŠã®ã¿æ±ºå®ãããŸãã ãã€ããªããŒã¿ãã·ãªã¢ã«åããã«ã¯ãããšãã°Base64ã䜿çšããŸãã
æååã®ã·ãªã¢ã«åã®äŸ
#include <wjson/json.hpp> #include <iostream> #include <cstring> int main() { const char* english = "\"hello world!\""; const char* russian = "\"\\u041F\\u0440\\u0438\\u0432\\u0435\\u0442\\u0020\\u043C\\u0438\\u0440\\u0021\""; const char* chinese = "\"\\u4E16\\u754C\\u4F60\\u597D!\""; typedef char str_t[128]; typedef wjson::value< std::string, 128 >::serializer sser_t; typedef wjson::value< std::vector<char> >::serializer vser_t; typedef wjson::value< str_t >::serializer aser_t; std::string sstr; std::vector<char> vstr; str_t astr={'\0'}; // sser_t()( sstr, english, english + std::strlen(english), 0); vser_t()( vstr, russian, russian + std::strlen(russian), 0); aser_t()( astr, chinese, chinese + std::strlen(chinese), 0); // std::cout << "English: " << sstr << "\tfrom JSON: " << english << std::endl; std::cout << "Russian: " << std::string(vstr.begin(), vstr.end() ) << "\tfrom JSON: " << russian << std::endl; std::cout << "Chinese: " << astr << "\tfrom JSON: " << chinese << std::endl; // english stdout std::cout << std::endl << "English JSON: "; sser_t()( sstr, std::ostream_iterator<char>( std::cout) ); std::cout << "\tfrom: " << sstr; // russian stdout std::cout << std::endl << "Russian JSON: "; vser_t()( vstr, std::ostream_iterator<char>( std::cout) ); std::cout << "\tfrom: " << std::string(vstr.begin(), vstr.end() ); // chinese stdout std::cout << std::endl << "Chinese JSON: "; aser_t()( astr, std::ostream_iterator<char>( std::cout) ); std::cout << "\tfrom: " << astr; std::cout << std::endl; }
çµæïŒ
English: hello world! from JSON: "hello world!"
Russian: ! from JSON: "\u041F\u0440\u0438\u0432\u0435\u0442\u0020\u043C\u0438\u0440\u0021"
Chinese: äžçäœ å¥œ! from JSON: "\u4E16\u754C\u4F60\u597D!"
English JSON: "hello world!" from: hello world!
Russian JSON: " !" from: !
Chinese JSON: "äžçäœ å¥œ!" from: äžçäœ å¥œ!
é å
JSONé åãèšè¿°ããããã«ãwjson :: valueã«äŒŒãæ§é ã䜿çšãããŸãã
template<typename T, int R = -1> struct array { typedef T target; typedef implementation_defined serializer; };
ããã§ãTã¯ã·ãªã¢ã«åå¯èœãªã³ã³ãããŒã§ãããRã¯ãã®ã¡ãœããããµããŒãããstlã³ã³ãããŒã®äºçŽã®ãµã€ãºã§ãã ãã¹ãŠãåçŽã«æããŸããã次ã®åœ¢åŒã®ã¬ã³ãŒãïŒwjson :: array <std :: vector <int >>ã¯æ©èœããŸããã ã³ã³ããèŠçŽ ïŒãã®å Žåã¯intïŒãã·ãªã¢ã«åããæ¹æ³ã¯ããããŸããã æ£ãããšã³ããªã¯æ¬¡ã®ããã«ãªããŸãã
typedef wjson::array< std::vector< wjson::value<int> > > vint_json;
ãã©ã¡ãŒã¿ãŒTãšããŠãå¿ èŠãªã³ã³ãããŒãæž¡ããŸãããã³ã³ãããŒèŠçŽ ã®ã¿ã€ãã®ä»£ããã«ãJSONèšè¿°ãæž¡ããŸãã 以äžããµããŒããããŠããŸãã
- V [N]
- std ::ãã¯ãã«
- std :: deque
- std ::é å
- std ::ãªã¹ã
- std ::ã»ãã
- std ::ãã«ãã»ãã
- std :: unordered_set
- std :: unordered_multiset
ãã¡ãããæåã®4ã€ã®ãªãã·ã§ã³ã¯æ倧ã®ããã©ãŒãã³ã¹ãæäŸããŸãã ãªã¹ããšé£æ³ã³ã³ããã®å ¥åã¯ãããèªäœãé«ãããŸãã
å€å žçãªs-arrayã®äŸïŒ
typedef wjson::value<int> int_json; typedef int vint_t[3]; typedef wjson::array< int_json[3] > vint_json;
ãã¡ããã次ã®äŸã«ç€ºãããã«ãå€æ¬¡å é åããµããŒããããŠããŸãïŒããšãã°ããã¯ãã«ã®ãã¯ãã«ãªã©ïŒã
ãã¯ãã«ãã¯ãã«ã®äŸ
#include <wjson/json.hpp> #include <wjson/strerror.hpp> #include <iostream> int main() { // typedef wjson::value<int> int_json; typedef std::vector<int> vint_t; typedef wjson::array< std::vector<int_json> > vint_json; std::string json="[ 1,\t2,\r3,\n4, /**/ 5 ]"; vint_t vint; vint_json::serializer()(vint, json.begin(), json.end(), NULL); json.clear(); vint_json::serializer()(vint, std::back_inserter(json)); std::cout << json << std::endl; // ( ) typedef std::vector< vint_t > vvint_t; typedef wjson::array< std::vector<vint_json> > vvint_json; json="[ [], [1], [2, 3], [4, 5, 6] ]"; vvint_t vvint; vvint_json::serializer()(vvint, json.begin(), json.end(), NULL); json.clear(); vvint_json::serializer()(vvint, std::back_inserter(json)); std::cout << json << std::endl; // ( ) typedef std::vector< vvint_t > vvvint_t; typedef wjson::array< std::vector<vvint_json> > vvvint_json; json="[ [[]], [[1]], [[2], [3]], [[4], [5, 6] ] ]"; vvvint_t vvvint; vvvint_json::serializer()(vvvint, json.begin(), json.end(), NULL); json.clear(); vvvint_json::serializer()(vvvint, std::back_inserter(json)); std::cout << json << std::endl; }
ããã§ã¯ãJSONæååãååŸãããããã³ã³ããã«ãã·ãªã¢ã©ã€ãºããã¯ãªã¢ããåãæååãšåºåã«ã·ãªã¢ã«åããŸãã
[1,2,3,4,5] [[],[1],[2,3],[4,5,6]] [[[]],[[1]],[[2],[3]],[[4],[5,6]]]
jsonè¡ã¯ãã©ã€ã³ãã£ãŒããCã¹ã¿ã€ã«ã®ã³ã¡ã³ããªã©ãé åã®èŠçŽ éã«ç©ºçœãååšããå¯èœæ§ãããããšã瀺ããŠããŸããããã¯jsonæ§æãå®è£ ãããšãã«éåžžã«äŸ¿å©ã§ãã
åçã³ã³ããã®æ倧ãµã€ãºã«å¶éã¯ãããŸãããs-arraysããã³std :: arrayã®å Žåãé åã®å®éã®ãµã€ãºã¯å¶éã§ãã çä¿¡JSONèŠçŽ ã®æ°ãå°ãªãå Žåãæ®ãã®èŠçŽ ã«ã¯ããã©ã«ãå€ãå ¥åããããã以äžã®å ŽåãäœåãªèŠçŽ ã¯åçŽã«ç Žæ£ãããŸãã
JSONé
åã«ç°ãªãã¿ã€ãã®èŠçŽ ãå«ãŸããå Žå
JSONé åã«ããŸããŸãªã¿ã€ãã®èŠçŽ ãå«ãŸããå Žåããããã¯2段éã§ã·ãªã¢ã«åããã³éã·ãªã¢ã«åãããŸãã ãŸãã次ã®ããã«ãä»»æã®éã·ãªã¢ã«åãããŠããªãJSONæ§é ãå«ãæååã®ã³ã³ãããèšè¿°ããå¿ èŠããããŸãã
çã®JSONãèšè¿°ããã«ã¯ïŒ
JSONæååããã®ãŸãŸã³ã³ããTã«ã³ããŒããŸãããããŠãããŒãµãŒã䜿çšããŠãJSONèŠçŽ ã®ã¿ã€ããå€å¥ããããã«å¿ããŠãã·ãªã¢ã©ã€ãºããå¿ èŠããããŸãã 以äžã®äŸã§ã¯ãæ°å€ã®é å[1ã "2"ã[3]]ãèªã¿åã£ãŠããã¹ãŠã®èŠçŽ ãã€ã³ã¯ãªã¡ã³ããããã©ãŒããããç¶æããªãããããã·ãªã¢ã«åããããšããŠããŸãã
çµæïŒ
ããã¯ãåŸã§èª¬æãããªããžã§ã¯ããšèŸæžã§ãæ©èœããŸãã æ°åãæååãŸãã¯å®éã«ã¯æ°åã®2ã€ã®ãªãã·ã§ã³ã§ããè¡šçŸã§ããªãå Žåã¯ãã©ãããŒã䜿çšã§ããŸãã
ãã®ã³ã³ã¹ãã©ã¯ãã¯ãå®éã«ã¯ã©ã®JSONèšè¿°ã§ãæ©èœããŸãã SerQãã©ã¡ãŒã¿ãŒã¯ããã¥ã¢ã«ã·ãªã¢ã«åãæå¹ã«ããŸãã ããšãã°ãæ°å€ã®å Žåãããã¯åã«åŒçšç¬Šãæå³ããŸãã ReqQãã©ã¡ãŒã¿ãŒã«ã¯ãäºééã·ãªã¢ã«åãå«ãŸããŸãã å ¥åã«JSONæååãå¿ èŠã§ãã ãªãã®å Žåãã«ãŒã«ã¯ããå°ãè€éã§ãã å ¥åãJSONæååã§ãªãå Žåã¯ãäºåã®éã·ãªã¢ã«åãè¡ããã«ãJãã·ãªã¢ã©ã€ã¶ãŒãåã«éå§ããŸãã å ¥åãJSONæååã®å Žåãäžéã®std ::æååã«éã·ãªã¢ã«åãããŸãã Jãéæååãšã³ãã£ãã£ãèšè¿°ããå Žåãäžéstd ::æååããã®åã·ãªã¢ã«å解é€ã æååãšã³ãã£ãã£ã®å Žåãéã·ãªã¢ã«åãç¹°ãè¿ãå¿ èŠããããšå€æããŸãã ããã¯ãæåã®éã·ãªã¢ã«åã®åŸãäžéè¡ãåŒçšç¬Šã§å§ãŸãå Žåãããã¯äºéã·ãªã¢ã«åãããè¡ã§ãããåã³éã·ãªã¢ã«åãããããšãæå³ããŸãã
wjson :: quoted <>ã¯è¿œå ã®ãªãŒããŒããããäžããããšã¯æããã§ãããäœããã®çç±ã§ãã¯ã©ã€ã¢ã³ããæéãŸã§ã«çªå·ããã ãŸãããã·ãªã¢ã«åãããããã¹ãããããªããžã§ã¯ãã®äºéã·ãªã¢ã«åãè¡ãå ŽåãäžæçãªæŸèæãšèŠãªãããå¿ èŠããããŸãã
JSONé åã«ããŸããŸãªã¿ã€ãã®èŠçŽ ãå«ãŸããå Žåããããã¯2段éã§ã·ãªã¢ã«åããã³éã·ãªã¢ã«åãããŸãã ãŸãã次ã®ããã«ãä»»æã®éã·ãªã¢ã«åãããŠããªãJSONæ§é ãå«ãæååã®ã³ã³ãããèšè¿°ããå¿ èŠããããŸãã
typedef std::vector<std::string> vstr;
çã®JSONãèšè¿°ããã«ã¯ïŒ
template<typename T = std::string, int R = -1> struct raw_value;
JSONæååããã®ãŸãŸã³ã³ããTã«ã³ããŒããŸãããããŠãããŒãµãŒã䜿çšããŠãJSONèŠçŽ ã®ã¿ã€ããå€å¥ããããã«å¿ããŠãã·ãªã¢ã©ã€ãºããå¿ èŠããããŸãã 以äžã®äŸã§ã¯ãæ°å€ã®é å[1ã "2"ã[3]]ãèªã¿åã£ãŠããã¹ãŠã®èŠçŽ ãã€ã³ã¯ãªã¡ã³ããããã©ãŒããããç¶æããªãããããã·ãªã¢ã«åããããšããŠããŸãã
ã³ãŒã
#include <wjson/json.hpp> #include <wjson/strerror.hpp> #include <iostream> int main() { typedef std::vector< std::string > vect_t; typedef ::wjson::array< std::vector< ::wjson::raw_value<std::string> > > vect_json; vect_t inv; vect_t outv; std::string json = "[1,\"2\",[3]]"; std::cout << json << std::endl; vect_json::serializer()( inv, json.begin(), json.end(), 0 ); for ( auto& v : inv ) { outv.push_back(""); if ( wjson::parser::is_number(v.begin(), v.end()) ) { int num = 0; wjson::value<int>::serializer()( num, v.begin(), v.end(), 0); ++num; wjson::value<int>::serializer()( num, std::back_inserter(outv.back()) ); } else if ( wjson::parser::is_string(v.begin(), v.end()) ) { std::string snum; wjson::value<std::string>::serializer()( snum, v.begin(), v.end(), 0); int num = 0; wjson::value<int>::serializer()( num, snum.begin(), snum.end(), 0); ++num; snum.clear(); wjson::value<int>::serializer()( num, std::back_inserter(snum) ); wjson::value<std::string>::serializer()( snum, std::back_inserter(outv.back()) ); } else if ( wjson::parser::is_array(v.begin(), v.end()) ) { std::vector<int> vnum; wjson::array< std::vector< wjson::value<int> > >::serializer()( vnum, v.begin(), v.end(), 0); ++vnum[0]; wjson::array< std::vector< wjson::value<int> > >::serializer()( vnum, std::back_inserter(outv.back()) ); } else { outv.back()="null"; } } json.clear(); vect_json::serializer()( outv, std::back_inserter(json) ); std::cout << json << std::endl; }
çµæïŒ
[1,"2",[3]] [2,"3",[4]]
ããã¯ãåŸã§èª¬æãããªããžã§ã¯ããšèŸæžã§ãæ©èœããŸãã æ°åãæååãŸãã¯å®éã«ã¯æ°åã®2ã€ã®ãªãã·ã§ã³ã§ããè¡šçŸã§ããªãå Žåã¯ãã©ãããŒã䜿çšã§ããŸãã
template<typename J, bool SerQ = true, bool ReqQ = true, int R = -1> struct quoted;
- J-ãœãŒã¹JSONã®èª¬æ
- SerQ-æååã«äºåã·ãªã¢ã«å
- ReqQ-å ¥åJSONã¯ãæååãã§ãªããã°ãªããŸãã
- äžéãããã¡ãŒïŒè¡ïŒã®RäºçŽ
ãã®ã³ã³ã¹ãã©ã¯ãã¯ãå®éã«ã¯ã©ã®JSONèšè¿°ã§ãæ©èœããŸãã SerQãã©ã¡ãŒã¿ãŒã¯ããã¥ã¢ã«ã·ãªã¢ã«åãæå¹ã«ããŸãã ããšãã°ãæ°å€ã®å Žåãããã¯åã«åŒçšç¬Šãæå³ããŸãã ReqQãã©ã¡ãŒã¿ãŒã«ã¯ãäºééã·ãªã¢ã«åãå«ãŸããŸãã å ¥åã«JSONæååãå¿ èŠã§ãã ãªãã®å Žåãã«ãŒã«ã¯ããå°ãè€éã§ãã å ¥åãJSONæååã§ãªãå Žåã¯ãäºåã®éã·ãªã¢ã«åãè¡ããã«ãJãã·ãªã¢ã©ã€ã¶ãŒãåã«éå§ããŸãã å ¥åãJSONæååã®å Žåãäžéã®std ::æååã«éã·ãªã¢ã«åãããŸãã Jãéæååãšã³ãã£ãã£ãèšè¿°ããå Žåãäžéstd ::æååããã®åã·ãªã¢ã«å解é€ã æååãšã³ãã£ãã£ã®å Žåãéã·ãªã¢ã«åãç¹°ãè¿ãå¿ èŠããããšå€æããŸãã ããã¯ãæåã®éã·ãªã¢ã«åã®åŸãäžéè¡ãåŒçšç¬Šã§å§ãŸãå Žåãããã¯äºéã·ãªã¢ã«åãããè¡ã§ãããåã³éã·ãªã¢ã«åãããããšãæå³ããŸãã
wjson :: quoted <>ã¯è¿œå ã®ãªãŒããŒããããäžããããšã¯æããã§ãããäœããã®çç±ã§ãã¯ã©ã€ã¢ã³ããæéãŸã§ã«çªå·ããã ãŸãããã·ãªã¢ã«åãããããã¹ãããããªããžã§ã¯ãã®äºéã·ãªã¢ã«åãè¡ãå ŽåãäžæçãªæŸèæãšèŠãªãããå¿ èŠããããŸãã
ããŒãµãŒ
wjsonã«ã¯ã2ã€ã®ã¿ã€ãã«åé¡ã§ããéçã¡ãœããã®ã¿ãå«ãããŒãµãŒã¯ã©ã¹ããããŸãã ããã¯ç¹å®ã®JSONã¿ã€ããžã®æºæ ã®ãã§ãã¯ã§ããããããã£ãŠãã¡ãœããã¯ããŒãµãŒã§ãã åJSONã¿ã€ãã«ã¯ç¬èªã®ã¡ãœããããããŸãã
æ¹åŒãªã¹ã
class parser { /*...*/ public: template<typename P> static P parse_space( P beg, P end, json_error* e); template<typename P> static P parse_null( P beg, P end, json_error* e ); template<typename P> static P parse_bool( P beg, P end, json_error* e ); template<typename P> static P parse_number( P beg, P end, json_error* e ); template<typename P> static P parse_string( P beg, P end, json_error* e ); template<typename P> static P parse_object( P beg, P end, json_error* e ); template<typename P> static P parse_array( P beg, P end, json_error* e ); template<typename P> static P parse_value( P beg, P end, json_error* e ); /*...*/ };
ãã·ãªã¢ã©ã€ã¶ãŒãšåæ§ã«ãããã§ã®begã¯ãããã¡ãŒã®å§ãŸãããããã¡ãŒã®çµãããããã³nullptrã§ãªãå Žåã¯ãeãã§ãšã©ãŒã³ãŒããæžã蟌ãŸããŸãã æåããå ŽåãçŸåšã®ãšã³ãã£ãã£ã®æåŸã®æåã«ç¶ãæåãžã®ãã€ã³ã¿ãè¿ãããŸãã ãšã©ãŒãçºçããå Žåãendãè¿ãããeãåæåãããŸãã
ç¹å®ã®æ§é ã®è€æ°ã®JSONãªããžã§ã¯ããå«ãæååããããããããæ¹è¡ãŸãã¯ä»ã®ç©ºçœã§åºåãããŠããå Žåã次ã®ããã«åŠçã§ããŸãïŒãšã©ãŒåŠçãªãïŒã
for (;beg!=end;) { beg = wjson::parser::parse_space(beg, end, 0); beg=my_json::serializer()(dict, beg, end, 0); /* âŠ. */ }
ãã¹ãŠã®ã·ãªã¢ã©ã€ã¶ã¯ãæåã®æåããã·ãªã¢ã©ã€ãºå¯èœãªãªããžã§ã¯ãã®æåã§ãªããã°ãªããªãããšãåæãšããŠããŸããããããªããšãšã©ãŒãçºçããŸãã ãããããã§ã«è¿°ã¹ãããã«ããªããžã§ã¯ããšé åã®å éšã«ã¯ããã·ãªã¢ã©ã€ã¶ãŒãåãparse_spaceã§è§£æããã³ã¡ã³ããå«ã空çœæåãå«ãŸããå ŽåããããŸãã è€æ°ã®JSONãšã³ãã£ãã£ã§æååã解æããäŸïŒ
wjson::json_error e; for (;beg!=end;) { beg = wjson::parser::parse_space(beg, end, &e); beg = wjson::parser::parse_value(beg, end, &e); if ( e ) abort(); }
ããã§ãparse_valueã¯JSONãšã³ãã£ãã£ã®åŠ¥åœæ§ããã§ãã¯ããŸãã å ¥åparse_spaceã空çœã§ãªãå ŽåãåçŽã«begãè¿ããŸãã ããšãã°ãéããããŠããªãCã¹ã¿ã€ã«ã®ã³ã¡ã³ããèŠã€ãã£ãå Žåããšã©ãŒãè¿ãããšããããŸãããããã§ã¯è¿œå ã®ãã§ãã¯ã¯äžèŠã§ãã åæåããããšã©ãŒãªããžã§ã¯ããããŒãµãŒãžã®å ¥åã«å°çããå ŽåïŒãã·ãªã¢ã©ã€ã¶ãŒãšåæ§ïŒãåã«endãè¿ããŸãã
ç¹å®ã®JSONãšã³ãã£ãã£ãå®çŸ©ããã«ã¯ã次ã®ã¡ãœããã»ããããããŸãã
æ¹åŒãªã¹ã
class parser { /*...*/ public: template<typename P> static bool is_space( P beg, P end ); template<typename P> static bool is_null( P beg, P end ); template<typename P> static bool is_bool( P beg, P end ); template<typename P> static bool is_number( P beg, P end ); template<typename P> static bool is_string( P beg, P end ); template<typename P> static bool is_object( P beg, P end ); template<typename P> static bool is_array( P beg, P end ); };
ãããã¡ã®å é ãšæ«å°Ÿãžã®ãã€ã³ã¿ãååŸãããšããäºå®ã«ããããããããããã®ã¡ãœããã¯æåã®æåã§ãšã³ãã£ãã£ã決å®ããŸãã{ã¯ãªããžã§ã¯ãã[ã¯é åã "ã¯æååãæ°åã¯æ°åãtãfãŸãã¯nã¯true ãfalseãŸãã¯nullã ãããã£ãŠãããšãã°is_objectãtrueãè¿ãå Žåããããæå¹ãªãªããžã§ã¯ãã§ããããšã確èªããã«ã¯ãparse_objectãåŒã³åºããŠãšã©ãŒããªãããšã確èªããå¿ èŠããããŸãã
ãšã©ãŒåŠç
éã·ãªã¢ã«åäžã®ãšã©ãŒãã§ãã¯ã¯ã»ãšãã©ã®å Žåå¿ èŠã§ãã äŸã§ã¯ãæ確ã«ããããã ãã«ãããè¡ããŸããã å€éšã®æåãå ã®é åã«åã蟌ãŸããŠããäŸãèããŠã¿ãŸãããã
#include <wjson/json.hpp> #include <wjson/strerror.hpp> #include <iostream> int main() { typedef wjson::array< std::vector< wjson::value<int> > >::serializer serializer_t; std::vector< int > value; std::string json = "[1,2,3}5,6]"; wjson::json_error e; serializer_t()(value, json.begin(), json.end(), &e ); if ( e ) { std::cout << "Error code: " << e.code() << std::endl; std::cout << "Error tail of: " << e.tail_of() << std::endl; if ( e.type() == wjson::error_code::ExpectedOf ) std::cout << "Error expected_of: " << e.expected_of() << std::endl; std::cout << "Error position: " << wjson::strerror::where(e, json.begin(), json.end() ) << std::endl; std::cout << "Error message: " << wjson::strerror::message(e) << std::endl; std::cout << "Error trace: " << wjson::strerror::trace(e, json.begin(), json.end()) << std::endl; std::cout << "Error message & trace: " << wjson::strerror::message_trace(e, json.begin(), json.end()) << std::endl; } }
å®éã«ã¯ãwjson :: json_errorãšã©ãŒãªããžã§ã¯ãèªäœã«ã¯ããšã©ãŒã³ãŒãã«é¢ããæ å ±ãšãããŒãµãŒãäžæŽåãæ€åºãããããã¡ãŒã®æ«å°Ÿã«å¯Ÿããäœçœ®ãå«ãŸããŠããŸãã ç¹å¥ãªã¿ã€ããExpected ofãã®ãšã©ãŒã®å Žåã圌ãæåŸ ãããã£ã©ã¯ã¿ãŒã
èªã¿åãå¯èœãªã¡ãã»ãŒãžãååŸããã«ã¯ãwjson :: strerrorã¯ã©ã¹ã䜿çšããŸãã äžèšã®äŸã§ã¯ãJSONé åã§}æåãæ€åºãããããŒãµãŒã¯ã³ã³ãïŒãŠã§ã«ããŸãã¯è§æ¬åŒ§ïŒãäºæãããããå ±åããŸãã ãã®äŸã¯ããšã©ãŒåæã«äœ¿çšå¯èœãªãã¹ãŠã®ã¡ãœããã瀺ããŠããŸãã çµæã¯æ¬¡ã®ãšããã§ãã
Error code: 3 Error tail of: 5 Error expected_of: , Error position: 6 Error message: Expected Of ',' Error trace: [1,2,3>>>}5,6] Error message & trace: Expected Of ',': [1,2,3>>>}5,6]
ãããã£ãŠããšã©ãŒã³ãŒããèªã¿åãå¯èœãªã¡ãã»ãŒãžã ãã§ãªãããããçºçããå ŽæãååŸã§ããŸãããã¬ãŒã¹æã«ã¯ãã>>>ãã®çµã¿åããã䜿çšãããŸãã
JSONãªããžã§ã¯ã
JSONãªããžã§ã¯ããããŒã¿æ§é ã«çŽæ¥ãã·ãªã¢ã©ã€ãºããããšããwjsonã®èšèšç®çã§ããåçŽãªæ§é ãèããŠã¿ãŸãããã
struct foo { bool flag = false; int value = 0; std::string string; };
JSONã¿ã€ãã§ã·ãªã¢ã«åããå¿ èŠããããŸãïŒ
{ "flag":true, "value":42, "string":" !"}
JSONãªããžã§ã¯ãã¯ãã³ãã³ã§åºåãããååãšå€ïŒä»»æã®JSONïŒã§æ§æããããã£ãŒã«ãã®ãªã¹ãã®åãªãåæã§ããåäžã®ãã£ãŒã«ããã·ãªã¢ã«åããã«ã¯ãã³ã³ãã€ã«æ®µéã§æ¢ç¥ã®ååãã³ããŒããã³ãã³ãè¿œå ããŠãå€ãã·ãªã¢ã«åããå¿ èŠããããŸããèšèšã¯ãã®æŠå¿µãå®è£ ããŸãã
template<typename N, typename T, typename M, MT::* m, typename J = value<M> > struct member;
- N-ãã£ãŒã«ãå
- T-æ§é ã®ã¿ã€ã
- M-ãã£ãŒã«ãã¿ã€ã
- m-æ§é äœãã£ãŒã«ããžã®ãã€ã³ã¿ãŒ
- J-JSONãã£ãŒã«ãã®èª¬æ
ãã ãããã³ãã¬ãŒããã©ã¡ãŒã¿ã䜿çšããŠæååãæ瀺çã«æž¡ãããšã«ã¯åé¡ããããŸãããããã£ãŠã次ã®ããªãã¯ã䜿çšããŸããæ§é ãã£ãŒã«ãåããšã«ã次ã®åœ¢åŒã®æ§é ãäœæããŸãã
æã®åå
struct n_flag { const char* operator()() const { return âflagâ; } };
ãã³ãã¬ãŒããã©ã¡ãŒã¿ã§æž¡ãããšãã§ããŸãããã¡ãããååããšã«ãã®ãããªæ§é ãäœæããããšã¯ããŸã䟿å©ã§ã¯ãªãããããŸãã«ãã¯ã眮æãèš±å¯ããå ŽåããããŸãããããè¡ãã«ã¯ããã¯ãã䜿çšã§ããŸãã
JSON_NAME(flag)
ã»ãŒåãæ§é ãäœæãããŸããn_ãã¬ãã£ãã¯ã¹ã¯æŽå²çãªçç±ã§äœ¿çšãããŸãããã ããæ°ã«å ¥ããªãå Žåã¯ã2çªç®ã®ãªãã·ã§ã³ã䜿çšã§ããŸãã
JSON_NAME2(n_flag, âflagâ)
ããã«ãããä»»æã®ååãšæååã§æ§é äœãäœæã§ããŸããåäžã®ãã£ãŒã«ãã説æããäŸïŒ
wjson::member< n_flag, foo, bool, &foo::flag>
åçŽåã®å ŽåãJSONã®èª¬æïŒwjson :: value <>ïŒã¯çç¥ã§ããŸãããä»ã®ãã¹ãŠã®å Žåã¯å¿ é ã§ããæ§é äœãã£ãŒã«ãèªäœã®ã·ãªã¢ã«åã¯ããŸãæå³ããªãããã次ã®ããã«ãã¹ãŠã®ãã£ãŒã«ãã®èª¬æããªã¹ãã«ãŸãšããå¿ èŠããããŸãã
wjson::member_list< wjson::member<n_flag, foo, bool, &foo::flag>, wjson::member<n_value, foo, int, &foo::value>, wjson::member<n_string, foo, std::string, &foo::string> >
C ++ 11ã®å Žåããã£ãŒã«ãã®æ°ã¯å¶éãããŸãããc++ 03ã®å Žåã26åã®èŠçŽ ã®å¶éãããããã¹ããããmember_listã䜿çšããŠç°¡åã«åé¿ã§ããŸããJSONãªããžã§ã¯ããæ§é ã«ã·ãªã¢ã«åããããã®èŠåã¯ã次ã®æ§æã«ãã£ãŠæäŸãããŸãã
template<typename T, typename L> struct object { typedef T target; typedef implementation_defined serializer; typedef implementation_defined member_list; };
ããã§ãTã¯ããŒã¿æ§é ã®ã¿ã€ããLã¯ã·ãªã¢ã«åå¯èœãªãã£ãŒã«ãã®ãªã¹ãïŒmember_listïŒã§ãã
JSONãªããžã§ã¯ãã®ã·ãªã¢ã©ã€ãºããã³ãã·ãªã¢ã©ã€ãºã®äŸ
#include <wjson/json.hpp> #include <wjson/strerror.hpp> #include <iostream> struct foo { bool flag = false; int value = 0; std::string string; }; JSON_NAME(flag) JSON_NAME(value) JSON_NAME(string) typedef wjson::object< foo, wjson::member_list< wjson::member<n_flag, foo,bool, &foo::flag>, wjson::member<n_value, foo,int, &foo::value>, wjson::member<n_string, foo,std::string, &foo::string> > > foo_json; int main() { std::string json="{\"flag\":false,\"value\":0,\"string\":\" \"}"; foo f; foo_json::serializer()( f, json.begin(), json.end(), nullptr ); f.flag = true; f.string = " "; std::cout << json << std::endl; foo_json::serializer()( f, std::ostream_iterator<char>(std::cout) ); }
çµæïŒ
{"flag":false,"value":0,"string":" "} {"flag":true,"value":0,"string":" "}
ç§ãããªãã«æ³šæãåŒãããã®ã¯ïŒ
- å ã®æ§é ïŒfooïŒã«ã¯æ°žç¶çã§ãããšããèšè¿°ã¯ãããŸããã
- ãã£ãŒã«ãã¯ãmember_listã«èšè¿°ãããŠããé åºã§æ£ç¢ºã«ã·ãªã¢ã«åãããŸãã
- JSON , member_list
- .
- JSON
- member_list
- , ( )
å ¥åJSONã§ãã£ãŒã«ãã®é åºãJSONèšè¿°ã®é åºãšäžèŽããå Žåãå®éã«ã¯1åã®ãã¹ã§éã·ãªã¢ã«åãå¯èœãªéãè¿ éã«è¡ãããŸããäžè¶³ããŠãããã£ãŒã«ããããã©ãŒãã³ã¹äžã®äœåãªèŠçŽ ã¯ã倧ããªåœ±é¿ãäžããŸããïŒãããã¯åã«ç¡èŠãããŸãïŒã
ããããJSONã®ãã£ãŒã«ããã©ã³ãã ãªé åºã§æ¥ããšã©ããªããŸããïŒãã¡ãããããã¯ããã©ãŒãã³ã¹ã«åœ±é¿ããŸããããã¯ãããŒãµãŒãæ··ä¹±ããæåã«ãã£ãŒã«ããå埩åŠçããããã§ãããããããã£ãŒã«ããªãŒããŒã®ãããã¯ããŸã£ããæ°ã«ããªãããšããå§ãããŸãã
å®éã«æãå§ããåã§ãã£ãŠãããã·ãªã¢ã©ã€ãºã®æéã§ã¯ãªããJSONã®åé·æ§ã®åé¡ã«ééããããŒã¿äº€æãã©ãŒãããã®å€æŽã«ã€ããŠèããå¿ èŠããããŸããããã¯ãå¿ ããããã€ããªãããã³ã«ã«åãæ¿ããããšãæå³ããŸãããããšãã°ãJSONé åã®åœ¢åŒã§ãªããžã§ã¯ãã転éããããšãã§ããŸãããã®å Žåãäœçœ®ã¯æ§é ã®ç¹å®ã®ãã£ãŒã«ãã«å³å¯ã«å¯Ÿå¿ããŸããç¹å¥ãªå Žåãå€ãã®ãŒããéä¿¡ãããå Žåããã®ãããªåœ¢åŒã¯protobufãããã³ã³ãã¯ãã§é«éã«ãªããŸãã
æ ¹æ ã«ãªããªãããã«ãããã©ãŒãã³ã¹ã®ããã«æ¬¡ã®æ§é ãã·ãªã¢ã©ã€ãŒãŒã·ã§ã³ãå®è¡ããŸããã
struct foo { int field1 = 0; int field2 = 0; int field3 = 0; std::vector<int> field5; };
fooã®JSONèšè¿°
JSON_NAME(field1) JSON_NAME(field2) JSON_NAME(field3) JSON_NAME(field5) typedef wjson::object< foo, wjson::member_list< wjson::member<n_field1, foo, int, &foo::field1>, wjson::member<n_field2, foo, int, &foo::field2>, wjson::member<n_field3, foo, int, &foo::field3>, wjson::member<n_field5, foo, std::vector<int>, &foo::field5, ::wjson::array< std::vector< ::wjson::value<int> > > > > > foo_json;
å ¥åJSONã®ãã£ãŒã«ãã®é æ¹åããã³éæ¹åïŒæã倱æããïŒã·ãŒã±ã³ã¹ããããããã®åŸã圌ã¯ãã£ãŒã«ãåãå®å šã«æ£çŽã§ã¯ãªãããšã«æ°ã¥ããŸãããæåŸã®æåã®äŸå€ãšäžèŽããããããªãã·ã§ã³ã®æž¬å®ãè¡ããŸããã
JSON_NAME2(n_field1, "1field") JSON_NAME2(n_field2, "2field") JSON_NAME2(n_field3, "3field") JSON_NAME2(n_field5, "5field")
ãã¹ãŠã®ãã£ãŒã«ããæåã®æåã§åºå¥ãããå ŽåãJSONã®èŠçŽïŒ
{"field1":12345,"field2":23456,"field3":34567,"field5":[45678,56789,67890,78901,89012]} {"5field":[45678,56789,67890,78901,89012],"1field":12345,"2field":23456,"3field":34567} {"field5":[45678,56789,67890,78901,89012],"field1":12345,"field2":23456,"field3":34567}
次ã®çµæãåŸãŸããïŒ
- ã·ãªã¢ã«åæéïŒ151321 nsïŒ6608468 persecïŒãä»ã§ã¯éèŠã§ã¯ãããŸãã
- ãæé©ãªãJSONã®éã·ãªã¢ã«åïŒ204113 nsïŒ4899246æ¯ç§ïŒ
- æé©ãªååãââæã€ãææªãã®ãã£ãŒã«ãé åºïŒ221140 nsïŒ4522022 persecïŒ
- ãæªããååãæã€ãã£ãŒã«ãã®ãææªã®ãé åºïŒ237616 nsïŒ4208470ããŒã»ã¯ïŒ
æ確ã«ããããã«ããŸãèšäºã®åé ã§åãäžãããããã¯sprintf / sscanfãéããããã«ããã®ãããªæ§æã®å®è¡æéã枬å®ããŸããã
sscanf( str, "{\"field1\":%d,\"field2\":%d,\"field3\":%d,\"field5\":[%d,%d,%d,%d,%d]}", &(f.field1), &(f.field2), &(f.field3), &(f.field5[0]), &(f.field5[1]), &(f.field5[2]),&(f.field5[3]), &(f.field5[4]) );
ããã§ã¯ãæ¬æ Œçãªãã·ãªã¢ã©ã€ãŒãŒã·ã§ã³ã®è©±ãã§ããªãããšã¯æããã§ã-ãã¿ãŒã³ãšã®ççŸã¯æ²æšãªçµæã«ã€ãªããå¯èœæ§ããããŸãããã ããçµæã¯2477942 nsïŒ403560 persecïŒã§ãããããã¯ãã¹ãŠã®ãã§ãã¯ã§wjsonããã10åæªãããæªããé åºãšãè¯ããªãããã£ãŒã«ãåã§ïŒ
ç®ãä¿¡ããŠããªãã£ãã確èªããã人ã®ããã«ãããã®æ°åïŒç§°è³ã«å€ããïŒãèšäºãèªãããšãªãïŒããã¯æè¿ãã«ããïŒãããã¯æé©åããªã³ã«ãªã£ãŠããå Žåã«ã®ã¿æ©èœããããšãããã«èŠåããŸãããããã°ã¢ãŒãã§ã¯ãæ°å€ããŸã£ããéã«ãããã«æªãå€ã«å€æŽããŸããåžžã«äœããç ç²ã«ããªããã°ãªããŸããã
ãã®ãããªãšã³ãã£ãã£ã®ã·ãªã¢ã«åã®é床ã«é¢ãã質åã¯æ°å¹ŽåãããããŸããããJSONã®åé·æ§ã®åé¡ãæã æµ®äžããŸãããã®åé¡ã解決ããããã«ãã€ã³ããã¯ã¹ã«ãã£ãŒã«ããå³å¯ã«é¢é£ä»ããããŠããé åã«æ§é äœãã·ãªã¢ã«åããããšãã§ããŸãã
typedef wjson::object_array< foo, wjson::member_list< wjson::member_array<foo, int, &foo::field1>, wjson::member_array<foo, int, &foo::field2>, wjson::member_array<foo, int, &foo::field3>, wjson::member_array<foo, std::vector<int>, &foo::field5, ::wjson::array< std::vector< ::wjson::value<int> > > > > > foo_json;
ãã®çµæãåãæ§é äœãé åã«ã·ãªã¢ã«åãããŸãã
[12345,23456,34567,[45678,56789,67890,78901,89012]]
139856 nsïŒ7150211 persecïŒã§ã131282 nsã§ãã·ãªã¢ã©ã€ãŒãŒã·ã§ã³ãçºçããŸãïŒ7617190ïŒ
ã¯ããé床ã«éãã¯ãããŸããããŸãã¯ããã³ã³ãã¯ãã§ãããµãŒããŒãã°ã©ãäœæçšã®ããŒã¿ãè¿ããããžã§ã¯ãã®1ã€ã§ã¯ãåãã€ã³ãã¯8ã€ã®ãã£ãŒã«ãã§èšè¿°ãããåã°ã©ãã§çŽ3000ã§ãããã°ã©ãèªäœã¯ç»é¢äžã§æ°åã«ãªãããšããããçµæã®JSONã¯æ°ã¡ã¬ãã€ãã«ãªãå¯èœæ§ããããŸããå ã®æ§é ã8ã€ã®èŠçŽ ã®é åã«ã·ãªã¢ã«åããããšã«ãããéä¿¡ããããã©ãã£ãã¯ã®éãå€§å¹ ã«åæžããã ãã§ãªããèªã¿ãããããŸããããããäžè¬ã«ããã¡ããããã¹ãŠã®APIãé åã«å€æããããšã¯ãå§ãã§ããŸããã
ç¶æ¿
äŸãšããŠæ¬¡ã®æ§é ã䜿çšããŠç¶æ¿ãæ€èšããŸãã
struct foo { bool flag = false; int value = 0; std::string string; }; struct bar: foo { std::vector<int> data; };
ç¶æ¿ãèšè¿°ããæ¹æ³ã¯2ã€ãããŸãã
typedef ::wjson::array< std::vector< ::wjson::value<int> > > vint_json; typedef wjson::object< bar, wjson::member_list< wjson::member<n_flag, foo,bool, &foo::flag>, wjson::member<n_value, foo,int, &foo::value>, wjson::member<n_string, foo,std::string, &foo::string>, wjson::member<n_data, bar, std::vector<int>, &bar::data, vint_json> > > bar_json;
ããã§ã芪ïŒãŸãã¯èŠªïŒãšçžç¶äººã®ãã£ãŒã«ããä»»æã®é åºã§é 眮ã§ããŸãããªãã·ã§ã³2ãããçŽæçïŒ
typedef wjson::object< foo, wjson::member_list< wjson::member<n_flag, foo,bool, &foo::flag>, wjson::member<n_value, foo,int, &foo::value>, wjson::member<n_string, foo,std::string, &foo::string> > > foo_json; typedef wjson::object< bar, wjson::member_list< wjson::base<foo_json>, wjson::member<n_data, bar, std::vector<int>, &bar::data, vint_json> > > bar_json;
åºæ¬ã¯ã©ã¹ã®åå¥ã®JSONèšè¿°ãäœæãããªã¹ãå ã®ä»»æã®å Žæã§foo_json :: member_listã®ãšã€ãªã¢ã¹ã§ããwjson :: base <foo_json>æ§æã䜿çšããŠå®è£ ããŸãã
ãã¹ãŠã®èŠçŽ ãæã€çŽ æŽãããäŸã
#include <wjson/json.hpp> #include <wjson/strerror.hpp> #include <iostream> struct foo { bool flag = false; int value = 0; std::string string; }; struct bar: foo { std::shared_ptr<foo> pfoo; std::vector<foo> vfoo; }; struct foo_json { JSON_NAME(flag) JSON_NAME(value) JSON_NAME(string) typedef wjson::object< foo, wjson::member_list< wjson::member<n_flag, foo,bool, &foo::flag>, wjson::member<n_value, foo,int, &foo::value>, wjson::member<n_string, foo,std::string, &foo::string> > > type; typedef type::serializer serializer; typedef type::target target; typedef type::member_list member_list; }; struct bar_json { JSON_NAME(pfoo) JSON_NAME(vfoo) typedef wjson::array< std::vector< foo_json > > vfoo_json; typedef wjson::pointer< std::shared_ptr<foo>, foo_json > pfoo_json; typedef wjson::object< bar, wjson::member_list< wjson::base<foo_json>, wjson::member<n_vfoo, bar, std::vector<foo>, &bar::vfoo, vfoo_json>, wjson::member<n_pfoo, bar, std::shared_ptr<foo>, &bar::pfoo, pfoo_json> > > type; typedef type::serializer serializer; typedef type::target target; typedef type::member_list member_list; }; int main() { std::string json="{\"flag\":true,\"value\":0,\"string\":\" \",\"vfoo\":[],\"pfoo\":null}"; bar b; bar_json::serializer()( b, json.begin(), json.end(), nullptr ); b.flag = true; b.vfoo.push_back( static_cast<const foo&>(b)); b.pfoo = std::make_shared<foo>(static_cast<const foo&>(b)); std::cout << json << std::endl; bar_json::serializer()(b, std::ostream_iterator<char>(std::cout) ); std::cout << std::endl; }
çµæïŒ
{"flag":true,"value":0,"string":" ","vfoo":[],"pfoo":null} {"flag":true,"value":0,"string":" ","vfoo":[{"flag":true,"value":0,"string":" "}],"pfoo":{"flag":true,"value":0,"string":" "}}
JSONãªããžã§ã¯ãã®èª¬æã¯å°ãç°ãªããŸãããæåã¯ãã€ã³ã¿ãŒã«ã€ããŠã§ããä»»æã®ãã€ã³ã¿ãŒãã·ãªã¢ã«åã§ããŸãããŒãã®å ŽåãnullãšããŠã·ãªã¢ã«åãããããã§ãªãå Žåã¯å€ã«ãã£ãŠã·ãªã¢ã«åãããŸãããŸããéã·ãªã¢ã«åã¯std :: shared_ptr <>ã«å¯ŸããŠã®ã¿å®è£ ãããŸããJSONãnullã®å Žåãnullptrã«ãªããŸãããã以å€ã®å Žåããªããžã§ã¯ããäœæãããéã·ãªã¢ã«åãçºçããŸããwjson :: pointerãšããŠèšè¿°ããªãã£ãèŠçŽ ã«ã€ããŠã¯ãnullãå ¥åã«å°çãããšãããã©ã«ãå€ã§äœæãããŸããããã¯ãé åãšåçŽåã«ãé©çšãããŸãã
å€æ°ã®ãã©ã¡ãŒã¿ãŒãæã€ã¹ã¯ãªãŒã³ãã³ãã¬ãŒãã¯ã©ã¹ãéžæããçç±
JSON- , , . , , . , :
:
ãªããªã deserializer , :
foo_json bar_json typedef, :
, . , , ., , typeid(T).name(). , , , , . , , :
, . . , , , ( ).
JSON- , , . , , . , :
template<typename J> struct deserealizer { typedef typename J::deserializer type; };
:
typedef deserealizer<bar_json>::type deser;
ãªããªã deserializer , :
error: no type named 'deserializer' in 'struct bar_json'
foo_json bar_json typedef, :
error: no type named 'deserializer' in 'struct wjson::object<bar, fas::type_list<wjson::member<n_flag, foo, bool, &foo::flag>, fas::type_list<wjson::member<n_value, foo, int, &foo::value>, fas::type_list<wjson::member<n_string, foo, std::basic_string<char>, &foo::string>, fas::type_list<wjson::member<n_vfoo, bar, std::vector<foo>, &bar::vfoo, wjson::array<std::vector<wjson::object<foo, fas::type_list<wjson::member<n_flag, foo, bool, &foo::flag>, fas::type_list<wjson::member<n_value, foo, int, &foo::value>, fas::type_list<wjson::member<n_string, foo, std::basic_string<char>, &foo::string>, fas::empty_list> > > > > > >, fas::type_list<wjson::member<n_pfoo, bar, std::shared_ptr<foo>, &bar::pfoo, wjson::pointer<std::shared_ptr<foo>, wjson::object<foo, fas::type_list<wjson::member<n_flag, foo, bool, &foo::flag>, fas::type_list<wjson::member<n_value, foo, int, &foo::value>, fas::type_list<wjson::member<n_string, foo, std::basic_string<char>, &foo::string>, fas::empty_list> > > > > >, fas::empty_list> > > > > >'
, . , , ., , typeid(T).name(). , , , , . , , :
template<typename J> struct deserealizer { struct type: J::deserializer {}; };
, . . , , , ( ).
ãã¡ãããåãfoo_jsonãã€ã©ãŒãã¬ãŒããäœæãããã³ãã¬ãŒãæ§é ãã·ãªã¢ã«åããããã«äœ¿çšã§ããå€ãã£ãŒã«ãã®ã¿ã€ããªã©ã®ãã©ã¡ãŒã¿ãŒãæž¡ãããšã¯èª°ãæ°ã«ããŸããã
èŸæž
é£æ³é åïŒããŒå€ïŒãã·ãªã¢ã«åããã«ã¯ãstd :: map <>ãªã©ã®èŸæžãå¿ èŠã§ããã»ãšãã©ã®JSONã©ã€ãã©ãªã¯ãã®ã¹ããŒã ã«åŸã£ãŠåäœããŸãããªããžã§ã¯ãã¯ããªãŒã«ãã·ãªã¢ã©ã€ãºãããããã調ã¹ãŠãå¿ èŠãªãã£ãŒã«ããæ€çŽ¢ãããªã©ããŸãããããŠãã·ãªã¢ã«åã®ããã«ãåçã«ãããåããå¿ èŠããããŸããããã©ãŒãã³ã¹ã®ç¹ã§ã¯ãããã¯æãå¹ççãªæ¹æ³ã§ã¯ãããŸããããããã£ãŠããã®std :: map <>ãããŒã¿æ§é ã§äœ¿çšããåã«ããããªãã§äœããã®æ¹æ³ã§å¯èœãã©ãããèããŠãã ããããã¡ãããèšå®ã«JSONã䜿çšããå Žåãããã¯é©çšãããŸããã
template<typename T, int R = -1> struct dict { typedef implementation_defined target; typedef implementation_defined serializer; };
ããã§ãæŠå¿µã¯é åã®æŠå¿µãšåãã§ã-Tã¯ããŒãšå€ã®JSONèšè¿°ããã©ã¡ãŒã¿ãŒãšããŠæã€é£æ³stlã³ã³ãããŒã§ãã äŸïŒ
typedef wjson::dict< std::map< wjson::value<std::string>, wjson::value<int> > > dict_json;
std :: map <std :: stringãint>ã®ã·ãªã¢ã«åã«äœ¿çšã§ããŸããèšèšã¯éåžžã«è€éã§ãããæååãããŒãšããŠæãé »ç¹ã«äœ¿çšããããšããäºå®ãèæ ®ãããšãstd :: map <std :: stringãJSON>ã«ã¯ããç°¡åãªãªãã·ã§ã³ããããŸãã
typedef wjson::dict_map< wjson::value<int> > dict_json;
ãã¡ããããã®èšäºã§äžèšã§èª¬æããJSONãšã³ãã£ãã£ã¯ãå€ãšããŠäœ¿çšã§ããŸãã
ãã©ã¡ãŒã¿ãŒRã¯ãã¿ã€ãstd :: vector <std :: pair <>>ãŸãã¯std :: deque <std :: pair <>>ã®ãã¢ã®é 次ã³ã³ãããŒã®äºçŽã®ãµã€ãºã決å®ããŸãããã£ãŒã«ãæ§é ã¯ãããŒãšå€ã®ãã¢ãèšè¿°ããããã«äœ¿çšãããŸãã
template<typename K, typename V> struct field;
ããã§ãKãšVã¯ããããããŒãšå€ã®JSONèšè¿°ã§ãã äŸïŒ
typedef wjson::dict< std::vector< wjson::field< wjson::value<std::string>, wjson::value<int> > >, 128 /* */ > dict_json;
ãã¢std :: vector <std :: pair <std :: stringãint >>ã®ãã¯ãã«ãã·ãªã¢ã«åããããã®èšèšã¯ãé«ããã·ãªã¢ã©ã€ãŒãŒã·ã§ã³ã¬ãŒããå¿ èŠãªå Žåã«äœ¿çšã§ããŸãããã¢ã®ãã¯ãã«ã¯ãstd :: mapãããã¯ããã«é«éã§åããããŸãïŒãã¡ãããå¿ èŠãªäºçŽãè¡ãããŠããå ŽåïŒããã®èšèšã¯ããã«è€éã§ãããããé »ç¹ã«äœ¿çšããããããç°¡åãªãªãã·ã§ã³ããããŸãã
typedef wjson::dict_vector< ::wjson::value<int> > dict_json; typedef wjson::dict_deque< ::wjson::value<int> > dict_json;
äŸïŒ
int main() { typedef std::vector< std::pair<std::string, int> > dict; typedef wjson::dict_vector< wjson::value<int> > dict_json; dict d; std::string json = "{\"\":1,\"\":2,\"\":3}"; std::cout << json << std::endl; dict_json::serializer()( d, json.begin(), json.end(), 0 ); d.push_back( std::make_pair("",4)); json.clear(); dict_json::serializer()( d, std::back_inserter(json) ); std::cout << json << std::endl; }
çµæïŒ
{"":1,"":2,"":3} {"":1,"":2,"":3,"":4}
èŸæžã¯ãæ§æã«äœ¿çšãããšäŸ¿å©ã§ããæãåçŽãªå Žåãããã¯åçŽãªããŒãšå€ã®é åã§ãããããŒã«ã¯æ§æå¯èœãªã³ã³ããŒãã³ãã®ååãå«ãŸããŸãããã ããã³ã³ããŒãã³ãã®æ§æãå¥ã®æ§é ã§åãåºããŠJSONèšè¿°ãäœæããã®ãé¢åã§ã¯ãªãå Žåã¯ãäœåãªã©ã³ã¿ã€ã ã³ãŒããåé€ãããã®çµæãåæåãšã©ãŒã®èª¬æãåé€ããŸããããã«ãïŒéçºæ®µéã§ïŒèªåãšãŠãŒã¶ãŒãåã¶ææ°ã®ãã£ãŒã«ãã»ããã䜿çšããŠæ§æãçæããããšãã§ããŸããããã«ããããŠãŒã¶ãŒã¯å»æ¢ããããšããããããã£ãæã€ããã¥ã¡ã³ãã®é¢é£æ§ãæ€èšŒã§ããŸãã
åæãšãã©ã°
äžè¬çã«ããŒã¿ã衚瀺ããããã®ããã¹ã圢åŒãäœæãããã®ãšåãçç±ã§ãããã¹ãè¡šçŸã§åæãã·ãªã¢ã«åããããšã¯çã«ããªã£ãŠããŸã-ããã¯èªã¿ãããã§ããé·ãååãä¹±çšããªãå Žåãå°ãªããšããæ°å€ãšããŠã®ã·ãªã¢ã«åãããé ããªãããµã€ãºãããã»ã©é«ããªãããšãããããŸãããããã®è¡ãŸã§èªãã å Žåãäžè¬çãªæŠå¿µå šäœãç解ã§ããå¯èœæ§ãé«ãã®ã§ãããšãã°ããã«ïŒ
åæåã·ãªã¢ã«åã®äŸ
#include <wjson/json.hpp> #include <wjson/strerror.hpp> #include <iostream> struct counter { typedef enum { one = 1, four = 4, five = 5, two = 2, three = 3, six = 6 } type; }; struct counter_json { JSON_NAME(one) JSON_NAME(two) JSON_NAME(three) JSON_NAME(four) JSON_NAME(five) JSON_NAME2(n_six, " !") typedef wjson::enumerator< counter::type, wjson::member_list< wjson::enum_value< n_one, counter::type, counter::one>, wjson::enum_value< n_two, counter::type, counter::two>, wjson::enum_value< n_three,counter::type, counter::three>, wjson::enum_value< n_four, counter::type, counter::four>, wjson::enum_value< n_five, counter::type, counter::five>, wjson::enum_value< n_six, counter::type, counter::six> > > type; typedef type::serializer serializer; typedef type::target target; typedef type::member_list member_list; }; int main() { typedef wjson::array< std::vector< counter_json > > array_counter_json; std::vector< counter::type > cl; std::string json = "[\"one\",\"two\",\"three\"]"; std::cout << json << std::endl; array_counter_json::serializer()( cl, json.begin(), json.end(), 0 ); cl.push_back(counter::four); cl.push_back(counter::five); cl.push_back(counter::six); array_counter_json::serializer()(cl, std::ostream_iterator<char>(std::cout) ); std::cout << std::endl; }
äŸã«ç€ºãããã«ãåæã¯1察1ã§ã·ãªã¢ã«åããå¿ èŠã¯ãããŸããããä»»æã®æååã«å«ããããšãã§ããŸãã çµæïŒ
["one","two","three"] ["one","two","three","four","five"," !"]
å®éãããã§ã®åæã¯äŸ¿å®äžã®ãã®ã§ãããä»»æã®æŽæ°åã䜿çšã§ããŸãã
次ã®JSONãæ€èšããŠãã ããã
{"code":1,"message":"Invalid JSON."}
ããã¯ãããçš®ã®JSON-RPCã¹ã¿ã€ã«ã®ãšã©ãŒã¡ãã»ãŒãžã§ããæããã«ãã¡ãã»ãŒãžã¡ãã»ãŒãžã¯ã³ãŒãã«çŽæ¥é¢é£ããŠããŸãããããã£ãŠãããã¹ããã£ãŒã«ããå«ãæ§é ãäœæããŠå ¥åããå¿ èŠã¯ãããŸãããããã§ååã§ãã
åæåã§ã¯ãªã2ã€
#include <wjson/json.hpp> #include <wjson/strerror.hpp> #include <iostream> enum class error_code { ValidJSON = 0, InvalidJSON = 1, ParseError = 2 }; struct error { int code = 0; }; struct code_json { JSON_NAME2(ValidJSON, "Valid JSON.") JSON_NAME2(InvalidJSON, "Invalid JSON.") JSON_NAME2(ParseError, "Parse Error.") typedef wjson::enumerator< int, wjson::member_list< wjson::enum_value< ValidJSON, int, static_cast<int>(error_code::ValidJSON)>, wjson::enum_value< InvalidJSON, int, static_cast<int>(error_code::InvalidJSON)>, wjson::enum_value< ParseError, int, static_cast<int>(error_code::ParseError)> > > type; typedef type::serializer serializer; typedef type::target target; typedef type::member_list member_list; }; struct error_json { JSON_NAME(code) JSON_NAME(message) typedef wjson::object< error, wjson::member_list< wjson::member< n_code, error, int, &error::code>, wjson::member< n_message, error, int, &error::code, code_json> > > type; typedef type::serializer serializer; typedef type::target target; typedef type::member_list member_list; }; int main() { error e; e.code = static_cast<int>(error_code::InvalidJSON); error_json::serializer()(e, std::ostream_iterator<char>(std::cout) ); std::cout << std::endl; }
çµæïŒ
{"code":1,"message":"Invalid JSON."}
ã·ãªã¢ã«åã¯ããããèŠããŸãããéã·ãªã¢ã«åã¯ã©ãã§ããïŒãã³ãŒããã¯2åãã·ãªã¢ã©ã€ãºãããŸãã1åã¯ãã³ãŒãããã£ãŒã«ãããã2åç®ã¯ãã¡ãã»ãŒãžããã£ãŒã«ãããã§ããerror_jsonã®åå¥ã®ããŒãžã§ã³ã¯ããã¡ãã»ãŒãžããã£ãŒã«ããªãã§éã·ãªã¢ã«åå°çšã«äœæã§ããŸãããã·ãªã¢ã«åäžã«è§£æããããããããã©ãŒãã³ã¹ã«å€§ããªåœ±é¿ã¯ãããŸããããŸãããã®æ©èœã䜿çšããŠãã³ãŒãã®å³å¯ãªæŽåæ§ãå確èªã§ããŸããããšãã°ãã¡ãã»ãŒãžå ã®ãã€ã³ããçå笊ã«çœ®ãæããå ŽåïŒ
e = error(); std::string json = "{\"code\":1,\"message\":\"Invalid JSON?\"}"; wjson::json_error ec; error_json::serializer()(e, json.begin(), json.end(), &ec );
次ã«ããšã©ãŒã衚瀺ãããŸãã
Invalid Enum: {"code":1,"message":">>>Invalid JSON?"}
åæã¯ãããããçš®é¡ã®ãã©ã°ã®çµã¿åããã«ãã䜿çšãããŸãããã·ãªã¢ã«åããããšãã§ããŸããããããã©ã®ãããªåœ¢ã§ïŒ2ã€ã®ã¡ãœãããæäŸãããŸãïŒé åãšããŠããŸãã¯æå®ãããåºåãæååãšããŠãåºåãæåã¯ãæåŸã®ãã©ã°ãã©ã¡ãŒã¿ãŒã«ãã£ãŠæå®ãããŸããã³ã³ã以å€ã®æåã¯æååã«ã·ãªã¢ã«åãããã³ã³ãã¯é åã«ã·ãªã¢ã«åãããŸãã以äžã®ãŠãŒã¢ã©ã¹ãªäŸã§ã¯ãäž¡æ¹ã®ãªãã·ã§ã³ã䜿çšãããŠããŸãïŒ
ç¥æ¯ã¯ç°è²ã®ã€ã®ãšäœãã§ãã
#include <wjson/json.hpp> #include <wjson/strerror.hpp> #include <iostream> template<char S> struct flags_json { JSON_NAME2(w1, "") JSON_NAME2(w2, "") JSON_NAME2(w4, "") JSON_NAME2(w8, "") JSON_NAME2(w16, "") JSON_NAME2(w32, "") typedef ::wjson::flags< int, wjson::member_list< wjson::enum_value< w1, int, 1>, wjson::enum_value< w2, int, 2>, wjson::enum_value< w4, int, 4>, wjson::enum_value< w8, int, 8>, wjson::enum_value< w16, int, 16>, wjson::enum_value< w32, int, 32> >, S > type; typedef typename type::serializer serializer; typedef typename type::target target; typedef typename type::member_list member_list; }; int main() { std::string json = "\" \""; int val = 0; flags_json<' '>::serializer()(val, json.begin(), json.end(), 0 ); std::cout << json << " = " << val << std::endl; std::cout << 63 << " = "; flags_json<' '>::serializer()(63, std::ostream_iterator<char>(std::cout) ); std::cout << std::endl; std::cout << 48 << " = "; flags_json<','>::serializer()(48, std::ostream_iterator<char>(std::cout) ); std::cout << std::endl; std::cout << 49 << " = "; flags_json<'|'>::serializer()(49, std::ostream_iterator<char>(std::cout) ); std::cout << std::endl; }
ããã§ã®èãæ¹ã¯ç°¡åã§ããç§ãã¡ã¯ãåäŸã®æã®è¡ã®ååèªãã察å¿ããæå³ãæã€ãã©ã°ãšããŠäœ¿çšããŸãããã©ã°ãçµã¿åããããšãããŸããŸãªãªãã·ã§ã³ãåŸãããŸãããŸããã¹ããŒã¹ãã»ãã¬ãŒã¿ãšããŠäœ¿çšããå Žåãããããã©ã°ã®ã»ããã§ããããšã¯ãŸã£ããæããã§ã¯ãããŸãããæåã«ããç°è²ã®ã€ã®ããããŸããããšããè¡ããã·ãªã¢ã©ã€ãºããŸããããã¯ã1 | 2 | 16 | 32 = 51ã®çµã¿åããã«å¯Ÿå¿ããã¹ããŒã¹ãšããŠã»ãã¬ãŒã¿ãŒã䜿çšããŸãã以äžã¯ãããŸããŸãªåºåãæåã䜿çšããã·ãªã¢ã«åã®äŸã§ããæããã«ã63ãŸã§ã®ãã¹ãŠã®æ°åã䜿çšã§ããŸã-ããã¯ãã¬ãŒãºå šäœã§ãã
çµæïŒ
" " = 51 63 = " " 48 = ["",""] 49 = "||"
ãããã«
ãã®åµé ã®çºå±ã®æŽå²ã«ã€ããŠã®é·ããŠéå±ãªç¡çœªã®ç©èªãå§ããªãããã«ãèªæã«è² ããªãããšã¯éåžžã«é£ããã§ãããããã£ãŠãç°¡åã«ããããæ§ç¯ãããŠããfaslibã®æŠå¿µã®ããã€ãã解決ããããã«ã2008幎ã«ã²ããŸãããŠæžãããŸããã 2009幎ãwjsonïŒåœæã¯ã³ããŒãããã³ãŒãã®ã³ã¬ã¯ã·ã§ã³ã§ããïŒãå®éšãããžã§ã¯ãã§äœ¿çšãããŸããããããããã€ã³ã¿ãŒãã§ãŒã¹ãæè»ã§ã¯ãªããäžè¬çã«ã²ã©ãããšãæããã«ãªããŸããã 2011幎ã«ã¯ãã°ããŒãã«ã§å æ¬çãã€æ£ããããšãè¡ãè©Šã¿ããããŸããããããŠãããã¯ã»ãšãã©èµ·ãããŸããããæŸæ£ãããŸãããåã幎ã«ãç§ãã¡ã¯ãã¹ãŠã®ãããžã§ã¯ããJSONã«å€æãå§ããŸãããçŸåšã®æ©èœã¯ãã¹ãŠã®ããŒãºã«å¯Ÿå¿ããŠãããã€ã³ã¿ãŒãã§ã€ã¹ã¯åå¿è ã§ãç°¡åã§ç解ããããããšãããããŸããã 2013幎以éãéåžžã«è² è·ã®é«ããããžã§ã¯ããå«ããã¹ãŠã®ãããžã§ã¯ããwjsonãšé£æºããŠããŸããäŸãã°cometããŒã¢ã³ã¯æ倧100äžã®åæã¢ã¯ãã£ãæ¥ç¶ããµããŒãã§ããçµ±èšåéã·ã¹ãã ã¯1ãã¹ãã§1.5 GBãè¶ ããJSON-RPCéç¥ãã°ã©ã€ã³ããã1ç§ãããæ倧450äžã®ããŸããŸãªã¡ããªãã¯ã®å€ãç»é²ããŸãã
ç§ãã¡ã¯JSONãæ§æãããããçš®é¡ã®ãã³ãããããŠãã¡ããåãååã§åäœããJSON-RPCãšã³ãžã³ãšäžç·ã«äœ¿çšããŸããããã«ã€ããŠã¯ããã«æ¬¡ã®èšäºã§èª¬æããŸãã
wjsonãšfaslibã¯ãwjsonãäŸåããŠãããããããŒã®ã¿ã®ã©ã€ãã©ãªã§ãããµã³ãã«ãšãã¹ããã³ã³ãã€ã«ããã«ã¯ïŒ
git clone https://github.com/migashko/faslib.git git clone https://github.com/mambaru/wjson.git # wjson cd faslib mkdir build cd build cmake .. # cd ../../wjson mkdir build cd build cmake -DWJSON_BUILD_ALL=ON .. make cd ./tests ctest
githubã®wjsonïŒgithub.com/mambaru/wjson