Skip to content

Instantly share code, notes, and snippets.

@nbardiuk
Created October 6, 2021 20:17
Show Gist options
  • Save nbardiuk/bae98139a0e85bdf2ace710535fcdabc to your computer and use it in GitHub Desktop.
Save nbardiuk/bae98139a0e85bdf2ace710535fcdabc to your computer and use it in GitHub Desktop.
Concat JSON
(ns core
(:require
[clj-http.client :as http]
[cheshire.core :as json]
[ring.adapter.jetty :refer [run-jetty]]))
;; Clojure sequences can be chunked in batches of 32 items
;; for the test I want to emit items one at a time
;; https://stackoverflow.com/a/3409568/187261
(defn unchunk [s]
(when (seq s)
(lazy-seq (cons (first s) (unchunk (next s))))))
(defn server [request]
{:status 200
:headers {"Content-Type" "application/json"}
:body
(for [id (unchunk (range 5))]
(do
(println ">>" id)
(json/generate-string
{:id id
:some-data (range 100000)})))})
(defn client []
(let [{body-reader :body} (http/get "http://localhost:3000"
{:as :reader})]
(doseq [item (json/parsed-seq body-reader true)]
(println "<<" (:id item)))))
(comment
(run-jetty #'server {:port 3000 :join? false})
(client))
(defproject concat-json "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.10.1"]
[clj-http "3.12.3"]
[cheshire "5.10.1"]
[ring/ring-core "1.9.4"]
[ring/ring-jetty-adapter "1.9.4"]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment