blob: 246d8bcbc58efe87b7cdd195654c738f280447a4 [file] [log] [blame]
Simon Hunt44aa2f82015-04-30 15:01:35 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Simon Hunt44aa2f82015-04-30 15:01:35 -07003 *
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.ui;
18
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21
22/**
23 * Provides convenience methods for dealing with JSON nodes, arrays etc.
24 */
25public final class JsonUtils {
26
27 private static final ObjectMapper MAPPER = new ObjectMapper();
28
29 // non-instantiable
Simon Huntb1ced8e2016-05-27 19:34:34 -070030 private JsonUtils() {
31 }
Simon Hunt44aa2f82015-04-30 15:01:35 -070032
33 /**
Simon Hunt8add9ee2016-09-20 17:05:07 -070034 * Composes a message structure for the given message type and payload.
Simon Hunt44aa2f82015-04-30 15:01:35 -070035 *
Simon Huntd7f7bcc2015-05-08 14:13:17 -070036 * @param type message type
37 * @param payload message payload
38 * @return the object node representation
39 */
40 public static ObjectNode envelope(String type, ObjectNode payload) {
41 ObjectNode event = MAPPER.createObjectNode();
42 event.put("event", type);
43 event.set("payload", payload);
44 return event;
45 }
46
47 /**
Simon Hunt44aa2f82015-04-30 15:01:35 -070048 * Returns the event type from the specified event.
49 * If the node does not have an "event" property, "unknown" is returned.
50 *
51 * @param event message event
52 * @return extracted event type
53 */
54 public static String eventType(ObjectNode event) {
55 return string(event, "event", "unknown");
56 }
57
58 /**
59 * Returns the payload from the specified event.
60 *
61 * @param event message event
62 * @return extracted payload object
63 */
64 public static ObjectNode payload(ObjectNode event) {
65 return (ObjectNode) event.path("payload");
66 }
67
68 /**
69 * Returns the specified node property as a number.
70 *
Simon Huntb1ced8e2016-05-27 19:34:34 -070071 * @param node object node
Simon Hunt44aa2f82015-04-30 15:01:35 -070072 * @param name property name
73 * @return property as number
74 */
75 public static long number(ObjectNode node, String name) {
76 return node.path(name).asLong();
77 }
78
79 /**
80 * Returns the specified node property as a string.
81 *
Simon Huntb1ced8e2016-05-27 19:34:34 -070082 * @param node object node
Simon Hunt44aa2f82015-04-30 15:01:35 -070083 * @param name property name
84 * @return property as a string
85 */
86 public static String string(ObjectNode node, String name) {
87 return node.path(name).asText();
88 }
89
90 /**
91 * Returns the specified node property as a string, with a default fallback.
92 *
Simon Huntd2747a02015-04-30 22:41:16 -070093 * @param node object node
Simon Hunt44aa2f82015-04-30 15:01:35 -070094 * @param name property name
95 * @param defaultValue fallback value if property is absent
96 * @return property as a string
97 */
98 public static String string(ObjectNode node, String name, String defaultValue) {
99 return node.path(name).asText(defaultValue);
100 }
101
Simon Huntd2747a02015-04-30 22:41:16 -0700102 /**
103 * Returns the specified node property as an object node.
104 *
105 * @param node object node
106 * @param name property name
107 * @return property as a node
108 */
109 public static ObjectNode node(ObjectNode node, String name) {
110 return (ObjectNode) node.path(name);
111 }
112
Simon Huntb1ced8e2016-05-27 19:34:34 -0700113 /**
114 * Returns the specified node property as a boolean.
115 *
116 * @param node object node
117 * @param name property name
118 * @return property as a boolean
119 */
120 public static boolean bool(ObjectNode node, String name) {
121 return node.path(name).asBoolean();
122 }
Simon Hunt44aa2f82015-04-30 15:01:35 -0700123}