Example usage for com.mongodb.util JSON parse

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

Introduction

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

Prototype

public static Object parse(final String jsonString) 

Source Link

Document

Parses a JSON string and returns a corresponding Java object.

Usage

From source file:MDBInt.DBMongo.java

License:Apache License

public String getObj(String dbName, String collName, String query) throws MDBIException {

    BasicDBObject constrains = null, results = null;
    DBCursor cursore;// w  ww. j ava  2  s .  c o  m
    DB db = this.getDB(dbName);
    DBCollection coll = db.getCollection(collName);
    BasicDBObject obj = (BasicDBObject) JSON.parse(query);
    constrains = new BasicDBObject("_id", 0);
    constrains.put("insertTimestamp", 0);
    cursore = coll.find(obj, constrains);
    try {
        results = (BasicDBObject) cursore.next();
    } catch (NoSuchElementException e) {
        LOGGER.error("manifest non trovato!");
        throw new MDBIException("Manifest required is not found inside DB.");
    }
    return results.toString();
}

From source file:MDBInt.DBMongo.java

License:Apache License

/**
 * Returns an ArraList<String> where each String is a JSONObject in String
 * version.//  w w  w  .j av a  2s . c  o  m
 *
 * @param tenant
 * @param geoShape
 * @return
 */
public ArrayList<String> getDatacenters(String tenant, String geoShape) {
    DB database = this.getDB(tenant);
    DBCollection collection = database.getCollection("datacenters");

    BasicDBObject shape = (BasicDBObject) JSON.parse(geoShape);
    ArrayList<String> datacenters = new ArrayList();
    BasicDBObject geoJSON = (BasicDBObject) shape.get("geometry");
    BasicDBObject geometry = new BasicDBObject();
    BasicDBObject geoSpazialOperator = new BasicDBObject();
    BasicDBObject query = new BasicDBObject();
    BasicDBObject constrains = new BasicDBObject("_id", 0);
    Iterator<DBObject> it;
    DBCursor cursore;

    geometry.put("$geometry", geoJSON);
    geoSpazialOperator.put("$geoIntersects", geometry);
    query.put("geometry", geoSpazialOperator);
    cursore = collection.find(query, constrains);

    it = cursore.iterator();
    while (it.hasNext()) {
        datacenters.add(it.next().toString());

    }
    return datacenters;
}

From source file:MDBInt.DBMongo.java

License:Apache License

public void insertfedsdnSite(String json) {

    DB dataBase = this.getDB(this.identityDB);
    DBCollection collezione = dataBase.getCollection("fedsdnSite");
    BasicDBObject obj = (BasicDBObject) JSON.parse(json);
    obj.append("insertTimestamp", System.currentTimeMillis());
    collezione.save(obj);//from  w  w  w  . ja v a2 s. c om
}

From source file:MDBInt.DBMongo.java

License:Apache License

public void insertfedsdnFednet(String json) {

    DB dataBase = this.getDB(this.identityDB);
    DBCollection collezione = dataBase.getCollection("fedsdnFednet");
    BasicDBObject obj = (BasicDBObject) JSON.parse(json);
    obj.append("insertTimestamp", System.currentTimeMillis());
    collezione.save(obj);/*from w  w w.ja  v  a 2 s.  com*/
}

From source file:MDBInt.DBMongo.java

License:Apache License

public void insertfedsdnNetSeg(String json) {

    DB dataBase = this.getDB(this.identityDB);
    DBCollection collezione = dataBase.getCollection("fedsdnNetSeg");
    BasicDBObject obj = (BasicDBObject) JSON.parse(json);
    obj.append("insertTimestamp", System.currentTimeMillis());
    collezione.save(obj);//from   ww w .  ja va  2  s. c o m
}

From source file:me.schiz.jmeter.protocol.mongodb.sampler.MongoFindOneSampler.java

License:Apache License

