示例#1
0
  @Test
  public void test1() {
    Bean1 bean1 = new Bean1();
    bean1.setName("9FB1");
    Bean2 bean2 = new Bean2();
    bean2.setAge(32);
    bean1.setBean2(bean2);

    StringBuilder printer = new StringBuilder();
    byte[] bytes = new BeanToBytes<Bean1>().toBytes(bean1, printer);
    byte[] expected = BeanBytesUtils.prependLen(RHex.decode("9FB1"), 1);
    expected = add(expected, toBytes(32));

    assertArrayEquals(expected, bytes);
    assertEquals("{name:9FB1, bean2:{age:32}}", printer.toString());

    FromBytesAware<Bean1> beanFromBytes2 = new BeanFromBytes<Bean1>();
    Bean1 simpleBean1 = beanFromBytes2.fromBytes(bytes, Bean1.class, 0).getBean();
    assertEquals(bean1, simpleBean1);
  }
示例#2
0
  protected Bean1 createBean1() {
    Bean1 b1 = new Bean1();
    b1.setBoolean1(true);
    b1.setByte1((byte) 12);
    b1.setShort1((short) 98);
    b1.setChar1('b');
    b1.setInt1(4);
    b1.setLong1(98);
    b1.setFloat1((float) 12.4);
    b1.setDouble1(89.224);
    b1.setString1("hello");
    b1.setStringArray1(new String[] {"string1", "string2", "string3", "string4"});
    {
      List l = new ArrayList();
      l.add(new Integer(14));
      l.add("another value");
      l.add(b1.getStringArray1());
      b1.setList1(l);
    }
    {
      Map m = new HashMap();
      m.put("key1", "value1");
      m.put(new Integer(14), "value2");
      m.put(new Long(14), "value3");
      m.put("recurse", b1);
      b1.setMap1(m);
    }

    Bean1 b2 = new Bean1();
    b2.setInt2(new Integer(-224));
    b2.setString2("bean2's string");
    b1.setBean1(b2);

    Bean1 b3 = new Bean1();
    b3.setDouble1(1422.332);
    b3.setString2("bean3's string");
    b2.setBean2(b3);

    return b1;
  }