Search in sources :

Example 1 with ObjectModelSerDes

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonValue(com.eclipsesource.json.JsonValue) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) ObjectModelSerDes(org.eclipse.leshan.core.model.json.ObjectModelSerDes)

Example 2 with ObjectModelSerDes

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

Aggregations

JsonValue (com.eclipsesource.json.JsonValue)2 ObjectModelSerDes (org.eclipse.leshan.core.model.json.ObjectModelSerDes)2 JsonArray (com.eclipsesource.json.JsonArray)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ObjectModel (org.eclipse.leshan.core.model.ObjectModel)1 Test (org.junit.Test)1