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;
26 import java.io.IOException;
27 import java.io.Writer;
28 import java.util.List;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
32 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.DefaultPropertiesNodeWalker;
33 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNode;
34 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNodeWalker;
36 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormat.JSON;
37 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.JSON_LIS_ERR;
38 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.JSON_TREE_ERR;
41 * Representation of JSON serializer which encodes properties to JSON and
42 * decodes properties from JSON with the data format serializer.
44 public class JsonSerializer extends DataFormatSerializer {
47 * Creates an instance of data format serializer.
49 * @param serializerContext data format serializer context
51 protected JsonSerializer(DataFormatSerializerContext serializerContext) {
52 super(JSON, serializerContext);
56 public String encode(Map<String, String> param,
57 Map<String, List<Annotation>> annotations)
58 throws SvcLogicException {
59 PropertiesNode propNode = serializerContext().getPropNodeSerializer()
61 PropertiesNodeWalker nodeWalker = new DefaultPropertiesNodeWalker<>();
62 PropertiesNodeJsonListener jsonLis = new PropertiesNodeJsonListener();
63 nodeWalker.walk(jsonLis, propNode);
64 Writer writer = jsonLis.getWriter();
65 return writer.toString();
69 public Map<String, String> decode(String dataFormatBody)
70 throws SvcLogicException {
71 if (!(serializerContext().listener() instanceof JsonListener)) {
72 throw new SvcLogicException(JSON_LIS_ERR);
75 JsonListener listener = (JsonListener) serializerContext().listener();
76 JsonWalker walker = new DefaultJsonWalker();
77 ObjectMapper mapper = new ObjectMapper();
81 jsonNode = mapper.readTree(dataFormatBody);
82 } catch (IOException e) {
83 throw new SvcLogicException(JSON_TREE_ERR, e);
86 walker.walk(listener, jsonNode);
88 return serializerContext().getPropNodeSerializer().decode(
89 listener.serializerHelper().getPropertiesNode());