use of org.eclipse.leshan.core.model.json.ObjectModelSerDes 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;
}
use of org.eclipse.leshan.core.model.json.ObjectModelSerDes 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);
}
Aggregations