Giter Site home page Giter Site logo

ebson's Introduction

ebson

ebson is an extensible BSON encoder/decoder library written in Java. The library is extensible in the sense that the mappings between Java and BSON types are configurable and the logic to serialize custom Java types is pluggable. Its single dependency is the Guava libraries by Google.

License

Released under the permissive MIT License.

Author

Kohányi Róbert.

Download

Add the library as a dependency in your project's pom.xml like this.

<dependency>
  <groupId>com.github.kohanyirobert</groupId>
  <artifactId>ebson</artifactId>
  <version>...</version>
</dependency>

Releases and snapshots are deployed to Sonatype's OSS repository (and synced to the Central Maven Repository from there). To download JARs from Sonatype's repository include the following repository tag inside your Maven installation's settings.xml or your project's pom.xml.

<repository>
  <id>sonatype-oss</id>
  <url>https://oss.sonatype.org/content/groups/public</url>
</repository>

Build

As the project is managed with Maven you simply clone it and issue mvn install or mvn package inside the clone's directory.

git clone git://github.com/kohanyirobert/ebson.git
cd ebson/
mvn package
# and/or
mvn install

Usage

Serialization

// create documents to serialize
BsonDocument document = BsonDocuments.of("key", new Date());

// grab a little-endian byte buffer
ByteBuffer buffer = ByteBuffer.allocate(32).order(ByteOrder.LITTLE_ENDIAN);

// use the documents utility class to write the document into the buffer
BsonDocuments.writeTo(buffer, document);

// use the serialized data
buffer.flip();

Deserialization

// given the previous buffer
BsonDocument newDocument = BsonDocuments.readFrom(buffer);

// prints true
System.out.println(document.equals(newDocument));

Extensibility

// to use joda-time's date-time instead of java's date supply
// a predicate (to test whether an input class is compatible with
// date-time or not) for the appropriate bson type
BsonObject.UTC_DATE_TIME.predicate(new Predicate<Class<?>>() {
  @Override public boolean apply(Class<?> input) {
    return input == null ? false : DateTime.class.isAssignableFrom(input);
  }
});

// register a writer with the same bson type which is
// able to serialize date-times into byte buffers
BsonObject.UTC_DATE_TIME.writer(new BsonWriter() {
  @Override public void writeTo(ByteBuffer buffer, Object reference) {
    buffer.putLong(((DateTime) reference).getMillis());
  }
});

// finally register a reader to do all this ass backwards
BsonObject.UTC_DATE_TIME.reader(new BsonReader() {
  @Override public Object readFrom(ByteBuffer buffer) {
    return new DateTime(buffer.getLong());
  }
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.