ÿØÿà JFIF    ÿÛ „ !.%+&8&+/1555$;@;4?.451 4,$,44444444444414444444444444444444444444444444444444ÿÀ  á á" ÿÄ     ÿÄ ?    !1AQaq"2‘¡±ÁðBRbrÑá#‚’¢²3S CñÿÄ   ÿÄ !    !1QAa‘2ÿÚ   ? 5˜Z¯V¦cø)›t/? z¨±>Õ5€¶‹Á¤·¼z¼Ü¬+ñ®v¤¨_ˆR­BFn©—˜ý®ç̝P8gýt·ÉSTŦˆìät?þé¼íìN/Þa)ì–í6ô… Ï¿øÃj´¿KÇü]ÿ ªô¹-eKànëÕHTx}ýSÜ›ÿ ”7Ø×&µ<¦  ¥ÑO¶[Ù¯ä¨ÞÃÿ PZ-¬;#õ|•oaÿ ©CìÞz3˜öː/¤­ñTûIØ}š^ mÓ%ªxˆ¥ÉŸu=Z+ISe¿45™¼u;ú&WØ÷€æßQ™®{|íx*TC“#ZŠìZ§²‹ 6pv…³¿¡äª*áZÐ%ÒOáˆo"x«OHk w±æ+¬V(kMúŸ5Vö«$ ÁrÏbàb57/luR ¸ÑÛj Òµì`Мq­û žICÀÊ•©4€Âcà¨Ï€O´<èÐ:›ù(Ë^L8þ‘ÍÌ#¸Ð_Ì©ÙK(Öz 4¬û+¸;ü’V’84‘¬ÃŽ:[â‡ÔÌáõp¢~§ªlæ£ö{®G>J¼"°‡7¯ÆÉèßû ‹É‹§ÁòÃýâßî ^ƾÙõ‹×óH#«LP½ïX=xÑÍ$|W?•~• îëÔ©ª‹ {ÝT…Kÿ ”hûâá)J*ö˜–ÔU;iÇ€/ ÆþjóZ\ýwØ=Ìm ºèËL9 ýèÆð/¨’¥öo=nË.%Îì ŽÕ¯È|{Oj²ƒE6e/ßdÄõ²Ìâ1O®ò×TsəԸhOMýíMˆ¿¼H˜l²,7Â¥#MF/Úf°Ö½± ¸–dr‹NýÊ íjqx{œÉ ä-È ¦ øÄër¨q°ð †nцýÑÄÆ’mä…n<0È™;ÁÝá¯ÁZƒ7FÀmì­ É&9ˆîéi¶ùN§Y• ÃZãAâ?•‡©‰ , ó¾IŸŠc1 4â&y­&pŠ­6;M À 0¹qç»p.á …ŸÅáK@%6·y6ƒ‰3?”úºŽ‰éX5ªPT §µ!=Mž«Ú½‹ÅgÂSâÉaþÓoö–¯ÁÔìR>5éÿ üs¶ÆUcÌ kÇR ]ÿ ù¬¼«VŽ;Â|‡~¢¦”ÏŰæ {L™Õ°Óv¹ò¸írޡעCÃ!íVÕ {¶»sŒNPg/ "uÕbkm²“$ďå¿é¹§°½æz¯6 †s¿!s–wÚÝ“™Œ °.ûj>·+™Òa…©Œ&rÝÎtÛë긪Ît’LAVp%c Úý[ÄzJ¾ÇàXXç@˜ó<êL]·T˜¾¥1Ó©V‡g´æ½¦Ý@¹óø!_@´ÞâSÁ —S3™•& ]@JHÚý©ZŽ €×æÔr»Áf!‡yÞ4Mv*èÓã_{‘åóUuљØ«Oïé*®EvÑ Œ÷‡U \"㪒ÍK+À 4“M¡ï:0¥5í!'<@î´”>Ç»&Z–ïCCV˜Ì5Šo&îhè.žû |ÓK©h$s6KìŒëã)¹hI¦GïOåóI;ììü#É$Š0…Ææ¥TØ.5­¾gn´ “ÂÖ\:hœ89G)J@„}œ:’Ò{/Š"¦_Æ×7Æ3VÇŠÊa]ÚŒÙ€Ä–=®uÁßâACZƒ§§£ Qnâ:«,×{tyø¬iÛcœÜÄ€H½ÄÍCk´÷šß .W'b¤Íåh]÷€=,Žv×cÚEÚHXJX¶îo¨FÒtèöŸ>ªª6[J®Fµ£sGÁeqõfe\íjÒÐïÄÐGˆe1Ø‹.Ø”‘Ëuø Y­ˆÜ ŽG|zùªüMpDnQWÄ”%JŠ™)â*p@Örš«ÕT2Ð%ˆG#ª„ ·¤!°ŸOTÂT¸aÚ%4&h™LµšØüÐ.F¿²ÐÞ_Ç‚¾ÅÃaÜ÷09Æ q€öy˜v‡85õN÷]¬äѼóS{°_MެúÔ#°Ç¸0åÞè2ëôPcvÆw9®ií1Ä8F™˜à‰´+‰Ik1òÝ7“Ñ×ÒsÝ\x‚h`ÞÑ`ó"|µEcý£n˜h`}GÞ !±ù²Ápü²ß6 0ïi󜵩SÈÇ7˜-ÕURO˜¦´f$ªž-Í6(œ}<„ éc øs]ŽŽ„*—¾ ìdŽ„)méª\¿êÎIg¾ØÞ~I#C/¼¼´EÁÈŽi8“©õådô·>euä ƒ'Ê×लR1ÉJE1ÐAát`t;ÇР%Ý<‡¥„ÍÆ`×Oyó)õiI€ñQaŸ4Ûù\áàaÃÔ¹HÃu¹*k€¦<„e S‡&õÏ B!ŽhüÞ`yj}mªf×\¿ Ç~æ­9‡û\՞Ǖg²1Žû5V7 !àöšm° c`ܬøÇìµÒ'P"?…´Ö,"§^•õލsÔ)6˜sæéÍR¼ ò|Sl”‹7 nPW Gòú÷½§O¯‡„l¡kSÞŒr½PÊ@æ¢pŽ-mÿ #Ÿ˜Àº¶Áä¦;ïÔæ$1££`“Õ>„—·ž)ßð³ñ#Ï Ô$¶œ‰ÊE‹À;÷º ¯«P:Ñ”8–IÊtpÞ3ª“>ê“þës4ò2OÏÕ­±zô†Õ§‰.÷ä¸;¿˜“'œ›žª}«Œ{ª±Ì 9ÔóÞÕ‡0 $íWV3Üì¬ —@kÝ4@¿r¼±½¬™›?øØæ´'Áé®CË3-g$˜ö‡×auÚi´Žp/êÛ æF›Ú2v‹ã¿¿,nB1̨ƃqÞa5͝@&Æû“él÷ \C²½UÍc ¯k×¢U ÖéQå™—-r wô ÞÏ<Ò=&=ÿ Ôê Òêˈt,i—;LîÜ á¸*ÚÃ1$êL•LÍ <É)ýÐà’ ;F™{ƒ™˜€&'}‚ãÄK`¡ÞT@I;®žZóè‚s’7®°›+§O­Åq©é»²9<Ô J ¼9O’HL»Ùïì¸rk¼Ž_ý‘TŸu[²ßÚŒ·ü÷B%¯E ŸÔX5êO´ Ç•€’I0 ÉJX` ñ¹õ%;µŸD‘«´€àwÒ™U ûئžÖö\×®×´8 ½‡ºÐÆÓ§?Àkmœ=;d5*@-ì0F Rªýš[Ü6âö̃ڸr*KA9· u*µæ£?U¸Âêí†8@¦X4 e-ò„0s{ HâUpU?¼mñRa°®a%Ð'tÉ×’\¾ÊÉ]t›h>·(Ë@R¼¡Ãt h}’O÷au<+nT…Ö…MӐ??Óe95 q>í/;&JSû °¯ÊéÞ øƒ*Ã2½Ài&:nôUl=¾¿5eˆ3”ñc|Ú2V”>„»&eE;«ÚäC p¢Û úy 9š[ŒÌx¼擼A&DåÒ¯ˆ¤ÀÌ;"˜ ÏQä¸åhÊ}Ûq«Û0WžÒ|»€ø®öCm5•\ÇÀ§Pe3£]0ÃàLDÉ‰1øªxjgwT‚÷¿LΨK‹›ùs—xˆÜ±µ kæ¸f‰‰ÜGk/LÛØ6d9ò¶ùA{ƒA3š/¬D¬khÓk‰`˜"㯒r¿±Óã jx‡°e}<Ñø\3y:'À•/h½Í€Ç4~g ?Û(¼]v‘ªlKÎâ~?O‚W%{Ì:“'©úNq¾›úo(X’¥¯ˆ nFê{Ç€ü?º'ë ø‹ì Þ09ŒÌç9Æ —ËC`j@ÓÄ(+a‹un¸#ÂꟋ{K`‘ÑÍÍ'à´»/Û,KW;Þ4²þð ï Nm|~fGÏ(…³Ã)«1ö­Õ ¥‡¨©ƒÃ™ü-s=à=U66Ï«Ýc蓦W¹íž®›nÔ%êÇìŒ<#Ü×84ån®Ð ÒåOC` ñânÑs‡¢ç 1õ%Îhì½Ã½® e:ݼUZo™`  ÅZŸŒÊ«ê1ÏÄo$q¹Þ€©ˆhÐÉä¯ñ[!…Ú˜àJ:x2$Íß&PåT£6ç— ‡Í*4Ýšçjÿ ‰É nófÐ ó(L5C•åÆ\rMÒ@ò }y-W}™üýVù—ú¢=Ù”c®‘< M ž ´Phr ¦©TD ‘ù.$´÷O‡‘V2Æò.=IUŒ=ž‡â¬i™aþÓåÙ?òUø'ØÖ•.~* šTŒ!•-×áºTâ®ä#õü'´ eýlYÅÓeÕKÂrT"CÚ@u!Óxƒ{š3€}1¿(r}%«nËamjÑ%ÑNEò v ˜à  σöK³,*º.àzù¨™Ó ÚçâU¦*¿ 9{%Ö¹ njûdaXöb) kÛÆ±ûÓ\°M7ˆÂ=û›ç¿Ã‚­V»Cg–8ÙêE- j)k$º`Ã-ùEýeBÆÇ]c¡°ñty&Òd0nõ'¡W+ƒ*|–øµFa\GQªEAÔp5\Ǽ·¼Ç8·õ -â§Ú[ ‡ uZeÖ 3}×d'+¹:ð+K†Û®s!Ï$úe€<Û”x)1»a­¡LC]¸µík…ÚàA»AYº{†ªS[¦5HÒ7ù --,ísòDØ€èk ÞÀîÜ ò@â( ËNˆë›4ô½•/¦o‡€Û7 ê•ÆêòðÜy'Án½µ á˜ݦ ndeo…[ì¶Ê,¥R³Ä=À±—–ß;£™´ñSâ*g§”ïaið‘Jå~™ÓÞ ß³Õ¢»8x埒²52>AÊb&-÷\7´éÄù€T˜,w;3{ï˜k…à¹ÄqÀ«œ{€\ ˆ¾[´¨јr &Úé„Ívˆ±8†¿]|¬ņ4I×pÞS1ÈÖz‰#Ìv‡G!YNògñ:màTz¢Ý1ô©^O=~ë|5Bã™ç•¼µõ•bÆ@úÕS¬ÈŒ#¬zünrŸ û” Z²•èðV"ÁHÚý©wÝ €7¼Ìu1hÑa3Éä û f$o¿É ™Ú›ÝçnpÒ3äÌ3†Í§,Äï]$‰/pê †«À¼¸e9­Æê_C]žƒ·ý·frÁN«, E=›Çq -‰öŒ:aÏ¿±í&£Í:-} 84‘ÿ eƒQÑeëSsuiA ³g㟥ú£?ÿ ʼn*”“÷aühe:ÊWa@ÒÞk±eØ] F Ô—r.åä˜ @ö¥ªZoÐýYL·¥S²G/‡ñ <~*ZÆ´è>JlòàÛÆ½ÿ 窘ìGN¢:I®KšJp/`íIÁÀõ#Ä-€ö­šµŒoF4|ÆQØÆ@Ì|£Ô…¢À{9˜è½Üó›€ôYÒÎYsið;ís¤€à²ˆ‚4qÉVŒI$ ‰"° æµ8cXGjœˏ¡Aâý•ËÜ¢ûï e·çLx']á"oÅÎê3¯Ç—¹”ó0nå‚âg{Œñ> S´˜îè°g238‚ãköÝfÚd´6Ò€;ò÷±¢™¼›º ¢Æ'¥Ðx'e¬ç ]bÈÆV¢ó‹kýBO ðÊâ$Ÿ!×T 3Mýמ žìٍàÌü‘8÷€àæØ8æ©6‰©L´«…oãpð„~Çk‰!ñ;‹”ÛžÍ àž±z Ÿôû øŸÝužÏ;ÿ #|u6™Þ¬ÚˆÐõA4¶â|ôl|Ê2ŽÇ¤ÝÅÇY.<#Aí.k§hóF‚”Y; M½Ö4hŸ4&›­¿tès´%FìL¥£Ãk‰ÇT¤haÁ¤ÚxfÉ`ÑìË›>i 3t‚:,–+^÷´–{Û–Nxi"x‘Ûg î¨>¥Õ܁ùZH,2Û“:8xÊ¢Çí9.É-Ìâã-=çjwµS˜dütžçwýGòú®®ûº_ˆýx$–¡ãøO EÚÛÏ÷R„×w+3£Á£öUMyR²¹âŒ°š›¸Ñãò9§Ó_Dl+Ùßc›úšGÅÌc†Ž!Ko=¶.‘Îÿ c²(2®V mª.ÿ ¹B›¹å ù„öŸSV>™ü¯$y:G¢Z×àøúdî¹û­·ýÇ´:•c LÍõi_‹ö+ÎæGÊè>OŠ•äž´§Þ{X}¨1ÚTc›»Qþ•êô°t¿OP?eæ~É{5]•ÙR£r5†nZ\ã@ &îJõ ¾àC°þV>fé¥/ü5ñÊIº_é5 ;e­h<@ Ä&æÃëE%;X,ÒãÆÞ`Oò¦kŸm#˜!ÀyÄ¢| óLšò¥Ä` ¶R=|ÈCâh5ò3DˆïF†ðÒ#ÅìÛœ?¸yhBãœí ZxßÎÄhºRK„`Þödvײ™ÀÈÑÒgŒuY w³%†ƒÓzõ ÖÏp‚dH®¦A´ù§»ÓÇMæ~)ˆð‡û:ù&Ä •vGD´À n ݇¼Ö8Fö óáà£~Ë¥x`oK|Ä?fxiØü%pìR>éò+Û±éÎ>núlFŤ'tq8LZÏvÃ?„¡ß±È⽆¯³íü@x|PöUäèØã¡ð‚ŒAìÏ"vÍwóŸÍ{ ý0.z È•Ö{,N¡£¡ŸKÕÙž>Ýœþ ÍÀ°<×EA!Å‚D™IúOÍ¡>ôG}Â` ÍßkÜL™Ž Þð™ {IøF²¹òQ3&!ÃÂÞz.d&Ï-sH¸,Ôõ˜ŽP€ 77ˆÝ¼ÊëÜw =cÕ Ú,ØÐ5ÎYÐ)ì´öœgŒ[¤ßv㙑8心>h]§µháYš£²ºÑ.{Ï7Sð•?´~×SÃKýJÛ˜ ™Íäiúu<µX¶1õ^kâçIÑ£sZ4h>j*ÔšD:4­¿_ ÷¸ Õxæÿ ¸?Mù _•­ÊÐ ä ÷ý ÑwL œ­ïnTkÛUÍN©ë:¦fV ¶ÜÔÜMªÅâA½–¿R×TXš-%iTÊT•‡Ù‚JôϐZxWÑè‰f‰òG º ×Õû2aZ7OU3[“×AT–ÞŒ…-‘¤”Ì ì&(ˆ¿­•ƒkï’:ðY¦W‘ Å)“†‘˜³Åtcø˜ñTÂwÚÇ4|üLÇªí–v- qˆèU qPE.†â‘˜µ Æ,ÐÅs]8¾„oúÑ i>ÜxxÈó)ƒ ´æÁâØ$À‰vžŸf$Ž |ãw;ÀÁIJ»b` {¦Ó¤Ú$©YÀ‘n@Óïž«9J¼êG m¤ ܯ¹ÌW4€ÐÒÅÛ‡#褕Ÿn-?í|с¥÷Ú¹¬'´ÞÜ9ÓK `hê£SÄSà?7—Wí_´…óB›»:=Ãïq`<8ñÓŒÑlú2d¬ê³£hÖ[l|$vÝro~'R®‰§°ñmY ͧäP |PUª¹·:3Œ[Û{Xÿ ºâ@‚W–Äé u‚ ¯´*=íή.pûÒdt @G‰¬ s¸ ëÉücr ÞæÑ¨Ê@>¤¢Ö±. Þ'¯°ÌME[YéïĵÂCå½ Ué©Áû'Ê9%eÔðNU”ë‘ÌsD3/®+UI˜9h.WC”빓$#:pz:YÓ ¿xž* ³$Í +$kñAŠ‹†¢ Uê>¸)_š¬÷©ßAÂÔb9ÇU ¯¾á•9¯ÏÏ÷O÷¼¼Fähal1‰3Ì[Ïr•´UCksNÐ] R‘¸¥H+§Šé†c©vÖÞ0iÓ76s†î!§=ß ¼~Ô'°Ãmäoäš³ªøi1úÉ)³yV8 CLÄØÁ‘WYïi€H6ÖÑiámø^ÈY´°Ñ7¥Û*—Ñ©L«Qƒï—Ùrÿ ›£Ð*š¸ˆL©ˆ$ˆ ÷¾D§9È®«qbqC)–ˆïv´çñsÑVT­Ø, <àïºÀO«Jý·õ àfPìð .wFšir´þ’2_Y *Æ€x\« ì€9š@ Ž|F⇥ˆkZ@hÖÄ0t¿-<“‹qµ¾*ZL¤Ú)&BJpÓF5=$„at*Zš$’ÑtdûÝRI1 2މ$€$I$#‰SÞ’Hë¬ï;Á$¡t$’`<(ñÇt)$‡Ð.Êf¢X’Kt=Éé$‚ˆªè¢oÝëòI%Rgcª÷ŠyI%¡‰ÿ !ñ)´õ $¤ Ô’IIGÿÙ--- -- Library methods for handling JSON data. It handles JSON encoding and -- decoding according to RFC 4627. -- -- There is a straightforward mapping between JSON and Lua data types. One -- exception is JSON NULL, which is not the same as Lua -- nil. (A better match for Lua nil is JavaScript -- undefined.) NULL values in JSON are represented by -- the special value json.NULL. -- -- @author Martin Holst Swende -- @author David Fifield -- @author Patrick Donnelly -- @copyright Same as Nmap--See https://nmap.org/book/man-legal.html -- Version 0.4 -- Created 01/25/2010 - v0.1 - created by Martin Holst Swende -- Heavily modified 02/22/2010 - v0.3. Rewrote the parser into an OO-form, to not have to handle -- all kinds of state with parameters and return values. -- Modified 02/27/2010 - v0.4 Added unicode handling (written by David Fifield). Renamed toJson -- and fromJson into generate() and parse(), implemented more proper numeric parsing and added some more error checking. local stdnse = require "stdnse" local string = require "string" local table = require "table" local unicode = require "unicode" local unittest = require "unittest" _ENV = stdnse.module("json", stdnse.seeall) local lpeg = require "lpeg"; local locale = lpeg.locale; local P = lpeg.P; local R = lpeg.R; local S = lpeg.S; local V = lpeg.V; local C = lpeg.C; local Cb = lpeg.Cb; local Cc = lpeg.Cc; local Cf = lpeg.Cf; local Cg = lpeg.Cg; local Cp = lpeg.Cp; local Cs = lpeg.Cs; local Ct = lpeg.Ct; local Cmt = lpeg.Cmt; -- case sensitive keyword local function K (a) return P(a) * -(locale().alnum + "_"); end local NULL = {}; _M.NULL = NULL; --- Makes a table be treated as a JSON Array when generating JSON -- -- A table treated as an Array has all non-number indices ignored. -- @param t a table to be treated as an array function make_array(t) local mt = getmetatable(t) or {} mt["json"] = "array" setmetatable(t, mt) return t end --- Makes a table be treated as a JSON Object when generating JSON -- -- @param t a table to be treated as an object function make_object(t) local mt = getmetatable(t) or {} mt["json"] = "object" setmetatable(t, mt) return t end -- Decode a Unicode escape, assuming that self.pos starts just after the -- initial \u. May consume an additional escape in the case of a UTF-16 -- surrogate pair. See RFC 2781 for UTF-16. local unicode_esc = P [[\u]] * C(locale().xdigit * locale().xdigit * locale().xdigit * locale().xdigit); local function unicode16 (subject, position, hex) local cp = assert(tonumber(hex, 16)); if cp < 0xD800 or cp > 0xDFFF then return position, unicode.utf8_enc(cp); elseif cp >= 0xDC00 and cp <= 0xDFFF then error(("Not a Unicode character: U+%04X"):format(cp)); end -- Beginning of a UTF-16 surrogate. local lowhex = unicode_esc:match(subject, position); if not lowhex then error(("Bad unicode escape \\u%s (missing low surrogate)"):format(hex)) else local cp2 = assert(tonumber(lowhex, 16)); if not (cp2 >= 0xDC00 and cp2 <= 0xDFFF) then error(("Bad unicode escape \\u%s\\u%s (bad low surrogate)"):format(hex, lowhex)) end position = position+6 -- consume '\uXXXX' cp = 0x10000 + (cp & 0x3FF) * 0x400 + (cp2 & 0x3FF) return position, unicode.utf8_enc(cp); end end -- call lpeg.locale on the grammar to add V "space" local json = locale { V "json"; json = V "space"^0 * V "value" * V "space"^0 * P(-1); -- FIXME should be 'V "object" + V "array"' instead of 'V "value"' ? value = V "string" + V "number" + V "object" + V "array" + K "true" * Cc(true)+ K "false" * Cc(false)+ K "null" * Cc(NULL); object = Cf(Ct "" * P "{" * V "space"^0 * (V "members")^-1 * V "space"^0 * P "}", rawset) / make_object; members = V "pair" * (V "space"^0 * P "," * V "space"^0 * V "pair")^0; pair = Cg(V "string" * V "space"^0 * P ":" * V "space"^0 * V "value"); array = Ct(P "[" * V "space"^0 * (V "elements")^-1 * V "space"^0 * P "]") / make_array; elements = V "value" * V "space"^0 * (P "," * V "space"^0 * V "value")^0; string = Ct(P [["]] * (V "char")^0 * P [["]]) / table.concat; char = P [[\"]] * Cc [["]] + P [[\\]] * Cc [[\]] + P [[\b]] * Cc "\b" + P [[\f]] * Cc "\f" + P [[\n]] * Cc "\n" + P [[\r]] * Cc "\r" + P [[\t]] * Cc "\t" + P [[\u]] * Cmt(C(V "xdigit" * V "xdigit" * V "xdigit" * V "xdigit"), unicode16) + P [[\]] * C(1) + (C(1) - P [["]]); number = C((P "-")^-1 * V "space"^0 * (V "hexadecimal" + V "floating" + V "integer")) / function (a) return assert(tonumber(a)) end; hexadecimal = P "0x" * V "xdigit"^1; floating = (V "digit"^1 * P "." * V "digit"^0 + V "digit"^0 * P "." * V "digit"^1) * (V "exponent")^-1; integer = V "digit"^1 * (V "exponent")^-1; exponent = S "eE" * (S "-+")^-1 * V "digit"^1; }; json = P(json); -- compile the grammar --- Parses JSON data into a Lua object. -- -- This is the method you probably want to use if you use this library from a -- script. --@param data a json string --@return status true if ok, false if bad --@return an object representing the json, or error message function parse (data) local status, object = pcall(json.match, json, data); if not status then return false, object; elseif object then return true, object; else return false, "syntax error"; end end --Some local shortcuts local function dbg(str,...) stdnse.debug1("Json:"..str, ...) end -- See section 2.5 for escapes. -- For convenience, ESCAPE_TABLE maps to escape sequences complete with -- backslash, and REVERSE_ESCAPE_TABLE maps from single escape characters -- (no backslash). local ESCAPE_TABLE = {} local REVERSE_ESCAPE_TABLE = {} do local escapes = { ["\x22"] = "\"", ["\x5C"] = "\\", ["\x2F"] = "/", ["\x08"] = "b", ["\x0C"] = "f", ["\x0A"] = "n", ["\x0D"] = "r", ["\x09"] = "t", } for k, v in pairs(escapes) do ESCAPE_TABLE[k] = "\\" .. v REVERSE_ESCAPE_TABLE[v] = k end end -- Escapes a string --@param str the string --@return a string where the special chars have been escaped local function escape(str) return "\"" .. string.gsub(str, ".", ESCAPE_TABLE) .. "\"" end --- Checks what JSON type a variable will be treated as when generating JSON -- @param var a variable to inspect -- @return a string containing the JSON type. Valid values are "array", -- "object", "number", "string", "boolean", and "null" function typeof(var) local t = type(var) if var == NULL then return "null" elseif t == "table" then local mtval = rawget(getmetatable(var) or {}, "json") if mtval == "array" or (mtval ~= "object" and #var > 0) then return "array" else return "object" end else return t end error("Unknown data type in typeof") end --- Creates json data from an object --@param obj a table containing data --@return a string containing valid json function generate(obj) -- NULL-check must be performed before -- checking type == table, since the NULL-object -- is a table if obj == NULL then return "null" elseif obj == false then return "false" elseif obj == true then return "true" elseif type(obj) == "number" then return tostring(obj) elseif type(obj) == "string" then return escape(obj) elseif type(obj) == "table" then local k, v, elems, jtype elems = {} jtype = typeof(obj) if jtype == "array" then for _, v in ipairs(obj) do elems[#elems + 1] = generate(v) end return "[" .. table.concat(elems, ", ") .. "]" elseif jtype == "object" then for k, v in pairs(obj) do elems[#elems + 1] = escape(k) .. ": " .. generate(v) end return "{" .. table.concat(elems, ", ") .. "}" end end error("Unknown data type in generate") end if not unittest.testing() then return _ENV end ---------------------------------------------------------------------------------- -- Test-code for debugging purposes below ---------------------------------------------------------------------------------- local TESTS = { { '{"a":1}', generates = '{"a": 1}', is = "object", test = function(o) return o["a"] == 1 end }, { '{"a":true}', generates = '{"a": true}', is = "object", test = function(o) return o["a"] == true end }, { '{"a": false}', generates = '{"a": false}', is = "object", test = function(o) return o["a"] == false end }, { '{"a": null \r\n, \t "b" \f:"ehlo"}', is = "object", test = function(o) return o["a"] == NULL end }, { '{"a\\"a":"a\\"b\\"c\\"d"}', generates = '{"a\\"a": "a\\"b\\"c\\"d"}', is = "object", test = function(o) return o['a"a'] == 'a"b"c"d' end }, { '{"foo":"gaz\\"onk", "pi":3.14159,"hello":{ "wo":"rl\\td"}}', is = "object", test = function(o) return ( o["foo"] == 'gaz"onk' and o["pi"] == 3.14159 and o["hello"]["wo"] == "rl\td" ) end }, { '{"a":1, "b":2}', is = "object", test = function(o) local j = generate(o) return ( -- order is random j == '{"a": 1, "b": 2}' or j == '{"b": 2, "a": 1}' ) end }, { '[1,2,3,4,5,null,false,true,"\195\164\195\165\195\182\195\177","bar"]', generates = '[1, 2, 3, 4, 5, null, false, true, "\195\164\195\165\195\182\195\177", "bar"]', is = "array", test = function(o) return #o == 10 end }, { '[]', generates = '[]', is = "array", test = function(o) return not next(o) end }, { '{}', generates = '{}', is = "object", test = function(o) return not next(o) end }, {'', valid=false}, { 'null', generates = 'null', is = "null" }, { '"abc"', generates = '"abc"', is = "string", }, {'{a":1}', valid=false}, -- error {'{"a" bad :1}', valid=false}, -- error { '["a\\\\t"]', generates = '["a\\\\t"]', is = "array", test = function(o) return o[1] == "a\\t" end }, -- Should become Lua {"a\\t"} {'[0.0.0]', valid=false}, -- error { '[-1]', generates = '[-1]', is = "array", }, { '[-1.123e-2]', generates = '[-0.01123]', is = "array", }, { '[5e3]', generates = '[5000.0]', is = "array", }, { '[5e+3]', generates = '[5000.0]', is = "array", }, { '[5E-3]', generates = '[0.005]', is = "array", }, { '[5.5e3]', generates = '[5500.0]', is = "array", }, { '["a\\\\"]', generates = '["a\\\\"]', is = "array", }, -- Should become Lua {"a\\"} { ' {"a}": 1} ', generates = '{"a}": 1}', is = "object", test = function(o) return o["a}"] == 1 end }, -- Should become Lua {"a}" = 1} {'["key": "value"]', valid=false}, -- error { '["\\u0041"]', generates = '["A"]', is = "array", }, -- Should become Lua {"A"} { '["\\uD800"]', valid=false, test = function(s) return s:find("Bad unicode escape.*missing low surrogate") ~= nil end }, -- error { '["\\uD834\\uDD1EX"]', generates = '["\240\157\132\158X"]', is = "array", }, -- Should become Lua {"\240\157\132\158X"} { '1684119503', generates = '1684119503', is = "number" } } test_suite = unittest.TestSuite:new() local equal = unittest.equal local is_false = unittest.is_false local is_true = unittest.is_true for _, test in ipairs(TESTS) do local status, val = parse(test[1]) if test.valid == false then test_suite:add_test(is_false(status), "Syntax error status is false") if not test.test then test_suite:add_test(equal(val, "syntax error"), "Syntax error") end end if test.generates then test_suite:add_test(equal(generate(val), test.generates), "Generate") end if test.is then test_suite:add_test(equal(typeof(val), test.is), "JSON type") end if test.test then test_suite:add_test(is_true(test.test(val)), "Extra test") end end return _ENV;