Search in sources :

Example 1 with JavaType

use of org.codehaus.jackson.type.JavaType in project motech by motech.

the class MdsRestBundleIT method testRestExposedFields.

@Test
public void testRestExposedFields() throws Exception {
    getLogger().info("Testing REST exposed fields");
    final JavaType mapType = OBJECT_MAPPER.getTypeFactory().constructMapType(Map.class, String.class, Object.class);
    // CREATE
    HttpPost post = new HttpPost(FILTERED_ENTITY_URL);
    post.setEntity(new StringEntity(recordJsonString("some-string", 42), ContentType.APPLICATION_JSON));
    String body = getHttpClient().execute(post, new BasicResponseHandler());
    Map record = OBJECT_MAPPER.readValue(body, mapType);
    assertEquals(FILTERED_REST_FIELDS.size(), record.size());
    assertEquals(FILTERED_REST_FIELDS, record.keySet());
    assertEquals(42, record.get("intField"));
    // UPDATE
    HttpPut put = new HttpPut(FILTERED_ENTITY_URL);
    put.setEntity(new StringEntity(recordJsonString(record.get("strField") + "Updated", (int) record.get("intField") + 13, (long) (int) record.get("id"))));
    body = getHttpClient().execute(put, new BasicResponseHandler());
    record = OBJECT_MAPPER.readValue(body, mapType);
    assertEquals(FILTERED_REST_FIELDS.size(), record.size());
    assertEquals(FILTERED_REST_FIELDS, record.keySet());
    assertEquals(42 + 13, record.get("intField"));
    // READ
    body = getHttpClient().get(FILTERED_ENTITY_URL, new BasicResponseHandler());
    RestResponse response = OBJECT_MAPPER.readValue(body, RestResponse.class);
    assertEquals(1, response.getData().size());
    record = response.getData().get(0);
    assertEquals(FILTERED_REST_FIELDS.size(), record.size());
    assertEquals(FILTERED_REST_FIELDS, record.keySet());
    assertEquals(42 + 13, record.get("intField"));
    // Make sure the string field is ignored
    List result = getDataServiceForFilteredEntity().retrieveAll();
    assertNotNull(result);
    assertFalse(result.isEmpty());
    for (Object obj : result) {
        assertNotNull(obj);
        assertNull(PropertyUtil.getProperty(obj, "strField"));
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) JavaType(org.codehaus.jackson.type.JavaType) RestResponse(org.motechproject.mds.rest.RestResponse) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 2 with JavaType

use of org.codehaus.jackson.type.JavaType in project motech by motech.

the class TaskDeserializer method deserialize.

@Override
public Task deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
    jsonNode = jsonParser.readValueAsTree();
    task = new Task();
    ObjectCodec codec = jsonParser.getCodec();
    if (codec instanceof ObjectMapper) {
        mapper = (ObjectMapper) codec;
    } else {
        mapper = new ObjectMapper();
    }
    TypeFactory typeFactory = mapper.getTypeFactory();
    JavaType stringType = typeFactory.constructType(String.class);
    JavaType longType = typeFactory.constructType(Long.class);
    JavaType intType = typeFactory.constructType(Integer.class);
    JavaType dateTime = typeFactory.constructType(DateTime.class);
    JavaType booleanType = typeFactory.constructType(boolean.class);
    JavaType timeType = typeFactory.constructType(Time.class);
    setProperty("id", longType);
    setProperty("owner", stringType);
    setProperty("creator", stringType);
    setProperty("creationDate", dateTime);
    setProperty("modificationDate", dateTime);
    setProperty("modifiedBy", stringType);
    setProperty("description", stringType);
    setProperty("name", stringType);
    setProperty("enabled", stringType);
    setProperty("hasRegisteredChannel", stringType);
    setProperty("taskConfig", typeFactory.constructType(TaskConfig.class));
    setProperty("trigger", typeFactory.constructType(TaskTriggerInformation.class));
    setProperty("numberOfRetries", intType);
    setProperty("retryIntervalInMilliseconds", intType);
    setProperty("retryTaskOnFailure", stringType);
    setProperty("useTimeWindow", booleanType);
    setProperty("startTime", timeType);
    setProperty("endTime", timeType);
    setProperty("validationErrors", typeFactory.constructCollectionType(Set.class, TaskError.class));
    setProperty("actions", typeFactory.constructCollectionType(List.class, TaskActionInformation.class));
    setProperty("days", typeFactory.constructCollectionType(List.class, String.class));
    return task;
}
Also used : TaskTriggerInformation(org.motechproject.tasks.domain.mds.task.TaskTriggerInformation) Task(org.motechproject.tasks.domain.mds.task.Task) JavaType(org.codehaus.jackson.type.JavaType) Set(java.util.Set) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) TaskActionInformation(org.motechproject.tasks.domain.mds.task.TaskActionInformation) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) List(java.util.List) ObjectCodec(org.codehaus.jackson.ObjectCodec) TypeFactory(org.codehaus.jackson.map.type.TypeFactory) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 3 with JavaType

use of org.codehaus.jackson.type.JavaType in project typescript-generator by vojtechhabarta.

the class Jackson1Parser method getBeanHelper.

private BeanHelper getBeanHelper(Class<?> beanClass) {
    if (beanClass == null) {
        return null;
    }
    try {
        final SerializationConfig serializationConfig = objectMapper.getSerializationConfig();
        final JavaType simpleType = objectMapper.constructType(beanClass);
        final JsonSerializer<?> jsonSerializer = BeanSerializerFactory.instance.createSerializer(serializationConfig, simpleType, null);
        if (jsonSerializer == null) {
            return null;
        }
        if (jsonSerializer instanceof BeanSerializer) {
            return new BeanHelper((BeanSerializer) jsonSerializer);
        } else {
            final String jsonSerializerName = jsonSerializer.getClass().getName();
            throw new RuntimeException(String.format("Unknown serializer '%s' for class '%s'", jsonSerializerName, beanClass));
        }
    } catch (JsonMappingException e) {
        throw new RuntimeException(e);
    }
}
Also used : JavaType(org.codehaus.jackson.type.JavaType)

Aggregations

JavaType (org.codehaus.jackson.type.JavaType)3 List (java.util.List)2 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Map (java.util.Map)1 Set (java.util.Set)1 HttpPost (org.apache.http.client.methods.HttpPost)1 HttpPut (org.apache.http.client.methods.HttpPut)1 StringEntity (org.apache.http.entity.StringEntity)1 BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)1 ObjectCodec (org.codehaus.jackson.ObjectCodec)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 TypeFactory (org.codehaus.jackson.map.type.TypeFactory)1 Test (org.junit.Test)1 RestResponse (org.motechproject.mds.rest.RestResponse)1 Task (org.motechproject.tasks.domain.mds.task.Task)1 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)1 TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)1 TaskError (org.motechproject.tasks.domain.mds.task.TaskError)1 TaskTriggerInformation (org.motechproject.tasks.domain.mds.task.TaskTriggerInformation)1