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:data.repository.pragma.DataObjectController.java

License:Apache License

@RequestMapping("/DO/update/metadata")
@ResponseBody/*www . jav a 2  s  . c om*/
public MessageResponse setMetadata(@RequestParam(value = "ID", required = true) String id,
        @RequestBody String metadata) {
    // Connect to MongoDB and set metadata as curation purposes
    // Return a new document ID

    try {
        // Get original Grid FS file
        GridFSDBFile doc = Staging_repository.findDOByID(id);

        // Form updated metadata object
        DBObject metadataObject = (DBObject) JSON.parse(metadata);
        // Push back updated DO to MongoDB and get a new doc ID
        String updated_id = Staging_repository.addDO(doc.getInputStream(), doc.getFilename(),
                doc.getContentType(), metadataObject);
        MessageResponse response = new MessageResponse(true, updated_id);
        return response;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        MessageResponse response = new MessageResponse(false, null);
        return response;
    }
}

From source file:DataBase.JavaMongoDB.java

/**
 *
 * @param JsonEntrada String con el documento en el formato JSON
 *//*w  ww.  j a v a 2s  .c  o m*/
public void InsertarDocumentoJSON(String JsonEntrada) {
    DBObject Documento;
    Documento = (BasicDBObject) JSON.parse(JsonEntrada);
    long time_start, time_end;
    time_start = System.currentTimeMillis();
    col.insert(Documento);
    time_end = System.currentTimeMillis();
    time = (time_end - time_start);

}

From source file:de.ifgi.fmt.mongo.conv.JSONConverter.java

License:Open Source License

/**
 * //from   w w  w.  j a  v  a2  s. c om
 * @param value
 * @param optionalExtraInfo
 * @return
 */
@Override
public Object encode(Object value, MappedField optionalExtraInfo) {
    if (value == null)
        return null;
    String s = ((JSONObject) value).toString();
    return JSON.parse(s);
}

From source file:de.taimos.dvalin.mongo.MongoDBInit.java

License:Apache License

/**
 * init database with demo data//from  ww w  . j  av  a 2s.c o m
 */
@PostConstruct
public void initDatabase() {
    MongoDBInit.LOGGER.info("initializing MongoDB");
    String dbName = System.getProperty("mongodb.name");
    if (dbName == null) {
        throw new RuntimeException("Missing database name; Set system property 'mongodb.name'");
    }
    MongoDatabase db = this.mongo.getDatabase(dbName);

    try {
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources("classpath*:mongodb/*.ndjson");
        MongoDBInit.LOGGER.info("Scanning for collection data");
        for (Resource res : resources) {
            String filename = res.getFilename();
            String collection = filename.substring(0, filename.length() - 7);
            MongoDBInit.LOGGER.info("Found collection file: " + collection);
            MongoCollection<DBObject> dbCollection = db.getCollection(collection, DBObject.class);
            try (Scanner scan = new Scanner(res.getInputStream(), "UTF-8")) {
                int lines = 0;
                while (scan.hasNextLine()) {
                    String json = scan.nextLine();
                    Object parse = JSON.parse(json);
                    if (parse instanceof DBObject) {
                        DBObject dbObject = (DBObject) parse;
                        dbCollection.insertOne(dbObject);
                    } else {
                        MongoDBInit.LOGGER.error("Invalid object found: " + parse);
                        throw new RuntimeException("Invalid object");
                    }
                    lines++;
                }
                MongoDBInit.LOGGER.info("Imported " + lines + " objects into collection " + collection);
            }
        }
    } catch (IOException e) {
        throw new RuntimeException("Error importing objects", e);
    }
}

From source file:dsll.pinterest.crawler.Reduce.java

