use of com.eclipsesource.json.JsonValue in project hazelcast by hazelcast.
the class JsonUtil method getArray.
/**
* Returns a field in a Json object as an array.
* Throws IllegalArgumentException if the field value is null.
*
* @param object the Json Object
* @param field the field in the Json object to return
* @return the Json field value as an array
*/
public static JsonArray getArray(JsonObject object, String field) {
final JsonValue value = object.get(field);
throwExceptionIfNull(value, field);
return value.asArray();
}
use of com.eclipsesource.json.JsonValue in project leshan by eclipse.
the class ObjectLoader method loadJsonStream.
/**
* Load object definitions from JSON stream.
*
* @param input An inputStream to a JSON stream.
*/
public static List<ObjectModel> loadJsonStream(InputStream input) {
try {
Reader reader = new InputStreamReader(input);
JsonValue json = Json.parse(reader);
return new ObjectModelSerDes().deserialize(json.asArray());
} catch (IOException e) {
LOG.error("Cannot load json model from inputstream");
}
return null;
}
use of com.eclipsesource.json.JsonValue in project leshan by eclipse.
the class IdentitySerDes method deserialize.
public static Identity deserialize(JsonObject peer) {
String address = peer.get(KEY_ADDRESS).asString();
int port = peer.get(KEY_PORT).asInt();
JsonValue jpsk = peer.get(KEY_ID);
if (jpsk != null) {
return Identity.psk(new InetSocketAddress(address, port), jpsk.asString());
}
JsonValue jrpk = peer.get(KEY_RPK);
if (jrpk != null) {
try {
byte[] rpk = Hex.decodeHex(jrpk.asString().toCharArray());
X509EncodedKeySpec spec = new X509EncodedKeySpec(rpk);
PublicKey publicKey = KeyFactory.getInstance("EC").generatePublic(spec);
return Identity.rpk(new InetSocketAddress(address, port), publicKey);
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
throw new IllegalStateException("Invalid security info content", e);
}
}
JsonValue jcn = peer.get(KEY_CN);
if (jcn != null) {
return Identity.x509(new InetSocketAddress(address, port), jcn.asString());
}
return Identity.unsecure(new InetSocketAddress(address, port));
}
use of com.eclipsesource.json.JsonValue in project leshan by eclipse.
the class RegistrationUpdateSerDes method deserialize.
public static RegistrationUpdate deserialize(byte[] data) throws UnknownHostException {
JsonObject v = (JsonObject) Json.parse(new String(data));
// mandatory fields
String regId = v.getString("regId", null);
Identity identity = IdentitySerDes.deserialize(v.get("identity").asObject());
// optional fields
BindingMode b = null;
if (v.get("bnd") != null) {
b = BindingMode.valueOf(v.getString("bnd", null));
}
Long lifetime = null;
if (v.get("lt") != null) {
lifetime = v.getLong("lt", 0);
}
String sms = null;
if (v.get("sms") != null) {
sms = v.getString("sms", "");
}
// parse object link
JsonArray links = (JsonArray) v.get("objLink");
Link[] linkObjs = null;
if (links != null) {
linkObjs = new Link[links.size()];
for (int i = 0; i < links.size(); i++) {
JsonObject ol = (JsonObject) links.get(i);
Map<String, Object> attMap = new HashMap<>();
JsonObject att = (JsonObject) ol.get("at");
for (String k : att.names()) {
JsonValue jsonValue = att.get(k);
if (jsonValue.isNull()) {
attMap.put(k, null);
} else if (jsonValue.isNumber()) {
attMap.put(k, jsonValue.asInt());
} else {
attMap.put(k, jsonValue.asString());
}
}
Link o = new Link(ol.getString("url", null), attMap);
linkObjs[i] = o;
}
}
Map<String, String> addAttr = new HashMap<>();
JsonObject o = (JsonObject) v.get("addAttr");
for (String k : o.names()) {
addAttr.put(k, o.getString(k, ""));
}
return new RegistrationUpdate(regId, identity, lifetime, sms, b, linkObjs, addAttr);
}
use of com.eclipsesource.json.JsonValue in project leshan by eclipse.
the class LwM2mNodeSerDes method deserialize.
public static LwM2mNode deserialize(JsonObject o) {
String kind = o.getString("kind", null);
int id = o.getInt("id", LwM2mObjectInstance.UNDEFINED);
switch(kind) {
case "object":
{
Collection<LwM2mObjectInstance> instances = new ArrayList<>();
JsonArray jInstances = (JsonArray) o.get("instances");
for (JsonValue jInstance : jInstances) {
LwM2mObjectInstance instance = (LwM2mObjectInstance) deserialize((JsonObject) jInstance);
instances.add(instance);
}
return new LwM2mObject(id, instances);
}
case "instance":
{
Collection<LwM2mResource> resources = new ArrayList<>();
JsonObject jResources = (JsonObject) o.get("resources");
for (Member jResource : jResources) {
LwM2mResource resource = (LwM2mResource) deserialize((JsonObject) jResource.getValue());
resources.add(resource);
}
return new LwM2mObjectInstance(id, resources);
}
case "singleResource":
{
String jType = o.getString("type", null);
if (jType == null)
throw new IllegalStateException("Invalid LwM2mNode missing type attribute");
Type type = Enum.valueOf(Type.class, jType);
Object value = ValueSerDes.deserialize(o.get("value"), type);
return LwM2mSingleResource.newResource(id, value, type);
}
case "multipleResource":
{
String jType = o.getString("type", null);
if (jType == null)
throw new IllegalStateException("Invalid LwM2mNode missing type attribute");
Type type = Enum.valueOf(Type.class, jType);
Map<Integer, Object> values = new HashMap<>();
JsonObject jValues = (JsonObject) o.get("values");
for (Member jValue : jValues) {
Integer valueId = Integer.valueOf(jValue.getName());
Object value = ValueSerDes.deserialize(jValue.getValue(), type);
values.put(valueId, value);
}
return LwM2mMultipleResource.newResource(id, values, type);
}
default:
throw new IllegalStateException("Invalid LwM2mNode missing kind attribute");
}
}
Aggregations