Liking cljdoc? Tell your friends :D

transit-java

Transit is a data format and a set of libraries for conveying values between applications written in different languages. This library provides support for marshalling Transit data to/from Java.

This implementation's major.minor version number corresponds to the version of the Transit specification it supports.

NOTE: Transit is intended primarily as a wire protocol for transferring data between applications. If storing Transit data durably, readers and writers are expected to use the same version of Transit and you are responsible for migrating/transforming/re-storing that data when and if the transit format changes.

Releases and Dependency Information

Maven dependency information:

<dependency>
  <groupId>com.cognitect</groupId>
  <artifactId>transit-java</artifactId>
  <version>1.1.401-alpha</version>
</dependency>

Usage

import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import com.cognitect.transit.TransitFactory;
import com.cognitect.transit.Reader;
import com.cognitect.transit.Writer;

// Write the data to a stream
OutputStream out = new ByteArrayOutputStream();
Writer writer = TransitFactory.writer(TransitFactory.Format.MSGPACK, out);
writer.write(data);

// Read the data from a stream
InputStream in = new ByteArrayInputStream(out.toByteArray());
Reader reader = TransitFactory.reader(TransitFactory.Format.MSGPACK, in);
Object data = reader.read();

Custom write handler

public class Point {
    public final int x;
    public final int y;
    public Point(int x,int y) {this.x = x; this.y = y;}
    public String toString() { return "Point at " + x + ", " + y; }
    public boolean equals(Object other) { return other instanceof Point &&
            ((Point)other).x == x &&
            ((Point)other).y == y; }
    public int hashCode() { return x * y; }
}

Map<Class, WriteHandler<?,?>> customHandlers = new HashMap<Class, WriteHandler<?,?>>(){{
    put(Point.class, new WriteHandler() {
        @Override
        public String tag(Object o) { return "point"; }
        @Override
        public Object rep(Object o) { return Arrays.asList(((Point)o).x, ((Point)o).y); }
        @Override
        public String stringRep(Object o) { return rep(o).toString(); }
        @Override
        public WriteHandler getVerboseHandler() { return this; }
    });
}};
OutputStream out = new ByteArrayOutputStream();
Writer w = TransitFactory.writer(TransitFactory.Format.JSON, out, TransitFactory.writeHandlerMap(customHandlers));
w.write(new Point(37, 42));
System.out.print(out.toString());
;; => ["~#point",[37,42]]

Custom read handler

Map<String, ReadHandler<?, ?>> customHandlers = new HashMap<String, ReadHandler<?, ?>>() {{
    put("point", new ReadHandler() {
        @Override
        public Object fromRep(Object o) {
            List coords = (List) o;
            int x = ((Long) coords.get(0)).intValue();
            int y = ((Long) coords.get(1)).intValue();
            return new Point(x,y);
        }
    });
}};
InputStream in = new ByteArrayInputStream("[\"~#point\",[37,42]]".getBytes());
Reader reader = TransitFactory.reader(TransitFactory.Format.JSON, in, customHandlers);
System.out.print(reader.read());
// => Point at 37, 42

Custom default write handler

WriteHandler customDefaultWriteHandler = new WriteHandler() {
    @Override
    public String tag(Object o) { return "unknown"; }
    @Override
    public Object rep(Object o) { return o.toString(); }
    @Override
    public String stringRep(Object o) { return o.toString(); }
    @Override
    public WriteHandler getVerboseHandler() { return this; }
};
OutputStream out = new ByteArrayOutputStream();
Writer w = TransitFactory.writer(TransitFactory.Format.JSON, out, customDefaultWriteHandler);
w.write(new Point(37,42));
System.out.print(out.toString());
// => "[\"~#unknown\",\"Point at 37, 42\"]"

Custom default read handler

DefaultReadHandler readHandler = new DefaultReadHandler() {
    @Override
    public Object fromRep(String tag, Object rep) {
        return tag + ": " + rep.toString();
    }
};
InputStream in = new ByteArrayInputStream("[\"~#unknown\",[37,42]]".getBytes());
Reader reader = TransitFactory.reader(TransitFactory.Format.JSON, in, readHandler);
System.out.print(reader.read());
// => unknown: [37, 42]

Default Type Mapping

Transit typeWrite acceptsRead returns
nullnullnull
stringjava.lang.Stringjava.lang.String
booleanjava.lang.Booleanjava.lang.Boolean
integerjava.lang.Byte, java.lang.Short, java.lang.Integer, java.lang.Longjava.lang.Long
decimaljava.lang.Float, java.lang.Doublejava.lang.Double
keywordcognitect.transit.Keywordcognitect.transit.Keyword
symbolcognitect.transit.Symbolcognitect.transit.Symbol
big decimaljava.math.BigDecimaljava.math.BigDecimal
big integerjava.math.BigIntegerjava.math.BigInteger
timejava.util.Datelong
urijava.net.URI, cognitect.transit.URIcognitect.transit.URI
uuidjava.util.UUIDjava.util.UUID
charjava.lang.Characterjava.lang.Character
arrayObject[],primitive arraysjava.util.ArrayList
listjava.util.Listjava.util.LinkedList
setjava.util.Setjava.util.HashSet
mapjava.util.Mapjava.util.HashMap
linkcognitect.transit.Linkcognitect.transit.Link
ratio +cognitect.transit.Ratiocognitect.transit.Ratio

+ Extension type

Layered Implementations

This library is specifically designed to support layering Transit implementations for other JVM-based languages on top of it. There are three steps to implementing a library for a new language on top of this:

  • Implement WriteHandlers and ReadHandlers specific for the target language. Typically, WriteHandlers will be used in addition to the ones provided by the Java library (see TransitFactory.defaultWriteHandlers). ReadHandlers will be used in place of some of the ones provided by the Java Libary (see TransitFactory.defaultReadHandlers).

  • Implement a factory API to create Readers and Writers. In general, Readers and Writers encapsulate the stream they work with. The APIs should enable an application to provide custom WriteHandlers and ReadHandlers, which get merged with the ones defined by the new library as well as the defaults provided by the Java library. The Reader API should also provide a way to specify a default behavior if no ReadHandler is available for a specific Transit value (see com.cognitect.transit.DefaultReadHandler). The factory API should delegate to TransitFactory to create Readers and Writers with the correct options.

  • Implement a MapReader and an ArrayReader for unmarshaling these Transit ground types into objects appropriate for the target language. In the factory API for creating Readers, use each new Reader's com.cognitect.transit.SPI.ReaderSPI interface to attach instances of the new library's custom MapReader and ArrayReader implementations to a Reader before returning it. This must be done before the Reader instance is used to read data.

    N.B. The ReaderSPI interface is in an impl package because it is only intended to be used by layered Transit libraries, not by applications using Transit.

The Clojure Transit library is implemented using this layering approach and can be used as an example of how to implement support for additional JVM languages without having to implement all of Transit from scratch.

Contributing

This library is open source, developed internally by Cognitect. We welcome discussions of potential problems and enhancement suggestions on the transit-format mailing list. Issues can be filed using GitHub issues for this project. Because transit is incorporated into products and client projects, we prefer to do development internally and are not accepting pull requests or patches.

Copyright and License

Copyright © 2026 Cognitect

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Can you improve this documentation? These fine people already did:
Alex Miller, puredanger, David Chelimsky, Tim Ewald, clojure-build, Brenton Ashworth, Bobby Calderwood, Jarrod, JarrodCTaylor & dnolen
Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close