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:com.ebay.cloud.cms.metadata.mongo.converter.ObjectConverter.java

License:Apache License

public T fromBson(DBObject dbObject, Class<T> type) {
    return fromJson(JSON.serialize(dbObject), type);
}

From source file:com.englishtown.vertx.GridFSModule.java

License:Open Source License

public void getFile(Message<JsonObject> message, JsonObject jsonObject) {

    ObjectId objectId = getObjectId(message, jsonObject, "id");
    if (objectId == null) {
        return;/*from ww w  . j ava  2 s .c  o  m*/
    }

    // Optional bucket, default is "fs"
    String bucket = jsonObject.getString("bucket", GridFS.DEFAULT_BUCKET);
    GridFS files = new GridFS(db, bucket);

    GridFSDBFile file = files.findOne(objectId);
    if (file == null) {
        sendError(message, "File does not exist: " + objectId.toString());
        return;
    }

    JsonObject fileInfo = new JsonObject().putString("filename", file.getFilename())
            .putString("contentType", file.getContentType()).putNumber("length", file.getLength())
            .putNumber("chunkSize", file.getChunkSize())
            .putNumber("uploadDate", file.getUploadDate().getTime());

    DBObject metadata = file.getMetaData();
    if (metadata != null) {
        fileInfo.putObject("metadata", new JsonObject(JSON.serialize(metadata)));
    }

    // Send file info
    sendOK(message, fileInfo);

}

From source file:com.github.bluetiger9.nosql.benchmarking.clients.document.couchbase.CouchbaseClient.java

License:Open Source License

@Override
public void insert(String key, Map<String, String> attributes) throws ClientException {
    super.insert(key, JSON.serialize(attributes));
}

From source file:com.github.mongo.labs.api.GeoService.java

License:Apache License

@GET
@Path("/{longitude}/{latitude}")
@ApiOperation(value = "Retrouve les speakers proche du point [longitude, latitude] (ex: 2.3521, 48.8670)", notes = "Un <b>index golocalis</b> doit tre prsent sur la collection des speakers")
public String near(@PathParam("longitude") double longitude, @PathParam("latitude") double latitude) {

    DBObject query = BasicDBObjectBuilder.start().push("geo").push("$near").add("$maxDistance", 1500)
            .push("$geometry").add("type", "Point").add("coordinates", new Double[] { longitude, latitude })
            .get();//from w  w w.  jav  a 2s .  com
    return JSON.serialize(speakers.find(query));
}

From source file:com.github.mongo.labs.api.GeoService.java

License:Apache License

@GET
@Path("/")
@ApiOperation(value = "Retourne tous les speakers avec leurs donnes de golocalisation, nom et id", notes = "C'est trs proche du service /speakers/ : les donnes sont les mmes, mais la structure est diffrente.")
public String all() {
    DBObject projection = new BasicDBObject();
    projection.put("name", 1);
    projection.put("geo", 1);
    return JSON.serialize(
            speakers.find(new BasicDBObject("geo", new BasicDBObject("$exists", "true")), projection));
}

From source file:com.github.mongo.labs.api.SearchService.java

License:Apache License

@GET
@Path("/{term}")
@ApiOperation(value = "Recherche les talks ayant le terme fourni en paramtre (ex: java). "
        + "Cette recherche utilise la fonction FullText Search de MongoDB. ", notes = "La cration d'un index Full Text est necessaire : "
                + "<b>db.talks.ensureIndex({ summary : \"text\", title : \"text\"  }, { default_language: \"french\" })</b>")
public String search(@PathParam("term") String term) {

    // Termes de la recheche
    // il est ici possible d'utiliser les differentes options fournies par le Search de Mongo:
    // - terme unique:   future
    // - plusieurs terme: future mobile
    // - negations : future mobile -bof
    BasicDBObject query = new BasicDBObject();
    BasicDBObject search = new BasicDBObject();
    search.put("$search", term);
    query.put("$text", search);

    // projection pour limiter les champs
    BasicDBObject projection = new BasicDBObject();
    projection.put("_id", 1);
    projection.put("title", 1);
    projection.put("summary", 1);
    projection.put("speakers", 1);

    return JSON.serialize(dbCollection.find(query, projection));
}

From source file:com.github.mongo.labs.api.SpeakersService.java

License:Apache License

@GET
@Path("/")
@ApiOperation(value = "Retourne tous les speakers prsent  Devoxx")
public String all() {
    DBObject sort = new BasicDBObject();
    sort.put("name.lastName", 1);
    sort.put("name.firstName", 1);
    return JSON.serialize(dbCollection.find().sort(sort));
}

From source file:com.github.mongo.labs.api.SpeakersService.java

License:Apache License

@GET
@Path("/{id}")
@ApiOperation(value = "Retrouve un speaker par son identifiant (ex: '5325b07a84ae2fdd99aa3ddb')", notes = "le service retourne un code 404 si non trouv")
public String get(@PathParam("id") String id) {
    ObjectId objId = new ObjectId(id);
    BasicDBObject query = new BasicDBObject("_id", objId);
    return JSON.serialize(dbCollection.findOne(query));
}

From source file:com.github.mongo.labs.api.SpeakersService.java

License:Apache License

@GET
@Path("/byname/{lastName}")
@ApiOperation(value = "Retrouve une liste de speakers par leur nom (ex: 'Aresti')", notes = "le service retourne un code 404 si non trouv")
public String getByName(@PathParam("lastName") String lastName) {
    BasicDBObject query = new BasicDBObject();
    query.put("name.lastName", java.util.regex.Pattern.compile(lastName, Pattern.CASE_INSENSITIVE));
    return JSON.serialize(dbCollection.find(query));
}

From source file:com.github.mongo.labs.api.TagsService.java

License:Apache License

@GET
@Path("/native/")
@ApiOperation(value = "Retourne les tags les plus utiliss avec leurs statistiques associes. Ce endpoint utilise le driver en native", notes = "Le framework d'aggrgation doit tre utilis pour remonter les bonnes donnes")
public String countByTag() {

    DBObject unwind = new BasicDBObject("$unwind", "$tags");
    DBObject project1 = new BasicDBObject("$project",
            new BasicDBObject("tags", new BasicDBObject("$toLower", "$tags")));

    DBObject groupFields = new BasicDBObject();
    groupFields.put("_id", "$tags");
    groupFields.put("count", new BasicDBObject("$sum", 1));
    DBObject group = new BasicDBObject("$group", groupFields);

    DBObject projectFields = new BasicDBObject();
    projectFields.put("_id", 0);
    projectFields.put("tags", "$_id");
    projectFields.put("count", 1);

    DBObject project2 = new BasicDBObject("$project", projectFields);

    DBObject sort = new BasicDBObject("$sort", new BasicDBObject("count", -1));

    return JSON.serialize(dbCollection.aggregate(unwind, project1, group, project2, sort).results());
}