public static void writeMetadataObject(Metadata metadata, JsonGenerator jsonGenerator, boolean prettyPrint) throws IOException {
        jsonGenerator.writeStartObject();
        String[] names = metadata.names();
        if (prettyPrint) {
            Arrays.sort(names, new PrettyMetadataKeyComparator());
        }
        for (String n : names) {
            String[] vals = metadata.getValues(n);
            if (vals.length == 0) {
                continue;
            } else if (vals.length == 1) {
                jsonGenerator.writeStringField(n, vals[0]);
            } else if (vals.length > 1) {
                jsonGenerator.writeArrayFieldStart(n);
                for (String val : vals) {
                    jsonGenerator.writeString(val);
                }
                jsonGenerator.writeEndArray();
            }
        }
        jsonGenerator.writeEndObject();
    }
Serializes a Metadata object to Json.  This does not flush or close the writer.

@param metadata metadata to write
@param writer   writer
@throws java.io.IOException if there is an IOException during writing
tika-serialization/src/main/java/org/apache/tika/serialization/JsonMetadata.java