private static Text getPinContent(String url, DBCollection pinsCollection) throws JSONException {
    Document html = null;/* w  w w.j a v a2 s  .  co  m*/
    JSONObject pin = new JSONObject();
    try {
        html = Jsoup.connect(url).get();
    } catch (Exception e) {
        return new Text("HTTP connection failed...");
    }

    // Gather major pins data
    Element doc = html.select("body").first();
    // Pin ID
    String id = (url.split("pin/")[1].split("/")[0]);
    pin.append("ID", id);

    // Pin image
    String imageURL = "";
    Element tmp = doc.select("div[class=pinImageSourceWrapper]").first();
    try {
        tmp = tmp.select("div[class=imageContainer]").select("img").first();
        imageURL = tmp.attr("src");
    } catch (Exception e) {
    }
    //        try{
    //            ByteArrayOutputStream pimg=new ByteArrayOutputStream(), cimg = new ByteArrayOutputStream();
    //            for(int i=0; i<3; i++){ 
    //                BufferedImage img=dummyImage;
    //                try{
    //                    img = ImageIO.read(new URL(imageURL));
    //                
    //                }catch(Exception e){}
    //                ImageIO.write(img, "jpg", cimg);
    //                if(pimg.size()<cimg.size()){
    //                        pimg = cimg;
    //                }
    //            }
    //            // save to hdfs
    //            Configuration conf = new Configuration();
    //            FileSystem fs = FileSystem.get(conf);
    //            Path outFile = new Path("/home/hadoop/"+id+".png");
    //            FSDataOutputStream out = fs.create(outFile);
    //            out.write(pimg.toByteArray());
    //
    //        }catch(Exception e){
    //                e.printStackTrace();
    //        }
    pin.append("image", imageURL);

    //Pin name
    tmp = doc.select("h2[itemprop=name]").first();
    String name = "";
    if (tmp != null) {
        name = tmp.text().trim();
    }
    pin.append("name", name);

    // Pin source
    Element sourceCont = doc.select("div[class=sourceFlagWrapper]").first();
    JSONObject source = new JSONObject();
    if (sourceCont != null) {
        String title = sourceCont.text().trim();
        String src = sourceCont.select("a").first().attr("href");
        source.append("title", title);
        source.append("src", src);
    }
    pin.append("source", source);

    //pin credit
    JSONObject pinCredit = new JSONObject();
    Element credit = doc.select("div[class=pinCredits]").first();
    String creditName = "", creditTitle = "", creditSource = "";
    try {
        creditName = credit.select("div[class=creditName]").text().trim();
    } catch (Exception e) {
    }
    try {
        creditTitle = credit.select("div[class=creditTitle]").text().trim();
    } catch (Exception e) {
    }
    try {
        creditSource = credit.select("a").attr("href");
    } catch (Exception e) {
    }
    pinCredit.append("name", creditName);
    pinCredit.append("title", creditTitle);
    pinCredit.append("src", creditSource);
    pin.append("credit", pinCredit);

    //comments
    JSONArray comments = new JSONArray();
    Elements commentsConts = doc.select("div[class=commenterNameCommentText]");
    for (Element commentCont : commentsConts) {
        JSONObject comment = new JSONObject();
        Element creatorEle = commentCont.select("div[class=commenterWrapper] a").first();
        String creatorName = creatorEle.text().trim();
        String creatorSrc = creatorEle.attr("href");
        String content = "", raw = "";
        Element commentContent = commentCont.select(".commentDescriptionContent").first();
        try {
            content = commentContent.text().trim();
            raw = commentContent.html();
            comment.append("creator", creatorName);
            comment.append("creator_url", creatorSrc);
            comment.append("content", content);
            comment.append("content_raw", raw);
            comments.put(comment);
        } catch (Exception e) {
        }

    }
    pin.append("comments", comments);

    //pin board link and related pins
    Element bottomDoc = doc.select("div[class=Module CloseupSidebar]").first();

    //pin board
    JSONArray board = new JSONArray();
    if (bottomDoc != null) {
        Element boardEle = bottomDoc.select("div[class=boardHeader]").first();
        JSONObject b = new JSONObject();
        String boardName = "";
        try {
            boardName = boardEle.select("h3[class=title]").text().trim();
        } catch (Exception ee) {
        }
        String boardSrc = "";
        try {
            boardSrc = "https://www.pinterest.com" + boardEle.select("a").attr("href").trim();
        } catch (Exception ee) {
        }
        b.append("name", boardName);
        b.append("src", boardSrc);
        board.put(b);
    }
    pin.append("board", board); //CAUTION: what if a pin shows up in different boards?

    //related pins
    bottomDoc = doc
            .select("div[class=closeupBottom] div[class=Module CloseupBottom] div[class=relatedPinsWrapper]")
            .first();

    JSONArray relatedPins = new JSONArray();
    if (bottomDoc != null) {
        Elements relatedPinsConts = bottomDoc.select("div[class=pinWrapper]");
        for (Element relatedPinsCont : relatedPinsConts) {
            JSONObject relatedPin = new JSONObject();
            try {
                relatedPin.append("src", "https://www.pinterest.com"
                        + relatedPinsCont.select("div[class=pinHolder] > a").attr("href"));
            } catch (Exception e) {
            }
            relatedPins.put(relatedPin);
        }
    }
    pin.append("related_pins", relatedPins);

    // Optional: push data to database
    BasicDBObject dbObject = (BasicDBObject) JSON.parse(pin.toString());
    pinsCollection.insert(dbObject);
    return new Text(pin.toString());
}

From source file:dsll.pinterest.crawler.Reduce.java

