blob: c1802acddf30ca2c237beaebc18f13bfbc09e161 [file] [log] [blame]
chengfanc58d4be2016-09-20 10:33:12 +08001/*
2 * Copyright 2016-present Open Networking Laboratory
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 */
16
17package org.onosproject.protocol.restconf.server.utils.parser.json;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import org.onosproject.protocol.restconf.server.utils.parser.api.JsonWalker;
23import org.onosproject.protocol.restconf.server.utils.parser.api.JsonListener;
24
25import java.util.Iterator;
26import java.util.Map;
27
28/**
29 * Represents implementation of JSON walk, which walks the JSON object node.
30 */
31public class DefaultJsonWalker implements JsonWalker {
32 @Override
33 public void walk(JsonListener jsonListener, String fieldName,
34 ObjectNode objectNode) {
35
36 //enter the object node, the original ObjectNode should have a module
37 //name as fieldName.
38 jsonListener.enterJsonNode(fieldName, objectNode);
39 //the node has no children, then exist and return.
40 if (!objectNode.isContainerNode()) {
41 jsonListener.exitJsonNode(objectNode);
42 return;
43 }
44
45 Iterator<Map.Entry<String, JsonNode>> fields = objectNode.fields();
46 while (fields.hasNext()) {
47 //get the children entry of the node
48 Map.Entry<String, JsonNode> currentChild = fields.next();
49 String key = currentChild.getKey();
50 JsonNode value = currentChild.getValue();
51 //if the entry's value has its own children, do a recursion.
52 //if the entry has no children, store the key and value.
53 //for we don't know the specific type of the entry's value, we
54 // should give it to a method which can handle JsonNode
55 if (value.isContainerNode()) {
56 walkJsonNode(jsonListener, key, value);
57 } else {
58 jsonListener.enterJsonNode(key, value);
59 jsonListener.exitJsonNode(value);
60 }
61 }
62 jsonListener.exitJsonNode(objectNode);
63 }
64
65 /**
66 * Walks the JSON data tree. This method is called when we don't know
67 * the exact type of a json node.
68 *
69 * @param jsonListener Json listener implemented by the user
70 * @param fieldName the original object node field
71 * @param rootNode the json node which needs to be walk
72 */
73 private void walkJsonNode(JsonListener jsonListener, String fieldName,
74 JsonNode rootNode) {
75 if (rootNode.isObject()) {
76 walk(jsonListener, fieldName, (ObjectNode) rootNode);
77 return;
78 }
79
80 if (rootNode.isArray()) {
81 walkArrayNode(jsonListener, fieldName, (ArrayNode) rootNode);
82 }
83 }
84
85 /**
86 * Walks the JSON data tree. This method is called when the user knows the
87 * json node type is ArrayNode.
88 *
89 * @param jsonListener Json listener implemented by the user
90 * @param fieldName the original object node field
91 * @param rootNode the json node which needs to be walk
92 */
93 private void walkArrayNode(JsonListener jsonListener, String fieldName,
94 ArrayNode rootNode) {
95 if (rootNode == null) {
96 return;
97 }
98 //enter the array node.
99 jsonListener.enterJsonNode(fieldName, rootNode);
100 Iterator<JsonNode> children = rootNode.elements();
101 while (children.hasNext()) {
102 JsonNode currentChild = children.next();
103 if (currentChild.isContainerNode()) {
104 walkJsonNode(jsonListener, null, currentChild);
105 }
106 }
107 jsonListener.exitJsonNode(rootNode);
108 }
109}