gjson.go 57 KB

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