gjson.go 70 KB

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