class JsonUtils {

    public static BsonValue parse(String json)
            throws JsonParseException {
        if (json == null) {
            return null;
        }

        String trimmed = json.trim();
        
        if (trimmed.isEmpty()) {
            return null;
        } else if (trimmed.startsWith("{")) {
            try {
                return BsonDocument.parse(json);
            } catch (BsonInvalidOperationException ex) {
                // this can happen parsing a bson type, e.g.
                // {"$oid": "xxxxxxxx" }
                // the string starts with { but is not a document
                return getBsonValue(json);
            }
        } else if (trimmed.startsWith("[")) {
            return BSON_ARRAY_CODEC.decode(
                    new JsonReader(json),
                    DecoderContext.builder().build());
        } else {
            return getBsonValue(json);
        }
    }

}