unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* small seq function for json data
@ 2016-10-16  5:29 Stefan Huchler
  2016-10-16  7:25 ` Helmut Eller
  2016-10-17  8:34 ` Nicolas Petton
  0 siblings, 2 replies; 4+ messages in thread
From: Stefan Huchler @ 2016-10-16  5:29 UTC (permalink / raw)
  To: emacs-devel

Hello,

accessing data coming from json requests can be a little bit anoying
in elisp I think.

So inspired by let-alist I wrote a more powerful version that supports
also vector which is also used in json messages.

(setq x '((foo . [((bar . "string of interest"))])))

(defun sbit-seq-get (seq path)
  (cond ((null path) seq)
	 ((listp seq)
	  (sbit-seq-get (cdr (assoc (car path) seq)) (cdr path))
	  )
	((vectorp seq)
	 (sbit-seq-get (elt seq (car path)) (cdr path))
	 )
	(t seq)))

(sbit-seq-get x '(foo 0 bar))

alternative manuall access would look like that:
(cdr (assoc 'bar (elt (cdr (assoc 'foo x))0)))

or with alist-get:
(let-alist (elt (let-alist x .foo) 0 ) .bar)


Does that make sense? Maybe integrate something similar to seq.el?
Eventualy plist support could maybe added, too. I dont know :)

I have more of a python background there its also fine to mix such keys
like:

x["foo"][0]["bar"]

Also I find it pretty inconsistent that elt as example uses the key
after the sequence also let-alist, but assoc and nth has first the key
as parameter, key first makes more sense in my oppinion (but) maybe I
am to bad with paredit to jump to the end of the parens :)

Well shure it would be a pain in the ass to change all addons so
I guess we have to live with that :)

Any thoughts on that?




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: small seq function for json data
  2016-10-16  5:29 small seq function for json data Stefan Huchler
@ 2016-10-16  7:25 ` Helmut Eller
  2016-10-17  8:34 ` Nicolas Petton
  1 sibling, 0 replies; 4+ messages in thread
From: Helmut Eller @ 2016-10-16  7:25 UTC (permalink / raw)
  To: emacs-devel; +Cc: Stefan Huchler

On Sun, Oct 16 2016, Stefan Huchler wrote:

> accessing data coming from json requests can be a little bit anoying
> in elisp I think.

Yes, sounds familiar.

> So inspired by let-alist I wrote a more powerful version that supports
> also vector which is also used in json messages.
>
> (setq x '((foo . [((bar . "string of interest"))])))
>
> (defun sbit-seq-get (seq path)
>   (cond ((null path) seq)
> 	 ((listp seq)
> 	  (sbit-seq-get (cdr (assoc (car path) seq)) (cdr path))
> 	  )
> 	((vectorp seq)
> 	 (sbit-seq-get (elt seq (car path)) (cdr path))
> 	 )
> 	(t seq)))
>
> (sbit-seq-get x '(foo 0 bar))

I've used this:

(defun json--ref (json key)
  (cl-etypecase key
    (symbol (let ((probe (assq key json)))
	      (cond (probe (cdr probe))
                    (t (error "No entry for key: %S %S" key json)))))
    (integer (aref json key))))

(defun json-ref (json key &rest keys)
  (let ((tmp (json--ref json key)))
    (while keys
      (setq tmp (json--ref tmp (pop keys))))
    tmp))

(json-ref x 'foo 0 'bar)

A difference is that json--ref signals an error if the key is not in
present.

> Does that make sense? Maybe integrate something similar to seq.el?

I think json.el would be the natural place; json objects are more like
trees than sequences.

Helmut




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: small seq function for json data
  2016-10-16  5:29 small seq function for json data Stefan Huchler
  2016-10-16  7:25 ` Helmut Eller
@ 2016-10-17  8:34 ` Nicolas Petton
  2016-10-18  3:04   ` Stefan Huchler
  1 sibling, 1 reply; 4+ messages in thread
From: Nicolas Petton @ 2016-10-17  8:34 UTC (permalink / raw)
  To: Stefan Huchler, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 613 bytes --]

Stefan Huchler <stefan.huchler@mail.de> writes:

> Hello,

Hi Stefan,

> So inspired by let-alist I wrote a more powerful version that supports
> also vector which is also used in json messages.
>
> (setq x '((foo . [((bar . "string of interest"))])))
>
> (defun sbit-seq-get (seq path)
>   (cond ((null path) seq)
> 	 ((listp seq)
> 	  (sbit-seq-get (cdr (assoc (car path) seq)) (cdr path))
> 	  )
> 	((vectorp seq)
> 	 (sbit-seq-get (elt seq (car path)) (cdr path))
> 	 )
> 	(t seq)))
>
> (sbit-seq-get x '(foo 0 bar))

Did you have a look at `map-nested-elt'? I often use it to access JSON
data.

Cheers,
Nico

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 512 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: small seq function for json data
  2016-10-17  8:34 ` Nicolas Petton
@ 2016-10-18  3:04   ` Stefan Huchler
  0 siblings, 0 replies; 4+ messages in thread
From: Stefan Huchler @ 2016-10-18  3:04 UTC (permalink / raw)
  To: emacs-devel

Nicolas Petton <nicolas@petton.fr> writes:

> Stefan Huchler <stefan.huchler@mail.de> writes:
>
>> Hello,
>
> Hi Stefan,
>
>> So inspired by let-alist I wrote a more powerful version that supports
>> also vector which is also used in json messages.
>>
>> (setq x '((foo . [((bar . "string of interest"))])))
>>
>> (defun sbit-seq-get (seq path)
>>   (cond ((null path) seq)
>> 	 ((listp seq)
>> 	  (sbit-seq-get (cdr (assoc (car path) seq)) (cdr path))
>> 	  )
>> 	((vectorp seq)
>> 	 (sbit-seq-get (elt seq (car path)) (cdr path))
>> 	 )
>> 	(t seq)))
>>
>> (sbit-seq-get x '(foo 0 bar))
>
> Did you have a look at `map-nested-elt'? I often use it to access JSON
> data.

Well,

as far as I can tell it does what I want to do (and better), now you make me
wanna update to emacs 25.1 (dont tease me).

I mean I have emacs 25.1 installed paralell anyway, but dont want to
fiddle with the exwm package in nixos, in which I live :)

Whatever not that important for now I guess but it is in upstream good
to know, then I have to not write a bug to add somethnig like that in
seq.el.




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-10-18  3:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-10-16  5:29 small seq function for json data Stefan Huchler
2016-10-16  7:25 ` Helmut Eller
2016-10-17  8:34 ` Nicolas Petton
2016-10-18  3:04   ` Stefan Huchler

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).