This repository has been archived by the owner on May 7, 2020. It is now read-only.
/
ConfigurationDeserializer.java
95 lines (87 loc) · 3.51 KB
/
ConfigurationDeserializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Copyright (c) 2014,2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.smarthome.config.core;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
/**
* Deserializes a {@link Configuration} object.
*
* As opposed to Gson's default behavior, it ensures that all numbers are represented as {@link BigDecimal}s.
*
* @author Simon Kaufmann - initial contribution and API
* @author Ana Dimova - added a deserializer for the configuration, conforming to the automation json format
*
*/
public class ConfigurationDeserializer implements JsonDeserializer<Configuration> {
@Override
public Configuration deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
JsonObject configurationObject = json.getAsJsonObject();
if (configurationObject.get("properties") != null) {
return deserialize(configurationObject.get("properties").getAsJsonObject());
} else {
return deserialize(configurationObject);
}
}
private Configuration deserialize(JsonObject propertiesObject) {
Configuration configuration = new Configuration();
for (Entry<String, JsonElement> entry : propertiesObject.entrySet()) {
JsonElement value = entry.getValue();
String key = entry.getKey();
if (value.isJsonPrimitive()) {
JsonPrimitive primitive = value.getAsJsonPrimitive();
configuration.put(key, deserialize(primitive));
} else if (value.isJsonArray()) {
JsonArray array = value.getAsJsonArray();
configuration.put(key, deserialize(array));
} else {
throw new IllegalArgumentException(
"Configuration parameters must be primitives or arrays of primities only but was " + value);
}
}
return configuration;
}
private Object deserialize(JsonPrimitive primitive) {
if (primitive.isString()) {
return primitive.getAsString();
} else if (primitive.isNumber()) {
return primitive.getAsBigDecimal();
} else if (primitive.isBoolean()) {
return primitive.getAsBoolean();
} else {
throw new IllegalArgumentException("Unsupported primitive: " + primitive);
}
}
private Object deserialize(JsonArray array) {
List<Object> list = new LinkedList<>();
for (JsonElement element : array) {
if (element.isJsonPrimitive()) {
JsonPrimitive primitive = element.getAsJsonPrimitive();
list.add(deserialize(primitive));
} else {
throw new IllegalArgumentException("Multiples must only contain primitives but was " + element);
}
}
return list;
}
}