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:ezbake.training.MongoDbServlet.java
License:Apache License
/** * Creates a text index on the "text" field if it doesn't exist * * @throws TException/*from w w w.j a v a 2 s . c o m*/ */ private void createTextIndex(MongoDatasetClient client) throws TException { boolean hasTextIndex = false; String namespace = null; logger.info("getting index info.."); List<String> indexList = client.getIndexInfo(COLLECTION_NAME); for (String index : indexList) { logger.info("we have an index: {}", index); DBObject indexObj = (DBObject) JSON.parse(index); String indexName = (String) indexObj.get("name"); if (namespace == null) { namespace = (String) indexObj.get("ns"); } if (indexName.equals(TWEET_TEXT_FIELD_NAME + "_text")) { hasTextIndex = true; } } if (!hasTextIndex) { DBObject obj = new BasicDBObject(); // we are putting a text index on the "text" field in the mongo collection obj.put(TWEET_TEXT_FIELD_NAME, "text"); String jsonKeys = JSON.serialize(obj); logger.info("creating text index with jsonKeys: {}, COLLECTION_NAME: {}", jsonKeys, COLLECTION_NAME); client.createIndex(COLLECTION_NAME, jsonKeys, null); logger.info("MongoDbServlet: created text index: {}", jsonKeys); } else { logger.info("MongoDbServlet: we already have the text index."); } }
From source file:fr.wseduc.mongodb.MongoQueryBuilder.java
License:Apache License
public static JsonObject build(QueryBuilder queryBuilder) { DBObject dbo = queryBuilder.get(); return new JsonObject(JSON.serialize(dbo)); }
From source file:jahspotify.storage.statistics.MongoDBHistoricalStorage.java
License:Apache License
@Override public TrackHistory getHistory(final Link trackLink) { final DBCollection tracks = _db.getCollection("history"); final DBObject query = new BasicDBObject("id", trackLink.getUri()); final DBCursor dbObjects = tracks.find(query); if (dbObjects.size() == 1) { return _gson.fromJson(JSON.serialize(dbObjects.next()), TrackHistory.class); }//from w w w . j a va2 s .c om return null; }
From source file:jahspotify.storage.statistics.MongoDBHistoricalStorage.java
License:Apache License
@Override public TrackStatistics getTrackStatistics(final Link trackLink) { final DBCollection tracks = _db.getCollection("history"); final BasicDBObject query = new BasicDBObject(); query.put("trackLink.id", trackLink.getId()); final DBCursor dbObjects = tracks.find(query); final BasicDBObject orderBy = new BasicDBObject(); orderBy.put("startTime", -1); dbObjects.sort(orderBy);// w w w.j a v a2 s. c o m TrackStatistics trackStatistics = new TrackStatistics(); int numCompleted = 0; int numSkipped = 0; int totalSecondsPlayed = 0; if (dbObjects.hasNext()) { final TrackHistory trackHistory = _gson.fromJson(JSON.serialize(dbObjects.next()), TrackHistory.class); trackStatistics.setTrackLink(trackHistory.getTrackLink()); trackStatistics.setLastPlayed(trackHistory.getStartTime()); totalSecondsPlayed += trackHistory.getSecondsPlayed(); numSkipped += trackHistory.isCompleteTrackPlayed() ? 0 : 1; numCompleted += trackHistory.isCompleteTrackPlayed() ? 1 : 0; if (!dbObjects.hasNext()) { trackStatistics.setFirstPlayed(trackHistory.getStartTime()); } } while (dbObjects.hasNext()) { final TrackHistory trackHistory = _gson.fromJson(JSON.serialize(dbObjects.next()), TrackHistory.class); totalSecondsPlayed += trackHistory.getSecondsPlayed(); numSkipped += trackHistory.isCompleteTrackPlayed() ? 0 : 1; numCompleted += trackHistory.isCompleteTrackPlayed() ? 1 : 0; if (!dbObjects.hasNext()) { trackStatistics.setFirstPlayed(trackHistory.getStartTime()); } } trackStatistics.setNumTimesCompleted(numCompleted); trackStatistics.setNumTimesSkipped(numSkipped); trackStatistics.setNumTimesPlayed(numCompleted + numSkipped); trackStatistics.setTotalPlaytime(totalSecondsPlayed); return trackStatistics; }
From source file:jahspotify.storage.statistics.MongoDBHistoryCursor.java
License:Apache License
@Override public TrackHistory next() { return _gson.fromJson(JSON.serialize(_dbObjects.next()), TrackHistory.class); }
From source file:me.lightspeed7.mongofs.gridfs.GridFSFile.java
License:Apache License
@Override public String toString() { return JSON.serialize(this); }
From source file:me.tfeng.play.mongodb.MongoDbTypeConverter.java
License:Apache License
public static <S, T> T convertFromMongoDbType(Class<T> dataClass, S object) { if (object == null) { return null; } else if (dataClass.isInstance(object)) { return dataClass.cast(object); } else {/*w w w . j a va 2 s .co m*/ @SuppressWarnings("unchecked") Converter<S, T> converter = (Converter<S, T>) CONVERTER_MAP .get(ImmutablePair.of(object.getClass(), dataClass)); if (converter != null) { return converter.convertFromMongoDbType(object); } else if (String.class.isAssignableFrom(dataClass) && object instanceof DBObject) { return dataClass.cast(JSON.serialize(object)); } else { return null; } } }
From source file:nosqltools.DBConnection.java
/** * This method returns JSON data found in the collection to be loaded in Panel_Text * /* w ww . java2 s. co m*/ */ public StringBuilder getCollectionData(String coll) { StringBuilder res = new StringBuilder(); if (checkSystemColl(coll)) { collection = db.getCollection(coll); DBCursor cursor = collection.find(); //Reference: http://www.codeconfuse.com/2014/03/mongodb-convert-data-getting-from.html JSON json = new JSON(); String serialize = json.serialize(cursor); System.out.println(serialize); res.append(serialize); } else { res = null; } return res; }
From source file:org.alfresco.bm.api.v1.ResultsRestAPI.java
License:Open Source License
@GET @Path("/eventNames") @Produces(MediaType.APPLICATION_JSON)/*from w w w. j a v a2 s. com*/ public String getEventResultEventNames() { final BasicDBList events = new BasicDBList(); // always add the "all events" name in the first position events.add(ALL_EVENT_NAMES); // distinct get all recorded event names from Mongo List<String> eventNames = getResultService().getEventNames(); for (String eventName : eventNames) { events.add(eventName); } return JSON.serialize(events); }
From source file:org.alfresco.bm.api.v1.ResultsRestAPI.java
License:Open Source License
@GET @Path("/allEventsFilterName") @Produces(MediaType.APPLICATION_JSON)//from w ww .ja v a2s. com public String getAllEventsFilterName() { final BasicDBList events = new BasicDBList(); events.add(ALL_EVENT_NAMES); return JSON.serialize(events); }