2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.ccsdk.sli.plugins.yangserializers.dfserializer;
23 import com.fasterxml.jackson.databind.JsonNode;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
26 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.DefaultPropertiesNodeWalker;
27 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNode;
28 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNodeWalker;
30 import java.io.IOException;
31 import java.io.Writer;
32 import java.util.List;
35 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormat.JSON;
36 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.*;
39 * Representation of JSON serializer which encodes properties to JSON and
40 * decodes properties from JSON with the data format serializer.
42 public class JsonSerializer extends DataFormatSerializer {
45 * Creates an instance of data format serializer.
47 * @param serializerContext data format serializer context
49 protected JsonSerializer(DataFormatSerializerContext serializerContext) {
50 super(JSON, serializerContext);
54 public String encode(Map<String, String> param,
55 Map<String, List<Annotation>> annotations)
56 throws SvcLogicException {
57 PropertiesNode propNode = serializerContext().getPropNodeSerializer()
59 PropertiesNodeWalker nodeWalker = new DefaultPropertiesNodeWalker<>();
60 PropertiesNodeJsonListener jsonLis = new PropertiesNodeJsonListener();
61 nodeWalker.walk(jsonLis, propNode);
62 Writer writer = jsonLis.getWriter();
63 return writer.toString();
67 public Map<String, String> decode(String dataFormatBody)
68 throws SvcLogicException {
69 if (!(serializerContext().listener() instanceof JsonListener)) {
70 throw new SvcLogicException(JSON_LIS_ERR);
73 JsonListener listener = (JsonListener) serializerContext().listener();
74 JsonWalker walker = new DefaultJsonWalker();
75 ObjectMapper mapper = new ObjectMapper();
79 jsonNode = mapper.readTree(dataFormatBody);
80 } catch (IOException e) {
81 throw new SvcLogicException(JSON_TREE_ERR, e);
84 walker.walk(listener, jsonNode);
86 return serializerContext().getPropNodeSerializer().decode(
87 listener.serializerHelper().getPropertiesNode());