You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gjson.go 66KB

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