private static void write(JsonElement gson) throws IOException {

    // BSON likes encoding full document (not simple elements like BsonValue)
    if (!gson.isJsonObject()) {
      JsonObject temp = new JsonObject();
      temp.add("ignore", gson);
      gson = temp;
    }

    JsonElement bson = Jsons.toGson(Jsons.toBson(gson.getAsJsonObject()));
    check(gson).is(bson);
  }
Check that writing nulls does not cause NPE
mongo/test/org/immutables/mongo/bson4gson/BsonWriterTest.java