001/*
002 * Copyright 2010-2013 Ning, Inc.
003 *
004 * Ning licenses this file to you under the Apache License, version 2.0
005 * (the "License"); you may not use this file except in compliance with the
006 * License.  You may obtain a copy of the License at:
007 *
008 *    http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
013 * License for the specific language governing permissions and limitations
014 * under the License.
015 */
016
017package com.ning.billing.recurly.model.jackson;
018
019import java.io.IOException;
020
021import com.ning.billing.recurly.model.RecurlyObject;
022import com.ning.billing.recurly.model.RecurlyObjects;
023
024import com.fasterxml.jackson.core.JsonGenerator;
025import com.fasterxml.jackson.databind.SerializerProvider;
026import com.fasterxml.jackson.databind.ser.std.StdSerializer;
027
028public class RecurlyObjectsSerializer<T extends RecurlyObjects<U>, U extends RecurlyObject> extends StdSerializer<T> {
029
030    private final String elementName;
031
032    public RecurlyObjectsSerializer(final Class<T> recurlyObjectsClassName, final String elementName) {
033        super(recurlyObjectsClassName);
034        this.elementName = elementName;
035    }
036
037    @Override
038    public void serialize(final T values, final JsonGenerator jgen, final SerializerProvider provider) throws IOException {
039        for (final U value : values) {
040            jgen.writeFieldName(elementName);
041            jgen.writeObject(value);
042        }
043    }
044}