private static Text getBoardContent(String url, DBCollection boardsCollection) throws JSONException {
    // NOTE: only board information is crawled. the pins are left to the expanding process
    Document html = null;/*from ww w  . jav a  2  s . c om*/
    JSONObject board = new JSONObject();
    try {
        html = Jsoup.connect(url).get();
    } catch (Exception e) {
        return new Text("HTTP connection failed...");
    }

    // board major information
    String[] tmp = url.split("/");
    String boardID = tmp[4];
    String boardOwnrID = tmp[3];
    String boardName = html.select("h1[class=boardName]").text().trim();
    String boardDesp = html.select("p[class=description]").text().trim();
    String boardOwnr = html.select("h4[classs=fullname]").text().trim();

    // Contained Pins
    Elements pinsCont = html.select("div[class=pinWrapper]");
    JSONArray pins = new JSONArray();
    for (Element pinCont : pinsCont) {
        JSONObject pin = new JSONObject();
        pin.append("src", pinCont.select("div[class=pinHolder]>a").first().attr("href"));
        pins.put(pin);
    }
    board.append("ID", boardID);
    board.append("owner_id", boardOwnrID);
    board.append("src", url);
    board.append("name", boardName);
    board.append("description", boardDesp);
    board.append("owner", boardOwnr);
    board.append("pins", pins);

    // Optional: push data to database
    BasicDBObject dbObject = (BasicDBObject) JSON.parse(board.toString());
    boardsCollection.insert(dbObject);
    return new Text(board.toString());
}

From source file:edu.byu.tweetsentiment.TwitterFilterStream.java

License:Apache License

public void insertTweet(String tweet) {

    //Adding data to the DbObject        
    BasicDBObject dbObject = (BasicDBObject) JSON.parse(tweet);

    //Using the MyTweet object to calculate sentiment and insert into the database
    MyTweet t = new MyTweet(dbObject);
    t.calculateSentiment();/*from w  ww.  j ava2 s  . c o m*/
    t.parseDate();
    t.insertToDB();

}

From source file:edu.cmu.cs.lti.discoursedb.io.twitter.converter.TwitterConverter.java

License:Open Source License

/**
 * //from ww  w.j  av a2s .  co m
 * @param tweetDocument
 *            a MongoDB document representing a tweet
 * @param pemsMetaDb
 *            database with the pems data
 * @param pemsMetaCollection
 *            collection with the pems meta data
 * @param pemsdatacollection
 *            collection with the pems station data
 * @return an object containing the pems station data to be mapped to the
 *         tweet
 */
private PemsStationMetaData document2Pems(Document tweetDocument, String pemsMetaDb,
        String pemsMetaCollection) {

    Assert.notNull(tweetDocument, "The mongodb document representing the tweet to be parsed cannot be null.");
    PemsStationMetaData pems = null;

    Document location = (Document) tweetDocument.get("location");
    Document place = (Document) tweetDocument.get("place");
    if (location != null && !location.isEmpty()) {
        // if available, map gps tag to station
        log.trace("Mapping gps location to PEMS station data.");

        BasicDBObject query = (BasicDBObject) JSON.parse("{location:{$near:{$geometry:" + location.toJson()
                + ",$minDistance:0,$maxDistance: " + MAX_DIST + "}}}");
        Document metaData = (Document) mongoClient.getDatabase(pemsMetaDb).getCollection(pemsMetaCollection)
                .find(query).first();
        pems = new PemsStationMetaData(metaData);
    } else if (place != null && !place.isEmpty()) {
        // if no gps tag is available, map place to station if available
        log.trace("Mapping place tag to PEMS station data.");
        log.warn("Not yet implemented yet."); // TODO implement place
        // mapping

    } else {
        // no gps tag or place available - do nothing for now
        log.trace("No location or place available for PEMS station data mapping.");
    }

    return pems;
}

From source file:edu.purdue.cybercenter.dm.util.DatasetUtils.java

public static Object deserialize(String json) {
    return JSON.parse(json);
}

From source file:edu.sjsu.carbonated.mongodbaccessors.MongoDBAlbum.java

License:Apache License

public void addPhotoToAlbum(AlbumResource albumRes) {

    // check to see if the user has the rights to the album
    DBCursor cur = albumColl.find(new BasicDBObject("$and", JSON.parse("[{\"album_id\":\""
            + albumRes.getAlbum_id() + "\"},{\"user_id\":\"" + albumRes.getUser_id() + "\"}]")));

    // if the is no match betwen an album id and user id, throw error
    if (!cur.hasNext()) {
        throw new WebApplicationException(Response.status(400).entity("Album not found").build());

    }/*from  ww  w .  j  av  a 2s .c o m*/

    BasicDBObject info = new BasicDBObject();
    info.putAll(albumRes.getMap());
    info.put("photo_url", photo_url_location + albumRes.getAlbum_id() + "/" + albumRes.getPhoto_id() + ".png");
    photoColl.insert(info);

}