Search in sources :

Example 21 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project topjava10 by JavaWebinar.

the class JsonUtilTest method testRestReadWriteValue.

@Test
public void testRestReadWriteValue() throws Exception {
    ObjectWriter uiWriter = JacksonObjectMapper.getMapper().writerWithView(View.JsonUI.class);
    String json = JsonUtil.writeValue(MealTestData.ADMIN_MEAL1, uiWriter);
    System.out.println(json);
    assertThat(json, containsString("dateTimeUI"));
}
Also used : ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Matchers.containsString(org.hamcrest.Matchers.containsString) View(ru.javawebinar.topjava.View) Test(org.junit.Test)

Example 22 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project CzechIdMng by bcvsolutions.

the class LoginControllerRestTest method serialize.

private String serialize(Map<String, String> login) throws IOException {
    ObjectMapper m = new ObjectMapper();
    StringWriter sw = new StringWriter();
    ObjectWriter writer = m.writerFor(HashMap.class);
    writer.writeValue(sw, login);
    // 
    return sw.toString();
}
Also used : StringWriter(java.io.StringWriter) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 23 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project cas by apereo.

the class TicketValidationResourceResolver method resolveFrom.

@Override
public String[] resolveFrom(final JoinPoint joinPoint, final Object object) {
    final List<String> auditResourceResults = new ArrayList<>();
    final String ticketId = AopUtils.unWrapJoinPoint(joinPoint).getArgs()[0].toString();
    auditResourceResults.add(ticketId);
    if (object instanceof Assertion) {
        final Assertion assertion = Assertion.class.cast(object);
        final Authentication authn = assertion.getPrimaryAuthentication();
        try (StringWriter writer = new StringWriter()) {
            final ObjectWriter objectWriter = mapper.writer();
            final Map<String, Object> results = new LinkedHashMap<>();
            results.put("principal", authn.getPrincipal().getId());
            final Map<String, Object> attributes = new LinkedHashMap<>(authn.getAttributes());
            attributes.putAll(authn.getPrincipal().getAttributes());
            results.put("attributes", attributes);
            objectWriter.writeValue(writer, results);
            auditResourceResults.add(writer.toString());
        } catch (final Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
    return auditResourceResults.toArray(new String[] {});
}
Also used : StringWriter(java.io.StringWriter) Authentication(org.apereo.cas.authentication.Authentication) ArrayList(java.util.ArrayList) Assertion(org.apereo.cas.validation.Assertion) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) LinkedHashMap(java.util.LinkedHashMap)

Example 24 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project uPortal by Jasig.

the class JpaStatisticalSummaryTest method testStorelessUnivariateStatistic.

public void testStorelessUnivariateStatistic(StorelessUnivariateStatistic sus, double expected) throws Exception {
    assertEquals(expected, sus.getResult(), 0.1);
    final ObjectMapper mapper = new ObjectMapper();
    mapper.findAndRegisterModules();
    // Configure Jackson to just use fields
    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    mapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.SETTER, Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.CREATOR, Visibility.NONE);
    mapper.addMixInAnnotations(Object.class, IgnoreTypeMixIn.class);
    final FilterProvider filters = new SimpleFilterProvider().addFilter("storedDataFilter", SimpleBeanPropertyFilter.serializeAllExcept("storedData"));
    final ObjectWriter ssWriter = mapper.writer(filters);
    final ObjectReader ssReader = mapper.reader(sus.getClass());
    final String susString = ssWriter.writeValueAsString(sus);
    System.out.println(susString);
    final StorelessUnivariateStatistic newSus = ssReader.readValue(susString);
    assertEquals(expected, newSus.getResult(), 0.1);
}
Also used : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) StorelessUnivariateStatistic(org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)

Example 25 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project graphhopper by graphhopper.

the class GHBaseServlet method writeJson.

protected void writeJson(HttpServletRequest req, HttpServletResponse res, JsonNode json) throws IOException {
    String type = getParam(req, "type", "json");
    res.setCharacterEncoding("UTF-8");
    final boolean indent = getBooleanParam(req, "debug", false) || getBooleanParam(req, "pretty", false);
    ObjectWriter objectWriter = indent ? objectMapper.writer().with(SerializationFeature.INDENT_OUTPUT) : objectMapper.writer();
    if ("jsonp".equals(type)) {
        res.setContentType("application/javascript");
        if (!jsonpAllowed) {
            writeError(res, SC_BAD_REQUEST, "Server is not configured to allow jsonp!");
            return;
        }
        String callbackName = getParam(req, "callback", null);
        if (callbackName == null) {
            writeError(res, SC_BAD_REQUEST, "No callback provided, necessary if type=jsonp");
            return;
        }
        writeResponse(res, callbackName + "(" + objectWriter.writeValueAsString(json) + ")");
    } else {
        res.setContentType("application/json");
        writeResponse(res, objectWriter.writeValueAsString(json));
    }
}
Also used : ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter)

Aggregations

ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)50 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)22 Test (org.junit.Test)12 IOException (java.io.IOException)9 JavaType (com.fasterxml.jackson.databind.JavaType)7 StringWriter (java.io.StringWriter)5 Writer (java.io.Writer)5 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)4 FilterProvider (com.fasterxml.jackson.databind.ser.FilterProvider)4 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)4 OutputStream (java.io.OutputStream)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)3 FileOutputStream (java.io.FileOutputStream)3 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 JCommander (com.beust.jcommander.JCommander)2 ParameterException (com.beust.jcommander.ParameterException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 SerializationConfig (com.fasterxml.jackson.databind.SerializationConfig)2