https://github.com/weavejester/coercer
Library to convert Clojure data into different types
https://github.com/weavejester/coercer
Last synced: 5 months ago
JSON representation
Library to convert Clojure data into different types
- Host: GitHub
- URL: https://github.com/weavejester/coercer
- Owner: weavejester
- Created: 2012-04-15T20:44:48.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2013-03-15T11:49:32.000Z (almost 13 years ago)
- Last Synced: 2025-06-09T17:13:34.283Z (7 months ago)
- Language: Clojure
- Homepage:
- Size: 109 KB
- Stars: 15
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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:
```clojure
(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:
```clojure
(coerce 1 String) ; => "1"
(coerce "5.5" Double) ; => 5.5
(coerce "foo" Keyword) ; => :foo
(coerce :abc String) ; => "abc"
```
Dates and date-times:
```clojure
(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) ; => #
```
## License
Copyright © 2012 James Reeves
Distributed under the Eclipse Public License, the same as Clojure.