blob: 7099c8340e7349edaec0e8cd4359b847d30adc29 [file] [log] [blame]
package net.onrc.onos.core.topology.web.serializers;
import java.io.IOException;
import java.util.Map.Entry;
import net.onrc.onos.core.topology.HostData;
import net.onrc.onos.core.topology.TopologyElement;
import net.onrc.onos.core.util.SwitchPort;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.ser.std.SerializerBase;
/**
* JSON serializer for HostData objects.
*/
public class HostDataSerializer extends SerializerBase<HostData> {
/**
* Default constructor.
*/
public HostDataSerializer() {
super(HostData.class);
}
/**
* Serializes a HostData object in JSON.
*
* @param hostData the HostData that is being converted to JSON
* @param jsonGenerator generator to place the serialized JSON into
* @param serializerProvider unused but required for method override
* @throws IOException if the JSON serialization process fails
*/
@Override
public void serialize(HostData hostData, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
throws IOException {
//
// TODO: For now, the JSON format of the serialized output should
// be same as the JSON format of the corresponding class Host.
// In the future, we will use a single serializer.
//
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField(TopologyElement.TYPE, hostData.getType());
jsonGenerator.writeStringField("mac", hostData.getMac().toString());
jsonGenerator.writeFieldName("attachmentPoints");
jsonGenerator.writeStartArray();
for (SwitchPort switchPort : hostData.getAttachmentPoints()) {
jsonGenerator.writeObject(switchPort);
}
jsonGenerator.writeEndArray();
jsonGenerator.writeObjectFieldStart("stringAttributes");
for (Entry<String, String> entry : hostData.getAllStringAttributes().entrySet()) {
jsonGenerator.writeStringField(entry.getKey(), entry.getValue());
}
jsonGenerator.writeEndObject(); // stringAttributes
jsonGenerator.writeEndObject();
}
}