public static void toJson(Metadata metadata, Writer writer) throws IOException {
        if (metadata == null) {
            writer.write("null");
            return;
        }
        long max = TikaConfig.getMaxJsonStringFieldLength();
        try (JsonGenerator jsonGenerator = new JsonFactory()
                .setStreamReadConstraints(StreamReadConstraints
                        .builder()
                        .maxStringLength(TikaConfig.getMaxJsonStringFieldLength())
                        .build())
                .createGenerator(CloseShieldWriter.wrap(writer))) {
            if (PRETTY_PRINT) {
                jsonGenerator.useDefaultPrettyPrinter();
            }
            writeMetadataObject(metadata, jsonGenerator, PRETTY_PRINT);
        }
    }
@Test
    public void testNull() {
        StringWriter writer = new StringWriter();
        boolean ex = false;
        try {
            JsonMetadata.toJson(null, writer);
        } catch (IOException e) {
            ex = true;
        }
        assertFalse(ex);
        assertEquals("null", writer.toString());
    }