Search in sources :

Example 11 with JsonValue

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();
}
Also used : JsonValue(com.eclipsesource.json.JsonValue)

Example 12 with JsonValue

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonValue(com.eclipsesource.json.JsonValue) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) ObjectModelSerDes(org.eclipse.leshan.core.model.json.ObjectModelSerDes)

Example 13 with JsonValue

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));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) PublicKey(java.security.PublicKey) JsonValue(com.eclipsesource.json.JsonValue) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 14 with JsonValue

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);
}
Also used : HashMap(java.util.HashMap) JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) BindingMode(org.eclipse.leshan.core.request.BindingMode) JsonArray(com.eclipsesource.json.JsonArray) JsonObject(com.eclipsesource.json.JsonObject) Identity(org.eclipse.leshan.core.request.Identity) RegistrationUpdate(org.eclipse.leshan.server.registration.RegistrationUpdate) Link(org.eclipse.leshan.Link)

Example 15 with JsonValue

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");
    }
}
Also used : JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) JsonArray(com.eclipsesource.json.JsonArray) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) Type(org.eclipse.leshan.core.model.ResourceModel.Type) Collection(java.util.Collection) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) JsonObject(com.eclipsesource.json.JsonObject) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) Member(com.eclipsesource.json.JsonObject.Member) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

JsonValue (com.eclipsesource.json.JsonValue)43 JsonObject (com.eclipsesource.json.JsonObject)19 JsonArray (com.eclipsesource.json.JsonArray)12 HashMap (java.util.HashMap)6 ParseException (com.eclipsesource.json.ParseException)4 IOException (java.io.IOException)4 InetSocketAddress (java.net.InetSocketAddress)3 Member (com.eclipsesource.json.JsonObject.Member)2 JsonUtil.getString (com.hazelcast.util.JsonUtil.getString)2 WalletCallException (com.vaklinov.zcashui.ZCashClientCaller.WalletCallException)2 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 PublicKey (java.security.PublicKey)2 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Map (java.util.Map)2 EndpointContext (org.eclipse.californium.elements.EndpointContext)2 Link (org.eclipse.leshan.Link)2