@Override
public SampleResult sample(Entry e) {
    SampleResult res = new SampleResult();
    //String data = getScript();

    BasicDBObject document = (BasicDBObject) JSON.parse(getDocument());

    res.setSampleLabel(getTitle());//from  w w  w  . j av  a  2s  .com
    res.setResponseCodeOK();
    res.setSuccessful(true);
    res.setResponseMessageOK();
    res.setSamplerData("db." + getCollection() + ".find(" + document + ")");
    res.setDataType(SampleResult.TEXT);
    res.setContentType("text/plain"); // $NON-NLS-1$
    res.sampleStart();

    try {
        MongoDB mongoDB = MongoSourceElement.getMongoDB(getSource());
        DB db = mongoDB.getDB(getDatabase(), getUsername(), getPassword());
        res.latencyEnd();
        DBObject dbObject = db.getCollection(getCollection()).findOne(document);
        if (dbObject != null)
            res.setResponseData(dbObject.toString().getBytes());
        else {
            res.setSuccessful(false);
            res.setResponseCode("404");
        }
    } catch (Exception ex) {
        res.setResponseCode("500"); // $NON-NLS-1$
        res.setSuccessful(false);
        res.setResponseMessage(ex.toString());
        res.setResponseData(ex.getMessage().getBytes());
    } finally {
        res.sampleEnd();
    }
    return res;
}

From source file:me.schiz.jmeter.protocol.mongodb.sampler.MongoFindSampler.java

License:Apache License

@Override
public SampleResult sample(Entry e) {
    SampleResult res = new SampleResult();
    //String data = getScript();

    BasicDBObject document = (BasicDBObject) JSON.parse(getDocument());

    res.setSampleLabel(getTitle());//from   w ww .ja v a2  s  .  co m
    res.setResponseCodeOK();
    res.setSuccessful(true);
    res.setResponseMessageOK();
    res.setSamplerData("db." + getCollection() + ".find(" + document + ")");
    res.setDataType(SampleResult.TEXT);
    res.setContentType("text/plain"); // $NON-NLS-1$
    res.sampleStart();

    try {
        MongoDB mongoDB = MongoSourceElement.getMongoDB(getSource());
        DB db = mongoDB.getDB(getDatabase(), getUsername(), getPassword());
        res.latencyEnd();
        DBCursor cursor = db.getCollection(getCollection()).find(document);
        String response = new String();
        List<DBObject> resultList = cursor.toArray();
        if (resultList != null || resultList.isEmpty()) {
            for (DBObject o : resultList) {
                response += o.toString() + "\n";
            }
            res.setResponseData(response.getBytes());
        } else {
            res.setResponseCode("404");
            res.setSuccessful(false);
        }
    } catch (Exception ex) {
        res.setResponseCode("500"); // $NON-NLS-1$
        res.setSuccessful(false);
        res.setResponseMessage(ex.toString());
        res.setResponseData(ex.getMessage().getBytes());
    } finally {
        res.sampleEnd();
    }
    return res;
}

From source file:me.schiz.jmeter.protocol.mongodb.sampler.MongoInsertSampler.java

License:Apache License

@Override
public SampleResult sample(Entry e) {
    SampleResult res = new SampleResult();
    //String data = getScript();

    res.setSampleLabel(getTitle());// w ww.  j  av a 2 s  .  co m
    res.setResponseCodeOK();
    res.setSuccessful(true);
    res.setResponseMessageOK();
    res.setSamplerData("db." + getCollection() + ".insert(<json>)");
    res.setDataType(SampleResult.TEXT);
    res.setContentType("text/plain"); // $NON-NLS-1$
    res.sampleStart();

    try {
        MongoDB mongoDB = MongoSourceElement.getMongoDB(getSource());
        //MongoScriptRunner runner = new MongoScriptRunner();
        DB db = mongoDB.getDB(getDatabase(), getUsername(), getPassword());
        res.latencyEnd();
        BasicDBObject document = (BasicDBObject) JSON.parse(getDocument());
        WriteResult wResult = db.getCollection(getCollection()).insert(document);
        //            latencyEnd result = runner.evaluate(db, data);
        //            EvalResultHandler handler = new EvalResultHandler();
        //            String resultAsString = handler.handle(result);
        //            res.setResponseData(resultAsString.getBytes());
        res.setResponseData(wResult.toString().getBytes());
    } catch (Exception ex) {
        res.setResponseCode("500"); // $NON-NLS-1$
        res.setSuccessful(false);
        res.setResponseMessage(ex.toString());
        res.setResponseData(ex.getMessage().getBytes());
    } finally {
        res.sampleEnd();
    }
    return res;
}

