Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6659811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:04:35+00:00 2026-05-26T02:04:35+00:00

I’m looking for a way to efficiently serialize Clojure objects into a binary format

  • 0

I’m looking for a way to efficiently serialize Clojure objects into a binary format – i.e. not just doing the classic print and read text serialization.

i.e. I want to do something like:

(def orig-data {:name "Data Object" 
                :data (get-big-java-array) 
                :other (get-clojure-data-stuff)})

(def binary (serialize orig-data))

;; here "binary" is a raw binary form, e.g. a Java byte array
;; so it can be persisted in key/value store or sent over network etc.

;; now check it works!

(def new-data (deserialize binary))

(= new-data orig-data)
=> true

The motivation is that I have some large data structures that contain a significant amount of binary data (in Java arrays), and I want to avoid the overhead of converting these all to text and back again. In addition, I’m trying to keep the format compact in order to minimise network bandwidth usage.

Specific features I’d like to have:

  • Lightweight, pure-Java implementation
  • Support all of Clojure’s standard data structures as well as all Java primitives, arrays etc.
  • No need for extra build steps / configuration files – I’d rather it just worked “out of the box”
  • Good performance both in terms of processing time required
  • Compactness in terms of binary encoded representation

What’s the best / standard approach to doing this in Clojure?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:04:36+00:00Added an answer on May 26, 2026 at 2:04 am

    I may be missing something here, but what’s wrong with the standard Java serialization? Too slow, too big, something else?

    A Clojure wrapper for plain Java serialization could be something like this:

    (defn serializable? [v]
      (instance? java.io.Serializable v))
    
    (defn serialize 
      "Serializes value, returns a byte array"
      [v]
      (let [buff (java.io.ByteArrayOutputStream. 1024)]
        (with-open [dos (java.io.ObjectOutputStream. buff)]
          (.writeObject dos v))
        (.toByteArray buff)))
    
    (defn deserialize 
      "Accepts a byte array, returns deserialized value"
      [bytes]
      (with-open [dis (java.io.ObjectInputStream.
                       (java.io.ByteArrayInputStream. bytes))]
        (.readObject dis)))
    
     user> (= (range 10) (deserialize (serialize (range 10))))
     true
    

    There are values that cannot be serialized, e.g. Java streams and Clojure atom/agent/future, but it should work for most plain values, including Java primitives and arrays and Clojure functions, collections and records.

    Whether you actually save anything depends. In my limited testing on smallish data sets serializing to text and binary seems to be about the same time and space.

    But for the special case where the bulk of the data is arrays of Java primitives, Java serialization can be orders of magnitude faster and save a significant chunk of space. (Quick test on a laptop, 100k random bytes: serialize 0.9 ms, 100kB; text 490 ms, 700kB.)

    Note that the (= new-data orig-data) test doesn’t work for arrays (it delegates to Java’s equals, which for arrays just tests whether it’s the same object), so you may want/need to write your own equality function to test the serialization.

    user> (def a (range 10))
    user> (= a (range 10))
    true
    user> (= (into-array a) (into-array a))
    false
    user> (.equals (into-array a) (into-array a))
    false
    user> (java.util.Arrays/equals (into-array a) (into-array a))
    true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.