Example usage for com.mongodb.util JSON serialize

List of usage examples for com.mongodb.util JSON serialize

Introduction

In this page you can find the example usage for com.mongodb.util JSON serialize.

Prototype

public static String serialize(final Object object) 

Source Link

Document

Serializes an object into its JSON form.

This method delegates serialization to JSONSerializers.getLegacy

Usage

From source file:org.uom.fit.level2.datavis.repository.ChatServicesImpl.java

@Override
public JSONArray getAll() {
    try {/*from w w  w  . j  a v a2 s  .co  m*/
        Mongo mongo = new Mongo("localhost", 27017);
        DB db = mongo.getDB("datarepo");
        DBCollection collection = db.getCollection("user");
        DBCursor cursor = collection.find();
        JSON json = new JSON();
        String dataUser = json.serialize(cursor);
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(dataUser);
        JSONArray jsonarray = (JSONArray) obj;
        return jsonarray;
    } catch (Exception e) {
        System.out.println("Exception Error getAll");
        return null;
    }
}

From source file:org.uom.fit.level2.datavis.repository.ChatServicesImpl.java

@Override
public JSONArray getAllChatData(String message) {
    try {//from   ww w .  j a  v  a  2 s .  c  o  m
        Mongo mongo = new Mongo("localhost", 27017);
        DB db = mongo.getDB("chat");
        DBCollection collection = db.getCollection("message");
        BasicDBObject whereQuery = new BasicDBObject();
        BasicDBObject field = new BasicDBObject();
        BasicDBObject sortQuery = new BasicDBObject();

        whereQuery.put("message", message);
        field.put("senderName", 1);
        field.put("message", 1);
        field.put("receiverName", 1);
        field.put("imageData", 1);
        field.put("discription", 1);

        sortQuery.put("DateTime", 1);

        DBCursor cursor = collection.find(whereQuery, field).sort(sortQuery);
        JSON json = new JSON();
        String dataUser = json.serialize(cursor);
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(dataUser);
        JSONArray jsonarray = (JSONArray) obj;
        return jsonarray;
    } catch (Exception e) {
        System.out.println("Exception Error getAll");
        return null;
    }
}

From source file:org.wrml.contrib.runtime.service.mongo.MongoService.java

License:Apache License

private Model convertToModel(final DBObject mongoObject, final Keys keys, final Dimensions dimensions)
        throws ModelReadingException {

    mongoObject.removeField("_id");

    // Is JSON serialization fast enough here?
    final String jsonStringRepresentation = JSON.serialize(mongoObject);
    final byte[] jsonStringBytes = jsonStringRepresentation.getBytes();
    final InputStream inStream = new ByteArrayInputStream(jsonStringBytes);

    final Context context = getContext();
    final Model model = context.readModel(inStream, keys, dimensions, SystemFormat.json.getFormatUri());
    return model;
}

From source file:xbdd.webapp.resource.feature.Report.java

License:Apache License

@PUT
@Path("/{product}/{major}.{minor}.{servicePack}/{build}")
public DBObject putReport(@BeanParam final Coordinates coordinates, final DBObject root) throws IOException {
    final BasicDBList doc = (BasicDBList) root;
    final DB grid = this.client.getDB("grid");
    final GridFS gridFS = new GridFS(grid);
    final DB bdd = this.client.getDB("bdd");
    final DBCollection features = bdd.getCollection("features");
    updateSummaryDocument(bdd, coordinates);

    for (int i = 0; i < doc.size(); i++) {
        // take each feature and give it a unique id.
        final DBObject feature = (DBObject) doc.get(i);
        final String _id = coordinates.getFeature_Id((String) feature.get("id"));
        feature.put("_id", _id);
        embedSteps(feature, gridFS, coordinates); // extract embedded content and hyperlink to it.
        packBackgroundsInToScenarios(feature); // nest background elements within their scenarios
        final BasicDBObject featureCo = coordinates.getReportCoordinates();
        feature.put("coordinates", featureCo);

        final BasicDBList newElements = mergeExistingScenarios(features, feature, _id);
        feature.put("elements", newElements);

        final String originalStatus = StatusHelper.getFeatureStatus(feature);
        feature.put("calculatedStatus", originalStatus);
        feature.put("originalAutomatedStatus", originalStatus);
        this.log.info("Saving: " + feature.get("name") + " - " + feature.get("calculatedStatus"));
        this.log.trace("Adding feature:" + JSON.serialize(feature));
        features.save(feature);/*from   w ww  . j a  v a  2s  .  c  o m*/
    }
    final DBCursor cursor = features.find(coordinates.getReportCoordinatesQueryObject()); // get new co-ordinates to exclude the "version"
    // field
    final List<DBObject> returns = new ArrayList<DBObject>();
    try {
        while (cursor.hasNext()) {
            returns.add(cursor.next());
        }
    } finally {
        cursor.close();
    }
    final BasicDBList list = new BasicDBList();
    list.addAll(returns);
    updateStatsDocument(bdd, coordinates, list);
    return list;
}

From source file:xbdd.webapp.util.BasicDBReader.java

License:Apache License

@Override
public void writeTo(DBObject myBean, Class<?> type, Type genericType, Annotation[] annotations,
        MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
        throws IOException, WebApplicationException {
    writeToAsString(JSON.serialize(myBean), entityStream, mediaType);
}