blob: 754e24e9d51116eaab891295db2d27ecfb443a23 [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
26/**
27 * Annotations JSON codec.
28 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080029public final class AnnotationsCodec extends JsonCodec<Annotations> {
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080030
31 @Override
32 public ObjectNode encode(Annotations annotations, CodecContext context) {
33 ObjectNode result = context.mapper().createObjectNode();
34 for (String key : annotations.keys()) {
35 result.put(key, annotations.value(key));
36 }
37 return result;
38 }
39
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070040 @Override
41 public Annotations decode(ObjectNode json, CodecContext context) {
42 Builder builder = DefaultAnnotations.builder();
43
44 json.fields().forEachRemaining(e ->
45 builder.set(e.getKey(), e.getValue().asText()));
46
47 return builder.build();
48 }
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080049}