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);
}
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);
}
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;
}
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();
}
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;
}
Aggregations