protected EndpointConfig initReader(ObjectMapper mapper) { // first common config if (_activeView != null) { _reader = mapper.readerWithView(_activeView); } else { _reader = mapper.reader(); } if (_rootName != null) { _reader = _reader.withRootName(_rootName); } // Then deser features if (_deserEnable != null) { _reader = _reader.withFeatures(_deserEnable); } if (_deserDisable != null) { _reader = _reader.withoutFeatures(_deserDisable); } /* Important: we are NOT to close the underlying stream after * mapping, so we need to instruct parser: */ _reader.getJsonFactory().disable(JsonParser.Feature.AUTO_CLOSE_SOURCE); return this; }
public static Object load(String filename) { try { SAXBuilder builder = new SAXBuilder(); InputStream is = new FileInputStream(filename); Document doc = builder.build(is); Element el = doc.getRootElement(); ObjectReader reader = new SimpleReader(); return reader.read(el); } catch (Exception e) { e.printStackTrace(); return null; } }
private void _testMapsWithLinefeeds(boolean useBytes) throws Exception { String CSV = "A,B,C\n" + "data11,data12\n" + "data21,data22,data23\r\n" + "data31,\"data32 data32\ndata32 data32\",data33\n" + "data41,\"data42 data42\r\ndata42\",data43\n"; CsvSchema cs = CsvSchema.emptySchema().withHeader(); ObjectReader or = MAPPER.readerFor(HashMap.class).with(cs); MappingIterator<Map<String, String>> mi; if (useBytes) { mi = or.readValues(CSV.getBytes("UTF-8")); } else { mi = or.readValues(CSV); } assertTrue(mi.hasNext()); Map<String, String> map = mi.nextValue(); assertNotNull(map); assertEquals("data11", map.get("A")); assertEquals("data12", map.get("B")); assertEquals(2, map.size()); assertTrue(mi.hasNext()); map = mi.nextValue(); assertNotNull(map); assertEquals(3, map.size()); // then entries with linefeeds assertTrue(mi.hasNext()); map = mi.nextValue(); assertNotNull(map); assertEquals(3, map.size()); assertEquals("data31", map.get("A")); assertEquals("data32 data32\ndata32 data32", map.get("B")); assertEquals("data33", map.get("C")); assertTrue(mi.hasNext()); map = mi.nextValue(); assertNotNull(map); assertEquals(3, map.size()); assertEquals("data41", map.get("A")); assertEquals("data42 data42\r\ndata42", map.get("B")); assertEquals("data43", map.get("C")); assertFalse(mi.hasNext()); mi.close(); }
private void _testSimpleExplicit(ObjectReader r, boolean useBytes) throws Exception { r = r.forType(FiveMinuteUser.class); FiveMinuteUser user; final String INPUT = "Bob,Robertson,MALE,AQIDBAU=,false\n"; if (useBytes) { user = r.readValue(INPUT); } else { user = r.readValue(INPUT.getBytes("UTF-8")); } assertEquals("Bob", user.firstName); assertEquals("Robertson", user.lastName); assertEquals(Gender.MALE, user.getGender()); assertFalse(user.isVerified()); assertArrayEquals(new byte[] {1, 2, 3, 4, 5}, user.getUserImage()); }
public void testSimpleExplicitWithBOM() throws Exception { ObjectReader r = MAPPER.reader(SIMPLE_SCHEMA); r = r.forType(FiveMinuteUser.class); FiveMinuteUser user; ByteArrayOutputStream b = new ByteArrayOutputStream(); // first, UTF-8 BOM: b.write(new byte[] {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}); b.write("Bob,Robertson,MALE,AQIDBAU=,false\n".getBytes("UTF-8")); b.close(); user = r.readValue(b.toByteArray()); String fn = user.firstName; if (!fn.equals("Bob")) { fail("Expected 'Bob' (3), got '" + fn + "' (" + fn.length() + ")"); } assertEquals("Robertson", user.lastName); assertEquals(Gender.MALE, user.getGender()); assertFalse(user.isVerified()); assertArrayEquals(new byte[] {1, 2, 3, 4, 5}, user.getUserImage()); }
protected final double _testRawDeser(int reps, byte[] json, ObjectReader reader) throws IOException { long start = System.nanoTime(); final JsonFactory f = reader.getFactory(); while (--reps >= 0) { JsonParser p = f.createParser(new ByteArrayInputStream(json)); JsonToken t; while ((t = p.nextToken()) != null) { if (t == JsonToken.VALUE_STRING) { p.getText(); } else if (t.isNumeric()) { p.getNumberValue(); } ; } p.close(); } hash = f.hashCode(); return _msecsFromNanos(System.nanoTime() - start); }
public void testSimple() throws Exception { final ObjectMapper mapper = new ObjectMapper(); final ObjectReader jsonReader = mapper.reader(POJO.class); final String JSON = "{\"name\":\"Bob\", \"id\":3}"; byte[] doc = _smileDoc(JSON, true); ObjectReader detecting = jsonReader.withFormatDetection(jsonReader, jsonReader.with(new SmileFactory())); POJO pojo = detecting.readValue(doc); assertEquals(3, pojo.id); assertEquals("Bob", pojo.name); // let's verify it also works for plain JSON... pojo = detecting.readValue(JSON.getBytes("UTF-8")); assertEquals(3, pojo.id); assertEquals("Bob", pojo.name); }
/** * read 1.5 compression stream ref table * * @throws PdfException */ private PdfObject readCompressedStream( PdfObject rootObj, int pointer, final PdfFileReader currentPdfFile, final ObjectReader objectReader, final PdfObject linearObj) throws PdfException { while (pointer != -1) { /** get values to read stream ref */ movePointer(pointer); final byte[] raw = objectReader.readObjectData(-1, null); /** read the object name from the start */ final StringBuilder objectName = new StringBuilder(); char current1, last = ' '; int matched = 0, i1 = 0; while (i1 < raw.length) { current1 = (char) raw[i1]; // treat returns same as spaces if (current1 == 10 || current1 == 13) { current1 = ' '; } if (current1 == ' ' && last == ' ') { // lose duplicate or spaces matched = 0; } else if (current1 == pattern.charAt(matched)) { // looking for obj at end matched++; } else { matched = 0; objectName.append(current1); } if (matched == 3) { break; } last = current1; i1++; } // add end and put into Map objectName.append('R'); final PdfObject pdfObject = new CompressedObject(objectName.toString()); pdfObject.setCompressedStream(true); final ObjectDecoder objectDecoder = new ObjectDecoder(currentPdfFile); objectDecoder.readDictionaryAsObject(pdfObject, 0, raw); // read the field sizes final int[] fieldSizes = pdfObject.getIntArray(PdfDictionary.W); // read the xrefs stream byte[] xrefs = pdfObject.getDecodedStream(); // if encr if (xrefs == null) { xrefs = currentPdfFile.readStream(pdfObject, true, true, false, false, true, null); } final int[] Index = pdfObject.getIntArray(PdfDictionary.Index); if (Index == null) { // single set of values // System.out.println("-------------1.Offsets-------------"+current+" "+numbEntries); CompressedObjects.readCompressedOffsets( 0, 0, pdfObject.getInt(PdfDictionary.Size), fieldSizes, xrefs, offset, pdf_datafile); } else { // pairs of values in Index[] array final int count = Index.length; int pntr = 0; for (int aa = 0; aa < count; aa += 2) { // System.out.println("-------------2.Offsets-------------"+Index[aa]+" "+Index[aa+1]); pntr = CompressedObjects.readCompressedOffsets( pntr, Index[aa], Index[aa + 1], fieldSizes, xrefs, offset, pdf_datafile); } } /** * now process trailer values - only first set of table values for root, encryption and info */ if (rootObj == null) { rootObj = pdfObject.getDictionary(PdfDictionary.Root); /** handle encryption */ encryptObj = pdfObject.getDictionary(PdfDictionary.Encrypt); if (encryptObj != null) { final byte[][] IDs = pdfObject.getStringArray(PdfDictionary.ID); if (IDs != null && this.ID == null) { // only the first encountered ID should be used as a fileID for decryption this.ID = IDs[0]; } } infoObject = pdfObject.getDictionary(PdfDictionary.Info); } // make sure first values used if several tables and code for prev so long as not linearized // may need adjusting as more examples turn up if (linearObj != null) { pointer = -1; } else { pointer = pdfObject.getInt(PdfDictionary.Prev); // a non-compressed object table can follow a compressed one so we need to allow for this if (!isCompressedStream(pointer, (int) eof)) { return readLegacyReferenceTable(rootObj, pointer, (int) eof, currentPdfFile); } } } return rootObj; }