gjson.go 60 KB

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