use of com.eclipsesource.json.JsonValue in project box-android-sdk by box.
the class BoxEntity method createEntityFromJson.
/**
* Helper method that will parse into a known child of BoxEntity.
*
* @param json JsonObject representing a BoxEntity or one of its known children.
* @return a BoxEntity or one of its known children.
*/
public static BoxEntity createEntityFromJson(final JsonObject json) {
JsonValue typeValue = json.get(BoxEntity.FIELD_TYPE);
if (!typeValue.isString()) {
return null;
}
String type = typeValue.asString();
BoxEntityCreator creator = ENTITY_ADDON_MAP.get(type);
BoxEntity entity = null;
if (creator == null) {
entity = new BoxEntity();
} else {
entity = creator.createEntity();
}
entity.createFromJson(json);
return entity;
}
use of com.eclipsesource.json.JsonValue 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.JsonValue in project leshan by eclipse.
the class JsonArrayEntrySerDes method deserialize.
@Override
public JsonArrayEntry deserialize(JsonObject o) {
if (o == null)
return null;
JsonArrayEntry jae = new JsonArrayEntry();
jae.setName(o.getString("n", null));
JsonValue t = o.get("t");
if (t != null && t.isNumber())
jae.setTime(t.asLong());
JsonValue v = o.get("v");
if (v != null && v.isNumber())
jae.setFloatValue(v.asDouble());
JsonValue bv = o.get("bv");
if (bv != null && bv.isBoolean())
jae.setBooleanValue(bv.asBoolean());
JsonValue sv = o.get("sv");
if (sv != null && sv.isString())
jae.setStringValue(sv.asString());
JsonValue ov = o.get("ov");
if (ov != null && ov.isString())
jae.setObjectLinkValue(ov.asString());
return jae;
}
use of com.eclipsesource.json.JsonValue in project leshan by eclipse.
the class JsonRootObjectSerDes method deserialize.
@Override
public JsonRootObject deserialize(JsonObject o) {
if (o == null)
return null;
JsonRootObject jro = new JsonRootObject();
JsonValue e = o.get("e");
if (e != null)
jro.setResourceList(serDes.deserialize(e.asArray()));
JsonValue bn = o.get("bn");
if (bn != null && bn.isString())
jro.setBaseName(bn.asString());
JsonValue bt = o.get("bt");
if (bt != null && bt.isNumber())
jro.setBaseTime(bt.asLong());
return jro;
}
use of com.eclipsesource.json.JsonValue in project leshan by eclipse.
the class EndpointContextSerDes method deserialize.
public static EndpointContext deserialize(JsonObject peer) {
String address = peer.get(KEY_ADDRESS).asString();
int port = peer.get(KEY_PORT).asInt();
InetSocketAddress socketAddress = new InetSocketAddress(address, port);
Principal principal = null;
JsonValue value = peer.get(KEY_ID);
if (value != null) {
principal = new PreSharedKeyIdentity(value.asString());
} else if ((value = peer.get(KEY_RPK)) != null) {
try {
byte[] rpk = Hex.decodeHex(value.asString().toCharArray());
X509EncodedKeySpec spec = new X509EncodedKeySpec(rpk);
PublicKey publicKey = KeyFactory.getInstance("EC").generatePublic(spec);
principal = new RawPublicKeyIdentity(publicKey);
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
throw new IllegalStateException("Invalid security info content", e);
}
} else if ((value = peer.get(KEY_DN)) != null) {
principal = new X500Principal(value.asString());
}
EndpointContext endpointContext;
value = peer.get(KEY_ATTRIBUTES);
if (value == null) {
endpointContext = new AddressEndpointContext(socketAddress, principal);
} else {
int index = 0;
String[] attributes = new String[value.asObject().size() * 2];
for (Member member : value.asObject()) {
attributes[index++] = member.getName();
attributes[index++] = member.getValue().asString();
}
endpointContext = new MapBasedEndpointContext(socketAddress, principal, attributes);
}
return endpointContext;
}
Aggregations