Пример #1
0
  @Test
  public void testMarshallUser() throws Exception {
    SerializationContext ctx = createContext();

    User user = new User();
    user.setId(1);
    user.setName("John");
    user.setSurname("Batman");
    user.setGender(User.Gender.MALE);
    user.setAccountIds(new HashSet<Integer>(Arrays.asList(1, 3)));
    user.setAddresses(Collections.singletonList(new Address("Old Street", "XYZ42")));

    byte[] bytes = ProtobufUtil.toByteArray(ctx, user);

    User decoded = ProtobufUtil.fromByteArray(ctx, bytes, User.class);

    assertEquals(1, decoded.getId());
    assertEquals("John", decoded.getName());
    assertEquals("Batman", decoded.getSurname());
    assertEquals(User.Gender.MALE, decoded.getGender());

    assertNotNull(decoded.getAddresses());
    assertEquals(1, decoded.getAddresses().size());
    assertEquals("Old Street", decoded.getAddresses().get(0).getStreet());
    assertEquals("XYZ42", decoded.getAddresses().get(0).getPostCode());

    assertNotNull(decoded.getAccountIds());
    assertEquals(2, decoded.getAccountIds().size());
    assertTrue(decoded.getAccountIds().contains(1));
    assertTrue(decoded.getAccountIds().contains(3));
  }
Пример #2
0
 @Override
 public void writeTo(ProtoStreamWriter writer, User user) throws IOException {
   writer.writeInt("id", user.getId());
   writer.writeCollection("accountIds", user.getAccountIds(), Integer.class);
   writer.writeString("name", user.getName());
   writer.writeString("surname", user.getSurname());
   writer.writeCollection("addresses", user.getAddresses(), Address.class);
   writer.writeInt("age", user.getAge());
   writer.writeEnum("gender", user.getGender(), User.Gender.class);
   writer.writeString("notes", user.getNotes());
 }