List of usage examples for com.mongodb.util JSON parse
public static Object parse(final String jsonString)
Parses a JSON string and returns a corresponding Java object.
From source file:com.edgytech.umongo.CollectionPanel.java
License:Apache License
public void removeTagRange(ButtonBase button) { final DB config = getCollectionNode().getCollection().getDB().getSisterDB("config"); final DBCollection col = config.getCollection("tags"); final String ns = getCollectionNode().getCollection().getFullName(); String value = getComponentStringFieldValue(Item.tagRangeList); BasicDBObject range = (BasicDBObject) JSON.parse(value); final DBObject min = (DBObject) range.get("min"); final DBObject doc = new BasicDBObject("_id", new BasicDBObject("ns", ns).append("min", min)); new DbJob() { @Override//from w w w. jav a 2s .c o m public Object doRun() { return col.remove(doc); } @Override public String getNS() { return ns; } @Override public String getShortName() { return "Remove Tag Range"; } @Override public DBObject getRoot(Object result) { BasicDBObject root = new BasicDBObject("doc", doc); return root; } @Override public void wrapUp(Object res) { super.wrapUp(res); refreshTagRangeList(); } }.addJob(); }
From source file:com.edgytech.umongo.DocBuilderField.java
License:Apache License
public void edit(ButtonBase button) { String txt = getComponentStringFieldValue(Item.jsonText); try {//ww w . j av a 2 s. c o m doc = (DBObject) JSON.parse(txt); } catch (Exception ex) { // this could be because of binary in field getLogger().log(Level.INFO, null, ex); } DocBuilderDialog dialog = UMongo.instance.getGlobalStore().getDocBuilderDialog(); dialog.setDBObject(doc); if (!dialog.show()) { return; } doc = dialog.getDBObject(); value = MongoUtils.getJSON(doc); setComponentStringFieldValue(Item.jsonText, value); notifyListener(getComponent()); }
From source file:com.edgytech.umongo.DocBuilderField.java
License:Apache License
@Override protected boolean checkComponentCustom(BoxPanel comp) { // String txt = _field.getText().trim(); String txt = getComponentStringFieldValue(Item.jsonText); if (nonEmpty && txt.isEmpty()) { setDisplayError("Field cannot be empty"); return false; }//w w w . j av a2 s .com if (!getComponentBooleanFieldValue(Item.validate)) { return true; } try { // 1st parse with GSON to check, since our parser has bugs MongoUtils.getJsonParser().parse(txt); DBObject doc = (DBObject) JSON.parse(txt); return true; } catch (Exception e) { // this could be because of binary in field getLogger().log(Level.INFO, null, e); } setDisplayError("Invalid JSON format: correct or disable validation"); return false; }
From source file:com.edgytech.umongo.DocBuilderField.java
License:Apache License
@Override protected void commitComponentCustom(BoxPanel comp) { // here we want to commit the string value, but doc is already uptodate try {/*from w w w. java 2s. c o m*/ // value = _field.getText(); value = getStringFieldValue(Item.jsonText); doc = (DBObject) JSON.parse(value); } catch (Exception e) { // this could be because of binary in field // in this case the doc already has the correct inner value getLogger().log(Level.INFO, null, e); } }
From source file:com.edgytech.umongo.DocumentDeserializer.java
License:Apache License
public DBObject readObject() throws IOException { if (first) {/*from www. jav a2s . c om*/ if (format != Format.BSON) { if (is == null) { FileReader fr = new FileReader(file); br = new BufferedReader(fr); } else { br = new BufferedReader(new InputStreamReader(is)); } if (format == Format.CSV) { fields = br.readLine(); if (fields != null) { filter = fields.split(delimiter); // field names are never quoted for (int i = 0; i < filter.length; ++i) { filter[i] = filter[i].trim(); } } } } else { if (is == null) { is = new FileInputStream(file); } callback = new DefaultDBCallback(null); decoder = new BasicBSONDecoder(); } if (format == Format.JSON_ARRAY) { String line = br.readLine(); BasicDBList list = (BasicDBList) JSON.parse(line); iterator = list.iterator(); } first = false; } if (format == Format.JSON_ARRAY) { if (iterator == null || !iterator.hasNext()) { return null; } return (DBObject) iterator.next(); } DBObject obj = null; if (format != Format.BSON) { String line = br.readLine(); if (line == null) { return null; } if (format == Format.JSON_SINGLE_DOC) { // keep reading all lines String line2 = null; while ((line2 = br.readLine()) != null) { line += line2; } } if (format == Format.CSV) { List<String> values = splitByCommasNotInQuotes(line); if (template == null) { obj = new BasicDBObject(); // set each field defined for (int i = 0; i < filter.length; ++i) { String val = values.get(i); // string values are always quoted obj.put(filter[i], JSON.parse(val)); } } else { obj = (BasicDBObject) template.copy(); fillInTemplate(obj, values); } } else { obj = (DBObject) JSON.parse(line); } } else { // BSON is binary callback.reset(); try { decoder.decode(is, callback); } catch (IOException e) { // most likely EOF return null; } obj = (DBObject) callback.get(); // // read length // byte[] buf = new byte[4096]; // int n = fis.read(buf, 0, 4); // if (n <= 0) { // return null; // } // int len = Bits.readInt(buf); // // ByteArrayOutputStream baos = new ByteArrayOutputStream(); // baos.write(buf, 0, 4); // int toread = len; // while (toread > 0) { // n = fis.read(buf, 0, Math.min(toread, buf.length)); // if (n <= 0) { // break; // } // baos.write(buf, 0, n); // toread -= n; // } // if (baos.size() != len) // throw new IOException("Lenght of read object " + baos.size() + " does not match expected size " + len); // obj = new BasicDBObject((BasicBSONObject) BSON.decode(baos.toByteArray())); } return obj; }
From source file:com.edgytech.umongo.DocumentDeserializer.java
License:Apache License
private void fillInTemplate(DBObject obj, List<String> values) { for (String field : obj.keySet()) { Object val = obj.get(field); if (val instanceof BasicDBObject) { fillInTemplate((BasicDBObject) val, values); } else if (val instanceof BasicDBList) { fillInTemplate((BasicDBList) val, values); } else if (val instanceof String) { String str = (String) val; if (str.startsWith("$")) { str = str.substring(1);/*from w w w. j ava 2s. co m*/ int slash = str.indexOf("/"); String ref = str; String type = null; if (slash > 0) { ref = str.substring(0, slash); type = str.substring(slash + 1); } // find field index int index = 0; while (index < filter.length && !filter[index].equals(ref)) { ++index; } if (index >= filter.length) { continue; } String value = values.get(index); try { if (type == null || "JSON".equals(type)) { // this is typically used for quoted Strings obj.put(field, JSON.parse(value)); } else if ("String".equals(type)) { obj.put(field, value); } else if ("Date".equals(type)) { Long time = Long.valueOf(value); obj.put(field, new Date(time)); } else if ("Boolean".equals(type)) { obj.put(field, Boolean.valueOf(value)); } else if ("Integer".equals(type)) { obj.put(field, Integer.valueOf(value)); } else if ("Long".equals(type)) { obj.put(field, Long.valueOf(value)); } else if ("Double".equals(type)) { obj.put(field, Double.valueOf(value)); } } catch (Exception ex) { Logger.getLogger(DocumentDeserializer.class.getName()).log(Level.WARNING, null, ex); } } else { // this is a static value obj.put(field, val); } } else { // this is a static value obj.put(field, val); } } }
From source file:com.edgytech.umongo.EditCodeDialog.java
License:Apache License
@Override public Object getValue() { String code = getStringFieldValue(Item.value); String scope = getStringFieldValue(Item.scope); if (!scope.isEmpty()) return new CodeWScope(code, (BSONObject) JSON.parse(scope)); return new Code(code); }
From source file:com.englishtown.vertx.GridFSModule.java
License:Open Source License
public void saveFile(Message<JsonObject> message, JsonObject jsonObject) { ObjectId id = getObjectId(message, jsonObject, "id"); if (id == null) { return;//from ww w . j a v a 2 s. c om } Integer length = getRequiredInt("length", message, jsonObject, 1); if (length == null) { return; } Integer chunkSize = getRequiredInt("chunkSize", message, jsonObject, 1); if (chunkSize == null) { return; } long uploadDate = jsonObject.getLong("uploadDate", 0); if (uploadDate <= 0) { uploadDate = System.currentTimeMillis(); } String filename = jsonObject.getString("filename"); String contentType = jsonObject.getString("contentType"); JsonObject metadata = jsonObject.getObject("metadata"); try { BasicDBObjectBuilder builder = BasicDBObjectBuilder.start().add("_id", id).add("length", length) .add("chunkSize", chunkSize).add("uploadDate", new Date(uploadDate)); if (filename != null) builder.add("filename", filename); if (contentType != null) builder.add("contentType", contentType); if (metadata != null) builder.add("metadata", JSON.parse(metadata.encode())); DBObject dbObject = builder.get(); String bucket = jsonObject.getString("bucket", GridFS.DEFAULT_BUCKET); DBCollection collection = db.getCollection(bucket + ".files"); // Ensure standard indexes as long as collection is small if (collection.count() < 1000) { collection.ensureIndex(BasicDBObjectBuilder.start().add("filename", 1).add("uploadDate", 1).get()); } collection.save(dbObject); sendOK(message); } catch (Exception e) { sendError(message, "Error saving file", e); } }
From source file:com.ff.reportgenerator.mongodb.DynamicDatabaseWS.java
public void updateDatabase(ArrayList projects, DelegateWS dg) throws Exception { DB myDB = getDB(DB_NAME);// w ww .j a va 2 s .c om myDB.dropDatabase(); // clear old records myDB = getDB(DB_NAME); DBCollection coll = myDB.getCollection("projects"); int total = projects.size(); //System.out.println(total+" records!"); Iterator it = projects.iterator(); int count = 0; while (it.hasNext()) { String json = (String) it.next(); if (json != null) { DBObject dbObject = (DBObject) JSON.parse(json); coll.insert(dbObject); count++; } else { System.out.println("NULL RECORD!"); } //count++; dg.sendUpdate(Utility.percentage(count, total) + ""); } }
From source file:com.fuction.MongoDB.java
public static void input(ArrayList<String> lsResult) { try {/* w w w .ja v a 2 s . c om*/ Mongo mongo = new Mongo(HOST, PORT); DB db = mongo.getDB(DB); DBCollection collection = db.getCollection("Data"); for (String s : lsResult) { DBObject dbObject = (DBObject) JSON.parse(s); collection.insert(dbObject); } } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } }