Search in sources :

Example 1 with RestObjectMapper

use of org.apache.servicecomb.foundation.common.utils.RestObjectMapper in project incubator-servicecomb-java-chassis by apache.

the class TestRestObjectMapper method testFormateDate.

@Test
public void testFormateDate() throws Exception {
    RestObjectMapper mapper = new RestObjectMapper();
    // must read/write ISO 8061 dates
    Date date = mapper.readValue("\"2017-07-21T17:32:28Z\"".getBytes(), Date.class);
    String dateStr = mapper.writeValueAsString(date);
    Assert.assertEquals(dateStr, "\"2017-07-21T17:32:28.000+00:00\"");
    date = mapper.readValue("\"2017-07-21T17:32:28.320+0100\"".getBytes(), Date.class);
    dateStr = mapper.writeValueAsString(date);
    // one hour later
    Assert.assertEquals(dateStr, "\"2017-07-21T16:32:28.320+00:00\"");
}
Also used : RestObjectMapper(org.apache.servicecomb.foundation.common.utils.RestObjectMapper) Date(java.util.Date) Test(org.junit.Test)

Example 2 with RestObjectMapper

use of org.apache.servicecomb.foundation.common.utils.RestObjectMapper in project incubator-servicecomb-java-chassis by apache.

the class TestRestObjectMapper method testJsonObjectWork.

@Test
public void testJsonObjectWork() {
    JsonObject obj = new JsonObject();
    obj.put("name", "a");
    obj.put("desc", "b");
    PojoModel model = RestObjectMapperFactory.getRestObjectMapper().convertValue(obj, TypeFactory.defaultInstance().constructType(PojoModel.class));
    Assert.assertEquals("a", model.getName());
    Assert.assertEquals("b", model.getDesc());
    RestObjectMapperFactory.setDefaultRestObjectMapper(new RestObjectMapper());
    model = RestObjectMapperFactory.getRestObjectMapper().convertValue(obj, TypeFactory.defaultInstance().constructType(PojoModel.class));
    Assert.assertEquals("a", model.getName());
    Assert.assertEquals("b", model.getDesc());
    InputStream inputStream = new ByteArrayInputStream(new byte[0]);
    try {
        RestObjectMapperFactory.getRestObjectMapper().readValue(inputStream, PojoModel.class);
        Assert.fail();
    } catch (MismatchedInputException e) {
    // right place, nothing to do.
    } catch (Exception e) {
        Assert.fail();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MismatchedInputException(com.fasterxml.jackson.databind.exc.MismatchedInputException) JsonObject(io.vertx.core.json.JsonObject) RestObjectMapper(org.apache.servicecomb.foundation.common.utils.RestObjectMapper) MismatchedInputException(com.fasterxml.jackson.databind.exc.MismatchedInputException) Test(org.junit.Test)

Example 3 with RestObjectMapper

use of org.apache.servicecomb.foundation.common.utils.RestObjectMapper in project java-chassis by ServiceComb.

the class TestRestObjectMapper method testFormateDate.

@Test
public void testFormateDate() throws Exception {
    RestObjectMapper mapper = new RestObjectMapper();
    // must read/write ISO 8061 dates
    Date date = mapper.readValue("\"2017-07-21T17:32:28Z\"".getBytes(), Date.class);
    String dateStr = mapper.writeValueAsString(date);
    Assert.assertEquals(dateStr, "\"2017-07-21T17:32:28.000+00:00\"");
    date = mapper.readValue("\"2017-07-21T17:32:28.320+0100\"".getBytes(), Date.class);
    dateStr = mapper.writeValueAsString(date);
    // one hour later
    Assert.assertEquals(dateStr, "\"2017-07-21T16:32:28.320+00:00\"");
}
Also used : RestObjectMapper(org.apache.servicecomb.foundation.common.utils.RestObjectMapper) Date(java.util.Date) Test(org.junit.Test)

Example 4 with RestObjectMapper

use of org.apache.servicecomb.foundation.common.utils.RestObjectMapper in project java-chassis by ServiceComb.

the class TestRestObjectMapper method testJsonObjectWork.

@Test
public void testJsonObjectWork() {
    JsonObject obj = new JsonObject();
    obj.put("name", "a");
    obj.put("desc", "b");
    PojoModel model = RestObjectMapperFactory.getRestObjectMapper().convertValue(obj, TypeFactory.defaultInstance().constructType(PojoModel.class));
    Assert.assertEquals("a", model.getName());
    Assert.assertEquals("b", model.getDesc());
    RestObjectMapperFactory.setDefaultRestObjectMapper(new RestObjectMapper());
    model = RestObjectMapperFactory.getRestObjectMapper().convertValue(obj, TypeFactory.defaultInstance().constructType(PojoModel.class));
    Assert.assertEquals("a", model.getName());
    Assert.assertEquals("b", model.getDesc());
    InputStream inputStream = new ByteArrayInputStream(new byte[0]);
    try {
        RestObjectMapperFactory.getRestObjectMapper().readValue(inputStream, PojoModel.class);
        Assert.fail();
    } catch (MismatchedInputException e) {
    // right place, nothing to do.
    } catch (Exception e) {
        Assert.fail();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MismatchedInputException(com.fasterxml.jackson.databind.exc.MismatchedInputException) JsonObject(io.vertx.core.json.JsonObject) RestObjectMapper(org.apache.servicecomb.foundation.common.utils.RestObjectMapper) MismatchedInputException(com.fasterxml.jackson.databind.exc.MismatchedInputException) Test(org.junit.Test)

Aggregations

RestObjectMapper (org.apache.servicecomb.foundation.common.utils.RestObjectMapper)4 Test (org.junit.Test)4 MismatchedInputException (com.fasterxml.jackson.databind.exc.MismatchedInputException)2 JsonObject (io.vertx.core.json.JsonObject)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 Date (java.util.Date)2