@Override public <C> ContentHandle<C> newHandle(Class<C> type) { if ( ! isHandled(type) ) return null; JacksonDatabindHandle<C> handle = new JacksonDatabindHandle<>(type); if ( mapper != null ) handle.setMapper(mapper); return handle; } }
@Override public T next() { JacksonDatabindHandle<T> handle = new JacksonDatabindHandle<>(entityClass); handle.getMapper().enableDefaultTyping( ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_OBJECT); return docPage.nextContent(handle).get(); }
/** * Assigns a your custom POJO as the content and returns the handle. * @param content your custom POJO * @return the handle */ public JacksonDatabindHandle<T> with(T content) { set(content); return this; }
JacksonDatabindHandle<String> handle1 = new JacksonDatabindHandle<String>(String.class); JacksonDatabindHandle<String> handle2 = new JacksonDatabindHandle<String>(String.class); JacksonDatabindHandle<String> handle3 = new JacksonDatabindHandle<String>(String.class); handle1.set(json1); handle2.set(json2); handle3.set(json3); JacksonDatabindHandle<Product> jacksonDBReadHandle = new JacksonDatabindHandle<Product>(Product.class); docMgr.read(docId[0], jacksonDBReadHandle); Product product1 = (Product) jacksonDBReadHandle.get(); Product product2 = (Product) jacksonDBReadHandle.get(); assertTrue("Did not return a iMac", product2.getName().equalsIgnoreCase("iMac")); assertTrue("Did not return a Desktop", product2.getIndustry().equalsIgnoreCase("Desktop")); Product product3 = (Product) jacksonDBReadHandle.get(); assertTrue("Did not return a iPad", product3.getName().equalsIgnoreCase("iPad")); assertTrue("Did not return a Tablet", product3.getIndustry().equalsIgnoreCase("Tablet"));
@Override public void addCity(City city) { if ( numCities >= MAX_TO_WRITE ) return; JacksonDatabindHandle handle = new JacksonDatabindHandle(city); // NOTICE: We've set the mapper to an XmlMapper, showing the versitility of Jackson handle.setMapper(mapper); handle.setFormat(Format.XML); writeSet.add(DIRECTORY + "/xmlCities/" + city.getGeoNameId() + ".xml", handle); numCities++; } @Override
JacksonDatabindHandle<ObjectNode> handle = new JacksonDatabindHandle<>( ObjectNode.class).withFormat(Format.JSON); handle.set(rootNode);
for (int numWritten = 0; numWritten < MAX_TO_WRITE && (line = cityReader.readLine()) != null; numWritten++ ) { Toponym city = reader.readValue(line); JacksonDatabindHandle handle = new JacksonDatabindHandle(city); handle.getMapper().addMixInAnnotations(Toponym.class, ToponymMixIn2.class); set.add(DIRECTORY + "/thirdPartyJsonCities/" + city.getGeoNameId() + ".json", handle); } docMgr.write(set);
DocumentRecord record = termQueryResults.next(); System.out.println("URI = " + record.getUri()); JacksonDatabindHandle<ObjectNode> recordHandle = new JacksonDatabindHandle<>( ObjectNode.class); record.getContent(recordHandle); System.out.println("Content = " + recordHandle.toString()); if (recordHandle.toString().contains("Updated")) { toDeleteURI = record.getUri();
validateClosingHandleClosesUnderlyingStream(new JacksonHandle(), "null"); validateClosingHandleClosesUnderlyingStream( new JacksonDatabindHandle<>(Object.class), "null"); validateClosingHandleClosesUnderlyingStream(new JacksonParserHandle(), "null"); validateClosingHandleClosesUnderlyingStream(
/** * Specifies the format of the content and returns the handle * as a fluent convenience. * @param format the format of the content * @return this handle */ public JacksonDatabindHandle<T> withFormat(Format format) { setFormat(format); return this; }
@Override protected void receiveContent(InputStream content) { if (content == null) return; try { this.content = (T) getMapper().readValue( new InputStreamReader(content, "UTF-8"), contentClass); } catch (JsonParseException e) { throw new MarkLogicIOException(e); } catch (JsonMappingException e) { throw new MarkLogicIOException(e); } catch (IOException e) { throw new MarkLogicIOException(e); } finally { try { content.close(); } catch (IOException e) { // ignore. } } } @Override
@BeforeClass public static void beforeClass() { // demonstrate our ability to set advanced configuration on a mapper ObjectMapper mapper = new ObjectMapper(); // in this case, we're saying wrap our serialization with the name of the pojo class mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_OBJECT); // register a JacksonDatabindHandleFactory ready to marshall any City object to/from json // this enables the writeAs method below DatabaseClientFactory.getHandleRegistry().register( JacksonDatabindHandle.newFactory(mapper, City.class) ); // we cannot use the singleton DatabaseClient here because this test requires // a DatabaseClient created after calling getHandleRegistry().register() with City.class client = Common.newClient(); }
JacksonDatabindHandle<String> handle1 = new JacksonDatabindHandle<String>(String.class); JacksonDatabindHandle<String> handle2 = new JacksonDatabindHandle<String>(String.class); JacksonDatabindHandle<String> handle3 = new JacksonDatabindHandle<String>(String.class); handle1.set(json1); handle2.set(json2); handle3.set(json3); JacksonDatabindHandle<Product> jacksonDBReadHandle = new JacksonDatabindHandle<Product>(Product.class); docMgr.read(docId[0], jacksonDBReadHandle); Product product1 = (Product) jacksonDBReadHandle.get(); Product product2 = (Product) jacksonDBReadHandle.get(); assertTrue("Did not return a iMac", product2.getName().equalsIgnoreCase("iMac")); assertTrue("Did not return a Desktop", product2.getIndustry().equalsIgnoreCase("Desktop")); Product product3 = (Product) jacksonDBReadHandle.get(); assertTrue("Did not return a iPad", product3.getName().equalsIgnoreCase("iPad")); assertTrue("Did not return a Tablet", product3.getIndustry().equalsIgnoreCase("Tablet"));
JacksonDatabindHandle<ObjectNode> handle = new JacksonDatabindHandle<>( ObjectNode.class).withFormat(Format.JSON); handle.set(rootNode);
@Override public void saveJob(Job job, Transaction transaction) { JacksonDatabindHandle<Job> contentHandle = new JacksonDatabindHandle<>(job); contentHandle.setMapper(objectMapper); DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); metadataHandle = metadataHandle.withCollections("job"); DocumentWriteSet writeSet = docMgr.newWriteSet(); writeSet.add("/jobs/" + job.getJobId() + ".json", metadataHandle, contentHandle); docMgr.write(writeSet, transaction); }
/** * Provides a handle on POJO content. * @param content the POJO which should be serialized */ public JacksonDatabindHandle(T content) { this((Class<T>) content.getClass()); set(content); }
@Override public void write(T entity, Transaction transaction, String... collections) { if ( entity == null ) return; JacksonDatabindHandle<T> contentHandle = new JacksonDatabindHandle<>(entity); contentHandle.setMapper(objectMapper); DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); metadataHandle = metadataHandle.withCollections(entityClass.getName()); if ( collections != null && collections.length > 0 ) { metadataHandle = metadataHandle.withCollections(collections); } DocumentWriteSet writeSet = docMgr.newWriteSet(); writeSet.add(getDocumentUri(entity), metadataHandle, contentHandle); try { docMgr.write(writeSet, transaction); } catch(MarkLogicIOException e) { checkForEmptyBeans(e); throw e; } }