Package org.opentripplanner.api.model

Source Code of org.opentripplanner.api.model.AgencyAndIdSerializer

package org.opentripplanner.api.model;

import java.io.IOException;

import org.onebusaway.gtfs.model.AgencyAndId;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;

public class AgencyAndIdSerializer extends JsonSerializer<AgencyAndId> {

    public static final String SEPARATOR = ":";

    public static SimpleModule makeModule () {
        Version moduleVersion = new Version(1, 0, 0, null, null, null);
        SimpleModule module = new SimpleModule("OTP", moduleVersion);
        module.addSerializer(AgencyAndId.class, new AgencyAndIdSerializer());
        return module;
    }

    @Override
    public void serialize(AgencyAndId a, JsonGenerator gen, SerializerProvider prov)
            throws IOException, JsonProcessingException {
        gen.writeString(a.getAgencyId() + SEPARATOR + a.getId());
    }

    // Gets around type erasure, allowing
    // module.addSerializer(new ThingSerializer()) to correctly associate
    // this serializer with the proper type.
    @Override
    public Class<AgencyAndId> handledType() {
        return AgencyAndId.class;
    }

}
TOP

Related Classes of org.opentripplanner.api.model.AgencyAndIdSerializer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.