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:org.basex.modules.MongoDB.java

License:BSD License

/**
 * Take string as Str and return DBObject of mongodb.
 * @param string/*  w  ww.  j a  v a2s .  c  o  m*/
 * @return
 * @throws QueryException
 */
protected DBObject getDbObjectFromStr(final Item item) throws QueryException {
    try {
        if (item instanceof Map) {
            return mapToDBObject((Map) item);
        } else if (item instanceof Str) {
            final String string = itemToString(item);
            return (DBObject) JSON.parse(string);
        } else {
            throw MongoDBErrors.generalExceptionError("Number Expected for key '");
        }
    } catch (JSONParseException e) {
        throw MongoDBErrors.jsonFormatError(item.toJava());
    }
}

From source file:org.basex.modules.nosql.MongoDB.java

License:BSD License

/**
 * Take string as Str and return DBObject of mongodb.
 * @param item item(Map or Str)//w w  w. j a  v a2 s.  com
 * @return DBObject
 * @throws QueryException query exception
 */
protected DBObject getDbObjectFromItem(final Item item) throws QueryException {
    try {
        if (item instanceof Map) {
            return mapToDBObject((Map) item);
        } else if (item instanceof Str) {
            final String string = itemToString(item);
            return (DBObject) JSON.parse(string);
        } else {
            throw MongoDBErrors.generalExceptionError("Number Expected for key '");
        }
    } catch (JSONParseException e) {
        throw MongoDBErrors.jsonFormatError(item.toJava());
    }
}

From source file:org.basex.modules.nosql.RethinkDB.java

License:BSD License

/**
 * insert json{map or json string} to table.
 * @param handler database handler/*from  w w w.j  a v  a  2s .  co m*/
 * @param table table name
 * @param insert Item to be inserted
 * @param options other options.
 * @return Item
 * @throws QueryException query exception
 */
public Item insert(final Str handler, final Str table, final Item insert, final Map options)
        throws QueryException {
    com.dkhenry.RethinkDB.RqlTopLevelQuery.DB db = getRethikClientDB(handler);
    if (insert == null) {
        throw new QueryException("insert string cannot be empty");
    }
    Object json;
    if (insert instanceof Str) {
        json = JSON.parse((String) insert.toJava());
    } else {
        json = ((Map) insert).toJava();
    }
    try {
        RqlQuery i;
        if (options == null) {
            i = db.table(table.toJava()).insert(json);
        } else {
            i = db.table(table.toJava()).insert(json, options.toJava());
        }
        return run(handler, i);
    } catch (Exception e) {
        throw new QueryException(e.getMessage());
    }
}

From source file:org.basex.modules.RethinkDB.java

License:BSD License

public Item insert(final Str handler, final Str table, final Item insertString, final Map options)
        throws QueryException {
    com.dkhenry.RethinkDB.RqlTopLevelQuery.DB db = getRethikClientDB(handler);
    if (insertString == null) {
        throw new QueryException("insert string cannot be empty");
    }//from  w w  w . ja  va 2 s.  c o m
    Object json;
    if (insertString instanceof Str) {
        json = JSON.parse((String) insertString.toJava());
    } else {
        json = ((Map) insertString).toJava();
    }
    try {
        RqlQuery i;
        if (options == null) {
            i = db.table(table.toJava()).insert(json);
        } else {
            i = db.table(table.toJava()).insert(json, options.toJava());
        }
        return run(handler, i);
    } catch (Exception e) {
        throw new QueryException(e.getMessage());
    }
}

From source file:org.benjp.server.ChatServer.java

License:Open Source License

@Resource
@Route("/sendMeetingNotes")
public Response.Content sendMeetingNotes(String user, String token, String room, String fromTimestamp,
        String toTimestamp) throws IOException {
    if (!tokenService.hasUserWithToken(user, token)) {
        return Response.notFound("Petit malin !");
    }//from   ww  w .j a  v a  2  s. c  o m

    Long from = null;
    Long to = null;
    String html = "";
    try {
        if (fromTimestamp != null && !"".equals(fromTimestamp))
            from = Long.parseLong(fromTimestamp);
    } catch (NumberFormatException nfe) {
        log.info("fromTimestamp is not a valid Long number");
    }
    try {
        if (toTimestamp != null && !"".equals(toTimestamp))
            to = Long.parseLong(toTimestamp);
    } catch (NumberFormatException nfe) {
        log.info("fromTimestamp is not a valid Long number");
    }
    String data = chatService.read(room, userService, false, from, to);
    BasicDBObject datao = (BasicDBObject) JSON.parse(data);
    if (datao.containsField("messages")) {
        List<UserBean> users = userService.getUsers(room);
        ReportBean reportBean = new ReportBean();
        reportBean.fill((BasicDBList) datao.get("messages"), users);

        ArrayList<String> tos = new ArrayList<String>();
        String senderFullname = user;
        for (UserBean userBean : users) {
            if (!"".equals(userBean.getEmail())) {
                tos.add(userBean.getEmail());
            }
            if (user.equals(userBean.getName())) {
                senderFullname = userBean.getFullname();
            }
        }

        String roomName = "";
        List<SpaceBean> spaces = userService.getSpaces(user);
        for (SpaceBean spaceBean : spaces) {
            if (room.equals(spaceBean.getRoom())) {
                roomName = spaceBean.getDisplayName();
            }
        }
        List<RoomBean> roomBeans = userService.getTeams(user);
        for (RoomBean roomBean : roomBeans) {
            if (room.equals(roomBean.getRoom())) {
                roomName = roomBean.getFullname();
            }
        }
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        String date = formatter.format(new GregorianCalendar().getTime());
        String title = roomName + " : Meeting Notes [" + date + "]";
        html = reportBean.getAsHtml(title);

        try {
            sendMailWithAuth(senderFullname, tos, html.toString(), title);
        } catch (Exception e) {
            log.info(e.getMessage());
        }

    }

    return Response.ok("sent").withMimeType("text/event-stream; charset=UTF-8").withHeader("Cache-Control",
            "no-cache");
}

