Git Product home page Git Product logo

jackson-dataformat-bencode's Introduction

Overview

Build Status

This project contains Jackson extension component for reading and writing Bencode encoded data, either as "raw" data (sequence of String arrays), or via data binding to/from Java Objects (POJOs).

The primary objective of this project is to take advantage of Jackson's easy to use and fast object mapper, and make serializing and de-serializing Bencoded content pussible using either full data-binding or streaming (tree model is not supported yet).

Project is licensed under Apache License 2.0.

Usage

As it is a data type extension to Jackson, it could be used like the usual JSON data format.

Given the following Bencoded data - identical counterpart of JSON example, used as a data binding example in Jackson in 5 minutes tutorial (please note that actual binary data replaced with asterisks, as Bencode format has raw binary representation):

d6:gender4:MALE4:named5:first3:Joe4:last7:Sixpacke9:userImage5:*****8:verified5:falsee

It could be turned into a Java POJO with the following code:

ObjectMapper mapper = new BEncodeMapper();
User u = mapper.readValue(new File("user.bencode"), User.class)

Or similarly a Java POJO could be turned into a Bencoded stream:

mapper.writeValue(new File("user.bencode"), u);

Where the user class used above is a simple POJO:

public class User {
    public enum Gender { MALE, FEMALE }

    public static class Name {
        private String first, last;

        public String getFirst() { return first; }
        public String getLast() { return last; }

        public void setFirst(String s) { first = s; }
        public void setLast(String s) { last = s; }
    }

    private Gender gender;
    private Name name;
    private boolean isVerified;
    private byte[] userImage;

    public Name getName() { return name; }
    public boolean isVerified() { return isVerified; }
    public Gender getGender() { return gender; }
    public byte[] getUserImage() { return userImage; }

    public void setName(Name n) { name = n; }
    public void setVerified(boolean b) { isVerified = b; }
    public void setGender(Gender g) { gender = g; }
    public void setUserImage(byte[] b) { userImage = b; }
}

Of course the POJOs could be decorated with the usual Jackson annotations like @JsonProperty. There is a more complex class within tests, com.fasterxml.jackson.dataformat.bencode.types.Torrent, which represents a complete BitTorent file.

Status

Initial release with decent unit test coverage. Ready to use, but might develop some unexpected surprises.

Next steps:

  • Add benchmarks, optimize to reach a level where this is as fast as JSON - theoretically it should be, cause Bencode is simpler than JSON.
  • Add proper encoding support; currently UTF-8 is hardwired as default.
  • Add some useful features.

jackson-dataformat-bencode's People

Contributors

zsoltm avatar

Watchers

 avatar

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.