### Question:
Model { public void writeTo(T object, DataWriter writer) throws IOException { writeTo(object, 0, writer); } Model(ModelBuilder parent, Class<T> type, @CheckForNull Class<?> propertyOwner, @Nullable String property); List<Property> getProperties(); void writeTo(T object, DataWriter writer); void writeTo(T object, TreePruner pruner, DataWriter writer); @Deprecated void writeTo(T object, int baseVisibility, DataWriter writer); final Class<T> type; final Model<? super T> superModel; }### Answer:
@Test public void merge() throws Exception { StringWriter sw = new StringWriter(); builder.get(B.class).writeTo(b, Flavor.JSON.createDataWriter(b, sw, config)); assertEquals("{'_class':'B','y':20,'z':30,'x':40}", sw.toString().replace('"','\'')); }
@Test public void merge_pathPrune() throws Exception { StringWriter sw = new StringWriter(); builder.get(B.class).writeTo(b, new NamedPathPruner("z,y"), Flavor.JSON.createDataWriter(b, sw, config)); assertEquals("{'_class':'B','y':20,'z':30}", sw.toString().replace('"','\'')); }
@Test public void skipNull() throws Exception { StringWriter sw = new StringWriter(); SomeNullProperty o = new SomeNullProperty(); builder.get(SomeNullProperty.class).writeTo(o, TreePruner.DEFAULT, Flavor.JSON.createDataWriter(o, sw, config)); assertEquals("{'_class':'SomeNullProperty','bbb':'bbb','ccc':null,'ddd':'ddd'}", sw.toString().replace('"','\'')); }
@Test public void testNotExportedBean() throws IOException { ExportConfig config = new ExportConfig().withFlavor(Flavor.JSON).withExportInterceptor(new ExportInterceptor1()).withSkipIfFail(true); StringWriter writer = new StringWriter(); ExportableBean b = new ExportableBean(); builder.get(ExportableBean.class).writeTo(b,Flavor.JSON.createDataWriter(b, writer, config)); Assert.assertEquals("{\"_class\":\""+ExportableBean.class.getName()+"\",\"name\":\"property1\",\"notExportedBean\":{},\"shouldBeNull\":null}", writer.toString()); }
@Test(expected = IOException.class) public void testNotExportedBeanFailing() throws IOException { ExportConfig config = new ExportConfig().withFlavor(Flavor.JSON).withExportInterceptor(new ExportInterceptor2()).withSkipIfFail(true); StringWriter writer = new StringWriter(); ExportableBean b = new ExportableBean(); builder.get(ExportableBean.class).writeTo(b,Flavor.JSON.createDataWriter(b, writer, config)); }