Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.codec.impl; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 19 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
| 22 | import org.onosproject.net.Annotations; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 23 | import org.onosproject.net.DefaultAnnotations; |
| 24 | import org.onosproject.net.DefaultAnnotations.Builder; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 25 | |
Laszlo Papp | 62c3e07 | 2018-01-18 12:40:21 +0000 | [diff] [blame] | 26 | import java.util.Set; |
| 27 | import java.util.TreeSet; |
| 28 | |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 29 | /** |
| 30 | * Annotations JSON codec. |
| 31 | */ |
Ray Milkey | 540b2ce | 2015-02-04 17:50:20 -0800 | [diff] [blame] | 32 | public final class AnnotationsCodec extends JsonCodec<Annotations> { |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 33 | |
| 34 | @Override |
| 35 | public ObjectNode encode(Annotations annotations, CodecContext context) { |
| 36 | ObjectNode result = context.mapper().createObjectNode(); |
Laszlo Papp | 62c3e07 | 2018-01-18 12:40:21 +0000 | [diff] [blame] | 37 | Set<String> keys = new TreeSet<>(annotations.keys()); |
| 38 | for (String key : keys) { |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 39 | result.put(key, annotations.value(key)); |
| 40 | } |
| 41 | return result; |
| 42 | } |
| 43 | |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 44 | @Override |
| 45 | public Annotations decode(ObjectNode json, CodecContext context) { |
| 46 | Builder builder = DefaultAnnotations.builder(); |
| 47 | |
| 48 | json.fields().forEachRemaining(e -> |
| 49 | builder.set(e.getKey(), e.getValue().asText())); |
| 50 | |
| 51 | return builder.build(); |
| 52 | } |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 53 | } |