Search in sources :

Example 6 with JsonArray

use of com.eclipsesource.json.JsonArray in project hazelcast by hazelcast.

the class LocalOperationStatsImpl method toJson.

@Override
public JsonObject toJson() {
    JsonObject root = new JsonObject();
    root.add("maxVisibleSlowOperationCount", maxVisibleSlowOperationCount);
    JsonArray slowOperationArray = new JsonArray();
    int logCount = 0;
    for (SlowOperationDTO slowOperation : slowOperations) {
        if (logCount++ < maxVisibleSlowOperationCount) {
            slowOperationArray.add(slowOperation.toJson());
        }
    }
    root.add("slowOperations", slowOperationArray);
    root.add("creationTime", creationTime);
    return root;
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) JsonObject(com.eclipsesource.json.JsonObject) SlowOperationDTO(com.hazelcast.internal.management.dto.SlowOperationDTO)

Example 7 with JsonArray

use of com.eclipsesource.json.JsonArray in project leshan by eclipse.

the class JsonRootObjectSerDes method jSerialize.

@Override
public JsonObject jSerialize(JsonRootObject jro) {
    JsonObject o = new JsonObject();
    if (jro.getBaseName() != null)
        o.add("bn", jro.getBaseName());
    JsonArray ja = serDes.jSerialize(jro.getResourceList());
    if (ja != null)
        o.add("e", ja);
    if (jro.getBaseTime() != null)
        o.add("bt", jro.getBaseTime());
    return o;
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) JsonObject(com.eclipsesource.json.JsonObject)

Example 8 with JsonArray

use of com.eclipsesource.json.JsonArray in project leshan by eclipse.

the class ObjectModelSerDes method jSerialize.

@Override
public JsonObject jSerialize(ObjectModel m) {
    final JsonObject o = Json.object();
    o.add("name", m.name);
    o.add("id", m.id);
    o.add("instancetype", m.multiple ? "multiple" : "single");
    o.add("mandatory", m.mandatory);
    if (!ObjectModel.DEFAULT_VERSION.equals(m.version))
        o.add("version", m.version);
    o.add("description", m.description);
    // sort resources value
    List<ResourceModel> resourceSpecs = new ArrayList<>(m.resources.values());
    Collections.sort(resourceSpecs, new Comparator<ResourceModel>() {

        @Override
        public int compare(ResourceModel r1, ResourceModel r2) {
            return r1.id - r2.id;
        }
    });
    JsonArray rs = new JsonArray();
    for (ResourceModel rm : resourceSpecs) {
        rs.add(resourceModelSerDes.jSerialize(rm));
    }
    o.add("resourcedefs", rs);
    return o;
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) ArrayList(java.util.ArrayList) JsonObject(com.eclipsesource.json.JsonObject) ResourceModel(org.eclipse.leshan.core.model.ResourceModel)

Example 9 with JsonArray

use of com.eclipsesource.json.JsonArray in project leshan by eclipse.

the class RegistrationUpdateSerDes method jSerialize.

public static JsonObject jSerialize(RegistrationUpdate u) {
    JsonObject o = Json.object();
    // mandatory fields
    o.add("regId", u.getRegistrationId());
    o.add("identity", IdentitySerDes.serialize(u.getIdentity()));
    // optional fields
    if (u.getLifeTimeInSec() != null)
        o.add("lt", u.getLifeTimeInSec());
    if (u.getSmsNumber() != null)
        o.add("sms", u.getSmsNumber());
    if (u.getBindingMode() != null)
        o.add("bnd", u.getBindingMode().name());
    if (u.getObjectLinks() != null) {
        JsonArray links = new JsonArray();
        for (Link l : u.getObjectLinks()) {
            JsonObject ol = Json.object();
            ol.add("url", l.getUrl());
            JsonObject at = Json.object();
            for (Map.Entry<String, Object> e : l.getAttributes().entrySet()) {
                if (e.getValue() == null) {
                    at.add(e.getKey(), Json.NULL);
                } else if (e.getValue() instanceof Integer) {
                    at.add(e.getKey(), (int) e.getValue());
                } else {
                    at.add(e.getKey(), e.getValue().toString());
                }
            }
            ol.add("at", at);
            links.add(ol);
        }
        o.add("objLink", links);
    }
    if (u.getAdditionalAttributes() != null) {
        JsonObject addAttr = Json.object();
        for (Map.Entry<String, String> e : u.getAdditionalAttributes().entrySet()) {
            addAttr.add(e.getKey(), e.getValue());
        }
        o.add("addAttr", addAttr);
    }
    return o;
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) JsonObject(com.eclipsesource.json.JsonObject) JsonObject(com.eclipsesource.json.JsonObject) Map(java.util.Map) HashMap(java.util.HashMap) Link(org.eclipse.leshan.Link)

Example 10 with JsonArray

use of com.eclipsesource.json.JsonArray 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)

Aggregations

JsonArray (com.eclipsesource.json.JsonArray)111 JsonObject (com.eclipsesource.json.JsonObject)96 JsonValue (com.eclipsesource.json.JsonValue)32 Test (org.junit.Test)30 URL (java.net.URL)28 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)11 Matchers.containsString (org.hamcrest.Matchers.containsString)6 JsonUtil.getString (com.hazelcast.util.JsonUtil.getString)5 Map (java.util.Map)5 Link (org.eclipse.leshan.Link)4 Date (java.util.Date)3 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)3 ClientEndPointDTO (com.hazelcast.internal.management.dto.ClientEndPointDTO)2 MalformedURLException (java.net.MalformedURLException)2 DecimalFormat (java.text.DecimalFormat)2 DecimalFormatSymbols (java.text.DecimalFormatSymbols)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)2