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:fr.wseduc.resizer.GridFsFileAccess.java

License:Apache License

private DBObject pathToDbObject(String s) {
    String str = new JsonObject().putString("_id", s).encode();
    return (DBObject) JSON.parse(str);
}

From source file:gr.ntua.ivml.awareness.search.SearchServiceAccess.java

public BasicDBObject searchEuropeana(String originalTerm, String type, int startPage) throws Exception {
    String[] termsarray = originalTerm.split(" ");
    List terms = Arrays.asList(termsarray);
    URI uri = constructURI(terms, type, startPage - 1);
    httpGet = new HttpGet(uri);
    BasicDBObject response = new BasicDBObject();

    httpRes = httpClient.execute(httpGet);
    httpEntity = httpRes.getEntity();//from w  ww. j a  va 2s.co m
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    httpEntity.writeTo(out);
    String resp = new String(out.toByteArray(), "UTF-8");
    BasicDBObject obj = (BasicDBObject) JSON.parse(resp);
    response.put("itemsCount", obj.getString("itemsCount"));
    response.put("totalResults", obj.getString("totalResults"));
    response.put("term", originalTerm);
    response.put("type", type);
    response.put("pageNumber", startPage);
    @SuppressWarnings("unchecked")
    ArrayList<Object> items = (ArrayList<Object>) obj.get("items");
    if (items != null) {
        Iterator<Object> it = items.iterator();
        ArrayList<BasicDBObject> responseItems = new ArrayList<BasicDBObject>();
        while (it.hasNext()) {
            BasicDBObject tmp = (BasicDBObject) it.next();
            BasicDBObject re = new BasicDBObject();
            String recid = tmp.getString("id");

            re.put("europeanaid", recid);
            re.put("type", tmp.getString("type"));
            re.put("source", tmp.getString("guid"));
            BasicDBList titleslist = (BasicDBList) tmp.get("title");
            if (titleslist != null)
                re.put("title", StringUtils.join(titleslist, ','));

            BasicDBList providerslist = (BasicDBList) tmp.get("dataProvider");
            if (providerslist != null)
                re.put("dataProvider", providerslist.get(0));
            providerslist = (BasicDBList) tmp.get("provider");
            if (providerslist != null)
                re.put("provider", providerslist.get(0));

            BasicDBList creatorlist = (BasicDBList) tmp.get("dcCreator");
            if (creatorlist != null)
                re.put("dcCreator", creatorlist.get(0));

            BasicDBList languagelist = (BasicDBList) tmp.get("language");
            if (languagelist != null)
                re.put("language", languagelist.get(0));

            BasicDBList thumbslist = (BasicDBList) tmp.get("edmPreview");
            if (thumbslist != null)
                re.put("url", thumbslist.get(0));

            BasicDBList rightslist = (BasicDBList) tmp.get("rights");
            if (rightslist != null)
                re.put("license", rightslist.get(0));

            responseItems.add(re);

        }
        response.put("items", responseItems);
    }
    return response;
}

From source file:gr.ntua.ivml.awareness.search.SearchServiceAccess.java

public BasicDBObject searchEuropeanaRecord(String recid) throws Exception {
    URI uri = constructURIRecord(recid);
    httpGet = new HttpGet(uri);

    httpRes = httpClient.execute(httpGet);
    httpEntity = httpRes.getEntity();/*from w ww  .j  a v a 2 s  .  c o  m*/
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    httpEntity.writeTo(out);
    String resprec = new String(out.toByteArray(), "UTF-8");
    BasicDBObject objrecord = (BasicDBObject) JSON.parse(resprec);

    BasicDBObject record = (BasicDBObject) objrecord.get("object");
    BasicDBObject re = new BasicDBObject();

    ArrayList<Object> items = (ArrayList<Object>) record.get("aggregations");
    Iterator<Object> it = items.iterator();
    while (it.hasNext()) {
        BasicDBObject tmp = (BasicDBObject) it.next();
        Object edmObject = tmp.get("edmObject");
        if (edmObject != null)
            re.put("europeana_image", edmObject.toString());

    }

    return re;

}

From source file:gr.teicm.toulou.SignupResource.java

@POST
@Consumes("text/plain")
public String postUser(String user) {
    System.out.println(user);//  w  ww  .j av a  2  s.  co  m
    Gson gson = new Gson();
    DBObject dbObjectUser = (DBObject) JSON.parse(user);
    System.out.println("parsed leei");

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    System.out.println("connected leei");

    DB db = mongoClient.getDB("snapchatydb");
    System.out.println("fetched db leei");
    DBCollection coll = (DBCollection) db.getCollection("user");
    System.out.println("fetched collection leei");

    coll.insert(dbObjectUser);
    System.out.println("inserted leei");
    return "";
}

