List of usage examples for com.mongodb.util JSON serialize
public static String serialize(final Object object)
Serializes an object into its JSON form.
This method delegates serialization to JSONSerializers.getLegacy
From source file:org.basex.modules.nosql.MongoDB.java
License:BSD License
/** * Mongodb Mapreduce function with 5 parameters, Map, reduce and query Option. * @param handler database handler//w ww . j a v a 2 s.co m * @param col collection name * @param map Map method * @param reduce Reduce Method * @param finalalize mongodb finalize parameter * @param query Selection options. * @param options additional options * @return Item * @throws Exception exception */ public Item mapreduce(final Str handler, final Str col, final Str map, final Str reduce, final Item finalalize, final Item query, final Map options) throws Exception { final DB db = getDbHandler(handler); if (map == null) { throw MongoDBErrors.generalExceptionError("Map function cannot be empty in Mapreduce"); } final DBObject q = query != null ? getDbObjectFromItem(query) : null; final DBCollection collection = db.getCollection(itemToString(col)); String out = null; String outType = null; OutputType op = MapReduceCommand.OutputType.INLINE; if (options != null) { for (Item k : options.keys()) { String key = (String) k.toJava(); if (key.equals("outputs")) { out = (String) options.get(k, null).toJava(); } if (key.equals("outputype")) { outType = (String) options.get(k, null).toJava(); } } if (out != null) { if (outType.toUpperCase().equals("REPLACE")) { op = MapReduceCommand.OutputType.REPLACE; } else if (outType.toUpperCase().equals("MERGE")) { op = MapReduceCommand.OutputType.MERGE; } else if (outType.toUpperCase().equals("REDUCE")) { op = MapReduceCommand.OutputType.REDUCE; } } } db.requestStart(); try { MapReduceCommand cmd = new MapReduceCommand(collection, map.toJava(), reduce.toJava(), out, op, q); if (finalalize != null) { cmd.setFinalize((String) finalalize.toJava()); } final MapReduceOutput outcmd = collection.mapReduce(cmd); return returnResult(handler, Str.get(JSON.serialize(outcmd.results()))); } catch (MongoException e) { throw MongoDBErrors.generalExceptionError(e); } finally { db.requestDone(); } }
From source file:org.basex.modules.nosql.MongoDB.java
License:BSD License
/** * Mapreduce all functions in xquery's Map like :{"map":"function(){..}" * , "reduce":"function(){}"}./*from w w w. j a va 2s .c om*/ * @param handler database handler * @param col collection name * @param options all options of Mapreduce including "map" in key. * @return Item * @throws Exception exception */ public Item mapreduce(final Str handler, final Str col, final Map options) throws Exception { if (options == null) { throw MongoDBErrors.generalExceptionError("Map optoins are empty"); } final DB db = getDbHandler(handler); final DBCollection collection = db.getCollection(itemToString(col)); String out = null; String outType = null; String map = null; String reduce = null; DBObject query = null; DBObject sort = null; int limit = 0; String finalalize = null; OutputType op = MapReduceCommand.OutputType.INLINE; for (Item k : options.keys()) { String key = (String) k.toJava(); Value val = options.get(k, null); String value = (String) val.toJava(); if (key.toLowerCase().equals(MAP)) { map = value; } else if (key.toLowerCase().equals(REDUCE)) { reduce = value; } else if (key.toLowerCase().equals(OUTPUTS)) { out = value; } else if (key.toLowerCase().equals(OUTPUTTYPE)) { outType = value; } else if (key.toLowerCase().equals(LIMIT)) { if (val.seqType().instanceOf(SeqType.ITR_OM)) { long l = ((Item) val).itr(null); limit = (int) l; } else { throw MongoDBErrors.generalExceptionError(" Expected integer Value"); } } else if (key.toLowerCase().equals(SORT)) { sort = getDbObjectFromItem(Str.get(value)); } else if (key.toLowerCase().equals(QUERY)) { query = getDbObjectFromItem(Str.get(value)); } else if (key.toLowerCase().equals(FINALIZE)) { finalalize = value; } } if (out != null && outType != null) { if (outType.toUpperCase().equals("REPLACE")) { op = MapReduceCommand.OutputType.REPLACE; } else if (outType.toUpperCase().equals("MERGE")) { op = MapReduceCommand.OutputType.MERGE; } else if (outType.toUpperCase().equals("REDUCE")) { op = MapReduceCommand.OutputType.REDUCE; } } else if (out != null) { op = MapReduceCommand.OutputType.REPLACE; } if (map == null) { throw MongoDBErrors.generalExceptionError("Map function cannot be empty"); } db.requestStart(); try { MapReduceCommand cmd = new MapReduceCommand(collection, map, reduce, out, op, query); if (finalalize != null) { cmd.setFinalize(finalalize); } if (limit != 0) { cmd.setLimit(limit); } if (sort != null) { cmd.setSort(sort); } final MapReduceOutput outcmd = collection.mapReduce(cmd); return returnResult(handler, Str.get(JSON.serialize(outcmd.results()))); } catch (MongoException e) { throw MongoDBErrors.generalExceptionError(e); } finally { db.requestDone(); } }
From source file:org.basex.modules.nosql.RethinkDB.java
License:BSD License
/** * Process RqlCursor from .run() command and return result. * @param handler database handler/*from w ww .j av a 2 s . c om*/ * @param cursor RqlCursor * @return Item * @throws Exception exception */ private Item processRqlCursor(final Str handler, final RqlCursor cursor) throws Exception { ArrayList<Object> array = new ArrayList<>(); for (RqlObject c : cursor) { try { Object o = c.as(); if (o instanceof java.util.List<?>) { List<Object> list = c.getList(); array.add(list); } else if (o instanceof ListIterator<?>) { List<Object> list = c.getList(); array.add(list); } else { java.util.Map<String, Object> map = c.getMap(); array.add(map); } } catch (RqlDriverException e) { throw new QueryException(e.getMessage()); } } // Str json = (array.size() == 1) ? Str.get(JSON.serialize(array.get(0))) // : Str.get(JSON.serialize(array)); Str json = Str.get(JSON.serialize(array)); return returnResult(handler, json); }
From source file:org.basex.modules.RethinkDB.java
License:BSD License
private Item processRqlCursor(final Str handler, final RqlCursor cursor) throws Exception { ArrayList<Object> array = new ArrayList<Object>(); for (RqlObject o : cursor) { try {/*from w ww. j a va 2 s . c o m*/ java.util.Map<String, Object> map = o.getMap(); array.add(map); } catch (RqlDriverException e) { throw new QueryException(e.getMessage()); } } // Str json = (array.size() == 1) ? Str.get(JSON.serialize(array.get(0))) // : Str.get(JSON.serialize(array)); Str json = Str.get(JSON.serialize(array)); return returnResult(handler, json); }
From source file:org.bson.LazyDBList.java
License:Apache License
/** * Returns a JSON serialization of this object * @return JSON serialization//from w w w.j av a 2s. c o m */ @Override public String toString() { return JSON.serialize(this); }
From source file:org.canedata.provider.mongodb.entity.MongoEntity.java
License:Apache License
/** * Finds the first document in the query and updates it. * @see com.mongodb.DBCollection#findAndModify(com.mongodb.DBObject, com.mongodb.DBObject, com.mongodb.DBObject, boolean, com.mongodb.DBObject, boolean, boolean) * @param expr/* www . j a va 2s . co m*/ * @return */ public Fields findAndUpdate(Expression expr) { if (logger.isDebug()) logger.debug("Finding and updating entity, Database is {0}, Collection is {1} ...", getSchema(), getName()); BasicDBObject options = new BasicDBObject(); try { validateState(); final BasicDBObject fields = new BasicDBObject(); MongoExpressionFactory expFactory = new MongoExpressionFactory.Impl(); BasicDBObject projection = new BasicDBObject(); Limiter limiter = new Limiter.Default(); BasicDBObject sorter = new BasicDBObject(); IntentParser.parse(getIntent(), expFactory, fields, projection, limiter, sorter, options); BasicDBObject query = expFactory.parse((MongoExpression) expr); if (logger.isDebug()) logger.debug( "Finding and updating entity, Database is {0}, Collection is {1}, expression is {2}, " + "Projections is {3}, Update is {4}, Sorter is {5}, Options is {6} ...", getSchema(), getName(), query.toString(), projection.toString(), fields.toString(), sorter.toString(), JSON.serialize(options)); DBObject rlt = getCollection().findAndModify(query, projection, sorter, options.getBoolean(Options.FIND_AND_REMOVE, false), fields, options.getBoolean(Options.RETURN_NEW, false), options.getBoolean(Options.UPSERT, false)); if (rlt == null || rlt.keySet().isEmpty()) return null; // alive cache if (null != getCache()) { invalidateCache(query); } return new MongoFields(this, getIntent(), (BasicDBObject) rlt).project(projection.keySet()); } catch (AnalyzeBehaviourException abe) { if (logger.isDebug()) logger.debug(abe, "Analyzing behaviour failure, cause by: {0}.", abe.getMessage()); throw new RuntimeException(abe); } finally { if (!options.getBoolean(Options.RETAIN, false)) getIntent().reset(); } }
From source file:org.cbioportal.genome_nexus.annotation.domain.internal.VariantAnnotationRepositoryImpl.java
License:Open Source License
/** * Maps the given raw annotation JSON string onto a VariantAnnotation instance. * * @param variant variant key/*from www.j av a2 s . c om*/ * @param annotationJSON raw annotation JSON string * @return a VariantAnnotation instance * @throws IOException */ @Override public VariantAnnotation mapAnnotationJson(String variant, String annotationJSON) throws IOException { //String toMap = removeArrayBrackets(annotationJSON); String toMap = JSON.serialize(convertToDbObject(annotationJSON)); VariantAnnotation vepVariantAnnotation; ObjectMapper mapper = new ObjectMapper(); // map annotation string onto VariantAnnotation instance vepVariantAnnotation = mapper.readValue(toMap, VariantAnnotation.class); // include original variant value too vepVariantAnnotation.setVariant(variant); //vepVariantAnnotation.setAnnotationJSON(annotationJSON); return vepVariantAnnotation; }
From source file:org.cbioportal.genome_nexus.annotation.util.Transformer.java
License:Open Source License
/** * Maps the given raw JSON string onto the provided class instance. * * @param jsonString raw JSON string/* ww w . ja va2 s .com*/ * @return a list of instances of the provided class * @throws IOException */ public static <T> List<T> mapJsonToInstance(String jsonString, Class<T> type) throws IOException { List<T> list = new ArrayList<>(); for (DBObject dbObject : convertToDbObject(jsonString)) { String toMap = JSON.serialize(dbObject); ObjectMapper mapper = new ObjectMapper(); // map json string onto the given class type list.add(mapper.readValue(toMap, type)); } return list; }
From source file:org.cbioportal.session_service.domain.Session.java
License:Open Source License
public void setData(String data) { this.data = JSON.parse(data); // JSON.serialize it so that formatting is the same if we test later this.checksum = DigestUtils.md5DigestAsHex(JSON.serialize(this.data).getBytes()); }
From source file:org.eclipse.birt.data.oda.mongodb.internal.impl.QueryProperties.java
License:Open Source License
public String serialize() { Map<String, Object> definedPropsMap = copyNonDefaultProperties(getPropertiesMap()); // convert property values not serializable by BSON serializer externalizePropValues(definedPropsMap); return JSON.serialize(definedPropsMap); }