Converting json to work with clojure.spec

688 views
Skip to first unread message

Jonathon McKitrick

unread,
Nov 19, 2016, 8:23:16 AM11/19/16
to Clojure
A good chunk of my API consists of returning Yesql directly as the body of the response. But clojure.spec wants namespaced keywords. Is there a simple way to wrap my json responses to make them conform to this requirement?

Alex Miller

unread,
Nov 19, 2016, 9:23:32 AM11/19/16
to Clojure
s/keys has :req-un and :opt-un alternatives for un-qualified keys.

Jonathon McKitrick

unread,
Dec 4, 2016, 10:11:55 AM12/4/16
to Clojure
Ah, I see that now.

That being said, I see the benefits in moving to namespace qualified keys. Currently, I'm returning structures directly in Compojure handlers, and the JSON conversion is implicitly handled. I checked Cheshire and didn't immediately see a way to generate namespaced keys. What's the best way to do this?

Brian Scaturro

unread,
Aug 6, 2017, 1:02:37 PM8/6/17
to Clojure
Hi Jonathon,

I am trying to figure out the same thing. I am using all namespace qualified keys, but curious how this works when using s/conform on a json payload. Did you ever find a solution for this?

Thanks!
Brian

Peter Hull

unread,
Aug 7, 2017, 4:04:21 AM8/7/17
to Clojure

On Sunday, 4 December 2016 15:11:55 UTC, Jonathon McKitrick wrote:
That being said, I see the benefits in moving to namespace qualified keys. Currently, I'm returning structures directly in Compojure handlers, and the JSON conversion is implicitly handled. I checked Cheshire and didn't immediately see a way to generate namespaced keys. What's the best way to do this?
parse-string takes a second argument that converts from a JSON string key to whatever you want for your clojure key - see 3rd example in https://github.com/dakrone/cheshire#decoding
So you could have something like:
 
(ns chtst.core
 
(:require [cheshire.core :refer [parse-string]]
           
[clojure.pprint :refer [pprint]])
 
(:gen-class))

(def fields { "name" ::name, "age" ::age})

(defn -main
 
[& args]
 
(let [example (parse-string "{\"name\":\"fred\", \"age\":29}" fields)]
   
(pprint example)))



However this doesn't produce good results if your JSON contains a key you haven't listed in fields - so you'd probably have to write an actual function to do the mapping.

Does that help?
Pete

jmcki...@gmail.com

unread,
Aug 8, 2017, 8:03:08 AM8/8/17
to Clojure
Hmm, that might be useful. I assume it would make sense for an API called from other apps, but not so much for internal web services called from your own client app.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/hfSq0gaeFAc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Jonathon McKitrick

Sean Corfield

unread,
Aug 11, 2017, 1:27:38 PM8/11/17
to clo...@googlegroups.com

> However this doesn't produce good results if your JSON contains a key you haven't listed in fields

> so you'd probably have to write an actual function to do the mapping.

 

(def my-ns *ns*) ; bind at compile time

;; or use a constant string for the ns if you want

(defn fields [k] (keyword (str my-ns) k))

 

(fields “name”)

;;=> :chtst.core/name

 

Sean Corfield -- (970) FOR-SEAN -- (904) 302-SEAN
An Architect's View -- http://corfield.org/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood

--

You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---

You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages