Search in sources :

Example 66 with JsonArray

use of com.eclipsesource.json.JsonArray in project box-android-sdk by box.

the class BoxUploadSession method setPartsSha1.

/**
 * Util method for storing sha1 for parts being uploaded
 */
public void setPartsSha1(List<String> sha1s) {
    JsonArray jsonArray = new JsonArray();
    for (String s : sha1s) {
        jsonArray.add(s);
    }
    set(FIELD_PARTS_SHA1, jsonArray);
}
Also used : JsonArray(com.eclipsesource.json.JsonArray)

Example 67 with JsonArray

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

the class ObjectModelSerDesTest method des_ser_must_be_equals.

@Test
public void des_ser_must_be_equals() throws IOException {
    // load file
    InputStream inputStream = ObjectModelSerDesTest.class.getResourceAsStream("/model.json");
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputStream.read(buffer)) != -1) {
        result.write(buffer, 0, length);
    }
    String smodel = result.toString("UTF-8");
    // deserialize
    ObjectModelSerDes serDes = new ObjectModelSerDes();
    JsonValue json = Json.parse(smodel);
    List<ObjectModel> models = serDes.deserialize(json.asArray());
    // serialize
    JsonArray arr = serDes.jSerialize(models);
    String res = arr.toString(WriterConfig.PRETTY_PRINT);
    Assert.assertEquals("value should be equals", smodel, res);
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) ObjectModel(org.eclipse.leshan.core.model.ObjectModel) InputStream(java.io.InputStream) JsonValue(com.eclipsesource.json.JsonValue) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectModelSerDes(org.eclipse.leshan.core.model.json.ObjectModelSerDes) Test(org.junit.Test)

Example 68 with JsonArray

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

the class RegistrationSerDes method jSerialize.

public static JsonObject jSerialize(Registration r) {
    JsonObject o = Json.object();
    o.add("regDate", r.getRegistrationDate().getTime());
    o.add("identity", IdentitySerDes.serialize(r.getIdentity()));
    o.add("regAddr", r.getRegistrationEndpointAddress().getHostString());
    o.add("regPort", r.getRegistrationEndpointAddress().getPort());
    o.add("lt", r.getLifeTimeInSec());
    if (r.getSmsNumber() != null) {
        o.add("sms", r.getSmsNumber());
    }
    o.add("ver", r.getLwM2mVersion());
    o.add("bnd", r.getBindingMode().name());
    o.add("ep", r.getEndpoint());
    o.add("regId", r.getId());
    JsonArray links = new JsonArray();
    for (Link l : r.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);
    JsonObject addAttr = Json.object();
    for (Map.Entry<String, String> e : r.getAdditionalRegistrationAttributes().entrySet()) {
        addAttr.add(e.getKey(), e.getValue());
    }
    o.add("addAttr", addAttr);
    o.add("root", r.getRootPath());
    o.add("lastUp", r.getLastUpdate().getTime());
    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 69 with JsonArray

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

the class RegistrationSerDes method deserialize.

public static Registration deserialize(JsonObject jObj) {
    Registration.Builder b = new Registration.Builder(jObj.getString("regId", null), jObj.getString("ep", null), IdentitySerDes.deserialize(jObj.get("identity").asObject()), new InetSocketAddress(jObj.getString("regAddr", null), jObj.getInt("regPort", 0)));
    b.bindingMode(BindingMode.valueOf(jObj.getString("bnd", null)));
    b.lastUpdate(new Date(jObj.getLong("lastUp", 0)));
    b.lifeTimeInSec(jObj.getLong("lt", 0));
    b.lwM2mVersion(jObj.getString("ver", "1.0"));
    b.registrationDate(new Date(jObj.getLong("regDate", 0)));
    if (jObj.get("sms") != null) {
        b.smsNumber(jObj.getString("sms", ""));
    }
    JsonArray links = (JsonArray) jObj.get("objLink");
    Link[] 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;
    }
    b.objectLinks(linkObjs);
    Map<String, String> addAttr = new HashMap<>();
    JsonObject o = (JsonObject) jObj.get("addAttr");
    for (String k : o.names()) {
        addAttr.put(k, o.getString(k, ""));
    }
    b.additionalRegistrationAttributes(addAttr);
    return b.build();
}
Also used : HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) Date(java.util.Date) JsonArray(com.eclipsesource.json.JsonArray) Registration(org.eclipse.leshan.server.registration.Registration) JsonObject(com.eclipsesource.json.JsonObject) Link(org.eclipse.leshan.Link)

Example 70 with JsonArray

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

the class DownlinkRequestSerDes method jSerialize.

public static JsonObject jSerialize(DownlinkRequest<?> r) {
    final JsonObject o = Json.object();
    o.add("path", r.getPath().toString());
    r.accept(new DownLinkRequestVisitorAdapter() {

        @Override
        public void visit(ObserveRequest request) {
            o.add("kind", "observe");
            if (request.getContentFormat() != null)
                o.add("contentFormat", request.getContentFormat().getCode());
        }

        @Override
        public void visit(DeleteRequest request) {
            o.add("kind", "delete");
        }

        @Override
        public void visit(DiscoverRequest request) {
            o.add("kind", "discover");
        }

        @Override
        public void visit(CreateRequest request) {
            o.add("kind", "create");
            o.add("contentFormat", request.getContentFormat().getCode());
            if (request.getInstanceId() != null)
                o.add("instanceId", request.getInstanceId());
            JsonArray resources = new JsonArray();
            for (LwM2mResource resource : request.getResources()) {
                resources.add(LwM2mNodeSerDes.jSerialize(resource));
            }
            o.add("resources", resources);
        }

        @Override
        public void visit(ExecuteRequest request) {
            o.add("kind", "execute");
            o.add("parameters", request.getParameters());
        }

        @Override
        public void visit(WriteAttributesRequest request) {
            o.add("kind", "writeAttributes");
            o.add("observeSpec", request.getObserveSpec().toString());
        }

        @Override
        public void visit(WriteRequest request) {
            o.add("kind", "write");
            o.add("contentFormat", request.getContentFormat().getCode());
            o.add("mode", request.isPartialUpdateRequest() ? "UPDATE" : "REPLACE");
            o.add("node", LwM2mNodeSerDes.jSerialize(request.getNode()));
        }

        @Override
        public void visit(ReadRequest request) {
            o.add("kind", "read");
            if (request.getContentFormat() != null)
                o.add("contentFormat", request.getContentFormat().getCode());
        }
    });
    return o;
}
Also used : CreateRequest(org.eclipse.leshan.core.request.CreateRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) JsonObject(com.eclipsesource.json.JsonObject) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) DownLinkRequestVisitorAdapter(org.eclipse.leshan.core.request.DownLinkRequestVisitorAdapter) JsonArray(com.eclipsesource.json.JsonArray) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

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