Merge "Updating fix for bug 3989 based on discussion in OVSDB meeting"
[ovsdb.git] / library / impl / src / main / java / org / opendaylight / ovsdb / lib / notation / json / UUIDSerializer.java
1 /*
2  * Copyright (c) 2013, 2015 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.ovsdb.lib.notation.json;
10
11 import java.io.IOException;
12
13 import org.opendaylight.ovsdb.lib.notation.UUID;
14
15 import com.fasterxml.jackson.core.JsonGenerator;
16 import com.fasterxml.jackson.core.JsonProcessingException;
17 import com.fasterxml.jackson.databind.JsonSerializer;
18 import com.fasterxml.jackson.databind.SerializerProvider;
19
20 public class UUIDSerializer extends JsonSerializer<UUID> {
21     @Override
22     public void serialize(UUID value, JsonGenerator generator,
23         SerializerProvider provider) throws IOException,
24             JsonProcessingException {
25         generator.writeStartArray();
26         try {
27             java.util.UUID.fromString(value.toString());
28             generator.writeString("uuid");
29         } catch (IllegalArgumentException ex) {
30             generator.writeString("named-uuid");
31         }
32         generator.writeString(value.toString());
33         generator.writeEndArray();
34     }
35 }