use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class JSONProviderTest method testWriteBeanIgnorePropertyAtJsonLevel.
@Test
public void testWriteBeanIgnorePropertyAtJsonLevel() throws Exception {
JSONProvider<Book> provider = new JSONProvider<Book>();
provider.setOutDropElements(Collections.singletonList("id"));
provider.setDropElementsInXmlStream(false);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
provider.writeTo(new Book("cxf", 123), Book.class, Book.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(), bos);
assertTrue(bos.toString().contains("\"name\":\"cxf\""));
assertFalse(bos.toString().contains("\"id\":123"));
assertTrue(bos.toString().startsWith("{\"Book\":"));
}
use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class JSONProviderTest method testReadNullStringAsNull.
@Test
public void testReadNullStringAsNull() throws Exception {
String input = "{\"Book\":{\"id\":123,\"name\":\"null\"}}";
JSONProvider<Book> provider = new JSONProvider<Book>();
Book theBook = provider.readFrom(Book.class, null, null, null, null, new ByteArrayInputStream(input.getBytes()));
assertEquals(123L, theBook.getId());
assertEquals("", theBook.getName());
}
use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class JSONProviderTest method testWriteBookWithStringConverter.
@Test
public void testWriteBookWithStringConverter() throws Exception {
JSONProvider<Book> p = new JSONProvider<Book>();
p.setConvertTypesToStrings(true);
Book book = new Book("CXF", 125);
ByteArrayOutputStream os = new ByteArrayOutputStream();
p.writeTo(book, Book.class, Book.class, Book.class.getAnnotations(), MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(), os);
String s = os.toString();
assertEquals("{\"Book\":{\"id\":\"125\",\"name\":\"CXF\",\"state\":\"\"}}", s);
}
use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class JSONProviderTest method testEscapeForwardSlashesAlways.
@Test
public void testEscapeForwardSlashesAlways() throws Exception {
JSONProvider<Book> provider = new JSONProvider<Book>();
provider.setEscapeForwardSlashesAlways(true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
provider.writeTo(new Book("http://cxf", 123), Book.class, Book.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(), bos);
assertTrue(bos.toString().contains("\"name\":\"http:\\/\\/cxf\""));
}
use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class JSONProviderTest method testReadEmbeddedArray.
@Test
public void testReadEmbeddedArray() throws Exception {
String input = "{\"store\":" + "{\"books\":{" + " \"book\":[" + " { " + " \"name\":\"CXF 1\"" + " }, " + " { " + " \"name\":\"CXF 2\"" + " } " + " ] " + " } " + " } " + "} ";
Object storeObject = new JSONProvider<Store>().readFrom(Store.class, null, null, null, null, new ByteArrayInputStream(input.getBytes()));
Store store = (Store) storeObject;
List<Book> books = store.getBooks();
assertEquals(2, books.size());
assertEquals("CXF 1", books.get(0).getName());
assertEquals("CXF 2", books.get(1).getName());
}
Aggregations