From source file:HAL.libraries.blackboard_client.BlackboardClient.java

License:Open Source License

/**
 * Utility function for parsing JSON that catches the runtime JSON exception and throws an InvalidJSONException instead.
 * @param jsonString The JSON string that needs to be parsed to a DBObject.
 * @return The DBObject parsed from the JSON string.
 * @throws InvalidJSONException An error exists within the JSON.
 **///w  w w. j av  a  2 s  . c o  m
static DBObject parseJSONWithCheckedException(String jsonString) throws InvalidJSONException {
    DBObject obj = null;
    try {
        obj = (DBObject) JSON.parse(jsonString);
    } catch (JSONParseException ex) {
        throw new InvalidJSONException(ex);
    }

    return obj;
}

From source file:implementations.mongoDB.java

License:GNU General Public License

@Override
public String readDB(String ID) {
    String ret;/*w  w w.j  av a 2  s.  co  m*/
    try {
        db.requestStart();
        BasicDBObject query = new BasicDBObject();
        query.put("_id", ID);
        DBCursor cur = dbCol.find(query);
        ret = "";
        while (cur.hasNext()) {
            DBObject temp = (DBObject) JSON.parse(cur.next().toString());
            ret = (String) temp.get("artValue");

        }
        db.requestDone();
    } catch (Exception e) {
        e.printStackTrace();
        ret = null;
    }
    return ret;
}

From source file:in.mtap.iincube.mongoser.codec.JsonArrayDecoder.java

License:Apache License

@Override
public List<DBObject> getAsDBObject() {
    List<DBObject> dbObjects = new LinkedList<DBObject>();
    if (!isValid())
        throw new IllegalArgumentException("Parse error invalid json: \n" + dataBuilder.toString());

    if (jsonElement.isJsonArray()) {
        JsonArray jsonArray = jsonElement.getAsJsonArray();
        for (JsonElement element : jsonArray) {
            dbObjects.add((DBObject) JSON.parse(element.toString()));
        }/*from   w ww  .ja va2s .com*/
    } else {
        dbObjects.add((DBObject) JSON.parse(jsonElement.toString()));
    }
    return dbObjects;
}

From source file:in.mtap.iincube.mongoser.codec.SimpleLineDecoder.java

License:Apache License

private void addIfParseable(String data) {
    try {//from   ww  w .  j ava2s . c  o  m
        dbObjects.add((DBObject) JSON.parse(data));
    } catch (JSONParseException e) {
        e.printStackTrace();
    }
}

From source file:integration.util.mongodb.JsonReader.java

License:Open Source License

public JsonReader(URL location) throws IOException {
    final File file = new File(location.getPath());

    final ObjectMapper mapper = new ObjectMapper();
    final TypeReference ref = new TypeReference<Map<String, List<Map<String, Object>>>>() {
    };//from w  w w . jav  a 2 s  .c o  m
    final Map<String, List<Map<String, Object>>> rawMap = mapper.readValue(file, ref);

    for (Map.Entry<String, List<Map<String, Object>>> entry : rawMap.entrySet()) {
        if (!collectionMap.containsKey(entry.getKey()))
            collectionMap.put(entry.getKey(), Lists.<DBObject>newArrayList());

        for (Map<String, Object> rawDoc : entry.getValue()) {
            final BasicDBObject dbObject = (BasicDBObject) JSON.parse(mapper.writeValueAsString(rawDoc));
            collectionMap.get(entry.getKey()).add(dbObject);
        }
    }
}

From source file:io.liveoak.mongo.MongoAggregationResource.java

License:Open Source License

private BasicDBList aggregate(RequestContext ctx) {
    BasicDBList queryObject = new BasicDBList();
    if (ctx.resourceParams() != null && ctx.resourceParams().contains("q")) {
        String queryString = ctx.resourceParams().value("q");
        DBObject paramObject = (DBObject) JSON.parse(queryString);

        if (paramObject instanceof BasicDBList) {
            queryObject = (BasicDBList) paramObject;
        } else {//  w ww .j  av a2  s. co  m
            queryObject.add(paramObject);
        }
    }

    DBCollection dbCollection = parent().getDBCollection();

    try {
        BasicDBList result = new BasicDBList();
        AggregationOutput output = dbCollection.aggregate((DBObject) queryObject.remove(0),
                queryObject.toArray(new DBObject[queryObject.size()]));
        for (DBObject dbObject : output.results()) {
            result.add(dbObject);
        }

        return result;

    } catch (Exception e) {
        logger().error("", e);
        throw new RuntimeException("Aggregation query failed: ", e);
    }
}