Skip to content

imrafaelmerino/mongo-values

Repository files navigation

logo

Maven Maintainability Rating

Introduction

It provides a set of codecs to work with json-values The codecs abstract the processes of going from BSON to JsValue and the other way around by providing a BsonReader and a BsonWriter.

Please find below the supported BSON types and their json-value equivalents:

    
Map<BsonType, Class<?>> map = new HashMap<>();
map.put(BsonType.NULL, JsNull.class);
map.put(BsonType.ARRAY, JsArray.class);
map.put(BsonType.BINARY, JsBinary.class);
map.put(BsonType.BOOLEAN, JsBool.class);
map.put(BsonType.DATE_TIME, JsInstant.class);
map.put(BsonType.DOCUMENT, JsObj.class);
map.put(BsonType.DOUBLE, JsDouble.class);
map.put(BsonType.INT32, JsInt.class);
map.put(BsonType.INT64, JsLong.class);
map.put(BsonType.DECIMAL128, JsBigDec.class);
map.put(BsonType.STRING, JsStr.class);

A CodecRegistry contains a set of Codec instances that are accessed according to the Java classes that they encode from and decode to

Just register the registry JsValuesRegistry, which contains the necessary codecs, and when defining the collection, specify the generic type JsObj. You can use the MongoDB API without doing any kind of conversion between BSON and JsObj:

import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import mongovalues.JsValuesRegistry;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import jsonvalues.JsObj

ConnectionString connString = ???;
 
MongoClientSettings  settings =
             MongoClientSettings.builder()
                                .applyConnectionString(connString)
                                .codecRegistry(JsValuesRegistry.INSTANCE)
                                .build();

MongoClient mongoClient = result = MongoClients.create(settings);

MongoCollection<JsObj> collection = mongoClient.getCollection("db","collection");
                                                                                             

Requirements

  • For versions prior to 1.0.0, json-values requires Java 8 or later. Please note that only fixes are accepted for these versions.
  • For versions starting from 1.0.0 and beyond, json-values mandates Java 17 or later.

Installation

To include json-values in your project, add the corresponding dependency to your build tool based on your Java version:

For Java 8 or higher:

<dependency>
    <groupId>com.github.imrafaelmerino</groupId>
    <artifactId>mongo-values</artifactId>
    <version>0.9.6</version>
</dependency>

For Java 17 or higher:

<dependency>
    <groupId>com.github.imrafaelmerino</groupId>
    <artifactId>mongo-values/artifactId>
    <version>1.0.3</version>
</dependency>

Choose the appropriate version according to your Java runtime.