Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.ui; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | |
| 22 | /** |
| 23 | * Provides convenience methods for dealing with JSON nodes, arrays etc. |
| 24 | */ |
| 25 | public final class JsonUtils { |
| 26 | |
| 27 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 28 | |
| 29 | // non-instantiable |
Simon Hunt | b1ced8e | 2016-05-27 19:34:34 -0700 | [diff] [blame] | 30 | private JsonUtils() { |
| 31 | } |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 34 | * Composes a message structure for the given message type and payload. |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 35 | * |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 36 | * @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 Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 48 | * 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 Hunt | b1ced8e | 2016-05-27 19:34:34 -0700 | [diff] [blame] | 71 | * @param node object node |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 72 | * @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 Hunt | b1ced8e | 2016-05-27 19:34:34 -0700 | [diff] [blame] | 82 | * @param node object node |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 83 | * @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 Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 93 | * @param node object node |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 94 | * @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 Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 102 | /** |
| 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 Hunt | b1ced8e | 2016-05-27 19:34:34 -0700 | [diff] [blame] | 113 | /** |
| 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 Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 123 | } |