List of usage examples for com.mongodb.util JSONCallback JSONCallback
JSONCallback
From source file:com.xinqihd.sns.gameserver.util.JSON.java
License:Apache License
/** * Create a new parser.//from w w w .j a v a2s . c o m */ public JSONParser(String s, BSONCallback callback) { this.s = s; _callback = (callback == null) ? new JSONCallback() : callback; }
From source file:de.inovex.andsync.util.BsonConverter.java
License:Apache License
public static DBObject fromBson(byte[] bson) { JSONCallback callback = new JSONCallback(); try {//from w w w . ja v a 2s . c o m decoderPool.get().decode(bson, callback); } catch (Exception ex) { Log.e("Data was not a valid BSON object."); return null; } return (DBObject) callback.get(); }
From source file:de.inovex.andsync.util.BsonConverter.java
License:Apache License
public static List<DBObject> fromBsonList(byte[] bson) { JSONCallback callback = new JSONCallback(); try {/*from ww w . jav a 2 s .c o m*/ decoderPool.get().decode(bson, callback); //new BasicBSONDecoder().decode(bson, callback); } catch (Exception ex) { Log.w("Data was not a valid BSON object."); return null; } BasicDBObject dbo = (BasicDBObject) callback.get(); List<DBObject> list = new ArrayList<DBObject>(dbo.size()); for (Object o : dbo.values()) { list.add((DBObject) o); } return list; }
From source file:org.apache.camel.component.mongodb.converters.MongoDbBasicConverters.java
License:Apache License
@Converter public static DBObject fromInputStreamToDBObject(InputStream is, Exchange exchange) { DBObject answer = null;/*from w ww .j a va2 s . c o m*/ try { byte[] input = IOConverter.toBytes(is); if (isBson(input)) { BSONCallback callback = new JSONCallback(); new BasicBSONDecoder().decode(input, callback); answer = (DBObject) callback.get(); } else { answer = (DBObject) 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; }
From source file:org.chililog.server.data.MongoJsonParser.java
License:Apache License
/** * Create a new parser./* www . j av a 2 s . co m*/ */ private MongoJsonParser(String s, BSONCallback callback) { this.s = s; _callback = (callback == null) ? new JSONCallback() : callback; }
From source file:org.jongo.query.BsonQueryFactory.java
License:Apache License
public Query createQuery(final String query, Object... parameters) { if (query == null) { return new BsonQuery((DBObject) JSON.parse(query)); }// w w w. ja v a 2 s . co m if (parameters == null) { parameters = new Object[] { null }; } // We have two different cases: // // - tokens as property names "{scores.#: 1}": they must be expanded before going // through the JSON parser, and their toString() is inserted in the query // // - tokens as property values "{id: #}": they are resolved by the JSON parser and // therefore marshalled as DBObjects (actually LazyDBObjects). StringBuilder sb = new StringBuilder(); int paramIncrement = 0; // how many params must be skipped by the next value param int paramPos = 0; // current position in the parameter list int start = 0; // start of the current string segment int pos; // position of the last token found while ((pos = query.indexOf(token, start)) != -1) { if (paramPos >= parameters.length) { throw new IllegalArgumentException("Not enough parameters passed to query: " + query); } // Insert chars before the token sb.append(query, start, pos); // Check if the character preceding the token is one that separates values. // Otherwise, it's a property name substitution if (isValueToken(query, pos)) { // Will be resolved by the JSON parser below sb.append("{\"").append(MARSHALL_OPERATOR).append("\":").append(paramIncrement).append("}"); paramIncrement = 0; } else { // Resolve it now sb.append(parameters[paramPos]); paramIncrement++; } paramPos++; start = pos + token.length(); } // Add remaining chars sb.append(query, start, query.length()); if (paramPos < parameters.length) { throw new IllegalArgumentException("Too many parameters passed to query: " + query); } final Object[] params = parameters; // Parse the query with a callback that will weave in marshalled parameters DBObject dbo; try { dbo = (DBObject) JSON.parse(sb.toString(), new JSONCallback() { int paramPos = 0; @Override public Object objectDone() { String name = curName(); Object o = super.objectDone(); if (o instanceof BSONObject && !(o instanceof List<?>)) { BSONObject dbo = (BSONObject) o; Object marshallValue = dbo.get(MARSHALL_OPERATOR); if (marshallValue != null) { paramPos += ((Number) marshallValue).intValue(); if (paramPos >= params.length) { throw new IllegalArgumentException( "Not enough parameters passed to query: " + query); } o = marshallParameter(params[paramPos++]); // Replace value set by super.objectDone() if (!isStackEmpty()) { _put(name, o); } else { o = !BSON.hasDecodeHooks() ? o : BSON.applyDecodingHooks(o); setRoot(o); } } } if (isStackEmpty()) { // End of object } return o; } }); } catch (Exception e) { throw new IllegalArgumentException("Cannot parse query: " + query, e); } return new BsonQuery(dbo); }