List of usage examples for com.mongodb.util JSON serialize
public static String serialize(final Object object)
Serializes an object into its JSON form.
This method delegates serialization to JSONSerializers.getLegacy
From source file:org.qi4j.entitystore.mongodb.MongoMapEntityStoreMixin.java
License:Apache License
@Override public Reader get(EntityReference entityReference) throws EntityStoreException { db.requestStart();/* w w w. j av a 2 s. co m*/ DBObject entity = db.getCollection(collectionName).findOne(byIdentity(entityReference)); if (entity == null) { throw new EntityNotFoundException(entityReference); } DBObject bsonState = (DBObject) entity.get(STATE_COLUMN); db.requestDone(); String jsonState = JSON.serialize(bsonState); return new StringReader(jsonState); }
From source file:org.qi4j.entitystore.mongodb.MongoMapEntityStoreMixin.java
License:Apache License
@Override public Input<Reader, IOException> entityStates() { return new Input<Reader, IOException>() { @Override//from ww w. j a v a 2 s . co m public <ReceiverThrowableType extends Throwable> void transferTo( Output<? super Reader, ReceiverThrowableType> output) throws IOException, ReceiverThrowableType { output.receiveFrom(new Sender<Reader, IOException>() { @Override public <ReceiverThrowableType extends Throwable> void sendTo( Receiver<? super Reader, ReceiverThrowableType> receiver) throws ReceiverThrowableType, IOException { db.requestStart(); DBCursor cursor = db.getCollection(collectionName).find(); while (cursor.hasNext()) { DBObject eachEntity = cursor.next(); DBObject bsonState = (DBObject) eachEntity.get(STATE_COLUMN); String jsonState = JSON.serialize(bsonState); receiver.receive(new StringReader(jsonState)); } db.requestDone(); } }); } }; }
From source file:org.springframework.batch.item.data.MongoItemReader.java
License:Apache License
private String getParameterWithIndex(List<Object> values, int index) { return JSON.serialize(values.get(index)); }
From source file:org.springframework.data.mongodb.core.query.SerializationUtils.java
License:Apache License
/** * Serializes the given object into pseudo-JSON meaning it's trying to create a JSON representation as far as possible * but falling back to the given object's {@link Object#toString()} method if it's not serializable. Useful for * printing raw {@link DBObject}s containing complex values before actually converting them into Mongo native types. * /*w w w .j a v a 2 s . c om*/ * @param value * @return */ public static String serializeToJsonSafely(Object value) { if (value == null) { return null; } try { return JSON.serialize(value); } catch (Exception e) { if (value instanceof Collection) { return toString((Collection<?>) value); } else if (value instanceof Map) { return toString((Map<?, ?>) value); } else if (value instanceof DBObject) { return toString(((DBObject) value).toMap()); } else { return String.format("{ $java : %s }", value.toString()); } } }
From source file:org.springframework.data.mongodb.repository.query.ExpressionEvaluatingParameterBinder.java
License:Apache License
/** * Returns the serialized value to be used for the given {@link ParameterBinding}. * /*from w w w. j ava 2 s .co m*/ * @param accessor must not be {@literal null}. * @param parameters * @param binding must not be {@literal null}. * @return */ private String getParameterValueForBinding(MongoParameterAccessor accessor, MongoParameters parameters, ParameterBinding binding) { Object value = binding.isExpression() ? evaluateExpression(binding.getExpression(), parameters, accessor.getValues()) : accessor.getBindableValue(binding.getParameterIndex()); if (value instanceof String && binding.isQuoted()) { return (String) value; } if (value instanceof byte[]) { String base64representation = DatatypeConverter.printBase64Binary((byte[]) value); if (!binding.isQuoted()) { return "{ '$binary' : '" + base64representation + "', '$type' : " + BSON.B_GENERAL + "}"; } return base64representation; } return JSON.serialize(value); }
From source file:org.springframework.data.services.rest.DataController.java
License:Apache License
@RequestMapping(value = "/", method = RequestMethod.GET) @ResponseBody/*w w w.ja va 2s.c om*/ public String showCollections() { Set<String> results = new HashSet<String>(); Set<String> collections = this.mongoTemplate.getCollectionNames(); for (String collection : collections) { if (!collection.contains("system")) { results.add(collection); } } return JSON.serialize(results); }
From source file:org.springframework.data.services.rest.DataController.java
License:Apache License
@RequestMapping(value = "/{collection}", method = RequestMethod.GET) @ResponseBody/* w w w . ja va 2s . com*/ public String list(@PathVariable String collection) { List<DBObject> results = this.mongoTemplate.findAll(DBObject.class, collection); for (DBObject result : results) { result.removeField("_class"); result.removeField("_id"); } return JSON.serialize(results); }
From source file:org.springframework.data.services.rest.DataController.java
License:Apache License
@RequestMapping(value = "/{collection}/{uid}", method = RequestMethod.GET) @ResponseBody//from w w w . j av a 2s . com public String get(@PathVariable String collection, @PathVariable long uid) { Query query = new Query(Criteria.where("uid").is(uid)); List<DBObject> results = this.mongoTemplate.find(query, DBObject.class, collection); if (CollectionUtils.isEmpty(results)) { return null; // 404? } DBObject result = results.get(0); result.removeField("_class"); result.removeField("_id"); return JSON.serialize(result); }
From source file:org.springframework.session.data.mongo.JacksonMongoSessionConverter.java
License:Apache License
@Override protected MongoExpiringSession convert(DBObject source) { String json = JSON.serialize(source); try {//from w w w.jav a 2 s. co m return this.objectMapper.readValue(json, MongoExpiringSession.class); } catch (IOException e) { LOG.error("Error during Mongo Session deserialization", e); return null; } }
From source file:org.teiid.translator.mongodb.MongoDBDirectQueryExecution.java
License:Open Source License
@Override public List<?> next() throws TranslatorException, DataNotAvailableException { final DBObject value = nextRow(); if (value == null) { return null; }/*from www . j a v a 2 s.c om*/ BlobType result = new BlobType(new BlobImpl(new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(JSON.serialize(value).getBytes(Streamable.CHARSET)); } })); if (returnsArray) { List<Object[]> row = new ArrayList<Object[]>(1); row.add(new Object[] { result }); return row; } return Arrays.asList(result); }