Example usage for com.mongodb BasicDBObject toJson

List of usage examples for com.mongodb BasicDBObject toJson

Introduction

In this page you can find the example usage for com.mongodb BasicDBObject toJson.

Prototype

@SuppressWarnings("deprecation")
public String toJson() 

Source Link

Document

Gets a JSON representation of this document using the org.bson.json.JsonMode#STRICT output mode, and otherwise the default settings of JsonWriterSettings.Builder and DBObjectCodec .

Usage

From source file:edu.ucuenca.storage.services.MongoServiceImpl.java

License:Apache License

@Override
public void registerSession(String orcid, String token) {
    BasicDBObject key = new BasicDBObject();
    key.put("orcid", orcid);
    key.put("token", token);
    BasicDBObject main = new BasicDBObject();
    main.append("_id", key);
    Document parse = Document.parse(main.toJson());
    sessions.insertOne(parse);//from  w  w  w.jav  a  2s . c  o m
}

From source file:org.restheart.hal.metadata.singletons.JsonSchemaChecker.java

License:Open Source License

@Override
public boolean check(HttpServerExchange exchange, RequestContext context, BasicDBObject contentToCheck,
        DBObject args) {// w  w  w.  j  a va2 s  .c o m
    Objects.requireNonNull(args, "missing metadata property 'args'");

    Object _schemaStoreDb = args.get(SCHEMA_STORE_DB_PROPERTY);
    String schemaStoreDb;

    Object schemaId = args.get(SCHEMA_ID_PROPERTY);

    Objects.requireNonNull(schemaId,
            "missing property '" + SCHEMA_ID_PROPERTY + "' in metadata property 'args'");

    if (_schemaStoreDb == null) {
        // if not specified assume the current db as the schema store db
        schemaStoreDb = context.getDBName();
    } else if (_schemaStoreDb instanceof String) {
        schemaStoreDb = (String) _schemaStoreDb;
    } else {
        throw new IllegalArgumentException(
                "property " + SCHEMA_STORE_DB_PROPERTY + " in metadata 'args' must be a string");
    }

    try {
        URLUtils.checkId(schemaId);
    } catch (UnsupportedDocumentIdException ex) {
        throw new IllegalArgumentException("schema 'id' is not a valid id", ex);
    }

    Schema theschema;

    try {
        theschema = JsonSchemaCacheSingleton.getInstance().get(schemaStoreDb, schemaId);
    } catch (JsonSchemaNotFoundException ex) {
        context.addWarning(ex.getMessage());
        return false;
    }

    if (Objects.isNull(theschema)) {
        throw new IllegalArgumentException("cannot validate, schema " + schemaStoreDb + "/"
                + RequestContext._SCHEMAS + "/" + schemaId.toString() + " not found");
    }

    String _data = contentToCheck == null ? "{}" : contentToCheck.toJson();

    try {
        theschema.validate(new JSONObject(_data));
    } catch (JSONException je) {
        context.addWarning(je.getMessage());

        return false;
    } catch (ValidationException ve) {
        context.addWarning(ve.getMessage());
        ve.getCausingExceptions().stream().map(ValidationException::getMessage).forEach(context::addWarning);

        return false;
    }

    return true;
}