gjson.go 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359
  1. // Package gjson provides searching for json strings.
  2. package gjson
  3. import (
  4. "strconv"
  5. "strings"
  6. "time"
  7. "unicode/utf16"
  8. "unicode/utf8"
  9. "unsafe"
  10. "github.com/tidwall/match"
  11. "github.com/tidwall/pretty"
  12. )
  13. // Type is Result type
  14. type Type int
  15. const (
  16. // Null is a null json value
  17. Null Type = iota
  18. // False is a json false boolean
  19. False
  20. // Number is json number
  21. Number
  22. // String is a json string
  23. String
  24. // True is a json true boolean
  25. True
  26. // JSON is a raw block of JSON
  27. JSON
  28. )
  29. // String returns a string representation of the type.
  30. func (t Type) String() string {
  31. switch t {
  32. default:
  33. return ""
  34. case Null:
  35. return "Null"
  36. case False:
  37. return "False"
  38. case Number:
  39. return "Number"
  40. case String:
  41. return "String"
  42. case True:
  43. return "True"
  44. case JSON:
  45. return "JSON"
  46. }
  47. }
  48. // Result represents a json value that is returned from Get().
  49. type Result struct {
  50. // Type is the json type
  51. Type Type
  52. // Raw is the raw json
  53. Raw string
  54. // Str is the json string
  55. Str string
  56. // Num is the json number
  57. Num float64
  58. // Index of raw value in original json, zero means index unknown
  59. Index int
  60. // Indexes of all the elements that match on a path containing the '#'
  61. // query character.
  62. Indexes []int
  63. }
  64. // String returns a string representation of the value.
  65. func (t Result) String() string {
  66. switch t.Type {
  67. default:
  68. return ""
  69. case False:
  70. return "false"
  71. case Number:
  72. if len(t.Raw) == 0 {
  73. // calculated result
  74. return strconv.FormatFloat(t.Num, 'f', -1, 64)
  75. }
  76. var i int
  77. if t.Raw[0] == '-' {
  78. i++
  79. }
  80. for ; i < len(t.Raw); i++ {
  81. if t.Raw[i] < '0' || t.Raw[i] > '9' {
  82. return strconv.FormatFloat(t.Num, 'f', -1, 64)
  83. }
  84. }
  85. return t.Raw
  86. case String:
  87. return t.Str
  88. case JSON:
  89. return t.Raw
  90. case True:
  91. return "true"
  92. }
  93. }
  94. // Bool returns an boolean representation.
  95. func (t Result) Bool() bool {
  96. switch t.Type {
  97. default:
  98. return false
  99. case True:
  100. return true
  101. case String:
  102. b, _ := strconv.ParseBool(strings.ToLower(t.Str))
  103. return b
  104. case Number:
  105. return t.Num != 0
  106. }
  107. }
  108. // Int returns an integer representation.
  109. func (t Result) Int() int64 {
  110. switch t.Type {
  111. default:
  112. return 0
  113. case True:
  114. return 1
  115. case String:
  116. n, _ := parseInt(t.Str)
  117. return n
  118. case Number:
  119. // try to directly convert the float64 to int64
  120. i, ok := safeInt(t.Num)
  121. if ok {
  122. return i
  123. }
  124. // now try to parse the raw string
  125. i, ok = parseInt(t.Raw)
  126. if ok {
  127. return i
  128. }
  129. // fallback to a standard conversion
  130. return int64(t.Num)
  131. }
  132. }
  133. // Uint returns an unsigned integer representation.
  134. func (t Result) Uint() uint64 {
  135. switch t.Type {
  136. default:
  137. return 0
  138. case True:
  139. return 1
  140. case String:
  141. n, _ := parseUint(t.Str)
  142. return n
  143. case Number:
  144. // try to directly convert the float64 to uint64
  145. i, ok := safeInt(t.Num)
  146. if ok && i >= 0 {
  147. return uint64(i)
  148. }
  149. // now try to parse the raw string
  150. u, ok := parseUint(t.Raw)
  151. if ok {
  152. return u
  153. }
  154. // fallback to a standard conversion
  155. return uint64(t.Num)
  156. }
  157. }
  158. // Float returns an float64 representation.
  159. func (t Result) Float() float64 {
  160. switch t.Type {
  161. default:
  162. return 0
  163. case True:
  164. return 1
  165. case String:
  166. n, _ := strconv.ParseFloat(t.Str, 64)
  167. return n
  168. case Number:
  169. return t.Num
  170. }
  171. }
  172. // Time returns a time.Time representation.
  173. func (t Result) Time() time.Time {
  174. res, _ := time.Parse(time.RFC3339, t.String())
  175. return res
  176. }
  177. // Array returns back an array of values.
  178. // If the result represents a null value or is non-existent, then an empty
  179. // array will be returned.
  180. // If the result is not a JSON array, the return value will be an
  181. // array containing one result.
  182. func (t Result) Array() []Result {
  183. if t.Type == Null {
  184. return []Result{}
  185. }
  186. if !t.IsArray() {
  187. return []Result{t}
  188. }
  189. r := t.arrayOrMap('[', false)
  190. return r.a
  191. }
  192. // IsObject returns true if the result value is a JSON object.
  193. func (t Result) IsObject() bool {
  194. return t.Type == JSON && len(t.Raw) > 0 && t.Raw[0] == '{'
  195. }
  196. // IsArray returns true if the result value is a JSON array.
  197. func (t Result) IsArray() bool {
  198. return t.Type == JSON && len(t.Raw) > 0 && t.Raw[0] == '['
  199. }
  200. // IsBool returns true if the result value is a JSON boolean.
  201. func (t Result) IsBool() bool {
  202. return t.Type == True || t.Type == False
  203. }
  204. // ForEach iterates through values.
  205. // If the result represents a non-existent value, then no values will be
  206. // iterated. If the result is an Object, the iterator will pass the key and
  207. // value of each item. If the result is an Array, the iterator will only pass
  208. // the value of each item. If the result is not a JSON array or object, the
  209. // iterator will pass back one value equal to the result.
  210. func (t Result) ForEach(iterator func(key, value Result) bool) {
  211. if !t.Exists() {
  212. return
  213. }
  214. if t.Type != JSON {
  215. iterator(Result{}, t)
  216. return
  217. }
  218. json := t.Raw
  219. var obj bool
  220. var i int
  221. var key, value Result
  222. for ; i < len(json); i++ {
  223. if json[i] == '{' {
  224. i++
  225. key.Type = String
  226. obj = true
  227. break
  228. } else if json[i] == '[' {
  229. i++
  230. key.Type = Number
  231. key.Num = -1
  232. break
  233. }
  234. if json[i] > ' ' {
  235. return
  236. }
  237. }
  238. var str string
  239. var vesc bool
  240. var ok bool
  241. var idx int
  242. for ; i < len(json); i++ {
  243. if obj {
  244. if json[i] != '"' {
  245. continue
  246. }
  247. s := i
  248. i, str, vesc, ok = parseString(json, i+1)
  249. if !ok {
  250. return
  251. }
  252. if vesc {
  253. key.Str = unescape(str[1 : len(str)-1])
  254. } else {
  255. key.Str = str[1 : len(str)-1]
  256. }
  257. key.Raw = str
  258. key.Index = s + t.Index
  259. } else {
  260. key.Num += 1
  261. }
  262. for ; i < len(json); i++ {
  263. if json[i] <= ' ' || json[i] == ',' || json[i] == ':' {
  264. continue
  265. }
  266. break
  267. }
  268. s := i
  269. i, value, ok = parseAny(json, i, true)
  270. if !ok {
  271. return
  272. }
  273. if t.Indexes != nil {
  274. if idx < len(t.Indexes) {
  275. value.Index = t.Indexes[idx]
  276. }
  277. } else {
  278. value.Index = s + t.Index
  279. }
  280. if !iterator(key, value) {
  281. return
  282. }
  283. idx++
  284. }
  285. }
  286. // Map returns back a map of values. The result should be a JSON object.
  287. // If the result is not a JSON object, the return value will be an empty map.
  288. func (t Result) Map() map[string]Result {
  289. if t.Type != JSON {
  290. return map[string]Result{}
  291. }
  292. r := t.arrayOrMap('{', false)
  293. return r.o
  294. }
  295. // Get searches result for the specified path.
  296. // The result should be a JSON array or object.
  297. func (t Result) Get(path string) Result {
  298. r := Get(t.Raw, path)
  299. if r.Indexes != nil {
  300. for i := 0; i < len(r.Indexes); i++ {
  301. r.Indexes[i] += t.Index
  302. }
  303. } else {
  304. r.Index += t.Index
  305. }
  306. return r
  307. }
  308. type arrayOrMapResult struct {
  309. a []Result
  310. ai []interface{}
  311. o map[string]Result
  312. oi map[string]interface{}
  313. vc byte
  314. }
  315. func (t Result) arrayOrMap(vc byte, valueize bool) (r arrayOrMapResult) {
  316. var json = t.Raw
  317. var i int
  318. var value Result
  319. var count int
  320. var key Result
  321. if vc == 0 {
  322. for ; i < len(json); i++ {
  323. if json[i] == '{' || json[i] == '[' {
  324. r.vc = json[i]
  325. i++
  326. break
  327. }
  328. if json[i] > ' ' {
  329. goto end
  330. }
  331. }
  332. } else {
  333. for ; i < len(json); i++ {
  334. if json[i] == vc {
  335. i++
  336. break
  337. }
  338. if json[i] > ' ' {
  339. goto end
  340. }
  341. }
  342. r.vc = vc
  343. }
  344. if r.vc == '{' {
  345. if valueize {
  346. r.oi = make(map[string]interface{})
  347. } else {
  348. r.o = make(map[string]Result)
  349. }
  350. } else {
  351. if valueize {
  352. r.ai = make([]interface{}, 0)
  353. } else {
  354. r.a = make([]Result, 0)
  355. }
  356. }
  357. for ; i < len(json); i++ {
  358. if json[i] <= ' ' {
  359. continue
  360. }
  361. // get next value
  362. if json[i] == ']' || json[i] == '}' {
  363. break
  364. }
  365. switch json[i] {
  366. default:
  367. if (json[i] >= '0' && json[i] <= '9') || json[i] == '-' {
  368. value.Type = Number
  369. value.Raw, value.Num = tonum(json[i:])
  370. value.Str = ""
  371. } else {
  372. continue
  373. }
  374. case '{', '[':
  375. value.Type = JSON
  376. value.Raw = squash(json[i:])
  377. value.Str, value.Num = "", 0
  378. case 'n':
  379. value.Type = Null
  380. value.Raw = tolit(json[i:])
  381. value.Str, value.Num = "", 0
  382. case 't':
  383. value.Type = True
  384. value.Raw = tolit(json[i:])
  385. value.Str, value.Num = "", 0
  386. case 'f':
  387. value.Type = False
  388. value.Raw = tolit(json[i:])
  389. value.Str, value.Num = "", 0
  390. case '"':
  391. value.Type = String
  392. value.Raw, value.Str = tostr(json[i:])
  393. value.Num = 0
  394. }
  395. value.Index = i + t.Index
  396. i += len(value.Raw) - 1
  397. if r.vc == '{' {
  398. if count%2 == 0 {
  399. key = value
  400. } else {
  401. if valueize {
  402. if _, ok := r.oi[key.Str]; !ok {
  403. r.oi[key.Str] = value.Value()
  404. }
  405. } else {
  406. if _, ok := r.o[key.Str]; !ok {
  407. r.o[key.Str] = value
  408. }
  409. }
  410. }
  411. count++
  412. } else {
  413. if valueize {
  414. r.ai = append(r.ai, value.Value())
  415. } else {
  416. r.a = append(r.a, value)
  417. }
  418. }
  419. }
  420. end:
  421. if t.Indexes != nil {
  422. if len(t.Indexes) != len(r.a) {
  423. for i := 0; i < len(r.a); i++ {
  424. r.a[i].Index = 0
  425. }
  426. } else {
  427. for i := 0; i < len(r.a); i++ {
  428. r.a[i].Index = t.Indexes[i]
  429. }
  430. }
  431. }
  432. return
  433. }
  434. // Parse parses the json and returns a result.
  435. //
  436. // This function expects that the json is well-formed, and does not validate.
  437. // Invalid json will not panic, but it may return back unexpected results.
  438. // If you are consuming JSON from an unpredictable source then you may want to
  439. // use the Valid function first.
  440. func Parse(json string) Result {
  441. var value Result
  442. i := 0
  443. for ; i < len(json); i++ {
  444. if json[i] == '{' || json[i] == '[' {
  445. value.Type = JSON
  446. value.Raw = json[i:] // just take the entire raw
  447. break
  448. }
  449. if json[i] <= ' ' {
  450. continue
  451. }
  452. switch json[i] {
  453. case '+', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  454. 'i', 'I', 'N':
  455. value.Type = Number
  456. value.Raw, value.Num = tonum(json[i:])
  457. case 'n':
  458. if i+1 < len(json) && json[i+1] != 'u' {
  459. // nan
  460. value.Type = Number
  461. value.Raw, value.Num = tonum(json[i:])
  462. } else {
  463. // null
  464. value.Type = Null
  465. value.Raw = tolit(json[i:])
  466. }
  467. case 't':
  468. value.Type = True
  469. value.Raw = tolit(json[i:])
  470. case 'f':
  471. value.Type = False
  472. value.Raw = tolit(json[i:])
  473. case '"':
  474. value.Type = String
  475. value.Raw, value.Str = tostr(json[i:])
  476. default:
  477. return Result{}
  478. }
  479. break
  480. }
  481. if value.Exists() {
  482. value.Index = i
  483. }
  484. return value
  485. }
  486. // ParseBytes parses the json and returns a result.
  487. // If working with bytes, this method preferred over Parse(string(data))
  488. func ParseBytes(json []byte) Result {
  489. return Parse(string(json))
  490. }
  491. func squash(json string) string {
  492. // expects that the lead character is a '[' or '{' or '(' or '"'
  493. // squash the value, ignoring all nested arrays and objects.
  494. var i, depth int
  495. if json[0] != '"' {
  496. i, depth = 1, 1
  497. }
  498. for ; i < len(json); i++ {
  499. if json[i] >= '"' && json[i] <= '}' {
  500. switch json[i] {
  501. case '"':
  502. i++
  503. s2 := i
  504. for ; i < len(json); i++ {
  505. if json[i] > '\\' {
  506. continue
  507. }
  508. if json[i] == '"' {
  509. // look for an escaped slash
  510. if json[i-1] == '\\' {
  511. n := 0
  512. for j := i - 2; j > s2-1; j-- {
  513. if json[j] != '\\' {
  514. break
  515. }
  516. n++
  517. }
  518. if n%2 == 0 {
  519. continue
  520. }
  521. }
  522. break
  523. }
  524. }
  525. if depth == 0 {
  526. if i >= len(json) {
  527. return json
  528. }
  529. return json[:i+1]
  530. }
  531. case '{', '[', '(':
  532. depth++
  533. case '}', ']', ')':
  534. depth--
  535. if depth == 0 {
  536. return json[:i+1]
  537. }
  538. }
  539. }
  540. }
  541. return json
  542. }
  543. func tonum(json string) (raw string, num float64) {
  544. for i := 1; i < len(json); i++ {
  545. // less than dash might have valid characters
  546. if json[i] <= '-' {
  547. if json[i] <= ' ' || json[i] == ',' {
  548. // break on whitespace and comma
  549. raw = json[:i]
  550. num, _ = strconv.ParseFloat(raw, 64)
  551. return
  552. }
  553. // could be a '+' or '-'. let's assume so.
  554. } else if json[i] == ']' || json[i] == '}' {
  555. // break on ']' or '}'
  556. raw = json[:i]
  557. num, _ = strconv.ParseFloat(raw, 64)
  558. return
  559. }
  560. }
  561. raw = json
  562. num, _ = strconv.ParseFloat(raw, 64)
  563. return
  564. }
  565. func tolit(json string) (raw string) {
  566. for i := 1; i < len(json); i++ {
  567. if json[i] < 'a' || json[i] > 'z' {
  568. return json[:i]
  569. }
  570. }
  571. return json
  572. }
  573. func tostr(json string) (raw string, str string) {
  574. // expects that the lead character is a '"'
  575. for i := 1; i < len(json); i++ {
  576. if json[i] > '\\' {
  577. continue
  578. }
  579. if json[i] == '"' {
  580. return json[:i+1], json[1:i]
  581. }
  582. if json[i] == '\\' {
  583. i++
  584. for ; i < len(json); i++ {
  585. if json[i] > '\\' {
  586. continue
  587. }
  588. if json[i] == '"' {
  589. // look for an escaped slash
  590. if json[i-1] == '\\' {
  591. n := 0
  592. for j := i - 2; j > 0; j-- {
  593. if json[j] != '\\' {
  594. break
  595. }
  596. n++
  597. }
  598. if n%2 == 0 {
  599. continue
  600. }
  601. }
  602. return json[:i+1], unescape(json[1:i])
  603. }
  604. }
  605. var ret string
  606. if i+1 < len(json) {
  607. ret = json[:i+1]
  608. } else {
  609. ret = json[:i]
  610. }
  611. return ret, unescape(json[1:i])
  612. }
  613. }
  614. return json, json[1:]
  615. }
  616. // Exists returns true if value exists.
  617. //
  618. // if gjson.Get(json, "name.last").Exists(){
  619. // println("value exists")
  620. // }
  621. func (t Result) Exists() bool {
  622. return t.Type != Null || len(t.Raw) != 0
  623. }
  624. // Value returns one of these types:
  625. //
  626. // bool, for JSON booleans
  627. // float64, for JSON numbers
  628. // Number, for JSON numbers
  629. // string, for JSON string literals
  630. // nil, for JSON null
  631. // map[string]interface{}, for JSON objects
  632. // []interface{}, for JSON arrays
  633. //
  634. func (t Result) Value() interface{} {
  635. if t.Type == String {
  636. return t.Str
  637. }
  638. switch t.Type {
  639. default:
  640. return nil
  641. case False:
  642. return false
  643. case Number:
  644. return t.Num
  645. case JSON:
  646. r := t.arrayOrMap(0, true)
  647. if r.vc == '{' {
  648. return r.oi
  649. } else if r.vc == '[' {
  650. return r.ai
  651. }
  652. return nil
  653. case True:
  654. return true
  655. }
  656. }
  657. func parseString(json string, i int) (int, string, bool, bool) {
  658. var s = i
  659. for ; i < len(json); i++ {
  660. if json[i] > '\\' {
  661. continue
  662. }
  663. if json[i] == '"' {
  664. return i + 1, json[s-1 : i+1], false, true
  665. }
  666. if json[i] == '\\' {
  667. i++
  668. for ; i < len(json); i++ {
  669. if json[i] > '\\' {
  670. continue
  671. }
  672. if json[i] == '"' {
  673. // look for an escaped slash
  674. if json[i-1] == '\\' {
  675. n := 0
  676. for j := i - 2; j > 0; j-- {
  677. if json[j] != '\\' {
  678. break
  679. }
  680. n++
  681. }
  682. if n%2 == 0 {
  683. continue
  684. }
  685. }
  686. return i + 1, json[s-1 : i+1], true, true
  687. }
  688. }
  689. break
  690. }
  691. }
  692. return i, json[s-1:], false, false
  693. }
  694. func parseNumber(json string, i int) (int, string) {
  695. var s = i
  696. i++
  697. for ; i < len(json); i++ {
  698. if json[i] <= ' ' || json[i] == ',' || json[i] == ']' ||
  699. json[i] == '}' {
  700. return i, json[s:i]
  701. }
  702. }
  703. return i, json[s:]
  704. }
  705. func parseLiteral(json string, i int) (int, string) {
  706. var s = i
  707. i++
  708. for ; i < len(json); i++ {
  709. if json[i] < 'a' || json[i] > 'z' {
  710. return i, json[s:i]
  711. }
  712. }
  713. return i, json[s:]
  714. }
  715. type arrayPathResult struct {
  716. part string
  717. path string
  718. pipe string
  719. piped bool
  720. more bool
  721. alogok bool
  722. arrch bool
  723. alogkey string
  724. query struct {
  725. on bool
  726. all bool
  727. path string
  728. op string
  729. value string
  730. }
  731. }
  732. func parseArrayPath(path string) (r arrayPathResult) {
  733. for i := 0; i < len(path); i++ {
  734. if path[i] == '|' {
  735. r.part = path[:i]
  736. r.pipe = path[i+1:]
  737. r.piped = true
  738. return
  739. }
  740. if path[i] == '.' {
  741. r.part = path[:i]
  742. if !r.arrch && i < len(path)-1 && isDotPiperChar(path[i+1:]) {
  743. r.pipe = path[i+1:]
  744. r.piped = true
  745. } else {
  746. r.path = path[i+1:]
  747. r.more = true
  748. }
  749. return
  750. }
  751. if path[i] == '#' {
  752. r.arrch = true
  753. if i == 0 && len(path) > 1 {
  754. if path[1] == '.' {
  755. r.alogok = true
  756. r.alogkey = path[2:]
  757. r.path = path[:1]
  758. } else if path[1] == '[' || path[1] == '(' {
  759. // query
  760. r.query.on = true
  761. qpath, op, value, _, fi, vesc, ok :=
  762. parseQuery(path[i:])
  763. if !ok {
  764. // bad query, end now
  765. break
  766. }
  767. if len(value) >= 2 && value[0] == '"' &&
  768. value[len(value)-1] == '"' {
  769. value = value[1 : len(value)-1]
  770. if vesc {
  771. value = unescape(value)
  772. }
  773. }
  774. r.query.path = qpath
  775. r.query.op = op
  776. r.query.value = value
  777. i = fi - 1
  778. if i+1 < len(path) && path[i+1] == '#' {
  779. r.query.all = true
  780. }
  781. }
  782. }
  783. continue
  784. }
  785. }
  786. r.part = path
  787. r.path = ""
  788. return
  789. }
  790. // splitQuery takes a query and splits it into three parts:
  791. // path, op, middle, and right.
  792. // So for this query:
  793. // #(first_name=="Murphy").last
  794. // Becomes
  795. // first_name # path
  796. // =="Murphy" # middle
  797. // .last # right
  798. // Or,
  799. // #(service_roles.#(=="one")).cap
  800. // Becomes
  801. // service_roles.#(=="one") # path
  802. // # middle
  803. // .cap # right
  804. func parseQuery(query string) (
  805. path, op, value, remain string, i int, vesc, ok bool,
  806. ) {
  807. if len(query) < 2 || query[0] != '#' ||
  808. (query[1] != '(' && query[1] != '[') {
  809. return "", "", "", "", i, false, false
  810. }
  811. i = 2
  812. j := 0 // start of value part
  813. depth := 1
  814. for ; i < len(query); i++ {
  815. if depth == 1 && j == 0 {
  816. switch query[i] {
  817. case '!', '=', '<', '>', '%':
  818. // start of the value part
  819. j = i
  820. continue
  821. }
  822. }
  823. if query[i] == '\\' {
  824. i++
  825. } else if query[i] == '[' || query[i] == '(' {
  826. depth++
  827. } else if query[i] == ']' || query[i] == ')' {
  828. depth--
  829. if depth == 0 {
  830. break
  831. }
  832. } else if query[i] == '"' {
  833. // inside selector string, balance quotes
  834. i++
  835. for ; i < len(query); i++ {
  836. if query[i] == '\\' {
  837. vesc = true
  838. i++
  839. } else if query[i] == '"' {
  840. break
  841. }
  842. }
  843. }
  844. }
  845. if depth > 0 {
  846. return "", "", "", "", i, false, false
  847. }
  848. if j > 0 {
  849. path = trim(query[2:j])
  850. value = trim(query[j:i])
  851. remain = query[i+1:]
  852. // parse the compare op from the value
  853. var opsz int
  854. switch {
  855. case len(value) == 1:
  856. opsz = 1
  857. case value[0] == '!' && value[1] == '=':
  858. opsz = 2
  859. case value[0] == '!' && value[1] == '%':
  860. opsz = 2
  861. case value[0] == '<' && value[1] == '=':
  862. opsz = 2
  863. case value[0] == '>' && value[1] == '=':
  864. opsz = 2
  865. case value[0] == '=' && value[1] == '=':
  866. value = value[1:]
  867. opsz = 1
  868. case value[0] == '<':
  869. opsz = 1
  870. case value[0] == '>':
  871. opsz = 1
  872. case value[0] == '=':
  873. opsz = 1
  874. case value[0] == '%':
  875. opsz = 1
  876. }
  877. op = value[:opsz]
  878. value = trim(value[opsz:])
  879. } else {
  880. path = trim(query[2:i])
  881. remain = query[i+1:]
  882. }
  883. return path, op, value, remain, i + 1, vesc, true
  884. }
  885. func trim(s string) string {
  886. left:
  887. if len(s) > 0 && s[0] <= ' ' {
  888. s = s[1:]
  889. goto left
  890. }
  891. right:
  892. if len(s) > 0 && s[len(s)-1] <= ' ' {
  893. s = s[:len(s)-1]
  894. goto right
  895. }
  896. return s
  897. }
  898. // peek at the next byte and see if it's a '@', '[', or '{'.
  899. func isDotPiperChar(s string) bool {
  900. if DisableModifiers {
  901. return false
  902. }
  903. c := s[0]
  904. if c == '@' {
  905. // check that the next component is *not* a modifier.
  906. i := 1
  907. for ; i < len(s); i++ {
  908. if s[i] == '.' || s[i] == '|' || s[i] == ':' {
  909. break
  910. }
  911. }
  912. _, ok := modifiers[s[1:i]]
  913. return ok
  914. }
  915. return c == '[' || c == '{'
  916. }
  917. type objectPathResult struct {
  918. part string
  919. path string
  920. pipe string
  921. piped bool
  922. wild bool
  923. more bool
  924. }
  925. func parseObjectPath(path string) (r objectPathResult) {
  926. for i := 0; i < len(path); i++ {
  927. if path[i] == '|' {
  928. r.part = path[:i]
  929. r.pipe = path[i+1:]
  930. r.piped = true
  931. return
  932. }
  933. if path[i] == '.' {
  934. r.part = path[:i]
  935. if i < len(path)-1 && isDotPiperChar(path[i+1:]) {
  936. r.pipe = path[i+1:]
  937. r.piped = true
  938. } else {
  939. r.path = path[i+1:]
  940. r.more = true
  941. }
  942. return
  943. }
  944. if path[i] == '*' || path[i] == '?' {
  945. r.wild = true
  946. continue
  947. }
  948. if path[i] == '\\' {
  949. // go into escape mode. this is a slower path that
  950. // strips off the escape character from the part.
  951. epart := []byte(path[:i])
  952. i++
  953. if i < len(path) {
  954. epart = append(epart, path[i])
  955. i++
  956. for ; i < len(path); i++ {
  957. if path[i] == '\\' {
  958. i++
  959. if i < len(path) {
  960. epart = append(epart, path[i])
  961. }
  962. continue
  963. } else if path[i] == '.' {
  964. r.part = string(epart)
  965. if i < len(path)-1 && isDotPiperChar(path[i+1:]) {
  966. r.pipe = path[i+1:]
  967. r.piped = true
  968. } else {
  969. r.path = path[i+1:]
  970. }
  971. r.more = true
  972. return
  973. } else if path[i] == '|' {
  974. r.part = string(epart)
  975. r.pipe = path[i+1:]
  976. r.piped = true
  977. return
  978. } else if path[i] == '*' || path[i] == '?' {
  979. r.wild = true
  980. }
  981. epart = append(epart, path[i])
  982. }
  983. }
  984. // append the last part
  985. r.part = string(epart)
  986. return
  987. }
  988. }
  989. r.part = path
  990. return
  991. }
  992. func parseSquash(json string, i int) (int, string) {
  993. // expects that the lead character is a '[' or '{' or '('
  994. // squash the value, ignoring all nested arrays and objects.
  995. // the first '[' or '{' or '(' has already been read
  996. s := i
  997. i++
  998. depth := 1
  999. for ; i < len(json); i++ {
  1000. if json[i] >= '"' && json[i] <= '}' {
  1001. switch json[i] {
  1002. case '"':
  1003. i++
  1004. s2 := i
  1005. for ; i < len(json); i++ {
  1006. if json[i] > '\\' {
  1007. continue
  1008. }
  1009. if json[i] == '"' {
  1010. // look for an escaped slash
  1011. if json[i-1] == '\\' {
  1012. n := 0
  1013. for j := i - 2; j > s2-1; j-- {
  1014. if json[j] != '\\' {
  1015. break
  1016. }
  1017. n++
  1018. }
  1019. if n%2 == 0 {
  1020. continue
  1021. }
  1022. }
  1023. break
  1024. }
  1025. }
  1026. case '{', '[', '(':
  1027. depth++
  1028. case '}', ']', ')':
  1029. depth--
  1030. if depth == 0 {
  1031. i++
  1032. return i, json[s:i]
  1033. }
  1034. }
  1035. }
  1036. }
  1037. return i, json[s:]
  1038. }
  1039. func parseObject(c *parseContext, i int, path string) (int, bool) {
  1040. var pmatch, kesc, vesc, ok, hit bool
  1041. var key, val string
  1042. rp := parseObjectPath(path)
  1043. if !rp.more && rp.piped {
  1044. c.pipe = rp.pipe
  1045. c.piped = true
  1046. }
  1047. for i < len(c.json) {
  1048. for ; i < len(c.json); i++ {
  1049. if c.json[i] == '"' {
  1050. // parse_key_string
  1051. // this is slightly different from getting s string value
  1052. // because we don't need the outer quotes.
  1053. i++
  1054. var s = i
  1055. for ; i < len(c.json); i++ {
  1056. if c.json[i] > '\\' {
  1057. continue
  1058. }
  1059. if c.json[i] == '"' {
  1060. i, key, kesc, ok = i+1, c.json[s:i], false, true
  1061. goto parse_key_string_done
  1062. }
  1063. if c.json[i] == '\\' {
  1064. i++
  1065. for ; i < len(c.json); i++ {
  1066. if c.json[i] > '\\' {
  1067. continue
  1068. }
  1069. if c.json[i] == '"' {
  1070. // look for an escaped slash
  1071. if c.json[i-1] == '\\' {
  1072. n := 0
  1073. for j := i - 2; j > 0; j-- {
  1074. if c.json[j] != '\\' {
  1075. break
  1076. }
  1077. n++
  1078. }
  1079. if n%2 == 0 {
  1080. continue
  1081. }
  1082. }
  1083. i, key, kesc, ok = i+1, c.json[s:i], true, true
  1084. goto parse_key_string_done
  1085. }
  1086. }
  1087. break
  1088. }
  1089. }
  1090. key, kesc, ok = c.json[s:], false, false
  1091. parse_key_string_done:
  1092. break
  1093. }
  1094. if c.json[i] == '}' {
  1095. return i + 1, false
  1096. }
  1097. }
  1098. if !ok {
  1099. return i, false
  1100. }
  1101. if rp.wild {
  1102. if kesc {
  1103. pmatch = matchLimit(unescape(key), rp.part)
  1104. } else {
  1105. pmatch = matchLimit(key, rp.part)
  1106. }
  1107. } else {
  1108. if kesc {
  1109. pmatch = rp.part == unescape(key)
  1110. } else {
  1111. pmatch = rp.part == key
  1112. }
  1113. }
  1114. hit = pmatch && !rp.more
  1115. for ; i < len(c.json); i++ {
  1116. var num bool
  1117. switch c.json[i] {
  1118. default:
  1119. continue
  1120. case '"':
  1121. i++
  1122. i, val, vesc, ok = parseString(c.json, i)
  1123. if !ok {
  1124. return i, false
  1125. }
  1126. if hit {
  1127. if vesc {
  1128. c.value.Str = unescape(val[1 : len(val)-1])
  1129. } else {
  1130. c.value.Str = val[1 : len(val)-1]
  1131. }
  1132. c.value.Raw = val
  1133. c.value.Type = String
  1134. return i, true
  1135. }
  1136. case '{':
  1137. if pmatch && !hit {
  1138. i, hit = parseObject(c, i+1, rp.path)
  1139. if hit {
  1140. return i, true
  1141. }
  1142. } else {
  1143. i, val = parseSquash(c.json, i)
  1144. if hit {
  1145. c.value.Raw = val
  1146. c.value.Type = JSON
  1147. return i, true
  1148. }
  1149. }
  1150. case '[':
  1151. if pmatch && !hit {
  1152. i, hit = parseArray(c, i+1, rp.path)
  1153. if hit {
  1154. return i, true
  1155. }
  1156. } else {
  1157. i, val = parseSquash(c.json, i)
  1158. if hit {
  1159. c.value.Raw = val
  1160. c.value.Type = JSON
  1161. return i, true
  1162. }
  1163. }
  1164. case 'n':
  1165. if i+1 < len(c.json) && c.json[i+1] != 'u' {
  1166. num = true
  1167. break
  1168. }
  1169. fallthrough
  1170. case 't', 'f':
  1171. vc := c.json[i]
  1172. i, val = parseLiteral(c.json, i)
  1173. if hit {
  1174. c.value.Raw = val
  1175. switch vc {
  1176. case 't':
  1177. c.value.Type = True
  1178. case 'f':
  1179. c.value.Type = False
  1180. }
  1181. return i, true
  1182. }
  1183. case '+', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  1184. 'i', 'I', 'N':
  1185. num = true
  1186. }
  1187. if num {
  1188. i, val = parseNumber(c.json, i)
  1189. if hit {
  1190. c.value.Raw = val
  1191. c.value.Type = Number
  1192. c.value.Num, _ = strconv.ParseFloat(val, 64)
  1193. return i, true
  1194. }
  1195. }
  1196. break
  1197. }
  1198. }
  1199. return i, false
  1200. }
  1201. // matchLimit will limit the complexity of the match operation to avoid ReDos
  1202. // attacks from arbritary inputs.
  1203. // See the github.com/tidwall/match.MatchLimit function for more information.
  1204. func matchLimit(str, pattern string) bool {
  1205. matched, _ := match.MatchLimit(str, pattern, 10000)
  1206. return matched
  1207. }
  1208. func queryMatches(rp *arrayPathResult, value Result) bool {
  1209. rpv := rp.query.value
  1210. if len(rpv) > 0 && rpv[0] == '~' {
  1211. // convert to bool
  1212. rpv = rpv[1:]
  1213. if value.Bool() {
  1214. value = Result{Type: True}
  1215. } else {
  1216. value = Result{Type: False}
  1217. }
  1218. }
  1219. if !value.Exists() {
  1220. return false
  1221. }
  1222. if rp.query.op == "" {
  1223. // the query is only looking for existence, such as:
  1224. // friends.#(name)
  1225. // which makes sure that the array "friends" has an element of
  1226. // "name" that exists
  1227. return true
  1228. }
  1229. switch value.Type {
  1230. case String:
  1231. switch rp.query.op {
  1232. case "=":
  1233. return value.Str == rpv
  1234. case "!=":
  1235. return value.Str != rpv
  1236. case "<":
  1237. return value.Str < rpv
  1238. case "<=":
  1239. return value.Str <= rpv
  1240. case ">":
  1241. return value.Str > rpv
  1242. case ">=":
  1243. return value.Str >= rpv
  1244. case "%":
  1245. return matchLimit(value.Str, rpv)
  1246. case "!%":
  1247. return !matchLimit(value.Str, rpv)
  1248. }
  1249. case Number:
  1250. rpvn, _ := strconv.ParseFloat(rpv, 64)
  1251. switch rp.query.op {
  1252. case "=":
  1253. return value.Num == rpvn
  1254. case "!=":
  1255. return value.Num != rpvn
  1256. case "<":
  1257. return value.Num < rpvn
  1258. case "<=":
  1259. return value.Num <= rpvn
  1260. case ">":
  1261. return value.Num > rpvn
  1262. case ">=":
  1263. return value.Num >= rpvn
  1264. }
  1265. case True:
  1266. switch rp.query.op {
  1267. case "=":
  1268. return rpv == "true"
  1269. case "!=":
  1270. return rpv != "true"
  1271. case ">":
  1272. return rpv == "false"
  1273. case ">=":
  1274. return true
  1275. }
  1276. case False:
  1277. switch rp.query.op {
  1278. case "=":
  1279. return rpv == "false"
  1280. case "!=":
  1281. return rpv != "false"
  1282. case "<":
  1283. return rpv == "true"
  1284. case "<=":
  1285. return true
  1286. }
  1287. }
  1288. return false
  1289. }
  1290. func parseArray(c *parseContext, i int, path string) (int, bool) {
  1291. var pmatch, vesc, ok, hit bool
  1292. var val string
  1293. var h int
  1294. var alog []int
  1295. var partidx int
  1296. var multires []byte
  1297. var queryIndexes []int
  1298. rp := parseArrayPath(path)
  1299. if !rp.arrch {
  1300. n, ok := parseUint(rp.part)
  1301. if !ok {
  1302. partidx = -1
  1303. } else {
  1304. partidx = int(n)
  1305. }
  1306. }
  1307. if !rp.more && rp.piped {
  1308. c.pipe = rp.pipe
  1309. c.piped = true
  1310. }
  1311. procQuery := func(qval Result) bool {
  1312. if rp.query.all {
  1313. if len(multires) == 0 {
  1314. multires = append(multires, '[')
  1315. }
  1316. }
  1317. var tmp parseContext
  1318. tmp.value = qval
  1319. fillIndex(c.json, &tmp)
  1320. parentIndex := tmp.value.Index
  1321. var res Result
  1322. if qval.Type == JSON {
  1323. res = qval.Get(rp.query.path)
  1324. } else {
  1325. if rp.query.path != "" {
  1326. return false
  1327. }
  1328. res = qval
  1329. }
  1330. if queryMatches(&rp, res) {
  1331. if rp.more {
  1332. left, right, ok := splitPossiblePipe(rp.path)
  1333. if ok {
  1334. rp.path = left
  1335. c.pipe = right
  1336. c.piped = true
  1337. }
  1338. res = qval.Get(rp.path)
  1339. } else {
  1340. res = qval
  1341. }
  1342. if rp.query.all {
  1343. raw := res.Raw
  1344. if len(raw) == 0 {
  1345. raw = res.String()
  1346. }
  1347. if raw != "" {
  1348. if len(multires) > 1 {
  1349. multires = append(multires, ',')
  1350. }
  1351. multires = append(multires, raw...)
  1352. queryIndexes = append(queryIndexes, res.Index+parentIndex)
  1353. }
  1354. } else {
  1355. c.value = res
  1356. return true
  1357. }
  1358. }
  1359. return false
  1360. }
  1361. for i < len(c.json)+1 {
  1362. if !rp.arrch {
  1363. pmatch = partidx == h
  1364. hit = pmatch && !rp.more
  1365. }
  1366. h++
  1367. if rp.alogok {
  1368. alog = append(alog, i)
  1369. }
  1370. for ; ; i++ {
  1371. var ch byte
  1372. if i > len(c.json) {
  1373. break
  1374. } else if i == len(c.json) {
  1375. ch = ']'
  1376. } else {
  1377. ch = c.json[i]
  1378. }
  1379. var num bool
  1380. switch ch {
  1381. default:
  1382. continue
  1383. case '"':
  1384. i++
  1385. i, val, vesc, ok = parseString(c.json, i)
  1386. if !ok {
  1387. return i, false
  1388. }
  1389. if rp.query.on {
  1390. var qval Result
  1391. if vesc {
  1392. qval.Str = unescape(val[1 : len(val)-1])
  1393. } else {
  1394. qval.Str = val[1 : len(val)-1]
  1395. }
  1396. qval.Raw = val
  1397. qval.Type = String
  1398. if procQuery(qval) {
  1399. return i, true
  1400. }
  1401. } else if hit {
  1402. if rp.alogok {
  1403. break
  1404. }
  1405. if vesc {
  1406. c.value.Str = unescape(val[1 : len(val)-1])
  1407. } else {
  1408. c.value.Str = val[1 : len(val)-1]
  1409. }
  1410. c.value.Raw = val
  1411. c.value.Type = String
  1412. return i, true
  1413. }
  1414. case '{':
  1415. if pmatch && !hit {
  1416. i, hit = parseObject(c, i+1, rp.path)
  1417. if hit {
  1418. if rp.alogok {
  1419. break
  1420. }
  1421. return i, true
  1422. }
  1423. } else {
  1424. i, val = parseSquash(c.json, i)
  1425. if rp.query.on {
  1426. if procQuery(Result{Raw: val, Type: JSON}) {
  1427. return i, true
  1428. }
  1429. } else if hit {
  1430. if rp.alogok {
  1431. break
  1432. }
  1433. c.value.Raw = val
  1434. c.value.Type = JSON
  1435. return i, true
  1436. }
  1437. }
  1438. case '[':
  1439. if pmatch && !hit {
  1440. i, hit = parseArray(c, i+1, rp.path)
  1441. if hit {
  1442. if rp.alogok {
  1443. break
  1444. }
  1445. return i, true
  1446. }
  1447. } else {
  1448. i, val = parseSquash(c.json, i)
  1449. if rp.query.on {
  1450. if procQuery(Result{Raw: val, Type: JSON}) {
  1451. return i, true
  1452. }
  1453. } else if hit {
  1454. if rp.alogok {
  1455. break
  1456. }
  1457. c.value.Raw = val
  1458. c.value.Type = JSON
  1459. return i, true
  1460. }
  1461. }
  1462. case 'n':
  1463. if i+1 < len(c.json) && c.json[i+1] != 'u' {
  1464. num = true
  1465. break
  1466. }
  1467. fallthrough
  1468. case 't', 'f':
  1469. vc := c.json[i]
  1470. i, val = parseLiteral(c.json, i)
  1471. if rp.query.on {
  1472. var qval Result
  1473. qval.Raw = val
  1474. switch vc {
  1475. case 't':
  1476. qval.Type = True
  1477. case 'f':
  1478. qval.Type = False
  1479. }
  1480. if procQuery(qval) {
  1481. return i, true
  1482. }
  1483. } else if hit {
  1484. if rp.alogok {
  1485. break
  1486. }
  1487. c.value.Raw = val
  1488. switch vc {
  1489. case 't':
  1490. c.value.Type = True
  1491. case 'f':
  1492. c.value.Type = False
  1493. }
  1494. return i, true
  1495. }
  1496. case '+', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  1497. 'i', 'I', 'N':
  1498. num = true
  1499. case ']':
  1500. if rp.arrch && rp.part == "#" {
  1501. if rp.alogok {
  1502. left, right, ok := splitPossiblePipe(rp.alogkey)
  1503. if ok {
  1504. rp.alogkey = left
  1505. c.pipe = right
  1506. c.piped = true
  1507. }
  1508. var indexes = make([]int, 0, 64)
  1509. var jsons = make([]byte, 0, 64)
  1510. jsons = append(jsons, '[')
  1511. for j, k := 0, 0; j < len(alog); j++ {
  1512. idx := alog[j]
  1513. for idx < len(c.json) {
  1514. switch c.json[idx] {
  1515. case ' ', '\t', '\r', '\n':
  1516. idx++
  1517. continue
  1518. }
  1519. break
  1520. }
  1521. if idx < len(c.json) && c.json[idx] != ']' {
  1522. _, res, ok := parseAny(c.json, idx, true)
  1523. if ok {
  1524. res := res.Get(rp.alogkey)
  1525. if res.Exists() {
  1526. if k > 0 {
  1527. jsons = append(jsons, ',')
  1528. }
  1529. raw := res.Raw
  1530. if len(raw) == 0 {
  1531. raw = res.String()
  1532. }
  1533. jsons = append(jsons, []byte(raw)...)
  1534. indexes = append(indexes, res.Index)
  1535. k++
  1536. }
  1537. }
  1538. }
  1539. }
  1540. jsons = append(jsons, ']')
  1541. c.value.Type = JSON
  1542. c.value.Raw = string(jsons)
  1543. c.value.Indexes = indexes
  1544. return i + 1, true
  1545. }
  1546. if rp.alogok {
  1547. break
  1548. }
  1549. c.value.Type = Number
  1550. c.value.Num = float64(h - 1)
  1551. c.value.Raw = strconv.Itoa(h - 1)
  1552. c.calcd = true
  1553. return i + 1, true
  1554. }
  1555. if !c.value.Exists() {
  1556. if len(multires) > 0 {
  1557. c.value = Result{
  1558. Raw: string(append(multires, ']')),
  1559. Type: JSON,
  1560. Indexes: queryIndexes,
  1561. }
  1562. } else if rp.query.all {
  1563. c.value = Result{
  1564. Raw: "[]",
  1565. Type: JSON,
  1566. }
  1567. }
  1568. }
  1569. return i + 1, false
  1570. }
  1571. if num {
  1572. i, val = parseNumber(c.json, i)
  1573. if rp.query.on {
  1574. var qval Result
  1575. qval.Raw = val
  1576. qval.Type = Number
  1577. qval.Num, _ = strconv.ParseFloat(val, 64)
  1578. if procQuery(qval) {
  1579. return i, true
  1580. }
  1581. } else if hit {
  1582. if rp.alogok {
  1583. break
  1584. }
  1585. c.value.Raw = val
  1586. c.value.Type = Number
  1587. c.value.Num, _ = strconv.ParseFloat(val, 64)
  1588. return i, true
  1589. }
  1590. }
  1591. break
  1592. }
  1593. }
  1594. return i, false
  1595. }
  1596. func splitPossiblePipe(path string) (left, right string, ok bool) {
  1597. // take a quick peek for the pipe character. If found we'll split the piped
  1598. // part of the path into the c.pipe field and shorten the rp.
  1599. var possible bool
  1600. for i := 0; i < len(path); i++ {
  1601. if path[i] == '|' {
  1602. possible = true
  1603. break
  1604. }
  1605. }
  1606. if !possible {
  1607. return
  1608. }
  1609. if len(path) > 0 && path[0] == '{' {
  1610. squashed := squash(path[1:])
  1611. if len(squashed) < len(path)-1 {
  1612. squashed = path[:len(squashed)+1]
  1613. remain := path[len(squashed):]
  1614. if remain[0] == '|' {
  1615. return squashed, remain[1:], true
  1616. }
  1617. }
  1618. return
  1619. }
  1620. // split the left and right side of the path with the pipe character as
  1621. // the delimiter. This is a little tricky because we'll need to basically
  1622. // parse the entire path.
  1623. for i := 0; i < len(path); i++ {
  1624. if path[i] == '\\' {
  1625. i++
  1626. } else if path[i] == '.' {
  1627. if i == len(path)-1 {
  1628. return
  1629. }
  1630. if path[i+1] == '#' {
  1631. i += 2
  1632. if i == len(path) {
  1633. return
  1634. }
  1635. if path[i] == '[' || path[i] == '(' {
  1636. var start, end byte
  1637. if path[i] == '[' {
  1638. start, end = '[', ']'
  1639. } else {
  1640. start, end = '(', ')'
  1641. }
  1642. // inside selector, balance brackets
  1643. i++
  1644. depth := 1
  1645. for ; i < len(path); i++ {
  1646. if path[i] == '\\' {
  1647. i++
  1648. } else if path[i] == start {
  1649. depth++
  1650. } else if path[i] == end {
  1651. depth--
  1652. if depth == 0 {
  1653. break
  1654. }
  1655. } else if path[i] == '"' {
  1656. // inside selector string, balance quotes
  1657. i++
  1658. for ; i < len(path); i++ {
  1659. if path[i] == '\\' {
  1660. i++
  1661. } else if path[i] == '"' {
  1662. break
  1663. }
  1664. }
  1665. }
  1666. }
  1667. }
  1668. }
  1669. } else if path[i] == '|' {
  1670. return path[:i], path[i+1:], true
  1671. }
  1672. }
  1673. return
  1674. }
  1675. // ForEachLine iterates through lines of JSON as specified by the JSON Lines
  1676. // format (http://jsonlines.org/).
  1677. // Each line is returned as a GJSON Result.
  1678. func ForEachLine(json string, iterator func(line Result) bool) {
  1679. var res Result
  1680. var i int
  1681. for {
  1682. i, res, _ = parseAny(json, i, true)
  1683. if !res.Exists() {
  1684. break
  1685. }
  1686. if !iterator(res) {
  1687. return
  1688. }
  1689. }
  1690. }
  1691. type subSelector struct {
  1692. name string
  1693. path string
  1694. }
  1695. // parseSubSelectors returns the subselectors belonging to a '[path1,path2]' or
  1696. // '{"field1":path1,"field2":path2}' type subSelection. It's expected that the
  1697. // first character in path is either '[' or '{', and has already been checked
  1698. // prior to calling this function.
  1699. func parseSubSelectors(path string) (sels []subSelector, out string, ok bool) {
  1700. modifier := 0
  1701. depth := 1
  1702. colon := 0
  1703. start := 1
  1704. i := 1
  1705. pushSel := func() {
  1706. var sel subSelector
  1707. if colon == 0 {
  1708. sel.path = path[start:i]
  1709. } else {
  1710. sel.name = path[start:colon]
  1711. sel.path = path[colon+1 : i]
  1712. }
  1713. sels = append(sels, sel)
  1714. colon = 0
  1715. modifier = 0
  1716. start = i + 1
  1717. }
  1718. for ; i < len(path); i++ {
  1719. switch path[i] {
  1720. case '\\':
  1721. i++
  1722. case '@':
  1723. if modifier == 0 && i > 0 && (path[i-1] == '.' || path[i-1] == '|') {
  1724. modifier = i
  1725. }
  1726. case ':':
  1727. if modifier == 0 && colon == 0 && depth == 1 {
  1728. colon = i
  1729. }
  1730. case ',':
  1731. if depth == 1 {
  1732. pushSel()
  1733. }
  1734. case '"':
  1735. i++
  1736. loop:
  1737. for ; i < len(path); i++ {
  1738. switch path[i] {
  1739. case '\\':
  1740. i++
  1741. case '"':
  1742. break loop
  1743. }
  1744. }
  1745. case '[', '(', '{':
  1746. depth++
  1747. case ']', ')', '}':
  1748. depth--
  1749. if depth == 0 {
  1750. pushSel()
  1751. path = path[i+1:]
  1752. return sels, path, true
  1753. }
  1754. }
  1755. }
  1756. return
  1757. }
  1758. // nameOfLast returns the name of the last component
  1759. func nameOfLast(path string) string {
  1760. for i := len(path) - 1; i >= 0; i-- {
  1761. if path[i] == '|' || path[i] == '.' {
  1762. if i > 0 {
  1763. if path[i-1] == '\\' {
  1764. continue
  1765. }
  1766. }
  1767. return path[i+1:]
  1768. }
  1769. }
  1770. return path
  1771. }
  1772. func isSimpleName(component string) bool {
  1773. for i := 0; i < len(component); i++ {
  1774. if component[i] < ' ' {
  1775. return false
  1776. }
  1777. switch component[i] {
  1778. case '[', ']', '{', '}', '(', ')', '#', '|', '!':
  1779. return false
  1780. }
  1781. }
  1782. return true
  1783. }
  1784. var hexchars = [...]byte{
  1785. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  1786. 'a', 'b', 'c', 'd', 'e', 'f',
  1787. }
  1788. func appendHex16(dst []byte, x uint16) []byte {
  1789. return append(dst,
  1790. hexchars[x>>12&0xF], hexchars[x>>8&0xF],
  1791. hexchars[x>>4&0xF], hexchars[x>>0&0xF],
  1792. )
  1793. }
  1794. // AppendJSONString is a convenience function that converts the provided string
  1795. // to a valid JSON string and appends it to dst.
  1796. func AppendJSONString(dst []byte, s string) []byte {
  1797. dst = append(dst, make([]byte, len(s)+2)...)
  1798. dst = append(dst[:len(dst)-len(s)-2], '"')
  1799. for i := 0; i < len(s); i++ {
  1800. if s[i] < ' ' {
  1801. dst = append(dst, '\\')
  1802. switch s[i] {
  1803. case '\n':
  1804. dst = append(dst, 'n')
  1805. case '\r':
  1806. dst = append(dst, 'r')
  1807. case '\t':
  1808. dst = append(dst, 't')
  1809. default:
  1810. dst = append(dst, 'u')
  1811. dst = appendHex16(dst, uint16(s[i]))
  1812. }
  1813. } else if s[i] == '>' || s[i] == '<' || s[i] == '&' {
  1814. dst = append(dst, '\\', 'u')
  1815. dst = appendHex16(dst, uint16(s[i]))
  1816. } else if s[i] == '\\' {
  1817. dst = append(dst, '\\', '\\')
  1818. } else if s[i] == '"' {
  1819. dst = append(dst, '\\', '"')
  1820. } else if s[i] > 127 {
  1821. // read utf8 character
  1822. r, n := utf8.DecodeRuneInString(s[i:])
  1823. if n == 0 {
  1824. break
  1825. }
  1826. if r == utf8.RuneError && n == 1 {
  1827. dst = append(dst, `\ufffd`...)
  1828. } else if r == '\u2028' || r == '\u2029' {
  1829. dst = append(dst, `\u202`...)
  1830. dst = append(dst, hexchars[r&0xF])
  1831. } else {
  1832. dst = append(dst, s[i:i+n]...)
  1833. }
  1834. i = i + n - 1
  1835. } else {
  1836. dst = append(dst, s[i])
  1837. }
  1838. }
  1839. return append(dst, '"')
  1840. }
  1841. type parseContext struct {
  1842. json string
  1843. value Result
  1844. pipe string
  1845. piped bool
  1846. calcd bool
  1847. lines bool
  1848. }
  1849. // Get searches json for the specified path.
  1850. // A path is in dot syntax, such as "name.last" or "age".
  1851. // When the value is found it's returned immediately.
  1852. //
  1853. // A path is a series of keys separated by a dot.
  1854. // A key may contain special wildcard characters '*' and '?'.
  1855. // To access an array value use the index as the key.
  1856. // To get the number of elements in an array or to access a child path, use
  1857. // the '#' character.
  1858. // The dot and wildcard character can be escaped with '\'.
  1859. //
  1860. // {
  1861. // "name": {"first": "Tom", "last": "Anderson"},
  1862. // "age":37,
  1863. // "children": ["Sara","Alex","Jack"],
  1864. // "friends": [
  1865. // {"first": "James", "last": "Murphy"},
  1866. // {"first": "Roger", "last": "Craig"}
  1867. // ]
  1868. // }
  1869. // "name.last" >> "Anderson"
  1870. // "age" >> 37
  1871. // "children" >> ["Sara","Alex","Jack"]
  1872. // "children.#" >> 3
  1873. // "children.1" >> "Alex"
  1874. // "child*.2" >> "Jack"
  1875. // "c?ildren.0" >> "Sara"
  1876. // "friends.#.first" >> ["James","Roger"]
  1877. //
  1878. // This function expects that the json is well-formed, and does not validate.
  1879. // Invalid json will not panic, but it may return back unexpected results.
  1880. // If you are consuming JSON from an unpredictable source then you may want to
  1881. // use the Valid function first.
  1882. func Get(json, path string) Result {
  1883. if len(path) > 1 {
  1884. if (path[0] == '@' && !DisableModifiers) || path[0] == '!' {
  1885. // possible modifier
  1886. var ok bool
  1887. var npath string
  1888. var rjson string
  1889. if path[0] == '@' && !DisableModifiers {
  1890. npath, rjson, ok = execModifier(json, path)
  1891. } else if path[0] == '!' {
  1892. npath, rjson, ok = execStatic(json, path)
  1893. }
  1894. if ok {
  1895. path = npath
  1896. if len(path) > 0 && (path[0] == '|' || path[0] == '.') {
  1897. res := Get(rjson, path[1:])
  1898. res.Index = 0
  1899. res.Indexes = nil
  1900. return res
  1901. }
  1902. return Parse(rjson)
  1903. }
  1904. }
  1905. if path[0] == '[' || path[0] == '{' {
  1906. // using a subselector path
  1907. kind := path[0]
  1908. var ok bool
  1909. var subs []subSelector
  1910. subs, path, ok = parseSubSelectors(path)
  1911. if ok {
  1912. if len(path) == 0 || (path[0] == '|' || path[0] == '.') {
  1913. var b []byte
  1914. b = append(b, kind)
  1915. var i int
  1916. for _, sub := range subs {
  1917. res := Get(json, sub.path)
  1918. if res.Exists() {
  1919. if i > 0 {
  1920. b = append(b, ',')
  1921. }
  1922. if kind == '{' {
  1923. if len(sub.name) > 0 {
  1924. if sub.name[0] == '"' && Valid(sub.name) {
  1925. b = append(b, sub.name...)
  1926. } else {
  1927. b = AppendJSONString(b, sub.name)
  1928. }
  1929. } else {
  1930. last := nameOfLast(sub.path)
  1931. if isSimpleName(last) {
  1932. b = AppendJSONString(b, last)
  1933. } else {
  1934. b = AppendJSONString(b, "_")
  1935. }
  1936. }
  1937. b = append(b, ':')
  1938. }
  1939. var raw string
  1940. if len(res.Raw) == 0 {
  1941. raw = res.String()
  1942. if len(raw) == 0 {
  1943. raw = "null"
  1944. }
  1945. } else {
  1946. raw = res.Raw
  1947. }
  1948. b = append(b, raw...)
  1949. i++
  1950. }
  1951. }
  1952. b = append(b, kind+2)
  1953. var res Result
  1954. res.Raw = string(b)
  1955. res.Type = JSON
  1956. if len(path) > 0 {
  1957. res = res.Get(path[1:])
  1958. }
  1959. res.Index = 0
  1960. return res
  1961. }
  1962. }
  1963. }
  1964. }
  1965. var i int
  1966. var c = &parseContext{json: json}
  1967. if len(path) >= 2 && path[0] == '.' && path[1] == '.' {
  1968. c.lines = true
  1969. parseArray(c, 0, path[2:])
  1970. } else {
  1971. for ; i < len(c.json); i++ {
  1972. if c.json[i] == '{' {
  1973. i++
  1974. parseObject(c, i, path)
  1975. break
  1976. }
  1977. if c.json[i] == '[' {
  1978. i++
  1979. parseArray(c, i, path)
  1980. break
  1981. }
  1982. }
  1983. }
  1984. if c.piped {
  1985. res := c.value.Get(c.pipe)
  1986. res.Index = 0
  1987. return res
  1988. }
  1989. fillIndex(json, c)
  1990. return c.value
  1991. }
  1992. // GetBytes searches json for the specified path.
  1993. // If working with bytes, this method preferred over Get(string(data), path)
  1994. func GetBytes(json []byte, path string) Result {
  1995. return getBytes(json, path)
  1996. }
  1997. // runeit returns the rune from the the \uXXXX
  1998. func runeit(json string) rune {
  1999. n, _ := strconv.ParseUint(json[:4], 16, 64)
  2000. return rune(n)
  2001. }
  2002. // unescape unescapes a string
  2003. func unescape(json string) string {
  2004. var str = make([]byte, 0, len(json))
  2005. for i := 0; i < len(json); i++ {
  2006. switch {
  2007. default:
  2008. str = append(str, json[i])
  2009. case json[i] < ' ':
  2010. return string(str)
  2011. case json[i] == '\\':
  2012. i++
  2013. if i >= len(json) {
  2014. return string(str)
  2015. }
  2016. switch json[i] {
  2017. default:
  2018. return string(str)
  2019. case '\\':
  2020. str = append(str, '\\')
  2021. case '/':
  2022. str = append(str, '/')
  2023. case 'b':
  2024. str = append(str, '\b')
  2025. case 'f':
  2026. str = append(str, '\f')
  2027. case 'n':
  2028. str = append(str, '\n')
  2029. case 'r':
  2030. str = append(str, '\r')
  2031. case 't':
  2032. str = append(str, '\t')
  2033. case '"':
  2034. str = append(str, '"')
  2035. case 'u':
  2036. if i+5 > len(json) {
  2037. return string(str)
  2038. }
  2039. r := runeit(json[i+1:])
  2040. i += 5
  2041. if utf16.IsSurrogate(r) {
  2042. // need another code
  2043. if len(json[i:]) >= 6 && json[i] == '\\' &&
  2044. json[i+1] == 'u' {
  2045. // we expect it to be correct so just consume it
  2046. r = utf16.DecodeRune(r, runeit(json[i+2:]))
  2047. i += 6
  2048. }
  2049. }
  2050. // provide enough space to encode the largest utf8 possible
  2051. str = append(str, 0, 0, 0, 0, 0, 0, 0, 0)
  2052. n := utf8.EncodeRune(str[len(str)-8:], r)
  2053. str = str[:len(str)-8+n]
  2054. i-- // backtrack index by one
  2055. }
  2056. }
  2057. }
  2058. return string(str)
  2059. }
  2060. // Less return true if a token is less than another token.
  2061. // The caseSensitive paramater is used when the tokens are Strings.
  2062. // The order when comparing two different type is:
  2063. //
  2064. // Null < False < Number < String < True < JSON
  2065. //
  2066. func (t Result) Less(token Result, caseSensitive bool) bool {
  2067. if t.Type < token.Type {
  2068. return true
  2069. }
  2070. if t.Type > token.Type {
  2071. return false
  2072. }
  2073. if t.Type == String {
  2074. if caseSensitive {
  2075. return t.Str < token.Str
  2076. }
  2077. return stringLessInsensitive(t.Str, token.Str)
  2078. }
  2079. if t.Type == Number {
  2080. return t.Num < token.Num
  2081. }
  2082. return t.Raw < token.Raw
  2083. }
  2084. func stringLessInsensitive(a, b string) bool {
  2085. for i := 0; i < len(a) && i < len(b); i++ {
  2086. if a[i] >= 'A' && a[i] <= 'Z' {
  2087. if b[i] >= 'A' && b[i] <= 'Z' {
  2088. // both are uppercase, do nothing
  2089. if a[i] < b[i] {
  2090. return true
  2091. } else if a[i] > b[i] {
  2092. return false
  2093. }
  2094. } else {
  2095. // a is uppercase, convert a to lowercase
  2096. if a[i]+32 < b[i] {
  2097. return true
  2098. } else if a[i]+32 > b[i] {
  2099. return false
  2100. }
  2101. }
  2102. } else if b[i] >= 'A' && b[i] <= 'Z' {
  2103. // b is uppercase, convert b to lowercase
  2104. if a[i] < b[i]+32 {
  2105. return true
  2106. } else if a[i] > b[i]+32 {
  2107. return false
  2108. }
  2109. } else {
  2110. // neither are uppercase
  2111. if a[i] < b[i] {
  2112. return true
  2113. } else if a[i] > b[i] {
  2114. return false
  2115. }
  2116. }
  2117. }
  2118. return len(a) < len(b)
  2119. }
  2120. // parseAny parses the next value from a json string.
  2121. // A Result is returned when the hit param is set.
  2122. // The return values are (i int, res Result, ok bool)
  2123. func parseAny(json string, i int, hit bool) (int, Result, bool) {
  2124. var res Result
  2125. var val string
  2126. for ; i < len(json); i++ {
  2127. if json[i] == '{' || json[i] == '[' {
  2128. i, val = parseSquash(json, i)
  2129. if hit {
  2130. res.Raw = val
  2131. res.Type = JSON
  2132. }
  2133. var tmp parseContext
  2134. tmp.value = res
  2135. fillIndex(json, &tmp)
  2136. return i, tmp.value, true
  2137. }
  2138. if json[i] <= ' ' {
  2139. continue
  2140. }
  2141. var num bool
  2142. switch json[i] {
  2143. case '"':
  2144. i++
  2145. var vesc bool
  2146. var ok bool
  2147. i, val, vesc, ok = parseString(json, i)
  2148. if !ok {
  2149. return i, res, false
  2150. }
  2151. if hit {
  2152. res.Type = String
  2153. res.Raw = val
  2154. if vesc {
  2155. res.Str = unescape(val[1 : len(val)-1])
  2156. } else {
  2157. res.Str = val[1 : len(val)-1]
  2158. }
  2159. }
  2160. return i, res, true
  2161. case 'n':
  2162. if i+1 < len(json) && json[i+1] != 'u' {
  2163. num = true
  2164. break
  2165. }
  2166. fallthrough
  2167. case 't', 'f':
  2168. vc := json[i]
  2169. i, val = parseLiteral(json, i)
  2170. if hit {
  2171. res.Raw = val
  2172. switch vc {
  2173. case 't':
  2174. res.Type = True
  2175. case 'f':
  2176. res.Type = False
  2177. }
  2178. return i, res, true
  2179. }
  2180. case '+', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  2181. 'i', 'I', 'N':
  2182. num = true
  2183. }
  2184. if num {
  2185. i, val = parseNumber(json, i)
  2186. if hit {
  2187. res.Raw = val
  2188. res.Type = Number
  2189. res.Num, _ = strconv.ParseFloat(val, 64)
  2190. }
  2191. return i, res, true
  2192. }
  2193. }
  2194. return i, res, false
  2195. }
  2196. // GetMany searches json for the multiple paths.
  2197. // The return value is a Result array where the number of items
  2198. // will be equal to the number of input paths.
  2199. func GetMany(json string, path ...string) []Result {
  2200. res := make([]Result, len(path))
  2201. for i, path := range path {
  2202. res[i] = Get(json, path)
  2203. }
  2204. return res
  2205. }
  2206. // GetManyBytes searches json for the multiple paths.
  2207. // The return value is a Result array where the number of items
  2208. // will be equal to the number of input paths.
  2209. func GetManyBytes(json []byte, path ...string) []Result {
  2210. res := make([]Result, len(path))
  2211. for i, path := range path {
  2212. res[i] = GetBytes(json, path)
  2213. }
  2214. return res
  2215. }
  2216. func validpayload(data []byte, i int) (outi int, ok bool) {
  2217. for ; i < len(data); i++ {
  2218. switch data[i] {
  2219. default:
  2220. i, ok = validany(data, i)
  2221. if !ok {
  2222. return i, false
  2223. }
  2224. for ; i < len(data); i++ {
  2225. switch data[i] {
  2226. default:
  2227. return i, false
  2228. case ' ', '\t', '\n', '\r':
  2229. continue
  2230. }
  2231. }
  2232. return i, true
  2233. case ' ', '\t', '\n', '\r':
  2234. continue
  2235. }
  2236. }
  2237. return i, false
  2238. }
  2239. func validany(data []byte, i int) (outi int, ok bool) {
  2240. for ; i < len(data); i++ {
  2241. switch data[i] {
  2242. default:
  2243. return i, false
  2244. case ' ', '\t', '\n', '\r':
  2245. continue
  2246. case '{':
  2247. return validobject(data, i+1)
  2248. case '[':
  2249. return validarray(data, i+1)
  2250. case '"':
  2251. return validstring(data, i+1)
  2252. case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  2253. return validnumber(data, i+1)
  2254. case 't':
  2255. return validtrue(data, i+1)
  2256. case 'f':
  2257. return validfalse(data, i+1)
  2258. case 'n':
  2259. return validnull(data, i+1)
  2260. }
  2261. }
  2262. return i, false
  2263. }
  2264. func validobject(data []byte, i int) (outi int, ok bool) {
  2265. for ; i < len(data); i++ {
  2266. switch data[i] {
  2267. default:
  2268. return i, false
  2269. case ' ', '\t', '\n', '\r':
  2270. continue
  2271. case '}':
  2272. return i + 1, true
  2273. case '"':
  2274. key:
  2275. if i, ok = validstring(data, i+1); !ok {
  2276. return i, false
  2277. }
  2278. if i, ok = validcolon(data, i); !ok {
  2279. return i, false
  2280. }
  2281. if i, ok = validany(data, i); !ok {
  2282. return i, false
  2283. }
  2284. if i, ok = validcomma(data, i, '}'); !ok {
  2285. return i, false
  2286. }
  2287. if data[i] == '}' {
  2288. return i + 1, true
  2289. }
  2290. i++
  2291. for ; i < len(data); i++ {
  2292. switch data[i] {
  2293. default:
  2294. return i, false
  2295. case ' ', '\t', '\n', '\r':
  2296. continue
  2297. case '"':
  2298. goto key
  2299. }
  2300. }
  2301. return i, false
  2302. }
  2303. }
  2304. return i, false
  2305. }
  2306. func validcolon(data []byte, i int) (outi int, ok bool) {
  2307. for ; i < len(data); i++ {
  2308. switch data[i] {
  2309. default:
  2310. return i, false
  2311. case ' ', '\t', '\n', '\r':
  2312. continue
  2313. case ':':
  2314. return i + 1, true
  2315. }
  2316. }
  2317. return i, false
  2318. }
  2319. func validcomma(data []byte, i int, end byte) (outi int, ok bool) {
  2320. for ; i < len(data); i++ {
  2321. switch data[i] {
  2322. default:
  2323. return i, false
  2324. case ' ', '\t', '\n', '\r':
  2325. continue
  2326. case ',':
  2327. return i, true
  2328. case end:
  2329. return i, true
  2330. }
  2331. }
  2332. return i, false
  2333. }
  2334. func validarray(data []byte, i int) (outi int, ok bool) {
  2335. for ; i < len(data); i++ {
  2336. switch data[i] {
  2337. default:
  2338. for ; i < len(data); i++ {
  2339. if i, ok = validany(data, i); !ok {
  2340. return i, false
  2341. }
  2342. if i, ok = validcomma(data, i, ']'); !ok {
  2343. return i, false
  2344. }
  2345. if data[i] == ']' {
  2346. return i + 1, true
  2347. }
  2348. }
  2349. case ' ', '\t', '\n', '\r':
  2350. continue
  2351. case ']':
  2352. return i + 1, true
  2353. }
  2354. }
  2355. return i, false
  2356. }
  2357. func validstring(data []byte, i int) (outi int, ok bool) {
  2358. for ; i < len(data); i++ {
  2359. if data[i] < ' ' {
  2360. return i, false
  2361. } else if data[i] == '\\' {
  2362. i++
  2363. if i == len(data) {
  2364. return i, false
  2365. }
  2366. switch data[i] {
  2367. default:
  2368. return i, false
  2369. case '"', '\\', '/', 'b', 'f', 'n', 'r', 't':
  2370. case 'u':
  2371. for j := 0; j < 4; j++ {
  2372. i++
  2373. if i >= len(data) {
  2374. return i, false
  2375. }
  2376. if !((data[i] >= '0' && data[i] <= '9') ||
  2377. (data[i] >= 'a' && data[i] <= 'f') ||
  2378. (data[i] >= 'A' && data[i] <= 'F')) {
  2379. return i, false
  2380. }
  2381. }
  2382. }
  2383. } else if data[i] == '"' {
  2384. return i + 1, true
  2385. }
  2386. }
  2387. return i, false
  2388. }
  2389. func validnumber(data []byte, i int) (outi int, ok bool) {
  2390. i--
  2391. // sign
  2392. if data[i] == '-' {
  2393. i++
  2394. if i == len(data) {
  2395. return i, false
  2396. }
  2397. if data[i] < '0' || data[i] > '9' {
  2398. return i, false
  2399. }
  2400. }
  2401. // int
  2402. if i == len(data) {
  2403. return i, false
  2404. }
  2405. if data[i] == '0' {
  2406. i++
  2407. } else {
  2408. for ; i < len(data); i++ {
  2409. if data[i] >= '0' && data[i] <= '9' {
  2410. continue
  2411. }
  2412. break
  2413. }
  2414. }
  2415. // frac
  2416. if i == len(data) {
  2417. return i, true
  2418. }
  2419. if data[i] == '.' {
  2420. i++
  2421. if i == len(data) {
  2422. return i, false
  2423. }
  2424. if data[i] < '0' || data[i] > '9' {
  2425. return i, false
  2426. }
  2427. i++
  2428. for ; i < len(data); i++ {
  2429. if data[i] >= '0' && data[i] <= '9' {
  2430. continue
  2431. }
  2432. break
  2433. }
  2434. }
  2435. // exp
  2436. if i == len(data) {
  2437. return i, true
  2438. }
  2439. if data[i] == 'e' || data[i] == 'E' {
  2440. i++
  2441. if i == len(data) {
  2442. return i, false
  2443. }
  2444. if data[i] == '+' || data[i] == '-' {
  2445. i++
  2446. }
  2447. if i == len(data) {
  2448. return i, false
  2449. }
  2450. if data[i] < '0' || data[i] > '9' {
  2451. return i, false
  2452. }
  2453. i++
  2454. for ; i < len(data); i++ {
  2455. if data[i] >= '0' && data[i] <= '9' {
  2456. continue
  2457. }
  2458. break
  2459. }
  2460. }
  2461. return i, true
  2462. }
  2463. func validtrue(data []byte, i int) (outi int, ok bool) {
  2464. if i+3 <= len(data) && data[i] == 'r' && data[i+1] == 'u' &&
  2465. data[i+2] == 'e' {
  2466. return i + 3, true
  2467. }
  2468. return i, false
  2469. }
  2470. func validfalse(data []byte, i int) (outi int, ok bool) {
  2471. if i+4 <= len(data) && data[i] == 'a' && data[i+1] == 'l' &&
  2472. data[i+2] == 's' && data[i+3] == 'e' {
  2473. return i + 4, true
  2474. }
  2475. return i, false
  2476. }
  2477. func validnull(data []byte, i int) (outi int, ok bool) {
  2478. if i+3 <= len(data) && data[i] == 'u' && data[i+1] == 'l' &&
  2479. data[i+2] == 'l' {
  2480. return i + 3, true
  2481. }
  2482. return i, false
  2483. }
  2484. // Valid returns true if the input is valid json.
  2485. //
  2486. // if !gjson.Valid(json) {
  2487. // return errors.New("invalid json")
  2488. // }
  2489. // value := gjson.Get(json, "name.last")
  2490. //
  2491. func Valid(json string) bool {
  2492. _, ok := validpayload(stringBytes(json), 0)
  2493. return ok
  2494. }
  2495. // ValidBytes returns true if the input is valid json.
  2496. //
  2497. // if !gjson.Valid(json) {
  2498. // return errors.New("invalid json")
  2499. // }
  2500. // value := gjson.Get(json, "name.last")
  2501. //
  2502. // If working with bytes, this method preferred over ValidBytes(string(data))
  2503. //
  2504. func ValidBytes(json []byte) bool {
  2505. _, ok := validpayload(json, 0)
  2506. return ok
  2507. }
  2508. func parseUint(s string) (n uint64, ok bool) {
  2509. var i int
  2510. if i == len(s) {
  2511. return 0, false
  2512. }
  2513. for ; i < len(s); i++ {
  2514. if s[i] >= '0' && s[i] <= '9' {
  2515. n = n*10 + uint64(s[i]-'0')
  2516. } else {
  2517. return 0, false
  2518. }
  2519. }
  2520. return n, true
  2521. }
  2522. func parseInt(s string) (n int64, ok bool) {
  2523. var i int
  2524. var sign bool
  2525. if len(s) > 0 && s[0] == '-' {
  2526. sign = true
  2527. i++
  2528. }
  2529. if i == len(s) {
  2530. return 0, false
  2531. }
  2532. for ; i < len(s); i++ {
  2533. if s[i] >= '0' && s[i] <= '9' {
  2534. n = n*10 + int64(s[i]-'0')
  2535. } else {
  2536. return 0, false
  2537. }
  2538. }
  2539. if sign {
  2540. return n * -1, true
  2541. }
  2542. return n, true
  2543. }
  2544. // safeInt validates a given JSON number
  2545. // ensures it lies within the minimum and maximum representable JSON numbers
  2546. func safeInt(f float64) (n int64, ok bool) {
  2547. // https://tc39.es/ecma262/#sec-number.min_safe_integer
  2548. // https://tc39.es/ecma262/#sec-number.max_safe_integer
  2549. if f < -9007199254740991 || f > 9007199254740991 {
  2550. return 0, false
  2551. }
  2552. return int64(f), true
  2553. }
  2554. // execStatic parses the path to find a static value.
  2555. // The input expects that the path already starts with a '!'
  2556. func execStatic(json, path string) (pathOut, res string, ok bool) {
  2557. name := path[1:]
  2558. if len(name) > 0 {
  2559. switch name[0] {
  2560. case '{', '[', '"', '+', '-', '0', '1', '2', '3', '4', '5', '6', '7',
  2561. '8', '9':
  2562. _, res = parseSquash(name, 0)
  2563. pathOut = name[len(res):]
  2564. return pathOut, res, true
  2565. }
  2566. }
  2567. for i := 1; i < len(path); i++ {
  2568. if path[i] == '|' {
  2569. pathOut = path[i:]
  2570. name = path[1:i]
  2571. break
  2572. }
  2573. if path[i] == '.' {
  2574. pathOut = path[i:]
  2575. name = path[1:i]
  2576. break
  2577. }
  2578. }
  2579. switch strings.ToLower(name) {
  2580. case "true", "false", "null", "nan", "inf":
  2581. return pathOut, name, true
  2582. }
  2583. return pathOut, res, false
  2584. }
  2585. // execModifier parses the path to find a matching modifier function.
  2586. // The input expects that the path already starts with a '@'
  2587. func execModifier(json, path string) (pathOut, res string, ok bool) {
  2588. name := path[1:]
  2589. var hasArgs bool
  2590. for i := 1; i < len(path); i++ {
  2591. if path[i] == ':' {
  2592. pathOut = path[i+1:]
  2593. name = path[1:i]
  2594. hasArgs = len(pathOut) > 0
  2595. break
  2596. }
  2597. if path[i] == '|' {
  2598. pathOut = path[i:]
  2599. name = path[1:i]
  2600. break
  2601. }
  2602. if path[i] == '.' {
  2603. pathOut = path[i:]
  2604. name = path[1:i]
  2605. break
  2606. }
  2607. }
  2608. if fn, ok := modifiers[name]; ok {
  2609. var args string
  2610. if hasArgs {
  2611. var parsedArgs bool
  2612. switch pathOut[0] {
  2613. case '{', '[', '"':
  2614. res := Parse(pathOut)
  2615. if res.Exists() {
  2616. args = squash(pathOut)
  2617. pathOut = pathOut[len(args):]
  2618. parsedArgs = true
  2619. }
  2620. }
  2621. if !parsedArgs {
  2622. idx := strings.IndexByte(pathOut, '|')
  2623. if idx == -1 {
  2624. args = pathOut
  2625. pathOut = ""
  2626. } else {
  2627. args = pathOut[:idx]
  2628. pathOut = pathOut[idx:]
  2629. }
  2630. }
  2631. }
  2632. return pathOut, fn(json, args), true
  2633. }
  2634. return pathOut, res, false
  2635. }
  2636. // unwrap removes the '[]' or '{}' characters around json
  2637. func unwrap(json string) string {
  2638. json = trim(json)
  2639. if len(json) >= 2 && (json[0] == '[' || json[0] == '{') {
  2640. json = json[1 : len(json)-1]
  2641. }
  2642. return json
  2643. }
  2644. // DisableModifiers will disable the modifier syntax
  2645. var DisableModifiers = false
  2646. var modifiers = map[string]func(json, arg string) string{
  2647. "pretty": modPretty,
  2648. "ugly": modUgly,
  2649. "reverse": modReverse,
  2650. "this": modThis,
  2651. "flatten": modFlatten,
  2652. "join": modJoin,
  2653. "valid": modValid,
  2654. "keys": modKeys,
  2655. "values": modValues,
  2656. "tostr": modToStr,
  2657. "fromstr": modFromStr,
  2658. "group": modGroup,
  2659. }
  2660. // AddModifier binds a custom modifier command to the GJSON syntax.
  2661. // This operation is not thread safe and should be executed prior to
  2662. // using all other gjson function.
  2663. func AddModifier(name string, fn func(json, arg string) string) {
  2664. modifiers[name] = fn
  2665. }
  2666. // ModifierExists returns true when the specified modifier exists.
  2667. func ModifierExists(name string, fn func(json, arg string) string) bool {
  2668. _, ok := modifiers[name]
  2669. return ok
  2670. }
  2671. // cleanWS remove any non-whitespace from string
  2672. func cleanWS(s string) string {
  2673. for i := 0; i < len(s); i++ {
  2674. switch s[i] {
  2675. case ' ', '\t', '\n', '\r':
  2676. continue
  2677. default:
  2678. var s2 []byte
  2679. for i := 0; i < len(s); i++ {
  2680. switch s[i] {
  2681. case ' ', '\t', '\n', '\r':
  2682. s2 = append(s2, s[i])
  2683. }
  2684. }
  2685. return string(s2)
  2686. }
  2687. }
  2688. return s
  2689. }
  2690. // @pretty modifier makes the json look nice.
  2691. func modPretty(json, arg string) string {
  2692. if len(arg) > 0 {
  2693. opts := *pretty.DefaultOptions
  2694. Parse(arg).ForEach(func(key, value Result) bool {
  2695. switch key.String() {
  2696. case "sortKeys":
  2697. opts.SortKeys = value.Bool()
  2698. case "indent":
  2699. opts.Indent = cleanWS(value.String())
  2700. case "prefix":
  2701. opts.Prefix = cleanWS(value.String())
  2702. case "width":
  2703. opts.Width = int(value.Int())
  2704. }
  2705. return true
  2706. })
  2707. return bytesString(pretty.PrettyOptions(stringBytes(json), &opts))
  2708. }
  2709. return bytesString(pretty.Pretty(stringBytes(json)))
  2710. }
  2711. // @this returns the current element. Can be used to retrieve the root element.
  2712. func modThis(json, arg string) string {
  2713. return json
  2714. }
  2715. // @ugly modifier removes all whitespace.
  2716. func modUgly(json, arg string) string {
  2717. return bytesString(pretty.Ugly(stringBytes(json)))
  2718. }
  2719. // @reverse reverses array elements or root object members.
  2720. func modReverse(json, arg string) string {
  2721. res := Parse(json)
  2722. if res.IsArray() {
  2723. var values []Result
  2724. res.ForEach(func(_, value Result) bool {
  2725. values = append(values, value)
  2726. return true
  2727. })
  2728. out := make([]byte, 0, len(json))
  2729. out = append(out, '[')
  2730. for i, j := len(values)-1, 0; i >= 0; i, j = i-1, j+1 {
  2731. if j > 0 {
  2732. out = append(out, ',')
  2733. }
  2734. out = append(out, values[i].Raw...)
  2735. }
  2736. out = append(out, ']')
  2737. return bytesString(out)
  2738. }
  2739. if res.IsObject() {
  2740. var keyValues []Result
  2741. res.ForEach(func(key, value Result) bool {
  2742. keyValues = append(keyValues, key, value)
  2743. return true
  2744. })
  2745. out := make([]byte, 0, len(json))
  2746. out = append(out, '{')
  2747. for i, j := len(keyValues)-2, 0; i >= 0; i, j = i-2, j+1 {
  2748. if j > 0 {
  2749. out = append(out, ',')
  2750. }
  2751. out = append(out, keyValues[i+0].Raw...)
  2752. out = append(out, ':')
  2753. out = append(out, keyValues[i+1].Raw...)
  2754. }
  2755. out = append(out, '}')
  2756. return bytesString(out)
  2757. }
  2758. return json
  2759. }
  2760. // @flatten an array with child arrays.
  2761. // [1,[2],[3,4],[5,[6,7]]] -> [1,2,3,4,5,[6,7]]
  2762. // The {"deep":true} arg can be provide for deep flattening.
  2763. // [1,[2],[3,4],[5,[6,7]]] -> [1,2,3,4,5,6,7]
  2764. // The original json is returned when the json is not an array.
  2765. func modFlatten(json, arg string) string {
  2766. res := Parse(json)
  2767. if !res.IsArray() {
  2768. return json
  2769. }
  2770. var deep bool
  2771. if arg != "" {
  2772. Parse(arg).ForEach(func(key, value Result) bool {
  2773. if key.String() == "deep" {
  2774. deep = value.Bool()
  2775. }
  2776. return true
  2777. })
  2778. }
  2779. var out []byte
  2780. out = append(out, '[')
  2781. var idx int
  2782. res.ForEach(func(_, value Result) bool {
  2783. var raw string
  2784. if value.IsArray() {
  2785. if deep {
  2786. raw = unwrap(modFlatten(value.Raw, arg))
  2787. } else {
  2788. raw = unwrap(value.Raw)
  2789. }
  2790. } else {
  2791. raw = value.Raw
  2792. }
  2793. raw = strings.TrimSpace(raw)
  2794. if len(raw) > 0 {
  2795. if idx > 0 {
  2796. out = append(out, ',')
  2797. }
  2798. out = append(out, raw...)
  2799. idx++
  2800. }
  2801. return true
  2802. })
  2803. out = append(out, ']')
  2804. return bytesString(out)
  2805. }
  2806. // @keys extracts the keys from an object.
  2807. // {"first":"Tom","last":"Smith"} -> ["first","last"]
  2808. func modKeys(json, arg string) string {
  2809. v := Parse(json)
  2810. if !v.Exists() {
  2811. return "[]"
  2812. }
  2813. obj := v.IsObject()
  2814. var out strings.Builder
  2815. out.WriteByte('[')
  2816. var i int
  2817. v.ForEach(func(key, _ Result) bool {
  2818. if i > 0 {
  2819. out.WriteByte(',')
  2820. }
  2821. if obj {
  2822. out.WriteString(key.Raw)
  2823. } else {
  2824. out.WriteString("null")
  2825. }
  2826. i++
  2827. return true
  2828. })
  2829. out.WriteByte(']')
  2830. return out.String()
  2831. }
  2832. // @values extracts the values from an object.
  2833. // {"first":"Tom","last":"Smith"} -> ["Tom","Smith"]
  2834. func modValues(json, arg string) string {
  2835. v := Parse(json)
  2836. if !v.Exists() {
  2837. return "[]"
  2838. }
  2839. if v.IsArray() {
  2840. return json
  2841. }
  2842. var out strings.Builder
  2843. out.WriteByte('[')
  2844. var i int
  2845. v.ForEach(func(_, value Result) bool {
  2846. if i > 0 {
  2847. out.WriteByte(',')
  2848. }
  2849. out.WriteString(value.Raw)
  2850. i++
  2851. return true
  2852. })
  2853. out.WriteByte(']')
  2854. return out.String()
  2855. }
  2856. // @join multiple objects into a single object.
  2857. // [{"first":"Tom"},{"last":"Smith"}] -> {"first","Tom","last":"Smith"}
  2858. // The arg can be "true" to specify that duplicate keys should be preserved.
  2859. // [{"first":"Tom","age":37},{"age":41}] -> {"first","Tom","age":37,"age":41}
  2860. // Without preserved keys:
  2861. // [{"first":"Tom","age":37},{"age":41}] -> {"first","Tom","age":41}
  2862. // The original json is returned when the json is not an object.
  2863. func modJoin(json, arg string) string {
  2864. res := Parse(json)
  2865. if !res.IsArray() {
  2866. return json
  2867. }
  2868. var preserve bool
  2869. if arg != "" {
  2870. Parse(arg).ForEach(func(key, value Result) bool {
  2871. if key.String() == "preserve" {
  2872. preserve = value.Bool()
  2873. }
  2874. return true
  2875. })
  2876. }
  2877. var out []byte
  2878. out = append(out, '{')
  2879. if preserve {
  2880. // Preserve duplicate keys.
  2881. var idx int
  2882. res.ForEach(func(_, value Result) bool {
  2883. if !value.IsObject() {
  2884. return true
  2885. }
  2886. if idx > 0 {
  2887. out = append(out, ',')
  2888. }
  2889. out = append(out, unwrap(value.Raw)...)
  2890. idx++
  2891. return true
  2892. })
  2893. } else {
  2894. // Deduplicate keys and generate an object with stable ordering.
  2895. var keys []Result
  2896. kvals := make(map[string]Result)
  2897. res.ForEach(func(_, value Result) bool {
  2898. if !value.IsObject() {
  2899. return true
  2900. }
  2901. value.ForEach(func(key, value Result) bool {
  2902. k := key.String()
  2903. if _, ok := kvals[k]; !ok {
  2904. keys = append(keys, key)
  2905. }
  2906. kvals[k] = value
  2907. return true
  2908. })
  2909. return true
  2910. })
  2911. for i := 0; i < len(keys); i++ {
  2912. if i > 0 {
  2913. out = append(out, ',')
  2914. }
  2915. out = append(out, keys[i].Raw...)
  2916. out = append(out, ':')
  2917. out = append(out, kvals[keys[i].String()].Raw...)
  2918. }
  2919. }
  2920. out = append(out, '}')
  2921. return bytesString(out)
  2922. }
  2923. // @valid ensures that the json is valid before moving on. An empty string is
  2924. // returned when the json is not valid, otherwise it returns the original json.
  2925. func modValid(json, arg string) string {
  2926. if !Valid(json) {
  2927. return ""
  2928. }
  2929. return json
  2930. }
  2931. // @fromstr converts a string to json
  2932. // "{\"id\":1023,\"name\":\"alert\"}" -> {"id":1023,"name":"alert"}
  2933. func modFromStr(json, arg string) string {
  2934. if !Valid(json) {
  2935. return ""
  2936. }
  2937. return Parse(json).String()
  2938. }
  2939. // @tostr converts a string to json
  2940. // {"id":1023,"name":"alert"} -> "{\"id\":1023,\"name\":\"alert\"}"
  2941. func modToStr(str, arg string) string {
  2942. return string(AppendJSONString(nil, str))
  2943. }
  2944. func modGroup(json, arg string) string {
  2945. res := Parse(json)
  2946. if !res.IsObject() {
  2947. return ""
  2948. }
  2949. var all [][]byte
  2950. res.ForEach(func(key, value Result) bool {
  2951. if !value.IsArray() {
  2952. return true
  2953. }
  2954. var idx int
  2955. value.ForEach(func(_, value Result) bool {
  2956. if idx == len(all) {
  2957. all = append(all, []byte{})
  2958. }
  2959. all[idx] = append(all[idx], ("," + key.Raw + ":" + value.Raw)...)
  2960. idx++
  2961. return true
  2962. })
  2963. return true
  2964. })
  2965. var data []byte
  2966. data = append(data, '[')
  2967. for i, item := range all {
  2968. if i > 0 {
  2969. data = append(data, ',')
  2970. }
  2971. data = append(data, '{')
  2972. data = append(data, item[1:]...)
  2973. data = append(data, '}')
  2974. }
  2975. data = append(data, ']')
  2976. return string(data)
  2977. }
  2978. // stringHeader instead of reflect.StringHeader
  2979. type stringHeader struct {
  2980. data unsafe.Pointer
  2981. len int
  2982. }
  2983. // sliceHeader instead of reflect.SliceHeader
  2984. type sliceHeader struct {
  2985. data unsafe.Pointer
  2986. len int
  2987. cap int
  2988. }
  2989. // getBytes casts the input json bytes to a string and safely returns the
  2990. // results as uniquely allocated data. This operation is intended to minimize
  2991. // copies and allocations for the large json string->[]byte.
  2992. func getBytes(json []byte, path string) Result {
  2993. var result Result
  2994. if json != nil {
  2995. // unsafe cast to string
  2996. result = Get(*(*string)(unsafe.Pointer(&json)), path)
  2997. // safely get the string headers
  2998. rawhi := *(*stringHeader)(unsafe.Pointer(&result.Raw))
  2999. strhi := *(*stringHeader)(unsafe.Pointer(&result.Str))
  3000. // create byte slice headers
  3001. rawh := sliceHeader{data: rawhi.data, len: rawhi.len, cap: rawhi.len}
  3002. strh := sliceHeader{data: strhi.data, len: strhi.len, cap: rawhi.len}
  3003. if strh.data == nil {
  3004. // str is nil
  3005. if rawh.data == nil {
  3006. // raw is nil
  3007. result.Raw = ""
  3008. } else {
  3009. // raw has data, safely copy the slice header to a string
  3010. result.Raw = string(*(*[]byte)(unsafe.Pointer(&rawh)))
  3011. }
  3012. result.Str = ""
  3013. } else if rawh.data == nil {
  3014. // raw is nil
  3015. result.Raw = ""
  3016. // str has data, safely copy the slice header to a string
  3017. result.Str = string(*(*[]byte)(unsafe.Pointer(&strh)))
  3018. } else if uintptr(strh.data) >= uintptr(rawh.data) &&
  3019. uintptr(strh.data)+uintptr(strh.len) <=
  3020. uintptr(rawh.data)+uintptr(rawh.len) {
  3021. // Str is a substring of Raw.
  3022. start := uintptr(strh.data) - uintptr(rawh.data)
  3023. // safely copy the raw slice header
  3024. result.Raw = string(*(*[]byte)(unsafe.Pointer(&rawh)))
  3025. // substring the raw
  3026. result.Str = result.Raw[start : start+uintptr(strh.len)]
  3027. } else {
  3028. // safely copy both the raw and str slice headers to strings
  3029. result.Raw = string(*(*[]byte)(unsafe.Pointer(&rawh)))
  3030. result.Str = string(*(*[]byte)(unsafe.Pointer(&strh)))
  3031. }
  3032. }
  3033. return result
  3034. }
  3035. // fillIndex finds the position of Raw data and assigns it to the Index field
  3036. // of the resulting value. If the position cannot be found then Index zero is
  3037. // used instead.
  3038. func fillIndex(json string, c *parseContext) {
  3039. if len(c.value.Raw) > 0 && !c.calcd {
  3040. jhdr := *(*stringHeader)(unsafe.Pointer(&json))
  3041. rhdr := *(*stringHeader)(unsafe.Pointer(&(c.value.Raw)))
  3042. c.value.Index = int(uintptr(rhdr.data) - uintptr(jhdr.data))
  3043. if c.value.Index < 0 || c.value.Index >= len(json) {
  3044. c.value.Index = 0
  3045. }
  3046. }
  3047. }
  3048. func stringBytes(s string) []byte {
  3049. return *(*[]byte)(unsafe.Pointer(&sliceHeader{
  3050. data: (*stringHeader)(unsafe.Pointer(&s)).data,
  3051. len: len(s),
  3052. cap: len(s),
  3053. }))
  3054. }
  3055. func bytesString(b []byte) string {
  3056. return *(*string)(unsafe.Pointer(&b))
  3057. }
  3058. func revSquash(json string) string {
  3059. // reverse squash
  3060. // expects that the tail character is a ']' or '}' or ')' or '"'
  3061. // squash the value, ignoring all nested arrays and objects.
  3062. i := len(json) - 1
  3063. var depth int
  3064. if json[i] != '"' {
  3065. depth++
  3066. }
  3067. if json[i] == '}' || json[i] == ']' || json[i] == ')' {
  3068. i--
  3069. }
  3070. for ; i >= 0; i-- {
  3071. switch json[i] {
  3072. case '"':
  3073. i--
  3074. for ; i >= 0; i-- {
  3075. if json[i] == '"' {
  3076. esc := 0
  3077. for i > 0 && json[i-1] == '\\' {
  3078. i--
  3079. esc++
  3080. }
  3081. if esc%2 == 1 {
  3082. continue
  3083. }
  3084. i += esc
  3085. break
  3086. }
  3087. }
  3088. if depth == 0 {
  3089. if i < 0 {
  3090. i = 0
  3091. }
  3092. return json[i:]
  3093. }
  3094. case '}', ']', ')':
  3095. depth++
  3096. case '{', '[', '(':
  3097. depth--
  3098. if depth == 0 {
  3099. return json[i:]
  3100. }
  3101. }
  3102. }
  3103. return json
  3104. }
  3105. // Paths returns the original GJSON paths for a Result where the Result came
  3106. // from a simple query path that returns an array, like:
  3107. //
  3108. // gjson.Get(json, "friends.#.first")
  3109. //
  3110. // The returned value will be in the form of a JSON array:
  3111. //
  3112. // ["friends.0.first","friends.1.first","friends.2.first"]
  3113. //
  3114. // The param 'json' must be the original JSON used when calling Get.
  3115. //
  3116. // Returns an empty string if the paths cannot be determined, which can happen
  3117. // when the Result came from a path that contained a multipath, modifier,
  3118. // or a nested query.
  3119. func (t Result) Paths(json string) []string {
  3120. if t.Indexes == nil {
  3121. return nil
  3122. }
  3123. paths := make([]string, 0, len(t.Indexes))
  3124. t.ForEach(func(_, value Result) bool {
  3125. paths = append(paths, value.Path(json))
  3126. return true
  3127. })
  3128. if len(paths) != len(t.Indexes) {
  3129. return nil
  3130. }
  3131. return paths
  3132. }
  3133. // Path returns the original GJSON path for a Result where the Result came
  3134. // from a simple path that returns a single value, like:
  3135. //
  3136. // gjson.Get(json, "friends.#(last=Murphy)")
  3137. //
  3138. // The returned value will be in the form of a JSON string:
  3139. //
  3140. // "friends.0"
  3141. //
  3142. // The param 'json' must be the original JSON used when calling Get.
  3143. //
  3144. // Returns an empty string if the paths cannot be determined, which can happen
  3145. // when the Result came from a path that contained a multipath, modifier,
  3146. // or a nested query.
  3147. func (t Result) Path(json string) string {
  3148. var path []byte
  3149. var comps []string // raw components
  3150. i := t.Index - 1
  3151. if t.Index+len(t.Raw) > len(json) {
  3152. // JSON cannot safely contain Result.
  3153. goto fail
  3154. }
  3155. if !strings.HasPrefix(json[t.Index:], t.Raw) {
  3156. // Result is not at the JSON index as exepcted.
  3157. goto fail
  3158. }
  3159. for ; i >= 0; i-- {
  3160. if json[i] <= ' ' {
  3161. continue
  3162. }
  3163. if json[i] == ':' {
  3164. // inside of object, get the key
  3165. for ; i >= 0; i-- {
  3166. if json[i] != '"' {
  3167. continue
  3168. }
  3169. break
  3170. }
  3171. raw := revSquash(json[:i+1])
  3172. i = i - len(raw)
  3173. comps = append(comps, raw)
  3174. // key gotten, now squash the rest
  3175. raw = revSquash(json[:i+1])
  3176. i = i - len(raw)
  3177. i++ // increment the index for next loop step
  3178. } else if json[i] == '{' {
  3179. // Encountered an open object. The original result was probably an
  3180. // object key.
  3181. goto fail
  3182. } else if json[i] == ',' || json[i] == '[' {
  3183. // inside of an array, count the position
  3184. var arrIdx int
  3185. if json[i] == ',' {
  3186. arrIdx++
  3187. i--
  3188. }
  3189. for ; i >= 0; i-- {
  3190. if json[i] == ':' {
  3191. // Encountered an unexpected colon. The original result was
  3192. // probably an object key.
  3193. goto fail
  3194. } else if json[i] == ',' {
  3195. arrIdx++
  3196. } else if json[i] == '[' {
  3197. comps = append(comps, strconv.Itoa(arrIdx))
  3198. break
  3199. } else if json[i] == ']' || json[i] == '}' || json[i] == '"' {
  3200. raw := revSquash(json[:i+1])
  3201. i = i - len(raw) + 1
  3202. }
  3203. }
  3204. }
  3205. }
  3206. if len(comps) == 0 {
  3207. if DisableModifiers {
  3208. goto fail
  3209. }
  3210. return "@this"
  3211. }
  3212. for i := len(comps) - 1; i >= 0; i-- {
  3213. rcomp := Parse(comps[i])
  3214. if !rcomp.Exists() {
  3215. goto fail
  3216. }
  3217. comp := escapeComp(rcomp.String())
  3218. path = append(path, '.')
  3219. path = append(path, comp...)
  3220. }
  3221. if len(path) > 0 {
  3222. path = path[1:]
  3223. }
  3224. return string(path)
  3225. fail:
  3226. return ""
  3227. }
  3228. // isSafePathKeyChar returns true if the input character is safe for not
  3229. // needing escaping.
  3230. func isSafePathKeyChar(c byte) bool {
  3231. return c <= ' ' || c > '~' || c == '_' || c == '-' || c == ':' ||
  3232. (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  3233. (c >= '0' && c <= '9')
  3234. }
  3235. // escapeComp escaped a path compontent, making it safe for generating a
  3236. // path for later use.
  3237. func escapeComp(comp string) string {
  3238. for i := 0; i < len(comp); i++ {
  3239. if !isSafePathKeyChar(comp[i]) {
  3240. ncomp := []byte(comp[:i])
  3241. for ; i < len(comp); i++ {
  3242. if !isSafePathKeyChar(comp[i]) {
  3243. ncomp = append(ncomp, '\\')
  3244. }
  3245. ncomp = append(ncomp, comp[i])
  3246. }
  3247. return string(ncomp)
  3248. }
  3249. }
  3250. return comp
  3251. }