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 61KB

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