From source file:me.tfeng.play.mongodb.MongoDbTypeConverter.java

License:Apache License

public static <S, T> S convertToMongoDbType(Class<S> mongoClass, T data) {
    if (data == null) {
        return null;
    } else if (mongoClass.isInstance(data)) {
        return mongoClass.cast(data);
    } else {/*from  w  w  w  .  ja  va 2s  .co  m*/
        @SuppressWarnings("unchecked")
        Converter<S, T> converter = (Converter<S, T>) CONVERTER_MAP
                .get(ImmutablePair.of(mongoClass, data.getClass()));
        if (converter != null) {
            return converter.convertToMongoDbType(data);
        } else if (DBObject.class.isAssignableFrom(mongoClass) && data instanceof String) {
            return mongoClass.cast(JSON.parse((String) data));
        } else {
            return null;
        }
    }
}

From source file:models.datasource.SingletonDataSource.java

public static User insertIntoUsersCollection(User user) {
    // Get the collection (connection to our mongo database)
    DBCollection collection = connectDB("mongo.usersCollection");

    // Create the query
    BasicDBObject query = new BasicDBObject().append(Constants.USER_NAME, user.name)
            .append(Constants.USER_SURNAMES, user.surnames).append(Constants.USER_EMAIL, user.email)
            .append(Constants.USER_PASSWORD, Utils.encryptWithSHA1(user.password))
            .append(Constants.USER_EMAIL_VERIFICATION_KEY, user.emailVerificationKey)
            .append(Constants.USER_CONNECTION_TIMESTAMP, user.connectionTimestamp)
            .append(Constants.USER_RESTORE_PASSWORD_TOKEN, user.restorePasswordToken)
            .append(Constants.USER_RESTORE_PASSWORD_TIMESTAMP, user.restorePasswordTimestamp)
            .append(Constants.USER_REGISTRATION_DATE, user.registrationDate)
            .append(Constants.USER_BIRTH_DATE, user.birthDate)
            .append(Constants.USER_RESIDENCE_CITY, user.residenceCity)
            .append(Constants.USER_RESIDENCE_ADDRESS, user.residenceAddress)
            .append(Constants.USER_RESIDENCE_NUMBER, user.residenceNumber)
            .append(Constants.USER_RESIDENCE_ZIP_CODE, user.residenceZipCode)
            .append(Constants.USER_PHONE_NUMBER, user.phoneNumber)
            .append(Constants.USER_STUDY_TITLE, user.studyTitle)
            .append(Constants.USER_STUDY_LOCATION, user.studyLocation)
            .append(Constants.USER_EDUCATION_LEVEL, user.educationLevel)
            .append(Constants.USER_DRIVING_LICENSE, user.drivingLicense)
            .append(Constants.USER_CERTIFICATE_OF_DISABILITY, user.certificateOfDisability)
            .append(Constants.USER_COURSES, JSON.parse(user.coursesToJson()))
            .append(Constants.USER_LANGUAGES, JSON.parse(user.languagesToJson()))
            .append(Constants.USER_SOFTWARE, JSON.parse(user.softwareToJson()))
            .append(Constants.USER_ORIENTATION_STEPS,
                    JSON.parse(user.completedOrientationSteps.orientationStepsToJson()))
            .append(Constants.USER_CURRENT_SITUATION, JSON.parse(user.currentSituation.toJsonString()))
            .append(Constants.USER_SKILLS_LIST, JSON.parse(user.skillsToJson()))
            .append(Constants.USER_INTERESTS_LIST, user.interests)
            .append(Constants.USER_PERSONAL_CHARACTERISTICS_LIST, user.personalCharacteristics)
            .append(Constants.USER_PROFESSIONAL_VALUES_LIST, JSON.parse(user.professionalValuesToJson()))
            .append(Constants.USER_PHOTO, JSON.parse(user.photo.toJsonString()))
            .append(Constants.USER_NEXT_INTERVIEWS_LIST, JSON.parse(user.interviewScheduleListToJson()));
    collection.insert(WriteConcern.SAFE, query);

    // Close connection
    mongoClient.close();/* w  w w .ja v a  2s  . co  m*/

    // Returns the new user
    return user;
}