├── .dockerignore
├── .github
└── workflows
│ ├── stale.yml
│ └── test.yml
├── .gitignore
├── LICENSE.txt
├── README.md
├── pom.xml
└── src
├── main
└── java
│ └── org
│ └── jongo
│ ├── Aggregate.java
│ ├── Command.java
│ ├── Distinct.java
│ ├── Find.java
│ ├── FindAndModify.java
│ ├── FindOne.java
│ ├── Insert.java
│ ├── Jongo.java
│ ├── Mapper.java
│ ├── MongoCollection.java
│ ├── MongoCursor.java
│ ├── ObjectIdUpdater.java
│ ├── Oid.java
│ ├── QueryModifier.java
│ ├── RawResultHandler.java
│ ├── ReflectiveObjectIdUpdater.java
│ ├── ResultHandler.java
│ ├── ResultHandlerFactory.java
│ ├── Update.java
│ ├── bson
│ ├── Bson.java
│ ├── BsonDBDecoder.java
│ ├── BsonDBEncoder.java
│ ├── BsonDBObject.java
│ ├── BsonDocument.java
│ ├── BufferedBsonDocument.java
│ ├── LazyBsonDocument.java
│ └── Primitives.java
│ ├── marshall
│ ├── Marshaller.java
│ ├── MarshallingException.java
│ ├── Unmarshaller.java
│ └── jackson
│ │ ├── IdSelector.java
│ │ ├── JacksonEngine.java
│ │ ├── JacksonIdFieldSelector.java
│ │ ├── JacksonMapper.java
│ │ ├── JacksonObjectIdUpdater.java
│ │ ├── JongoAnnotationIntrospector.java
│ │ ├── bson4jackson
│ │ ├── BsonDeserializers.java
│ │ ├── BsonModule.java
│ │ ├── BsonSerializers.java
│ │ ├── MongoBsonFactory.java
│ │ ├── MongoBsonGenerator.java
│ │ └── MongoBsonParser.java
│ │ ├── configuration
│ │ ├── AbstractMappingBuilder.java
│ │ ├── AnnotationModifier.java
│ │ ├── DefaultReaderCallback.java
│ │ ├── DefaultWriterCallback.java
│ │ ├── DeserializationFeatureModifier.java
│ │ ├── MapperFeatureModifier.java
│ │ ├── MapperModifier.java
│ │ ├── Mapping.java
│ │ ├── PropertyModifier.java
│ │ ├── ReaderCallback.java
│ │ ├── SerializationFeatureModifier.java
│ │ ├── ViewReaderCallback.java
│ │ ├── ViewWriterCallback.java
│ │ ├── VisibilityModifier.java
│ │ └── WriterCallback.java
│ │ └── oid
│ │ ├── Id.java
│ │ ├── MongoId.java
│ │ ├── MongoObjectId.java
│ │ ├── ObjectId.java
│ │ ├── ObjectIdDeserializer.java
│ │ └── ObjectIdSerializer.java
│ └── query
│ ├── BsonQueryFactory.java
│ ├── BsonSpecialChar.java
│ ├── Context.java
│ ├── Query.java
│ ├── QueryFactory.java
│ └── Stack.java
└── test
├── java
└── org
│ └── jongo
│ ├── AggregateTest.java
│ ├── AlreadyCheckDBObjectTest.java
│ ├── AnnotationsMisusedTest.java
│ ├── BinaryTest.java
│ ├── CommandTest.java
│ ├── CountTest.java
│ ├── DeprecatedAnnotationsCompatibilitySuiteTest.java
│ ├── DistinctTest.java
│ ├── FindAndModifyTest.java
│ ├── FindOneTest.java
│ ├── FindPartialFieldTest.java
│ ├── FindSkipSortLimitTest.java
│ ├── FindTest.java
│ ├── FindWithModifierTest.java
│ ├── FindWithResultMapperTest.java
│ ├── GridFsTest.java
│ ├── InsertTest.java
│ ├── JacksonAnnotationsHandlingTest.java
│ ├── JongoTest.java
│ ├── MongoCollectionTest.java
│ ├── MongoCursorTest.java
│ ├── NestedPolymorphismTest.java
│ ├── NonPojoTest.java
│ ├── PolymorphismTest.java
│ ├── ReflectiveObjectIdUpdaterTest.java
│ ├── RemoveTest.java
│ ├── SaveTest.java
│ ├── UpdateTest.java
│ ├── WriteConcernTest.java
│ ├── bench
│ ├── BenchUtil.java
│ ├── DecoderBench.java
│ ├── EncoderBench.java
│ ├── FindBench.java
│ ├── SaveBench.java
│ └── YourkitBench.java
│ ├── bson
│ ├── BsonDBEncoderTest.java
│ ├── LazyBsonDocumentTest.java
│ └── PrimitivesTest.java
│ ├── marshall
│ ├── DocumentMarshallingTest.java
│ ├── ParameterQueryBindingTest.java
│ └── jackson
│ │ ├── IdSpecTest.java
│ │ ├── JacksonEngineTest.java
│ │ ├── JacksonIdFieldSelectorTest.java
│ │ ├── JacksonMapperTest.java
│ │ ├── JacksonMixinTest.java
│ │ ├── JacksonViewTest.java
│ │ ├── ParameterBindingWithJacksonTest.java
│ │ ├── UnmarshallingWithJsonCreatorTest.java
│ │ ├── configuration
│ │ └── MappingTest.java
│ │ └── oid
│ │ └── AnnotationsTest.java
│ ├── model
│ ├── Animal.java
│ ├── Article.java
│ ├── Coordinate.java
│ ├── ExposableFriend.java
│ ├── ExternalFriend.java
│ ├── Fox.java
│ ├── Friend.java
│ ├── Gender.java
│ ├── IdSpecSet.java
│ ├── LinkedFriend.java
│ ├── MapReduceData.java
│ ├── TypeWithNested.java
│ └── Views.java
│ ├── query
│ ├── BsonQueryFactoryTest.java
│ └── QueryTest.java
│ ├── spike
│ ├── MarshallingListener.java
│ ├── MongoDumpTest.java
│ ├── QuestionsSpikeTest.java
│ └── projection
│ │ ├── JacksonProjection.java
│ │ ├── JacksonProjectionTest.java
│ │ └── Projection.java
│ └── util
│ ├── BsonUtil.java
│ ├── ErrorObject.java
│ ├── IdResultHandler.java
│ ├── JongoTestBase.java
│ ├── MongoEmbeddedRule.java
│ ├── MongoResource.java
│ ├── RandomPortNumberGenerator.java
│ └── compatibility
│ ├── CompatibilitySuite.java
│ └── TestContext.java
├── resources
└── 1000friends.bson
└── sh
├── compatibility
├── jackson-versions-tests.sh
└── mongodb-versions-tests.sh
└── test.sh
/.dockerignore:
--------------------------------------------------------------------------------
1 | target/
2 | .idea/
--------------------------------------------------------------------------------
/.github/workflows/stale.yml:
--------------------------------------------------------------------------------
1 | name: 'Close stale issues and PRs'
2 | on:
3 | schedule:
4 | - cron: '0 2 * * *'
5 | jobs:
6 | stale:
7 | runs-on: ubuntu-latest
8 | steps:
9 | - uses: actions/stale@v3
10 | with:
11 | stale-issue-label: 'stale'
12 | days-before-issue-stale: 60
13 | days-before-issue-close: 30
14 | exempt-pr-labels: 'work-in-progress'
15 | stale-issue-message: >
16 | This issue has been automatically marked as stale because it has not had
17 | recent activity. It will be closed if no further activity occurs. Thank you
18 | for your contributions.
19 | stale-pr-label: 'stale'
20 | days-before-pr-stale: 60
21 | days-before-pr-close: 30
22 | stale-pr-message: >
23 | This pull request has been automatically marked as stale because it has not had
24 | recent activity. It will be closed if no further activity occurs. Thank you
25 | for your contributions.
26 |
27 |
--------------------------------------------------------------------------------
/.github/workflows/test.yml:
--------------------------------------------------------------------------------
1 | name: CI
2 | on: [ push ]
3 | jobs:
4 | test:
5 | runs-on: ubuntu-latest
6 | steps:
7 | - uses: actions/checkout@v2
8 | - name: Set up JDK 1.8
9 | uses: actions/setup-java@v1
10 | with:
11 | java-version: 1.8
12 | - name: Run tests
13 | run: mvn --batch-mode --update-snapshots verify
14 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea/
2 | target/
3 | *.iml
4 | *.ipr
5 | *.iws
6 | .project
7 | .classpath
8 | .settings/
9 | node_modules/
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | ### Jongo, Query in Java as in Mongo shell
4 |
5 | 
6 | [](https://codecov.io/gh/bguerout/jongo)
7 |
8 | **Faithful spirit**, Mongo query language isn't available in Java, Jongo fixes that. Copy/paste your queries to string.
9 |
10 | **Object oriented**, Save & find objects into & from collections. Use embedded Jackson marshalling or your own.
11 |
12 | **Wood solid**, As fast as Mongo Java driver. Open source, fully tested & made of rock solid libraries.
13 |
14 |
15 | ```xml
16 |
17 | org.jongo
18 | jongo
19 | [1.5.0,)
20 |
21 | ```
22 |
23 | Documentation available at jongo.org
24 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/Command.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo;
18 |
19 |
20 | import com.mongodb.CommandResult;
21 | import com.mongodb.DB;
22 | import com.mongodb.DBObject;
23 | import org.jongo.marshall.Unmarshaller;
24 | import org.jongo.query.Query;
25 | import org.jongo.query.QueryFactory;
26 |
27 | import java.util.ArrayList;
28 | import java.util.List;
29 |
30 | import static org.jongo.ResultHandlerFactory.newResultHandler;
31 |
32 | public class Command {
33 |
34 | private final DB db;
35 | private final Unmarshaller unmarshaller;
36 | private Query query;
37 | private boolean throwOnError;
38 |
39 | public Command(DB db, Unmarshaller unmarshaller, QueryFactory queryFactory, String query, Object... parameters) {
40 | this.db = db;
41 | this.unmarshaller = unmarshaller;
42 | this.query = queryFactory.createQuery(query, parameters);
43 | }
44 |
45 | public Command throwOnError() {
46 | throwOnError = true;
47 | return this;
48 | }
49 |
50 | public ResultCommand field(String fieldName) {
51 | return new ResultCommand(fieldName);
52 | }
53 |
54 | public T as(final Class clazz) {
55 | return map(newResultHandler(clazz, unmarshaller));
56 | }
57 |
58 | public T map(ResultHandler resultHandler) {
59 | CommandResult commandResult = executeCommand();
60 | return resultHandler.map(commandResult);
61 | }
62 |
63 | private CommandResult executeCommand() {
64 | CommandResult commandResult = db.command(query.toDBObject());
65 | if (throwOnError) {
66 | commandResult.throwOnError();
67 | }
68 | return commandResult;
69 | }
70 |
71 | public class ResultCommand {
72 |
73 | private String fieldName;
74 |
75 | public ResultCommand(String fieldName) {
76 | this.fieldName = fieldName;
77 | }
78 |
79 | public List as(final Class clazz) {
80 | return map(newResultHandler(clazz, unmarshaller));
81 | }
82 |
83 | public List map(ResultHandler resultHandler) {
84 | CommandResult commandResult = executeCommand();
85 | List results = (List) commandResult.get(fieldName);
86 | if (results == null) {
87 | return new ArrayList();
88 | }
89 | List mappedResult = new ArrayList(results.size());
90 | for (DBObject dbObject : results) {
91 | mappedResult.add(resultHandler.map(dbObject));
92 | }
93 | return mappedResult;
94 | }
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/Distinct.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo;
18 |
19 | import com.mongodb.BasicDBObject;
20 | import com.mongodb.DBCollection;
21 | import com.mongodb.DBObject;
22 | import org.jongo.marshall.Unmarshaller;
23 | import org.jongo.query.Query;
24 | import org.jongo.query.QueryFactory;
25 |
26 | import java.util.ArrayList;
27 | import java.util.List;
28 |
29 | import static org.jongo.ResultHandlerFactory.newResultHandler;
30 |
31 | public class Distinct {
32 | private final DBCollection dbCollection;
33 | private final Unmarshaller unmarshaller;
34 | private final String key;
35 | private Query query;
36 | private final QueryFactory queryFactory;
37 |
38 | Distinct(DBCollection dbCollection, Unmarshaller unmarshaller, QueryFactory queryFactory, String key) {
39 | this.dbCollection = dbCollection;
40 | this.unmarshaller = unmarshaller;
41 | this.key = key;
42 | this.queryFactory = queryFactory;
43 | this.query = this.queryFactory.createQuery("{}");
44 | }
45 |
46 | public Distinct query(String query) {
47 | this.query = queryFactory.createQuery(query);
48 | return this;
49 | }
50 |
51 | public Distinct query(String query, Object... parameters) {
52 | this.query = queryFactory.createQuery(query, parameters);
53 | return this;
54 | }
55 |
56 | @SuppressWarnings("unchecked")
57 | public List as(final Class clazz) {
58 | DBObject ref = query.toDBObject();
59 | List> distinct = dbCollection.distinct(key, ref);
60 |
61 | if (distinct.isEmpty() || resultsAreBSONPrimitive(distinct))
62 | return (List) distinct;
63 | else {
64 | return typedList((List) distinct, newResultHandler(clazz, unmarshaller));
65 | }
66 | }
67 |
68 | public List map(ResultHandler resultHandler) {
69 | DBObject ref = query.toDBObject();
70 | List> distinct = dbCollection.distinct(key, ref);
71 |
72 | if (distinct.isEmpty() || resultsAreBSONPrimitive(distinct)) {
73 | return typedList(asDBObjectList(distinct), resultHandler);
74 | } else {
75 | return typedList((List) distinct, resultHandler);
76 | }
77 | }
78 |
79 | private List asDBObjectList(List> distinct) {
80 | List objects = new ArrayList();
81 | for (Object object : distinct) {
82 | objects.add(new BasicDBObject(key, object));
83 | }
84 | return objects;
85 | }
86 |
87 | private boolean resultsAreBSONPrimitive(List> distinct) {
88 | return !(distinct.get(0) instanceof DBObject);
89 | }
90 |
91 | private List typedList(List distinct, ResultHandler handler) {
92 | List results = new ArrayList();
93 | for (DBObject dbObject : distinct) {
94 | results.add(handler.map(dbObject));
95 | }
96 | return results;
97 | }
98 |
99 | }
100 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/FindAndModify.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo;
18 |
19 | import com.mongodb.DBCollection;
20 | import com.mongodb.DBObject;
21 | import org.jongo.marshall.Unmarshaller;
22 | import org.jongo.query.Query;
23 | import org.jongo.query.QueryFactory;
24 |
25 | import static org.jongo.ResultHandlerFactory.newResultHandler;
26 |
27 | public class FindAndModify {
28 |
29 | private final DBCollection collection;
30 | private final Unmarshaller unmarshaller;
31 | private final QueryFactory queryFactory;
32 | private final Query query;
33 | private Query fields, sort, modifier;
34 | private boolean remove = false;
35 | private boolean returnNew = false;
36 | private boolean upsert = false;
37 |
38 | FindAndModify(DBCollection collection, Unmarshaller unmarshaller, QueryFactory queryFactory, String query, Object... parameters) {
39 | this.unmarshaller = unmarshaller;
40 | this.collection = collection;
41 | this.queryFactory = queryFactory;
42 | this.query = this.queryFactory.createQuery(query, parameters);
43 | }
44 |
45 | public FindAndModify with(String modifier, Object... parameters) {
46 | if (modifier == null) throw new IllegalArgumentException("Modifier may not be null");
47 | this.modifier = queryFactory.createQuery(modifier, parameters);
48 | return this;
49 | }
50 |
51 | public T as(final Class clazz) {
52 | return map(newResultHandler(clazz, unmarshaller));
53 | }
54 |
55 | public T map(ResultHandler resultHandler) {
56 | DBObject result = collection.findAndModify(query.toDBObject(),
57 | getAsDBObject(fields),
58 | getAsDBObject(sort),
59 | remove,
60 | getAsDBObject(modifier),
61 | returnNew,
62 | upsert);
63 |
64 | return result == null ? null : resultHandler.map(result);
65 | }
66 |
67 | public FindAndModify projection(String fields) {
68 | this.fields = queryFactory.createQuery(fields);
69 | return this;
70 | }
71 |
72 | public FindAndModify projection(String fields, Object... parameters) {
73 | this.fields = queryFactory.createQuery(fields, parameters);
74 | return this;
75 | }
76 |
77 | public FindAndModify sort(String sort) {
78 | this.sort = queryFactory.createQuery(sort);
79 | return this;
80 | }
81 |
82 | public FindAndModify remove() {
83 | this.remove = true;
84 | return this;
85 | }
86 |
87 | public FindAndModify returnNew() {
88 | this.returnNew = true;
89 | return this;
90 | }
91 |
92 | public FindAndModify upsert() {
93 | this.upsert = true;
94 | return this;
95 | }
96 |
97 | private DBObject getAsDBObject(Query query) {
98 | return query == null ? null : query.toDBObject();
99 | }
100 | }
101 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/FindOne.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo;
18 |
19 | import com.mongodb.DBCollection;
20 | import com.mongodb.DBObject;
21 | import com.mongodb.ReadPreference;
22 | import org.jongo.marshall.Unmarshaller;
23 | import org.jongo.query.Query;
24 | import org.jongo.query.QueryFactory;
25 |
26 | import static org.jongo.ResultHandlerFactory.newResultHandler;
27 |
28 | public class FindOne {
29 |
30 | private final Unmarshaller unmarshaller;
31 | private final DBCollection collection;
32 | private final ReadPreference readPreference;
33 | private final Query query;
34 | private Query fields, orderBy;
35 | private final QueryFactory queryFactory;
36 |
37 | FindOne(DBCollection collection, ReadPreference readPreference, Unmarshaller unmarshaller, QueryFactory queryFactory, String query, Object... parameters) {
38 | this.unmarshaller = unmarshaller;
39 | this.collection = collection;
40 | this.readPreference = readPreference;
41 | this.queryFactory = queryFactory;
42 | this.query = this.queryFactory.createQuery(query, parameters);
43 | }
44 |
45 | public T as(final Class clazz) {
46 | return map(newResultHandler(clazz, unmarshaller));
47 | }
48 |
49 | public T map(ResultHandler resultHandler) {
50 | DBObject result = collection.findOne(query.toDBObject(), getFieldsAsDBObject(), getOrderByAsDBObject(), readPreference);
51 | return result == null ? null : resultHandler.map(result);
52 | }
53 |
54 | public FindOne projection(String fields) {
55 | this.fields = queryFactory.createQuery(fields);
56 | return this;
57 | }
58 |
59 | public FindOne projection(String fields, Object... parameters) {
60 | this.fields = queryFactory.createQuery(fields, parameters);
61 | return this;
62 | }
63 |
64 | public FindOne orderBy(String orderBy) {
65 | this.orderBy = queryFactory.createQuery(orderBy);
66 | return this;
67 | }
68 |
69 | private DBObject getFieldsAsDBObject() {
70 | return fields == null ? null : fields.toDBObject();
71 | }
72 |
73 | private DBObject getOrderByAsDBObject() {
74 | return orderBy == null ? null : orderBy.toDBObject();
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/Jongo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo;
18 |
19 | import com.mongodb.DB;
20 | import com.mongodb.DBCollection;
21 | import com.mongodb.client.MongoDatabase;
22 | import org.jongo.bson.BsonDBDecoder;
23 | import org.jongo.bson.BsonDBEncoder;
24 | import org.jongo.query.Query;
25 |
26 | import static org.jongo.marshall.jackson.JacksonMapper.Builder.jacksonMapper;
27 |
28 | public class Jongo {
29 |
30 | private final DB database;
31 | private final Mapper mapper;
32 |
33 | public Jongo(DB database) {
34 | this(database, jacksonMapper().build());
35 | }
36 |
37 | public Jongo(DB database, Mapper mapper) {
38 | this.database = database;
39 | this.mapper = mapper;
40 | }
41 |
42 | public MongoCollection getCollection(String name) {
43 | DBCollection dbCollection = database.getCollection(name);
44 | dbCollection.setDBDecoderFactory(BsonDBDecoder.FACTORY);
45 | dbCollection.setDBEncoderFactory(BsonDBEncoder.FACTORY);
46 | dbCollection.setReadConcern(database.getReadConcern());
47 | return new MongoCollection(dbCollection, mapper);
48 | }
49 |
50 | public DB getDatabase() {
51 | return database;
52 | }
53 |
54 | public Mapper getMapper() {
55 | return mapper;
56 | }
57 |
58 | public Query createQuery(String query, Object... parameters) {
59 | return mapper.getQueryFactory().createQuery(query, parameters);
60 | }
61 |
62 | public Command runCommand(String query) {
63 | return runCommand(query, new Object[0]);
64 | }
65 |
66 | public Command runCommand(String query, Object... parameters) {
67 | return new Command(database, mapper.getUnmarshaller(), mapper.getQueryFactory(), query, parameters);
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/Mapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo;
18 |
19 | import org.jongo.marshall.Marshaller;
20 | import org.jongo.marshall.Unmarshaller;
21 | import org.jongo.query.QueryFactory;
22 |
23 | public interface Mapper {
24 |
25 | Marshaller getMarshaller();
26 |
27 | Unmarshaller getUnmarshaller();
28 |
29 | ObjectIdUpdater getObjectIdUpdater();
30 |
31 | QueryFactory getQueryFactory();
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/MongoCursor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo;
18 |
19 | import com.mongodb.DBCursor;
20 | import com.mongodb.DBObject;
21 |
22 | import java.io.Closeable;
23 | import java.io.IOException;
24 | import java.util.Iterator;
25 | import java.util.NoSuchElementException;
26 |
27 | public class MongoCursor implements Iterator, Iterable, Closeable {
28 |
29 | private final DBCursor cursor;
30 | private final ResultHandler resultHandler;
31 |
32 | public MongoCursor(DBCursor cursor, ResultHandler resultHandler) {
33 | this.cursor = cursor;
34 | this.resultHandler = resultHandler;
35 | }
36 |
37 | public boolean hasNext() {
38 | return cursor.hasNext();
39 | }
40 |
41 | public E next() {
42 | if (!hasNext())
43 | throw new NoSuchElementException();
44 |
45 | DBObject dbObject = cursor.next();
46 | return resultHandler.map(dbObject);
47 | }
48 |
49 | public void remove() {
50 | throw new UnsupportedOperationException("remove() method is not supported");
51 | }
52 |
53 | public Iterator iterator() {
54 | return new MongoCursor(cursor.copy(), resultHandler);
55 | }
56 |
57 | public void close() throws IOException {
58 | cursor.close();
59 | }
60 |
61 | public int count() {
62 | return cursor.count();
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/ObjectIdUpdater.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo;
18 |
19 | import org.bson.types.ObjectId;
20 |
21 | public interface ObjectIdUpdater {
22 |
23 | boolean mustGenerateObjectId(Object pojo);
24 |
25 | Object getId(Object pojo);
26 |
27 | void setObjectId(Object target, ObjectId id);
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/Oid.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo;
18 |
19 | import static org.jongo.MongoCollection.MONGO_QUERY_OID;
20 |
21 | public class Oid {
22 |
23 | public static String withOid(String id) {
24 | return "{_id: {" + MONGO_QUERY_OID + ":\"" + id + "\"}}";
25 | }
26 |
27 | private Oid() {
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/QueryModifier.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo;
18 |
19 | import com.mongodb.DBCursor;
20 |
21 | public interface QueryModifier {
22 | void modify(DBCursor cursor);
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/RawResultHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo;
18 |
19 | import com.mongodb.DBObject;
20 |
21 | public class RawResultHandler implements ResultHandler {
22 |
23 | private final Class clazz;
24 |
25 | public RawResultHandler(Class clazz) {
26 | this.clazz = clazz;
27 | }
28 |
29 | public RawResultHandler() {
30 | this((Class) DBObject.class);
31 | }
32 |
33 | public T map(DBObject result) {
34 | return clazz.cast(result);
35 | }
36 |
37 | public static RawResultHandler asRaw(Class clazz) {
38 | return new RawResultHandler(clazz);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/ResultHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo;
18 |
19 | import com.mongodb.DBObject;
20 |
21 | public interface ResultHandler {
22 | T map(DBObject result);
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/ResultHandlerFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo;
18 |
19 | import com.mongodb.DBObject;
20 | import org.jongo.bson.Bson;
21 | import org.jongo.bson.BsonDocument;
22 | import org.jongo.marshall.Unmarshaller;
23 |
24 | class ResultHandlerFactory {
25 |
26 |
27 | public static ResultHandler newResultHandler(final Class clazz, final Unmarshaller unmarshaller) {
28 | return new UnmarshallingResultHandler(unmarshaller, clazz);
29 | }
30 |
31 | private static class UnmarshallingResultHandler implements ResultHandler {
32 |
33 | private final Unmarshaller unmarshaller;
34 | private final Class clazz;
35 |
36 | public UnmarshallingResultHandler(Unmarshaller unmarshaller, Class clazz) {
37 | this.unmarshaller = unmarshaller;
38 | this.clazz = clazz;
39 | }
40 |
41 | public T map(DBObject result) {
42 | BsonDocument bsonDocument = Bson.createDocument(result);
43 | return unmarshaller.unmarshall(bsonDocument, clazz);
44 | }
45 | }
46 |
47 | private ResultHandlerFactory() {
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/Update.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo;
18 |
19 | import com.mongodb.*;
20 | import org.bson.LazyBSONObject;
21 | import org.jongo.query.Query;
22 | import org.jongo.query.QueryFactory;
23 |
24 | public class Update {
25 |
26 | private final DBCollection collection;
27 | private final Query query;
28 | private final QueryFactory queryFactory;
29 |
30 | private WriteConcern writeConcern;
31 | private boolean upsert = false;
32 | private boolean multi = false;
33 |
34 | Update(DBCollection collection, WriteConcern writeConcern, QueryFactory queryFactory, String query, Object... parameters) {
35 | this.collection = collection;
36 | this.writeConcern = writeConcern;
37 | this.queryFactory = queryFactory;
38 | this.query = createQuery(query, parameters);
39 | }
40 |
41 | public WriteResult with(String modifier) {
42 | return with(modifier, new Object[0]);
43 | }
44 |
45 | public WriteResult with(String modifier, Object... parameters) {
46 | Query updateQuery = queryFactory.createQuery(modifier, parameters);
47 | return collection.update(this.query.toDBObject(), updateQuery.toDBObject(), upsert, multi, writeConcern);
48 | }
49 |
50 | public WriteResult with(Object pojo) {
51 |
52 | DBObject updateDbo = queryFactory.createQuery("{$set:#}", pojo).toDBObject();
53 | removeIdField(updateDbo);
54 | return collection.update(this.query.toDBObject(), updateDbo, upsert, multi, writeConcern);
55 | }
56 |
57 | private void removeIdField(DBObject updateDbo) {
58 | DBObject pojoAsDbo = (DBObject) updateDbo.get("$set");
59 | if (pojoAsDbo.containsField("_id")) {
60 | // Need to materialize lazy objects which are read only
61 | if (pojoAsDbo instanceof LazyBSONObject) {
62 | BasicDBObject expanded = new BasicDBObject();
63 | expanded.putAll(pojoAsDbo);
64 | updateDbo.put("$set", expanded);
65 | pojoAsDbo = expanded;
66 | }
67 | pojoAsDbo.removeField("_id");
68 | }
69 | }
70 |
71 | public Update upsert() {
72 | this.upsert = true;
73 | return this;
74 | }
75 |
76 | public Update multi() {
77 | this.multi = true;
78 | return this;
79 | }
80 |
81 | private Query createQuery(String query, Object[] parameters) {
82 | try {
83 | return this.queryFactory.createQuery(query, parameters);
84 | } catch (Exception e) {
85 | String message = String.format("Unable execute update operation using query %s", query);
86 | throw new IllegalArgumentException(message, e);
87 | }
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/bson/Bson.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.bson;
18 |
19 | import com.mongodb.DBObject;
20 |
21 | public class Bson {
22 |
23 | public static boolean isPrimitive(Object obj) {
24 | return Primitives.contains(obj.getClass());
25 | }
26 |
27 | public static BsonDocument createDocument(DBObject dbo) {
28 | if (dbo instanceof BsonDocument) {
29 | return (BsonDocument) dbo;
30 | }
31 | return new BufferedBsonDocument(dbo);
32 | }
33 |
34 | public static BsonDocument createDocument(byte[] bytes) {
35 | return new LazyBsonDocument(bytes);
36 | }
37 |
38 | private Bson() {
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/bson/BsonDBDecoder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.bson;
18 |
19 | import com.mongodb.*;
20 | import com.mongodb.gridfs.GridFSDBFile;
21 |
22 | import java.util.Iterator;
23 |
24 | public class BsonDBDecoder extends LazyDBDecoder implements DBDecoder {
25 |
26 | public final static DBDecoderFactory FACTORY = new BsonDBDecoderFactory();
27 |
28 | private BsonDBDecoder() {
29 | }
30 |
31 | public DBCallback getDBCallback(DBCollection collection) {
32 | return new CollectionDBCallback(collection);
33 | }
34 |
35 | private static class BsonDBDecoderFactory implements DBDecoderFactory {
36 |
37 | public DBDecoder create() {
38 | return new BsonDBDecoder();
39 | }
40 | }
41 |
42 | private static class CollectionDBCallback extends LazyDBCallback {
43 |
44 | private final DBCollection collection;
45 |
46 | public CollectionDBCallback(DBCollection collection) {
47 | super(collection);
48 | this.collection = collection;
49 | }
50 |
51 | @Override
52 | public Object createObject(byte[] data, int offset) {
53 |
54 | if (isGridFSCollection()) {
55 | return DefaultDBDecoder.FACTORY.create().decode(data, collection);
56 | }
57 |
58 | DBObject dbo = new BsonDBObject(data, offset);
59 | if (isDBRef(dbo)) {
60 | return new DBRef((String) dbo.get("$ref"), dbo.get("$id"));
61 | }
62 | return dbo;
63 | }
64 |
65 | private boolean isGridFSCollection() {
66 | return GridFSDBFile.class.equals(collection.getObjectClass());
67 | }
68 |
69 | private boolean isDBRef(DBObject dbo) {
70 | Iterator iterator = dbo.keySet().iterator();
71 | return iterator.hasNext() && iterator.next().equals("$ref") && iterator.hasNext() && iterator.next().equals("$id");
72 | }
73 |
74 | }
75 |
76 | }
77 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/bson/BsonDBEncoder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.bson;
18 |
19 | import com.mongodb.*;
20 | import org.bson.BSONObject;
21 | import org.bson.io.OutputBuffer;
22 |
23 | import java.io.IOException;
24 |
25 | public class BsonDBEncoder implements DBEncoder {
26 |
27 | public final static DBEncoderFactory FACTORY = new BsonDBEncoderFactory();
28 |
29 | private BsonDBEncoder() {
30 | }
31 |
32 | public int writeObject(final OutputBuffer buf, BSONObject o) {
33 |
34 | if (!(o instanceof LazyDBObject)) {
35 | return DefaultDBEncoder.FACTORY.create().writeObject(buf, o);
36 | }
37 |
38 | try {
39 | return ((LazyDBObject) o).pipe(buf);
40 | } catch (IOException e) {
41 | throw new MongoException("Exception serializing a LazyDBObject", e);
42 | }
43 | }
44 |
45 | private static class BsonDBEncoderFactory implements DBEncoderFactory {
46 | public DBEncoder create() {
47 | return new BsonDBEncoder();
48 | }
49 |
50 | }
51 | }
52 |
53 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/bson/BsonDBObject.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.bson;
18 |
19 | import com.mongodb.DBObject;
20 | import com.mongodb.LazyDBCallback;
21 | import com.mongodb.LazyDBObject;
22 |
23 | class BsonDBObject extends LazyDBObject implements BsonDocument {
24 |
25 | public BsonDBObject(byte[] data, int offset) {
26 | super(data, offset, new BsonCallback());
27 | }
28 |
29 | public byte[] toByteArray() {
30 | return getBytes();
31 | }
32 |
33 | public DBObject toDBObject() {
34 | return this;
35 | }
36 |
37 | public int getSize() {
38 | return getBSONSize();
39 | }
40 |
41 | static class BsonCallback extends LazyDBCallback {
42 |
43 | public BsonCallback() {
44 | super(null);
45 | }
46 |
47 | @Override
48 | public Object createObject(byte[] bytes, int offset) {
49 | return new LazyDBObject(bytes, offset, new BsonCallback());
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/bson/BsonDocument.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.bson;
18 |
19 |
20 | import com.mongodb.DBObject;
21 |
22 | public interface BsonDocument {
23 |
24 | byte[] toByteArray();
25 |
26 | DBObject toDBObject();
27 |
28 | int getSize();
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/bson/BufferedBsonDocument.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.bson;
18 |
19 | import com.mongodb.DBEncoder;
20 | import com.mongodb.DBObject;
21 | import com.mongodb.DefaultDBEncoder;
22 | import org.bson.BSONObject;
23 | import org.bson.io.BasicOutputBuffer;
24 | import org.bson.io.OutputBuffer;
25 |
26 | class BufferedBsonDocument implements BsonDocument {
27 |
28 | private final OutputBuffer buffer;
29 | private final DBObject dbo;
30 |
31 | BufferedBsonDocument(DBObject dbo) {
32 | this.buffer = new BasicOutputBuffer();
33 | this.dbo = dbo;
34 | encode(this.dbo);
35 | }
36 |
37 | private void encode(BSONObject dbo) {
38 | DBEncoder dbEncoder = DefaultDBEncoder.FACTORY.create();
39 | dbEncoder.writeObject(buffer, dbo);
40 | }
41 |
42 | public int getSize() {
43 | return buffer.size();
44 | }
45 |
46 | public byte[] toByteArray() {
47 | return buffer.toByteArray();
48 | }
49 |
50 | public DBObject toDBObject() {
51 | return dbo;
52 | }
53 |
54 | @Override
55 | public String toString() {
56 | return dbo.toString();
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/bson/LazyBsonDocument.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.bson;
18 |
19 | import com.mongodb.DBObject;
20 |
21 | import java.nio.ByteBuffer;
22 | import java.nio.ByteOrder;
23 |
24 | class LazyBsonDocument implements BsonDocument {
25 |
26 | private final byte[] bytes;
27 |
28 | LazyBsonDocument(byte[] bytes) {
29 | this.bytes = bytes;
30 | }
31 |
32 | public int getSize() {
33 | return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getInt(0);
34 | }
35 |
36 | public byte[] toByteArray() {
37 | return bytes;
38 | }
39 |
40 | public DBObject toDBObject() {
41 | return new BsonDBObject(bytes, 0);
42 | }
43 |
44 | @Override
45 | public String toString() {
46 | return toDBObject().toString();
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/bson/Primitives.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.bson;
18 |
19 | import com.mongodb.DBObject;
20 | import com.mongodb.DBRef;
21 | import org.bson.types.*;
22 |
23 | import java.util.Date;
24 | import java.util.HashSet;
25 | import java.util.Set;
26 | import java.util.UUID;
27 | import java.util.regex.Pattern;
28 |
29 | class Primitives {
30 |
31 | private static final Set> PRIMITIVES;
32 |
33 | static {
34 | PRIMITIVES = new HashSet>();
35 | PRIMITIVES.add(String.class);
36 | PRIMITIVES.add(Number.class);
37 | PRIMITIVES.add(Boolean.class);
38 | PRIMITIVES.add(MinKey.class);
39 | PRIMITIVES.add(MaxKey.class);
40 | PRIMITIVES.add(ObjectId.class);
41 | PRIMITIVES.add(Pattern.class);
42 | PRIMITIVES.add(BSONTimestamp.class);
43 | PRIMITIVES.add(Date.class);
44 | PRIMITIVES.add(UUID.class);
45 | PRIMITIVES.add(Code.class);
46 | PRIMITIVES.add(DBObject.class);
47 | PRIMITIVES.add(DBRef.class);
48 | PRIMITIVES.add(CodeWScope.class);
49 | PRIMITIVES.add(Binary.class);
50 | }
51 |
52 | public static boolean contains(Class clazz) {
53 | if (PRIMITIVES.contains(clazz) || isAJavaPrimitiveArray(clazz))
54 | return true;
55 |
56 | for (Class> primitive : PRIMITIVES) {
57 | if (primitive.isAssignableFrom(clazz)) {
58 | return true;
59 | }
60 | }
61 | return false;
62 | }
63 |
64 | private static boolean isAJavaPrimitiveArray(Class clazz) {
65 | return clazz.isArray() && clazz.getComponentType().isPrimitive();
66 | }
67 |
68 | private Primitives() {
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/Marshaller.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall;
18 |
19 | import org.jongo.bson.BsonDocument;
20 |
21 | public interface Marshaller {
22 |
23 | BsonDocument marshall(Object pojo) throws MarshallingException;
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/MarshallingException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall;
18 |
19 | public class MarshallingException extends RuntimeException {
20 |
21 | public MarshallingException(String message) {
22 | super(message);
23 | }
24 |
25 | public MarshallingException(String message, Throwable cause) {
26 | super(message, cause);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/Unmarshaller.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall;
18 |
19 | import org.jongo.bson.BsonDocument;
20 |
21 | public interface Unmarshaller {
22 |
23 | T unmarshall(BsonDocument document, Class clazz) throws MarshallingException;
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/IdSelector.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson;
18 |
19 | public interface IdSelector {
20 | boolean isId(T a);
21 | boolean isObjectId(T a);
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/JacksonEngine.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson;
18 |
19 | import com.fasterxml.jackson.databind.ObjectMapper;
20 | import org.jongo.bson.Bson;
21 | import org.jongo.bson.BsonDocument;
22 | import org.jongo.marshall.Marshaller;
23 | import org.jongo.marshall.MarshallingException;
24 | import org.jongo.marshall.Unmarshaller;
25 | import org.jongo.marshall.jackson.configuration.Mapping;
26 |
27 | import java.io.ByteArrayOutputStream;
28 | import java.io.IOException;
29 |
30 |
31 | public class JacksonEngine implements Unmarshaller, Marshaller {
32 |
33 | private final Mapping mapping;
34 |
35 | public JacksonEngine(Mapping mapping) {
36 | this.mapping = mapping;
37 | }
38 |
39 | /**
40 | * @deprecated Use {@link Mapping#getObjectMapper()} instead
41 | */
42 | @Deprecated
43 | public ObjectMapper getObjectMapper() {
44 | return mapping.getObjectMapper();
45 | }
46 |
47 | @SuppressWarnings("unchecked")
48 | public T unmarshall(BsonDocument document, Class clazz) throws MarshallingException {
49 |
50 | try {
51 | return (T) mapping.getReader(clazz).readValue(document.toByteArray(), 0, document.getSize());
52 | } catch (IOException e) {
53 | String message = String.format("Unable to unmarshall result to %s from content %s", clazz, document.toString());
54 | throw new MarshallingException(message, e);
55 | }
56 | }
57 |
58 | public BsonDocument marshall(Object pojo) throws MarshallingException {
59 |
60 | ByteArrayOutputStream output = new ByteArrayOutputStream();
61 | try {
62 | mapping.getWriter(pojo).writeValue(output, pojo);
63 | } catch (IOException e) {
64 | throw new MarshallingException("Unable to marshall " + pojo + " into bson", e);
65 | }
66 | return Bson.createDocument(output.toByteArray());
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/JacksonIdFieldSelector.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson;
18 |
19 | import com.fasterxml.jackson.annotation.JsonProperty;
20 | import org.bson.types.ObjectId;
21 | import org.jongo.ReflectiveObjectIdUpdater;
22 | import org.jongo.marshall.jackson.oid.Id;
23 | import org.jongo.marshall.jackson.oid.MongoId;
24 | import org.jongo.marshall.jackson.oid.MongoObjectId;
25 |
26 | import java.lang.reflect.Field;
27 |
28 | /**
29 | * Use {@link JacksonObjectIdUpdater} instead
30 | */
31 | @Deprecated
32 | public class JacksonIdFieldSelector implements ReflectiveObjectIdUpdater.IdFieldSelector {
33 |
34 | public boolean isId(Field f) {
35 | return has_IdName(f) || hasJsonProperty(f) || hasIdAnnotation(f);
36 | }
37 |
38 | public boolean isObjectId(Field f) {
39 | return f.isAnnotationPresent(org.jongo.marshall.jackson.oid.ObjectId.class)
40 | || f.isAnnotationPresent(MongoObjectId.class)
41 | || ObjectId.class.isAssignableFrom(f.getType());
42 | }
43 |
44 | private boolean has_IdName(Field f) {
45 | return "_id".equals(f.getName());
46 | }
47 |
48 | private boolean hasJsonProperty(Field f) {
49 | JsonProperty annotation = f.getAnnotation(JsonProperty.class);
50 | return annotation != null && "_id".equals(annotation.value());
51 | }
52 |
53 | private boolean hasIdAnnotation(Field f) {
54 | Id id = f.getAnnotation(Id.class);
55 | MongoId mongoId = f.getAnnotation(MongoId.class);
56 | return id != null || mongoId != null;
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/JacksonMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson;
18 |
19 | import com.fasterxml.jackson.databind.ObjectMapper;
20 | import org.jongo.Mapper;
21 | import org.jongo.ObjectIdUpdater;
22 | import org.jongo.marshall.Marshaller;
23 | import org.jongo.marshall.Unmarshaller;
24 | import org.jongo.marshall.jackson.configuration.AbstractMappingBuilder;
25 | import org.jongo.marshall.jackson.configuration.Mapping;
26 | import org.jongo.query.BsonQueryFactory;
27 | import org.jongo.query.QueryFactory;
28 |
29 | public class JacksonMapper implements Mapper {
30 |
31 | private final JacksonEngine engine;
32 | private final ObjectIdUpdater objectIdUpdater;
33 | private final QueryFactory queryFactory;
34 |
35 | private JacksonMapper(JacksonEngine engine, QueryFactory queryFactory, ObjectIdUpdater objectIdUpdater) {
36 | this.engine = engine;
37 | this.queryFactory = queryFactory;
38 | this.objectIdUpdater = objectIdUpdater;
39 | }
40 |
41 | public Marshaller getMarshaller() {
42 | return engine;
43 | }
44 |
45 | public Unmarshaller getUnmarshaller() {
46 | return engine;
47 | }
48 |
49 | public ObjectIdUpdater getObjectIdUpdater() {
50 | return objectIdUpdater;
51 | }
52 |
53 | public QueryFactory getQueryFactory() {
54 | return queryFactory;
55 | }
56 |
57 | public static class Builder extends AbstractMappingBuilder {
58 |
59 | private QueryFactory queryFactory;
60 | private ObjectIdUpdater objectIdUpdater;
61 |
62 | public Builder() {
63 | super();
64 | }
65 |
66 | public Builder(ObjectMapper mapper) {
67 | super(mapper);
68 | }
69 |
70 | public Mapper build() {
71 | Mapping mapping = createMapping();
72 | JacksonEngine jacksonEngine = new JacksonEngine(mapping);
73 | if (queryFactory == null) {
74 | queryFactory = new BsonQueryFactory(jacksonEngine);
75 | }
76 | if (objectIdUpdater == null) {
77 | objectIdUpdater = new JacksonObjectIdUpdater(mapping.getObjectMapper());
78 | }
79 | return new JacksonMapper(jacksonEngine, queryFactory, objectIdUpdater);
80 | }
81 |
82 | public Builder withQueryFactory(QueryFactory factory) {
83 | this.queryFactory = factory;
84 | return getBuilderInstance();
85 | }
86 |
87 | public Builder withObjectIdUpdater(ObjectIdUpdater objectIdUpdater) {
88 | this.objectIdUpdater = objectIdUpdater;
89 | return getBuilderInstance();
90 | }
91 |
92 | @Override
93 | protected Builder getBuilderInstance() {
94 | return this;
95 | }
96 |
97 | public static Builder jacksonMapper() {
98 | return new Builder();
99 | }
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/JongoAnnotationIntrospector.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson;
18 |
19 | import com.fasterxml.jackson.annotation.JsonInclude.Include;
20 | import com.fasterxml.jackson.databind.PropertyName;
21 | import com.fasterxml.jackson.databind.introspect.Annotated;
22 | import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector;
23 | import org.jongo.marshall.jackson.oid.*;
24 |
25 | @SuppressWarnings("deprecation")
26 | public class JongoAnnotationIntrospector extends NopAnnotationIntrospector {
27 |
28 | private final IdSelector idSelector;
29 |
30 | public JongoAnnotationIntrospector() {
31 | this(new AnnotatedIdSelector());
32 | }
33 |
34 | public JongoAnnotationIntrospector(IdSelector idSelector) {
35 | this.idSelector = idSelector;
36 | }
37 |
38 | @Override
39 | public Include findSerializationInclusion(Annotated a, Include defValue) {
40 | return idSelector.isObjectId(a) ? Include.NON_NULL : defValue;
41 | }
42 |
43 | @Override
44 | public Object findSerializer(Annotated a) {
45 | return idSelector.isObjectId(a) ? ObjectIdSerializer.class : super.findSerializer(a);
46 | }
47 |
48 | @Override
49 | public Object findDeserializer(Annotated a) {
50 | return idSelector.isObjectId(a) ? ObjectIdDeserializer.class : super.findDeserializer(a);
51 | }
52 |
53 | @Override
54 | public PropertyName findNameForSerialization(Annotated a) {
55 | return idSelector.isId(a) ? new PropertyName("_id") : super.findNameForSerialization(a);
56 | }
57 |
58 | @Override
59 | public PropertyName findNameForDeserialization(Annotated a) {
60 | return idSelector.isId(a) ? new PropertyName("_id") : super.findNameForDeserialization(a);
61 | }
62 |
63 | public static class AnnotatedIdSelector implements IdSelector {
64 |
65 | public boolean isId(Annotated a) {
66 | return a.hasAnnotation(MongoId.class) || a.hasAnnotation(Id.class);
67 | }
68 |
69 | public boolean isObjectId(Annotated a) {
70 | return a.hasAnnotation(MongoObjectId.class) || a.hasAnnotation(ObjectId.class);
71 | }
72 | }
73 | }
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/bson4jackson/BsonModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.bson4jackson;
18 |
19 | public class BsonModule extends de.undercouch.bson4jackson.BsonModule {
20 |
21 | @Override
22 | public void setupModule(SetupContext context) {
23 | super.setupModule(context);
24 | context.addSerializers(new BsonSerializers());
25 | context.addDeserializers(new BsonDeserializers());
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/bson4jackson/MongoBsonFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.bson4jackson;
18 |
19 | import com.fasterxml.jackson.core.JsonEncoding;
20 | import com.fasterxml.jackson.core.ObjectCodec;
21 | import com.fasterxml.jackson.core.io.IOContext;
22 | import de.undercouch.bson4jackson.BsonFactory;
23 | import de.undercouch.bson4jackson.BsonGenerator;
24 | import de.undercouch.bson4jackson.BsonParser;
25 |
26 | import java.io.IOException;
27 | import java.io.InputStream;
28 | import java.io.OutputStream;
29 |
30 | public class MongoBsonFactory extends BsonFactory {
31 |
32 | public static BsonFactory createFactory() {
33 | BsonFactory factory = new MongoBsonFactory();
34 | factory.enable(BsonParser.Feature.HONOR_DOCUMENT_LENGTH);
35 | return factory;
36 | }
37 |
38 | @Override
39 | protected BsonParser _createParser(InputStream in, IOContext ctxt) {
40 | BsonParser p = new MongoBsonParser(ctxt, _parserFeatures, _bsonParserFeatures, in);
41 | ObjectCodec codec = getCodec();
42 | if (codec != null) {
43 | p.setCodec(codec);
44 | }
45 | return p;
46 | }
47 |
48 | @Override
49 | public BsonGenerator createGenerator(OutputStream out, JsonEncoding enc) throws IOException {
50 |
51 | IOContext ctxt = _createContext(out, true);
52 | ctxt.setEncoding(enc);
53 | if (enc == JsonEncoding.UTF8 && _outputDecorator != null) {
54 | out = _outputDecorator.decorate(ctxt, out);
55 | }
56 | BsonGenerator g = new MongoBsonGenerator(_generatorFeatures, _bsonGeneratorFeatures, out);
57 | ObjectCodec codec = getCodec();
58 | if (codec != null) {
59 | g.setCodec(codec);
60 | }
61 | if (_characterEscapes != null) {
62 | g.setCharacterEscapes(_characterEscapes);
63 | }
64 | return g;
65 |
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/bson4jackson/MongoBsonGenerator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.bson4jackson;
18 |
19 | import de.undercouch.bson4jackson.BsonConstants;
20 | import de.undercouch.bson4jackson.BsonGenerator;
21 | import org.bson.types.*;
22 |
23 | import java.io.IOException;
24 | import java.io.OutputStream;
25 |
26 | class MongoBsonGenerator extends BsonGenerator {
27 |
28 | public MongoBsonGenerator(int jsonFeatures, int bsonFeatures, OutputStream out) {
29 | super(jsonFeatures, bsonFeatures, out);
30 | }
31 |
32 | public void writeNativeObjectId(org.bson.types.ObjectId objectId) throws IOException {
33 | _writeArrayFieldNameIfNeeded();
34 | _verifyValueWrite("write datetime");
35 | _buffer.putByte(_typeMarker, BsonConstants.TYPE_OBJECTID);
36 | _buffer.putBytes(objectId.toByteArray());
37 | flushBuffer();
38 | }
39 |
40 | public void writeBSONTimestamp(BSONTimestamp timestamp) throws IOException {
41 | _writeArrayFieldNameIfNeeded();
42 | _verifyValueWrite("write timestamp");
43 | _buffer.putByte(_typeMarker, BsonConstants.TYPE_TIMESTAMP);
44 | _buffer.putInt(timestamp.getInc());
45 | _buffer.putInt(timestamp.getTime());
46 | flushBuffer();
47 | }
48 |
49 | public void writeMinKey(MinKey key) throws IOException {
50 | _writeArrayFieldNameIfNeeded();
51 | _verifyValueWrite("write int");
52 | _buffer.putByte(_typeMarker, BsonConstants.TYPE_MINKEY);
53 | flushBuffer();
54 | }
55 |
56 | public void writeMaxKey(MaxKey key) throws IOException {
57 | _writeArrayFieldNameIfNeeded();
58 | _verifyValueWrite("write boolean");
59 | _buffer.putByte(_typeMarker, BsonConstants.TYPE_MAXKEY);
60 | flushBuffer();
61 | }
62 |
63 | public void writeBinary(Binary binary) throws IOException {
64 | _writeArrayFieldNameIfNeeded();
65 | _verifyValueWrite("write binary");
66 | byte[] bytes = binary.getData();
67 | _buffer.putByte(_typeMarker, BsonConstants.TYPE_BINARY);
68 | _buffer.putInt(bytes.length);
69 | _buffer.putByte(binary.getType());
70 | _buffer.putBytes(binary.getData());
71 | flushBuffer();
72 | }
73 |
74 | public void writeDecima128(Decimal128 decimal) throws IOException {
75 | _writeArrayFieldNameIfNeeded();
76 | _verifyValueWrite("write number");
77 | _buffer.putByte(_typeMarker, BsonConstants.TYPE_DECIMAL128);
78 | _buffer.putLong(decimal.getLow());
79 | _buffer.putLong(decimal.getHigh());
80 | flushBuffer();
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/configuration/AnnotationModifier.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.configuration;
18 |
19 | import com.fasterxml.jackson.databind.AnnotationIntrospector;
20 | import com.fasterxml.jackson.databind.ObjectMapper;
21 | import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair;
22 | import org.jongo.marshall.jackson.JongoAnnotationIntrospector;
23 |
24 | public class AnnotationModifier implements MapperModifier {
25 |
26 | public void modify(ObjectMapper mapper) {
27 | AnnotationIntrospector jongoIntrospector = new JongoAnnotationIntrospector();
28 | AnnotationIntrospector defaultIntrospector = mapper.getSerializationConfig().getAnnotationIntrospector();
29 | AnnotationIntrospector pair = new AnnotationIntrospectorPair(jongoIntrospector, defaultIntrospector);
30 |
31 | mapper.setAnnotationIntrospector(pair);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/configuration/DefaultReaderCallback.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.configuration;
18 |
19 | import com.fasterxml.jackson.databind.ObjectMapper;
20 | import com.fasterxml.jackson.databind.ObjectReader;
21 |
22 | class DefaultReaderCallback implements ReaderCallback {
23 | public ObjectReader getReader(ObjectMapper mapper, Class> clazz) {
24 | return mapper.reader(clazz);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/configuration/DefaultWriterCallback.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.configuration;
18 |
19 | import com.fasterxml.jackson.databind.ObjectMapper;
20 | import com.fasterxml.jackson.databind.ObjectWriter;
21 |
22 | class DefaultWriterCallback implements WriterCallback {
23 | public ObjectWriter getWriter(ObjectMapper mapper, Object pojo) {
24 | return mapper.writer();
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/configuration/DeserializationFeatureModifier.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.configuration;
18 |
19 | import com.fasterxml.jackson.databind.DeserializationFeature;
20 | import com.fasterxml.jackson.databind.ObjectMapper;
21 |
22 | class DeserializationFeatureModifier implements MapperModifier {
23 | private final DeserializationFeature feature;
24 | private final boolean enable;
25 |
26 | public DeserializationFeatureModifier(DeserializationFeature feature, boolean enable) {
27 | this.feature = feature;
28 | this.enable = enable;
29 | }
30 |
31 | public void modify(ObjectMapper mapper) {
32 | if (enable)
33 | mapper.enable(feature);
34 | else
35 | mapper.disable(feature);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/configuration/MapperFeatureModifier.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.configuration;
18 |
19 | import com.fasterxml.jackson.databind.MapperFeature;
20 | import com.fasterxml.jackson.databind.ObjectMapper;
21 |
22 | class MapperFeatureModifier implements MapperModifier {
23 | private final MapperFeature feature;
24 | private final boolean enable;
25 |
26 | public MapperFeatureModifier(MapperFeature feature, boolean enable) {
27 | this.feature = feature;
28 | this.enable = enable;
29 | }
30 |
31 | public void modify(ObjectMapper mapper) {
32 | if (enable)
33 | mapper.enable(feature);
34 | else
35 | mapper.disable(feature);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/configuration/MapperModifier.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.configuration;
18 |
19 | import com.fasterxml.jackson.databind.ObjectMapper;
20 |
21 | public interface MapperModifier {
22 |
23 | void modify(ObjectMapper mapper);
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/configuration/Mapping.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.configuration;
18 |
19 | import com.fasterxml.jackson.databind.ObjectMapper;
20 | import com.fasterxml.jackson.databind.ObjectReader;
21 | import com.fasterxml.jackson.databind.ObjectWriter;
22 |
23 | public class Mapping {
24 |
25 | private ObjectMapper mapper;
26 | private ReaderCallback readerCallback;
27 | private WriterCallback writerCallback;
28 |
29 | public Mapping(ObjectMapper mapper, ReaderCallback readerCallback, WriterCallback writerCallback) {
30 | this.mapper = mapper;
31 | this.readerCallback = readerCallback;
32 | this.writerCallback = writerCallback;
33 | }
34 |
35 | public ObjectReader getReader(Class> clazz) {
36 | return readerCallback.getReader(mapper, clazz);
37 | }
38 |
39 | public ObjectWriter getWriter(Object pojo) {
40 | return writerCallback.getWriter(mapper, pojo);
41 | }
42 |
43 | public ObjectMapper getObjectMapper() {
44 | return mapper;
45 | }
46 |
47 | public static Mapping defaultMapping() {
48 | return new Builder().build();
49 | }
50 |
51 | public static class Builder extends AbstractMappingBuilder {
52 |
53 | public Builder() {
54 | super();
55 | }
56 |
57 | public Builder(ObjectMapper mapper) {
58 | super(mapper);
59 | }
60 |
61 | @Override
62 | protected Builder getBuilderInstance() {
63 | return this;
64 | }
65 |
66 | public Mapping build() {
67 | return createMapping();
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/configuration/PropertyModifier.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.configuration;
18 |
19 | import com.fasterxml.jackson.databind.ObjectMapper;
20 |
21 | import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
22 | import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
23 |
24 | public class PropertyModifier implements MapperModifier {
25 |
26 | public void modify(ObjectMapper mapper) {
27 | mapper.disable(FAIL_ON_UNKNOWN_PROPERTIES);
28 | mapper.setSerializationInclusion(NON_NULL);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/configuration/ReaderCallback.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.configuration;
18 |
19 | import com.fasterxml.jackson.databind.ObjectMapper;
20 | import com.fasterxml.jackson.databind.ObjectReader;
21 |
22 | public interface ReaderCallback {
23 |
24 | ObjectReader getReader(ObjectMapper mapper, Class> clazz);
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/configuration/SerializationFeatureModifier.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.configuration;
18 |
19 | import com.fasterxml.jackson.databind.ObjectMapper;
20 | import com.fasterxml.jackson.databind.SerializationFeature;
21 |
22 | class SerializationFeatureModifier implements MapperModifier {
23 | private final SerializationFeature feature;
24 | private final boolean enable;
25 |
26 | public SerializationFeatureModifier(SerializationFeature feature, boolean enable) {
27 | this.feature = feature;
28 | this.enable = enable;
29 | }
30 |
31 | public void modify(ObjectMapper mapper) {
32 | if (enable)
33 | mapper.enable(feature);
34 | else
35 | mapper.disable(feature);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/configuration/ViewReaderCallback.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.configuration;
18 |
19 | import com.fasterxml.jackson.databind.ObjectMapper;
20 | import com.fasterxml.jackson.databind.ObjectReader;
21 |
22 | class ViewReaderCallback implements ReaderCallback {
23 | private final Class> viewClass;
24 |
25 | public ViewReaderCallback(Class> viewClass) {
26 | this.viewClass = viewClass;
27 | }
28 |
29 | public ObjectReader getReader(ObjectMapper mapper, Class> clazz) {
30 | return mapper.reader(clazz).withView(viewClass);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/configuration/ViewWriterCallback.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.configuration;
18 |
19 | import com.fasterxml.jackson.databind.ObjectMapper;
20 | import com.fasterxml.jackson.databind.ObjectWriter;
21 |
22 | class ViewWriterCallback implements WriterCallback {
23 | private final Class> viewClass;
24 |
25 | public ViewWriterCallback(Class> viewClass) {
26 | this.viewClass = viewClass;
27 | }
28 |
29 | public ObjectWriter getWriter(ObjectMapper mapper, Object pojo) {
30 | return mapper.writerWithView(viewClass);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/configuration/VisibilityModifier.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.configuration;
18 |
19 | import com.fasterxml.jackson.databind.ObjectMapper;
20 | import com.fasterxml.jackson.databind.introspect.VisibilityChecker;
21 |
22 | import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
23 | import static com.fasterxml.jackson.databind.MapperFeature.AUTO_DETECT_GETTERS;
24 | import static com.fasterxml.jackson.databind.MapperFeature.AUTO_DETECT_SETTERS;
25 |
26 | public class VisibilityModifier implements MapperModifier {
27 |
28 | public void modify(ObjectMapper mapper) {
29 | mapper.disable(AUTO_DETECT_SETTERS);
30 | mapper.disable(AUTO_DETECT_GETTERS);
31 |
32 | VisibilityChecker> checker = mapper.getSerializationConfig().getDefaultVisibilityChecker();
33 | mapper.setVisibilityChecker(checker.withFieldVisibility(ANY));
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/configuration/WriterCallback.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.configuration;
18 |
19 | import com.fasterxml.jackson.databind.ObjectMapper;
20 | import com.fasterxml.jackson.databind.ObjectWriter;
21 |
22 | public interface WriterCallback {
23 |
24 | ObjectWriter getWriter(ObjectMapper mapper, Object pojo);
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/oid/Id.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.oid;
18 |
19 | import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
20 | import com.fasterxml.jackson.annotation.JsonProperty;
21 |
22 | import java.lang.annotation.Retention;
23 |
24 | import static java.lang.annotation.RetentionPolicy.RUNTIME;
25 |
26 | @Deprecated
27 | @Retention(RUNTIME)
28 | @JacksonAnnotationsInside
29 |
30 | @JsonProperty("_id")
31 | /**
32 | * This class has been deprecated because it has side effects when used with an external Jackson ObjectMapper.
33 | * It will be removed in a future release.
34 | * Use @MongoId instead.
35 | */
36 | public @interface Id {
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/oid/MongoId.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.oid;
18 |
19 | import java.lang.annotation.Retention;
20 |
21 | import static java.lang.annotation.RetentionPolicy.RUNTIME;
22 |
23 | @Retention(RUNTIME)
24 | public @interface MongoId {
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/oid/MongoObjectId.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.oid;
18 |
19 | import java.lang.annotation.Retention;
20 |
21 | import static java.lang.annotation.RetentionPolicy.RUNTIME;
22 |
23 | @Retention(RUNTIME)
24 | public @interface MongoObjectId {
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/oid/ObjectId.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.oid;
18 |
19 | import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
20 | import com.fasterxml.jackson.annotation.JsonInclude;
21 | import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
22 | import com.fasterxml.jackson.databind.annotation.JsonSerialize;
23 |
24 | import java.lang.annotation.Retention;
25 |
26 | import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
27 | import static java.lang.annotation.RetentionPolicy.RUNTIME;
28 |
29 | @Deprecated
30 | @Retention(RUNTIME)
31 | @JacksonAnnotationsInside
32 |
33 | @JsonInclude(NON_NULL)
34 | @JsonSerialize(using = ObjectIdSerializer.class)
35 | @JsonDeserialize(using = ObjectIdDeserializer.class)
36 | /**
37 | * This class has been deprecated because it has side effects when used with an external Jackson ObjectMapper.
38 | * It will be removed in a future release.
39 | * Use @MongoObjectId instead.
40 | */
41 | public @interface ObjectId {
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/org/jongo/marshall/jackson/oid/ObjectIdDeserializer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 Benoît GUÉROUT and Yves AMSELLEM
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jongo.marshall.jackson.oid;
18 |
19 | import com.fasterxml.jackson.core.JsonParser;
20 | import com.fasterxml.jackson.core.TreeNode;
21 | import com.fasterxml.jackson.databind.*;
22 | import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
23 | import org.bson.types.ObjectId;
24 |
25 | import java.io.IOException;
26 |
27 | import static org.jongo.MongoCollection.MONGO_QUERY_OID;
28 |
29 | public class ObjectIdDeserializer extends JsonDeserializer