Skip to content

Commit f34bed2

Browse files
committed
Readme.md additions
1 parent facbfe5 commit f34bed2

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

README.md

+56-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,58 @@
11
# Protocol Buffer Annotation
22

3-
Inspired by jackson's fasterxml, a simple library to (de)serialize Protocol Buffer data.
3+
Inspired by jackson's fasterxml, a simple library to (de)serialize Protocol Buffer data using annotations.
4+
The usage of Google's protobuf compiler is not necessary.
5+
6+
## Usage
7+
8+
Create an instance of PbObjectMapper and call read/write to read from data/stream or write into one.
9+
10+
```java
11+
public class SampleClass1 {
12+
13+
@PbField(field = 1, type = PbFieldType.INT32)
14+
private int id;
15+
16+
@PbField(field = 2, type = PbFieldType.STRING)
17+
private String name;
18+
19+
// Optional Constructor, getter and setter
20+
}
21+
```
22+
23+
### Read from Data (byte array)
24+
25+
```java
26+
try {
27+
PbObjectMapper mapper = new ObjectMapper();
28+
byte[] sampleData = new byte[]{
29+
0x08, 0x14, // ID = 20
30+
0x12, 0x06, 0x72, 0x75, 0x6D, 0x70, 0x66, 0x63 // name = rumpfc
31+
};
32+
33+
SampleClass1 sample = mapper.read(sampleData, SampleClass1.class);
34+
System.out.println(sample);
35+
// SampleClass1{id=20, name='rumpfc'}
36+
} catch(IOException ex) {
37+
ex.printStackTrace();
38+
}
39+
```
40+
41+
### Write data (byte array)
42+
43+
```java
44+
try {
45+
PbObjectMapper mapper = new ObjectMapper();
46+
47+
SampleClass1 sample = new SampleClass1();
48+
sample.setId(20);
49+
sample.setName("rumpfc");
50+
System.out.println(sample);
51+
// SampleClass1{id=20, name='rumpfc'}
52+
53+
byte[] output = mapper.write(sample);
54+
// hex output = [08 14 12 06 72 75 6D 70 66 63]
55+
} catch(IOException ex) {
56+
ex.printStackTrace();
57+
}
58+
```

src/test/java/com/rumpf/mapper/PbObjectMapperTest.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ public void setId(int id) {
4747
public void setName(String name) {
4848
this.name = name;
4949
}
50+
51+
@Override
52+
public String toString() {
53+
return "SampleClass1{" +
54+
"id=" + id +
55+
", name='" + name + '\'' +
56+
'}';
57+
}
5058
}
5159

5260
private PbObjectMapper mapper;
@@ -58,8 +66,8 @@ public void setup() {
5866

5967
@Test
6068
public void objectMapperWriteTest() throws Exception {
61-
SampleClass1 sample = new SampleClass1(20, "Rumpfi");
62-
byte[] expected = new byte[]{ 0x08, 0x14, 0x12, 0x06, 0x52, 0x75, 0x6D, 0x70, 0x66, 0x69};
69+
SampleClass1 sample = new SampleClass1(20, "rumpfc");
70+
byte[] expected = new byte[]{ 0x08, 0x14, 0x12, 0x06, 0x72, 0x75, 0x6D, 0x70, 0x66, 0x63};
6371

6472
byte[] result = mapper.write(sample);
6573

@@ -68,13 +76,15 @@ public void objectMapperWriteTest() throws Exception {
6876

6977
@Test
7078
public void objectMapperReadTest() throws Exception {
71-
byte[] sampleData = new byte[]{ 0x08, 0x14, 0x12, 0x06, 0x52, 0x75, 0x6D, 0x70, 0x66, 0x69};
79+
byte[] sampleData = new byte[]{ 0x08, 0x14, 0x12, 0x06, 0x72, 0x75, 0x6D, 0x70, 0x66, 0x63};
7280

7381
SampleClass1 sampleObject = mapper.read(sampleData, SampleClass1.class);
7482

83+
System.out.println(sampleObject);
84+
7585
Assertions.assertAll(
7686
() -> Assertions.assertEquals(20, sampleObject.getId()),
77-
() -> Assertions.assertEquals("Rumpfi", sampleObject.getName())
87+
() -> Assertions.assertEquals("rumpfc", sampleObject.getName())
7888
);
7989
}
8090

0 commit comments

Comments
 (0)