GithubHelp home page GithubHelp logo

binarydataparser's Introduction

BinaryDataParser Travis master Coverage Status Codacy Badge Maven Central GitHub license

BinaryDataParser is used to parse binary data into POJOs and to serialize it. It generates a custom mapper (combined parser/serializer) for each class. The mapping for a property is defined with an annotation.

The code is written in Kotlin but the unit tests are in Java. The software is licensed under the Apache License 2.0

Quickstart

You need to add a runtime and a compile time dependency. Then you add the annotations to the class you want to map. When you compile this clas, a custom mapper will be generated and can then be used from your code.

Add dependency

Add this dependency to you project

        <dependency>
            <groupId>org.ak80.bdp</groupId>
            <artifactId>bdp-runtime</artifactId>
            <version>1.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.ak80.bdp</groupId>
            <artifactId>bdp-processor</artifactId>
            <version>1.0.1</version>
            <optional>true</optional>
        </dependency>

Add annotations to the class you want to map

public class ClassToMap {

  @MappedByte(index = 0)
  private int byte0;

  @MappedByte(index = 1)
  private int byte1;

  @MappedWord(index = 2, endianess = Endian.BIG_ENDIAN)
  private int wordBig;

  @MappedWord(index = 4, endianess = Endian.LITTLE_ENDIAN)
  private int wordLittle;

  // getters / setters omitted for brevity
  
}

Use generated mapper

public class ClassToMapTest {

  @Test
  public void testParse() {
    // Given
    ClassToMap classToMap = new ClassToMap();
    ClassToMapParser parser = new ClassToMapParser();

    int[] data = new int[]{0x01, 0x02, 0x03, 0x04, 0x05, 0x06};

    // When
    parser.parse(classToMap, data);

    // Then
    assertThat(classToMap.getByte0(), is(0x01));
    assertThat(classToMap.getByte1(), is(0x02));
    assertThat(classToMap.getWordBig(), is(0x0304));
    assertThat(classToMap.getWordLittle(), is(0x0605));
  }

  @Test
  public void testSerialize() {
    // Given
    ClassToMap classToMap = new ClassToMap();
    ClassToMapParser parser = new ClassToMapParser();

    classToMap.setByte0(0x01);
    classToMap.setByte1(0x02);
    classToMap.setWordBig(0x0304);
    classToMap.setWordLittle(0x0506);

    int[] data = new int[6];

    // When
    parser.serialize(classToMap, data);

    // Then
    assertThat(data, is(new int[]{0x01, 0x02, 0x03, 0x04, 0x06, 0x05}));
  }

}

Purpose

Generate parser and serializer for byte data based an annotations on properties of the class.

For each class with annotations a Parser-Serializer class will be generated. When parsing it consumes an int array and sets the properties according to the definitions made with the annotations. When serializing it produces an int array based on the properties of the given object.

Use of the AnnotationsProcessor

The AnnotationsProcessor is in the bdp-runtime JAR file and registers a Service. Just drop it into your classpath!

Supported mappings

Mapping a single byte value

A single byte value is mapped to / from a numeric with the @MappedByte annotation:

  @MappedByte(index = 0)
  private int byte0;

The parameter index defined the position of the byte in the array.

Mapping a two byte value

A two byte value is mapped to / from a numeric with the @MappedWord annotation:

  @MappedWord(index = 0)
  private int wordBig;

  @MappedWord(index = 2, endianess = Endian.LITTLE_ENDIAN)
  private int wordLittle;

The parameter index defined the position of the word in the array. The parameter endianess defined the byte order, either Endian.BIG_ENDIAN, which is the default, or Endian.LITTLE_ENDIAN.

Mapping a single bit to a boolean

** only supported in snapshot, please see in the tests or wait for the next release **

Mapping a group of bits to an enum

** only supported in snapshot, please see in the tests or wait for the next release **

Planned Features

  • Mapping one or more bits to a numeric
  • Mapping one or more bits to an enum, with a builder class
  • Handling bit flags for a group of booleans
  • Create a new array when serializing
  • Generate new instance instead of need to pass an existing instance
  • Generate documentation
  • Sanity checking for overlapping definitions
  • Sanity checking for unmapped parts
  • Default settings e.g. for endianess
  • Suppport has/get with flags instead of only is

binarydataparser's People

Contributors

ak80 avatar

Stargazers

 avatar send2vinnie avatar

Watchers

send2vinnie avatar James Cloos avatar  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.