blob: a155e3b179b1d4df8cf16cb412436f0316e78390 [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.codec.impl;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080017
18import com.fasterxml.jackson.databind.node.ObjectNode;
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070019
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
22import org.onosproject.net.Annotations;
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070023import org.onosproject.net.DefaultAnnotations;
24import org.onosproject.net.DefaultAnnotations.Builder;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080025
Laszlo Papp62c3e072018-01-18 12:40:21 +000026import java.util.Set;
27import java.util.TreeSet;
28
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080029/**
30 * Annotations JSON codec.
31 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080032public final class AnnotationsCodec extends JsonCodec<Annotations> {
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080033
34 @Override
35 public ObjectNode encode(Annotations annotations, CodecContext context) {
36 ObjectNode result = context.mapper().createObjectNode();
Laszlo Papp62c3e072018-01-18 12:40:21 +000037 Set<String> keys = new TreeSet<>(annotations.keys());
38 for (String key : keys) {
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080039 result.put(key, annotations.value(key));
40 }
41 return result;
42 }
43
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070044 @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 Vachuskaca60f2b2014-11-06 01:34:28 -080053}