Package: CloudPayloadJsonDecoder

CloudPayloadJsonDecoder

nameinstructionbranchcomplexitylinemethod
buildFromString(String)
M: 0 C: 98
100%
M: 2 C: 12
86%
M: 2 C: 6
75%
M: 0 C: 20
100%
M: 0 C: 1
100%
decodeBody(KuraPayload, JsonValue)
M: 0 C: 12
100%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 0 C: 3
100%
M: 0 C: 1
100%
decodeMetric(KuraPayload, JsonObject)
M: 5 C: 65
93%
M: 1 C: 9
90%
M: 1 C: 5
83%
M: 1 C: 16
94%
M: 0 C: 1
100%
decodePosition(KuraPayload, JsonObject)
M: 0 C: 157
100%
M: 9 C: 29
76%
M: 9 C: 11
55%
M: 0 C: 27
100%
M: 0 C: 1
100%
decodeTimestamp(KuraPayload, JsonValue)
M: 0 C: 15
100%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 0 C: 4
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2017, 2019 Eurotech and/or its affiliates
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License v1.0
6: * which accompanies this distribution, and is available at
7: * http://www.eclipse.org/legal/epl-v10.html
8: *
9: * Contributors:
10: * Eurotech
11: *******************************************************************************/
12: package org.eclipse.kura.internal.json.marshaller.unmarshaller.message;
13:
14: import static org.eclipse.kura.internal.json.marshaller.unmarshaller.message.CloudPayloadJsonFields.BODY;
15: import static org.eclipse.kura.internal.json.marshaller.unmarshaller.message.CloudPayloadJsonFields.METRICS;
16: import static org.eclipse.kura.internal.json.marshaller.unmarshaller.message.CloudPayloadJsonFields.POSITION;
17: import static org.eclipse.kura.internal.json.marshaller.unmarshaller.message.CloudPayloadJsonFields.SENTON;
18: import static org.eclipse.kura.internal.json.marshaller.unmarshaller.message.CloudPayloadJsonFields.CloudPayloadJsonPositionFields.ALTITUDE;
19: import static org.eclipse.kura.internal.json.marshaller.unmarshaller.message.CloudPayloadJsonFields.CloudPayloadJsonPositionFields.HEADING;
20: import static org.eclipse.kura.internal.json.marshaller.unmarshaller.message.CloudPayloadJsonFields.CloudPayloadJsonPositionFields.LATITUDE;
21: import static org.eclipse.kura.internal.json.marshaller.unmarshaller.message.CloudPayloadJsonFields.CloudPayloadJsonPositionFields.LONGITUDE;
22: import static org.eclipse.kura.internal.json.marshaller.unmarshaller.message.CloudPayloadJsonFields.CloudPayloadJsonPositionFields.PRECISION;
23: import static org.eclipse.kura.internal.json.marshaller.unmarshaller.message.CloudPayloadJsonFields.CloudPayloadJsonPositionFields.SATELLITES;
24: import static org.eclipse.kura.internal.json.marshaller.unmarshaller.message.CloudPayloadJsonFields.CloudPayloadJsonPositionFields.SPEED;
25: import static org.eclipse.kura.internal.json.marshaller.unmarshaller.message.CloudPayloadJsonFields.CloudPayloadJsonPositionFields.STATUS;
26:
27: import java.nio.charset.StandardCharsets;
28: import java.util.Base64;
29: import java.util.Date;
30:
31: import org.eclipse.kura.message.KuraPayload;
32: import org.eclipse.kura.message.KuraPosition;
33: import org.slf4j.Logger;
34: import org.slf4j.LoggerFactory;
35:
36: import com.eclipsesource.json.Json;
37: import com.eclipsesource.json.JsonObject;
38: import com.eclipsesource.json.JsonValue;
39:
40: /**
41: * This class contains all the necessary methods that can be used to decode a Json payload into a {@link KuraPayload}.
42: *
43: */
44: public class CloudPayloadJsonDecoder {
45:
46: private static final Logger logger = LoggerFactory.getLogger(CloudPayloadJsonDecoder.class);
47:
48: private CloudPayloadJsonDecoder() {
49: }
50:
51: /**
52: * Builds a {@link KuraPayload} from a Json string. The method will try to parse the received Json in order to
53: * fill the corresponding {@link KuraPayload} fields.
54: * If the mapping fails, the entire string, received as argument, will be placed in the body of the returned
55: * {@link KuraPayload}.
56: *
57: * @param stringJson
58: * a Json encoded as a String.
59: * @return a {@link KuraPayload} that directly maps the received array.
60: */
61: public static KuraPayload buildFromString(String stringJson) {
62: JsonObject json = Json.parse(stringJson).asObject();
63:
64: KuraPayload payload = new KuraPayload();
65:
66: try {
67:• for (JsonObject.Member member : json) {
68: String name = member.getName();
69: JsonValue value = member.getValue();
70:• if (SENTON.value().equalsIgnoreCase(name)) {
71: decodeTimestamp(payload, value);
72:• } else if (BODY.value().equalsIgnoreCase(name)) {
73: decodeBody(payload, value);
74:• } else if (POSITION.value().equalsIgnoreCase(name) && value.isObject()) {
75: decodePosition(payload, value.asObject());
76:• } else if (METRICS.value().equalsIgnoreCase(name) && value.isObject()) {
77: decodeMetric(payload, value.asObject());
78: } else {
79: throw new IllegalArgumentException(String.format("Unrecognized value: %s", name));
80: }
81: }
82: } catch (Exception e) {
83: logger.warn("Cannot parse Json", e);
84: payload = new KuraPayload();
85: payload.setBody(stringJson.getBytes(StandardCharsets.UTF_8));
86: }
87: return payload;
88: }
89:
90: private static void decodeTimestamp(KuraPayload payload, JsonValue timestampValue) {
91:• if (timestampValue != null && timestampValue.isNumber()) {
92: long timestamp = timestampValue.asLong();
93: payload.setTimestamp(new Date(timestamp));
94: }
95: }
96:
97: private static void decodeBody(KuraPayload payload, JsonValue body) {
98:• if (body != null && body.isString()) {
99: payload.setBody(Base64.getDecoder().decode(body.asString()));
100: }
101: }
102:
103: private static void decodePosition(KuraPayload payload, JsonObject positionObject) {
104: KuraPosition position = new KuraPosition();
105:
106: payload.setPosition(position);
107:• for (JsonObject.Member member : positionObject) {
108: String name = member.getName();
109: JsonValue value = member.getValue();
110:• if (LATITUDE.value().equalsIgnoreCase(name) && value.isNumber()) {
111: position.setLatitude(value.asDouble());
112:• } else if (LONGITUDE.value().equalsIgnoreCase(name) && value.isNumber()) {
113: position.setLongitude(value.asDouble());
114:• } else if (ALTITUDE.value().equalsIgnoreCase(name) && value.isNumber()) {
115: position.setAltitude(value.asDouble());
116:• } else if (HEADING.value().equalsIgnoreCase(name) && value.isNumber()) {
117: position.setHeading(value.asDouble());
118:• } else if (PRECISION.value().equalsIgnoreCase(name) && value.isNumber()) {
119: position.setPrecision(value.asDouble());
120:• } else if (SATELLITES.value().equalsIgnoreCase(name) && value.isNumber()) {
121: position.setSatellites(value.asInt());
122:• } else if (SPEED.value().equalsIgnoreCase(name) && value.isNumber()) {
123: position.setSpeed(value.asDouble());
124:• } else if (CloudPayloadJsonFields.CloudPayloadJsonPositionFields.TIMESTAMP.value().equalsIgnoreCase(name)
125:• && value.isNumber()) {
126: position.setTimestamp(new Date(value.asLong()));
127:• } else if (STATUS.value().equalsIgnoreCase(name) && value.isNumber()) {
128: position.setStatus(value.asInt());
129: } else {
130: throw new IllegalArgumentException(String.format("Cannot parse position: %s.", name));
131: }
132: }
133: }
134:
135: // TODO: doesn't properly decode characters, ints, floats and byte arrays - the supported format has no metadata
136: private static void decodeMetric(KuraPayload payload, JsonObject metricsObject) {
137:• if (metricsObject == null) {
138: throw new IllegalArgumentException("Cannot parse metric object!");
139: }
140:
141:• for (JsonObject.Member member : metricsObject) {
142: String name = member.getName();
143: JsonValue value = member.getValue();
144:
145: Object javaValue;
146:• if (value.isNumber()) {
147: try {
148: javaValue = value.asLong();
149: } catch (Exception e) {
150: javaValue = value.asDouble();
151: }
152:• } else if (value.isBoolean()) {
153: javaValue = value.asBoolean();
154:• } else if (value.isString()) {
155: javaValue = value.asString();
156: } else {
157: throw new IllegalArgumentException(String.format("Unparsable metric %s", name));
158: }
159: payload.addMetric(name, javaValue);
160: }
161: }
162: }