@Override public Date createDate(String iso) throws ParseException { if (iso == null || iso.length() < 20) return null; int indexOfLastChar = iso.length() - 1; char lastChar = iso.charAt(indexOfLastChar); if (lastChar == 'Z' || lastChar == 'z') { // the date format doesn't like 'Z', so use timezone and offset (e.g., "GMT+00:00") ... iso = iso.substring(0, indexOfLastChar) + "GMT+00:00"; } else if (iso.length() >= 22) { // 1997-07-16T19:20:30.45 // 0123456789012345678901 iso = iso.substring(0, 22) + "GMT" + iso.substring(22); } return Bson.getDateParsingFormatter().parse(iso); }
/** * Write to the supplied stream the binary BSON representation of the supplied in-memory {@link Document}. * * @param bson the BSON object or BSON value; may not be null * @param stream the output stream; may not be null * @throws IOException if there was a problem writing to the stream */ public static void write( Document bson, OutputStream stream ) throws IOException { getBsonWriter().write(bson, stream); }
@Override public int getType( String name ) { return Bson.getTypeForValue(get(name)); }
protected void write( Date value, Writer writer ) throws IOException { String isoDate = Bson.getDateFormatter().format(value); write(new BasicDocument(DATE, isoDate), writer); }
protected byte[] writeDocument(Document content) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (OutputStream out = config.compress() ? new GZIPOutputStream(bos) : bos) { Bson.write(content, out); } return bos.toByteArray(); } catch (IOException e) { throw new RelationalProviderException(e); } }
protected Document readDocument(InputStream is) { try (InputStream contentStream = config.compress() ? new GZIPInputStream(is) : is) { return Bson.read(contentStream); } catch (IOException e) { throw new RelationalProviderException(e); } }
protected void write( Date value, Writer writer ) throws IOException { String isoDate = Bson.getDateFormatter().format(value); write(new BasicDocument(DATE, isoDate), writer); }
@Test public void shouldCorrectlyWriteDate() { Date now = new Date(); String dateStr = Bson.getDateFormatter().format(now); String expected = "{ \"$date\" : \"" + dateStr + "\" }"; String actual = writer.write(now); assertSame(expected, actual); }
@Override public Date createDate(String iso) throws ParseException { if (iso == null || iso.length() < 20) return null; int indexOfLastChar = iso.length() - 1; char lastChar = iso.charAt(indexOfLastChar); if (lastChar == 'Z' || lastChar == 'z') { // the date format doesn't like 'Z', so use timezone and offset (e.g., "GMT+00:00") ... iso = iso.substring(0, indexOfLastChar) + "GMT+00:00"; } else if (iso.length() >= 22) { // 1997-07-16T19:20:30.45 // 0123456789012345678901 iso = iso.substring(0, 22) + "GMT" + iso.substring(22); } return Bson.getDateParsingFormatter().parse(iso); }
/** * Write to the supplied output the binary BSON representation of the supplied in-memory {@link Document}. * * @param bson the BSON object or BSON value; may not be null * @param output the output; may not be null * @throws IOException if there was a problem writing to the ObjectOutput */ public static void write( Document bson, ObjectOutput output ) throws IOException { getBsonWriter().write(bson, output); }
@Override public int getType( String name ) { return Bson.getTypeForValue(get(name)); }
@Test public void shouldParseIsoDateWithZuluTimeZone() throws Exception { Date date = now(); value = JsonReader.DATE_VALUE_MATCHER.parseValue(Bson.getDateFormatter().format(date)); assertTrue(value instanceof Date); }
@Test public void shouldParseIsoFormat() throws Exception { value = Bson.getDateParsingFormatter().parse("2011-06-14T16:05:11GMT+00:00"); Date date = (Date)value; assertTrue(date.after(date(2011, 06, 13))); assertTrue(date.before(date(2011, 06, 16))); }
/** * Write to the supplied stream the binary BSON representation of the supplied in-memory {@link Document}. * * @param bson the BSON object or BSON value; may not be null * @param stream the output stream; may not be null * @throws IOException if there was a problem writing to the stream */ public static void write( Document bson, OutputStream stream ) throws IOException { getBsonWriter().write(bson, stream); }
@Override public int getType( String name ) { return Bson.getTypeForValue(get(name)); }
/** * Return the array of bytes containing the standard BSON binary form of the supplied in-memory {@link Document}. * * @param object the BSON object or BSON value; may not be null * @return the bytes * @throws IOException if there was a problem reading from the stream */ public static byte[] write( Object object ) throws IOException { return getBsonWriter().write(object); }
@Override public int getType( String name ) { return Bson.getTypeForValue(get(name)); }
/** * Write to the supplied output the binary BSON representation of the supplied in-memory {@link Document}. * * @param bson the BSON object or BSON value; may not be null * @param output the output; may not be null * @throws IOException if there was a problem writing to the ObjectOutput */ public static void write( Document bson, ObjectOutput output ) throws IOException { getBsonWriter().write(bson, output); }
/** * Return the array of bytes containing the standard BSON binary form of the supplied in-memory {@link Document}. * * @param object the BSON object or BSON value; may not be null * @return the bytes * @throws IOException if there was a problem reading from the stream */ public static byte[] write( Object object ) throws IOException { return getBsonWriter().write(object); }