From source file:org.benjp.server.ChatServer.java

License:Open Source License

@Resource
@Route("/getMeetingNotes")
public Response.Content getMeetingNotes(String user, String token, String room, String fromTimestamp,
        String toTimestamp, String serverBase) throws IOException {
    if (!tokenService.hasUserWithToken(user, token)) {
        return Response.notFound("Petit malin !");
    }//  w w w . j a  v a2  s .com

    Long from = null;
    Long to = null;
    String xwiki = "";
    try {
        if (fromTimestamp != null && !"".equals(fromTimestamp))
            from = Long.parseLong(fromTimestamp);
    } catch (NumberFormatException nfe) {
        log.info("fromTimestamp is not a valid Long number");
    }
    try {
        if (toTimestamp != null && !"".equals(toTimestamp))
            to = Long.parseLong(toTimestamp);
    } catch (NumberFormatException nfe) {
        log.info("fromTimestamp is not a valid Long number");
    }
    String data = chatService.read(room, userService, false, from, to);
    BasicDBObject datao = (BasicDBObject) JSON.parse(data);
    if (datao.containsField("messages")) {
        List<UserBean> users = userService.getUsers(room);
        ReportBean reportBean = new ReportBean();
        reportBean.fill((BasicDBList) datao.get("messages"), users);

        String roomName = "";
        List<SpaceBean> spaces = userService.getSpaces(user);
        for (SpaceBean spaceBean : spaces) {
            if (room.equals(spaceBean.getRoom())) {
                roomName = spaceBean.getDisplayName();
            }
        }
        List<RoomBean> roomBeans = userService.getTeams(user);
        for (RoomBean roomBean : roomBeans) {
            if (room.equals(roomBean.getRoom())) {
                roomName = roomBean.getFullname();
            }
        }
        xwiki = reportBean.getAsXWiki(serverBase);

    }

    return Response.ok(xwiki).withMimeType("text/event-stream; charset=UTF-8").withHeader("Cache-Control",
            "no-cache");
}

From source file:org.bireme.interop.fromJson.Json2Mongo.java

License:Open Source License

private DBObject convertToDBObj(final JSONObject jobj) {
    assert jobj != null;

    return (DBObject) JSON.parse(jobj.toString());
}

From source file:org.bireme.interop.toJson.Mongo2Json.java

License:Open Source License

public void setQuery(final String query, final int from, final int to) throws Exception {
    if (query == null) {
        throw new NullPointerException("query");
    }//from  w  ww.  j  a v  a 2  s  . com
    if (from < 1) {
        throw new IllegalArgumentException("from[" + from + "] < 1");
    }
    if (to < from) {
        throw new IllegalArgumentException("to[" + to + "] < from[" + from + "]");
    }
    this.to = to;
    this.from = from;

    final Object obj = JSON.parse(query);
    final DBObject dbObj = (DBObject) obj;

    cursor = coll.find(dbObj).skip(from - 1).limit(to - from + 1);
    next = getNext();
}

From source file:org.botdefender.collector.MongoMessageWriter.java

License:Open Source License

public void writeHit(String json) throws Exception {
    try {//from w w  w .ja  v  a 2s.c  o  m
        if (collection == null) {
            logger.info("Connecting to the mongodb database");
            collection = connectHitsCollection();
            logger.info("successfully connected");
        }

        if (json != null) {
            jsonList.add((DBObject) JSON.parse(json));
        }

        if ((json == null && jsonList.size() > 0)
                || (jsonList.size() > config.getMessageWriterMaxBatchSize())) {
            collection.insert(jsonList, WriteConcern.ACKNOWLEDGED);
            jsonList.clear();
            logger.info("INSERTING INTO DATABASE");
        }
    } catch (Exception e) {
        // We can throw away old events if the queue is full
        logger.error("Error trying to write data, trying again [" + e.getMessage() + "]");
        collection = null;
        Thread.sleep(1000);
    }
}

From source file:org.cbioportal.genome_nexus.annotation.domain.internal.VariantAnnotationRepositoryImpl.java

License:Open Source License

/**
  * Transforms the given annotationJSON to a DBObject instance.
  * If the given annotation JSON is an array, returns only the first element.
  */*  w  w  w . j ava  2 s  .c  o  m*/
  * @param annotationJSON    annotation JSON as a string
  * @return DBObject instance
  */
private DBObject convertToDbObject(String annotationJSON) {
    DBObject dbObject = (DBObject) JSON.parse(annotationJSON);

    // if it is a list, get the first element of the list
    if (dbObject instanceof List) {
        List list = ((List) dbObject);

        // get the first element, ignore the rest
        if (list.size() > 0) {
            dbObject = (DBObject) list.iterator().next();
        }
    }

    return dbObject;
}