Skip to content

Commit bc84a26

Browse files
authored
Merge pull request #90 from eclipse/json_provider
Defines a default class to provider JSON-B
2 parents 28d6867 + eb94a85 commit bc84a26

File tree

14 files changed

+252
-20
lines changed

14 files changed

+252
-20
lines changed

arangodb-driver/src/main/java/org/jnosql/diana/arangodb/key/ArangoDBBucketManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
import org.jnosql.diana.api.Value;
2121
import org.jnosql.diana.api.key.BucketManager;
2222
import org.jnosql.diana.api.key.KeyValueEntity;
23+
import org.jnosql.diana.driver.JsonbSupplier;
2324
import org.jnosql.diana.driver.ValueJSON;
2425

2526
import javax.json.bind.Jsonb;
26-
import javax.json.bind.JsonbBuilder;
2727
import java.time.Duration;
2828
import java.util.Objects;
2929
import java.util.Optional;
@@ -43,7 +43,7 @@ public class ArangoDBBucketManager implements BucketManager {
4343

4444
private static final String VALUE = "_value";
4545
private static final Function<BaseDocument, String> TO_JSON = e -> e.getAttribute(VALUE).toString();
46-
private static final Jsonb JSONB = JsonbBuilder.create();
46+
private static final Jsonb JSONB = JsonbSupplier.getInstance().get();
4747

4848
private final ArangoDB arangoDB;
4949

couchbase-driver/src/main/java/org/jnosql/diana/couchbase/key/CouchbaseBucketManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
import org.jnosql.diana.api.Value;
2525
import org.jnosql.diana.api.key.BucketManager;
2626
import org.jnosql.diana.api.key.KeyValueEntity;
27+
import org.jnosql.diana.driver.JsonbSupplier;
2728
import org.jnosql.diana.driver.ValueJSON;
2829

2930
import javax.json.bind.Jsonb;
30-
import javax.json.bind.JsonbBuilder;
3131
import java.time.Duration;
3232
import java.util.Objects;
3333
import java.util.Optional;
@@ -45,7 +45,7 @@ public class CouchbaseBucketManager implements BucketManager {
4545

4646
private static final Logger LOGGER = Logger.getLogger(CouchbaseBucketManager.class.getName());
4747

48-
private static final Jsonb JSONB = JsonbBuilder.create();
48+
private static final Jsonb JSONB = JsonbSupplier.getInstance().get();
4949

5050
private final Bucket bucket;
5151

couchbase-driver/src/main/java/org/jnosql/diana/couchbase/key/CouchbaseCollection.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
package org.jnosql.diana.couchbase.key;
1616

1717

18+
import org.jnosql.diana.driver.JsonbSupplier;
19+
1820
import javax.json.bind.Jsonb;
19-
import javax.json.bind.JsonbBuilder;
2021
import java.util.function.Function;
2122

2223
abstract class CouchbaseCollection<T> {
2324

24-
protected static final Jsonb JSONB = JsonbBuilder.create();
25+
protected static final Jsonb JSONB = JsonbSupplier.getInstance().get();
2526

2627
protected final Class<T> clazz;
2728

couchbase-driver/src/main/java/org/jnosql/diana/couchbase/key/CouchbaseMap.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
import com.couchbase.client.java.Bucket;
1818
import com.couchbase.client.java.document.json.JsonObject;
1919
import org.jnosql.diana.api.Value;
20+
import org.jnosql.diana.driver.JsonbSupplier;
2021

2122
import javax.json.bind.Jsonb;
22-
import javax.json.bind.JsonbBuilder;
2323
import java.util.ArrayList;
2424
import java.util.Collection;
2525
import java.util.HashMap;
@@ -42,7 +42,7 @@
4242
*/
4343
class CouchbaseMap<K, V> implements Map<K, V> {
4444

45-
private static final Jsonb JSONB = JsonbBuilder.create();
45+
private static final Jsonb JSONB = JsonbSupplier.getInstance().get();
4646

4747

4848
private final String bucketName;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2017 Otávio Santana and others
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.jnosql.diana.driver;
16+
17+
import javax.json.bind.Jsonb;
18+
import javax.json.bind.JsonbBuilder;
19+
import javax.json.bind.JsonbConfig;
20+
import javax.json.bind.config.PropertyVisibilityStrategy;
21+
import java.lang.reflect.Field;
22+
import java.lang.reflect.Method;
23+
24+
enum DefaultJsonbSupplier implements JsonbSupplier {
25+
26+
INSTANCE;
27+
28+
{
29+
JsonbConfig config = new JsonbConfig().withPropertyVisibilityStrategy(new PrivateVisibilityStrategy());
30+
this.json = JsonbBuilder.newBuilder().withConfig(config).build();
31+
}
32+
33+
private final Jsonb json;
34+
35+
@Override
36+
public Jsonb get() {
37+
return json;
38+
}
39+
40+
41+
class PrivateVisibilityStrategy implements PropertyVisibilityStrategy {
42+
43+
@Override
44+
public boolean isVisible(Field field) {
45+
return true;
46+
}
47+
48+
@Override
49+
public boolean isVisible(Method method) {
50+
return false;
51+
}
52+
53+
}
54+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2017 Otávio Santana and others
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.jnosql.diana.driver;
16+
17+
import javax.json.bind.Jsonb;
18+
import java.util.function.Supplier;
19+
20+
/**
21+
* Defines a supplier to {@link Jsonb} already configured and ready to use in the drivers whose need a JSON processor.
22+
*/
23+
public interface JsonbSupplier extends Supplier<Jsonb> {
24+
25+
/**
26+
* It returns a {@link JsonbSupplier} from {@link java.util.ServiceLoader} otherwise,
27+
* it will return the default JsonbSupplier that reads from the field instead of the method.
28+
*
29+
* @return {@link JsonbSupplier} instance
30+
*/
31+
static JsonbSupplier getInstance() {
32+
return JsonbSupplierServiceLoader.getInstance();
33+
}
34+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2017 Otávio Santana and others
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.jnosql.diana.driver;
16+
17+
import java.util.List;
18+
import java.util.Optional;
19+
import java.util.ServiceLoader;
20+
import java.util.stream.StreamSupport;
21+
22+
import static java.util.stream.Collectors.toList;
23+
24+
final class JsonbSupplierServiceLoader {
25+
26+
private static final List<JsonbSupplier> LOADERS;
27+
28+
static final Optional<JsonbSupplier> INSTANCE;
29+
30+
static {
31+
ServiceLoader<JsonbSupplier> serviceLoader = ServiceLoader.load(JsonbSupplier.class);
32+
LOADERS = StreamSupport.stream(serviceLoader.spliterator(), false).collect(toList());
33+
INSTANCE = LOADERS.stream().findFirst();
34+
}
35+
36+
private JsonbSupplierServiceLoader() {
37+
}
38+
39+
static JsonbSupplier getInstance() {
40+
return INSTANCE.orElse(DefaultJsonbSupplier.INSTANCE);
41+
}
42+
}

diana-driver-commons/src/main/java/org/jnosql/diana/driver/ValueJSON.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.jnosql.diana.api.Value;
1919

2020
import javax.json.bind.Jsonb;
21-
import javax.json.bind.JsonbBuilder;
2221
import java.util.Objects;
2322

2423

@@ -27,7 +26,7 @@
2726
*/
2827
public class ValueJSON implements Value {
2928

30-
private static final Jsonb JSONB = JsonbBuilder.create();
29+
private static final Jsonb JSONB = JsonbSupplier.getInstance().get();
3130

3231
private final String json;
3332

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2017 Otávio Santana and others
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.jnosql.diana.driver;
16+
17+
import org.junit.jupiter.api.Test;
18+
19+
import javax.json.bind.Jsonb;
20+
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertNotNull;
23+
24+
public class DefaultJsonBSupplierTest {
25+
26+
@Test
27+
public void shouldReturnDefaultInstance() {
28+
assertNotNull(JsonbSupplier.getInstance());
29+
}
30+
31+
@Test
32+
public void shouldProvideJSON() {
33+
JsonbSupplier supplier = JsonbSupplier.getInstance();
34+
assertNotNull(supplier);
35+
assertNotNull(supplier.get());
36+
}
37+
38+
@Test
39+
public void shouldReadFromField() {
40+
Jsonb jsonb = JsonbSupplier.getInstance().get();
41+
User user = new User("Ada", 32);
42+
String json = jsonb.toJson(user);
43+
assertNotNull(json);
44+
assertEquals(user, jsonb.fromJson(json, User.class));
45+
}
46+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2017 Otávio Santana and others
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.jnosql.diana.driver;
16+
17+
import java.util.Objects;
18+
19+
public class User {
20+
21+
private String name;
22+
23+
private Integer age;
24+
25+
public User() {
26+
}
27+
28+
public User(String name, Integer age) {
29+
this.name = name;
30+
this.age = age;
31+
}
32+
33+
34+
@Override
35+
public boolean equals(Object o) {
36+
if (this == o) {
37+
return true;
38+
}
39+
if (!(o instanceof User)) {
40+
return false;
41+
}
42+
User user = (User) o;
43+
return Objects.equals(name, user.name) &&
44+
Objects.equals(age, user.age);
45+
}
46+
47+
@Override
48+
public int hashCode() {
49+
return Objects.hash(name, age);
50+
}
51+
52+
@Override
53+
public String toString() {
54+
final StringBuilder sb = new StringBuilder("User{");
55+
sb.append("name='").append(name).append('\'');
56+
sb.append(", age=").append(age);
57+
sb.append('}');
58+
return sb.toString();
59+
}
60+
}

0 commit comments

Comments
 (0)