001package org.gbif.api.jackson;
002
003import org.gbif.dwc.terms.Term;
004
005import java.io.IOException;
006import java.util.List;
007import java.util.Map;
008import java.util.Map.Entry;
009
010import org.codehaus.jackson.JsonGenerator;
011import org.codehaus.jackson.map.JsonSerializer;
012import org.codehaus.jackson.map.SerializationConfig.Feature;
013import org.codehaus.jackson.map.SerializerProvider;
014
015/**
016 * Serializes list of maps of terms values.
017 */
018public class TermMapListSerializer extends JsonSerializer<List<Map<Term, String>>> {
019
020  @Override
021  public void serialize(List<Map<Term, String>> value, JsonGenerator jgen, SerializerProvider provider)
022    throws IOException {
023    if ((value == null || value.isEmpty()) && provider.getConfig().isEnabled(Feature.WRITE_EMPTY_JSON_ARRAYS)) {
024      jgen.writeStartArray();
025      jgen.writeEndArray();
026    } else {
027      jgen.writeStartArray();
028      for (Map<Term, String> extension : value) {
029        jgen.writeStartObject();
030        for (Entry<Term, String> entry : extension.entrySet()) {
031          jgen.writeStringField(entry.getKey().qualifiedName(), entry.getValue());
032        }
033        jgen.writeEndObject();
034      }
035      jgen.writeEndArray();
036    }
037  }
038}