Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Library to convert Clojure data into different types
Clojure
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
src/coercer
test/coercer/test
.gitignore
README.md
project.clj

README.md

Coercer

A library for converting data of one type into another type.

Installation

Add the following dependency to your project.clj file:

[coercer "0.2.0"]

Usage

Use the coerce multimethod to convert between types:

(use 'coercer.core)

(coerce "50" Integer)  ; => 50

Supported Types

Currently coerce supports the following types by default:

  • java.lang.String
  • java.lang.Integer
  • java.lang.Long
  • java.lang.Float
  • java.lang.Double
  • clojure.lang.Keyword
  • clojure.lang.Symbol
  • java.util.Date
  • org.joda.time.DateTime

But new coercions can be added by extending the coerce multimethod.

More Examples

Basic types:

(coerce 1 String)      ; => "1"
(coerce "5.5" Double)  ; => 5.5
(coerce "foo" Keyword) ; => :foo
(coerce :abc String)   ; => "abc"

Dates and date-times:

(use '[clj-time.core :only (now)])
(import 'java.util.Date)

(coerce (now) String)       ; => "2012-04-17T20:53:55.552Z"
(coerce (now) Long)         ; => 1334696086921
(coerce 1334696086921 Date) ; => #<Date Tue Apr 17 21:54:46 BST 2012>

License

Copyright © 2012 James Reeves

Distributed under the Eclipse Public License, the same as Clojure.

Something went wrong with that request. Please try again.