@Converter public static BasicDBObject fromInputStreamToDBObject(InputStream is, Exchange exchange) { BasicDBObject answer = null; try { byte[] input = IOConverter.toBytes(is); if (isBson(input)) { BSONCallback callback = new JSONCallback(); new BasicBSONDecoder().decode(input, callback); answer = (BasicDBObject) callback.get(); } else { answer = (BasicDBObject) JSON.parse(IOConverter.toString(input, exchange)); } } catch (Exception e) { LOG.warn("String -> DBObject conversion selected, but the following exception occurred. Returning null.", e); } finally { // we need to make sure to close the input stream IOHelper.close(is, "InputStream", LOG); } return answer; }
/** * Parses a JSON string representing a JSON value * * @param s the string to parse * @return the object */ public static Object parse( String s, BSONCallback c ){ if (s == null || (s=s.trim()).equals("")) { return (DBObject)null; } JSONParser p = new JSONParser(s, c); return p.parse(); }
/** * Parses a JSON string representing a JSON value * * @param s * the string to parse * @return the object */ public static Object parse(String s, BSONCallback c) { if (s == null || (s = s.trim()).equals("")) { return (DBObject) null; } JSONParser p = new JSONParser(s, c); return p.parse(); }
/** * Create a new parser. */ public JSONParser(String s, BSONCallback callback) { this.s = s; _callback = (callback == null) ? new JSONCallback() : callback; }
/** * Parses a JSON string and constructs a corresponding Java object by calling * the methods of a {@link org.bson.BSONCallback BSONCallback} during parsing. * If the callback <code>c</code> is null, this method is equivalent to * {@link com.mongodb.JSON#parse(String) parse(String)}. * * @param s the string to parse * @param c the BSONCallback to call during parsing * @return a Java object representing the JSON data * @throws JSONParseException if s is not valid JSON */ public static Object parse( String s, BSONCallback c ){ if (s == null || (s=s.trim()).equals("")) { return (DBObject)null; } JSONParser p = new JSONParser(s, c); return p.parse(); }