List of usage examples for com.fasterxml.jackson.databind ObjectMapper writer
public ObjectWriter writer(ContextAttributes attrs)
From source file:com.evanzeimet.queryinfo.QueryInfoTestUtils.java
@SuppressWarnings("deprecation") public static ObjectWriter createObjectWriter() { ObjectMapper objectMapper = createObjectMapper(); DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter(); prettyPrinter.indentArraysWith(new DefaultPrettyPrinter.Lf2SpacesIndenter()); return objectMapper.writer(prettyPrinter); }
From source file:com.kingen.web.CommonController.java
public static String serializeOnlyGivenFields(Object o, Collection<String> fields) throws JsonProcessingException { if ((fields == null) || fields.isEmpty()) fields = new HashSet<String>(); Set<String> properties = new HashSet<String>(fields); SimpleBeanPropertyFilter filter = new SimpleBeanPropertyFilter.FilterExceptFilter(properties); SimpleFilterProvider fProvider = new SimpleFilterProvider(); fProvider.addFilter(FILTER_NAME, filter); ObjectMapper mapper = new ObjectMapper(); mapper.setAnnotationIntrospector(new AnnotationIntrospector()); String json = mapper.writer(fProvider).writeValueAsString(o); return json;//from w w w .ja v a 2 s.co m }
From source file:com.mirth.connect.util.MirthJsonUtil.java
public static String prettyPrint(String input) { ObjectMapper mapper = new ObjectMapper(new JsonFactory()); mapper.enable(SerializationFeature.INDENT_OUTPUT); try {//from ww w .j a v a 2s .c o m // Modified Jackson's default pretty printer to separate each array element onto its own line DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter(); prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE); JsonNode json = mapper.readTree(input); return mapper.writer(prettyPrinter).writeValueAsString(json); } catch (Exception e) { logger.warn("Error pretty printing json.", e); } return input; }
From source file:ratpack.jackson.JacksonModule.java
@Provides @Singleton//from ww w . j a va 2 s .co m protected ObjectWriter objectWriter(ObjectMapper objectMapper) { return objectMapper.writer(prettyPrint ? new DefaultPrettyPrinter() : new MinimalPrettyPrinter()); }
From source file:com.couchbase.lite.util.DeepCloneTest.java
public void testDeepCloneWithJsonParser() throws Exception { Map<String, Object> map1 = new HashMap<String, Object>(); Object[] objs = { new String("a"), new String("b") }; Map<String, Object> map = new HashMap<String, Object>(); map.put("hello", "world"); List<Object> list = new ArrayList<Object>(); list.add("a"); list.add("b"); int[] ints = { 1, 2 }; map1.put("objs", objs); map1.put("ints", ints); map1.put("map", map); map1.put("list", list); Map<String, Object> deepMap1 = DeepClone.deepClone(map1); ObjectMapper mapper = new ObjectMapper(); String str1 = mapper.writer(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS).writeValueAsString(map1); String str2 = mapper.writer(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS).writeValueAsString(deepMap1); assertEquals(str1, str2);/*from w w w. ja v a 2s .c om*/ assertTrue(str1.equals(str2)); ((Map) deepMap1.get("map")).put("extra", "hey!"); str1 = mapper.writer(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS).writeValueAsString(map1); str2 = mapper.writer(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS).writeValueAsString(deepMap1); assertFalse(str1.equals(str2)); }
From source file:org.ratpackframework.jackson.JacksonModule.java
/** * Creates an object writer based on the bound {@link ObjectMapper} and the {@link #isPrettyPrint()} value. * * @param objectMapper The object mapper. * @return An object writer.//www . j a v a 2s . com */ @Provides @Singleton ObjectWriter objectWriter(ObjectMapper objectMapper) { return objectMapper.writer(isPrettyPrint() ? new DefaultPrettyPrinter() : new MinimalPrettyPrinter()); }
From source file:com.orange.ngsi.model.RegisterContextResponseTest.java
@Test public void serializationSimpleRegisterContextResponse() throws IOException { RegisterContextResponse registerContextResponse = new RegisterContextResponse("52a744b011f5816465943d58"); registerContextResponse.setDuration("P1M"); ObjectMapper mapper = new ObjectMapper(); ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter()); String json = writer.writeValueAsString(registerContextResponse); assertEquals("52a744b011f5816465943d58", JsonPath.read(json, "$.registrationId")); assertEquals("P1M", JsonPath.read(json, "$.duration")); }
From source file:com.orange.ngsi.model.QueryContextModelTest.java
@Test public void serializationSimpleQueryContext() throws IOException { QueryContext queryContext = createQueryContextTemperature(); ObjectMapper mapper = new ObjectMapper(); ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter()); String json = writer.writeValueAsString(queryContext); List<EntityId> entityIdList = JsonPath.read(json, "$.entities[*]"); assertEquals(1, entityIdList.size()); assertEquals("S*", JsonPath.read(json, "$.entities[0].id")); assertEquals("TempSensor", JsonPath.read(json, "$.entities[0].type")); assertEquals(true, JsonPath.read(json, "$.entities[0].isPattern")); List<String> attributeList = JsonPath.read(json, "$.attributes[*]"); assertEquals(1, attributeList.size()); assertEquals("temp", JsonPath.read(json, "$.attributes[0]")); }
From source file:com.orange.ngsi.model.SubscribeContextModelTest.java
@Test public void serializationSimpleSubscribeContext() throws IOException { SubscribeContext subscribeContext = null; try {/*from w w w . j av a 2s .c o m*/ subscribeContext = createSubscribeContextTemperature(); } catch (URISyntaxException e) { Assert.fail(e.getMessage()); } ObjectMapper mapper = new ObjectMapper(); ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter()); String json = writer.writeValueAsString(subscribeContext); List<EntityId> entityIdList = JsonPath.read(json, "$.entities[*]"); assertEquals(1, entityIdList.size()); assertEquals("Room1", JsonPath.read(json, "$.entities[0].id")); assertEquals("Room", JsonPath.read(json, "$.entities[0].type")); assertEquals(false, JsonPath.read(json, "$.entities[0].isPattern")); assertEquals("P1M", JsonPath.read(json, "$.duration")); List<String> attributes = JsonPath.read(json, "$.attributes[*]"); assertEquals(1, attributes.size()); assertEquals("temperature", JsonPath.read(json, "$.attributes[0]")); assertEquals("http://localhost:1028/accumulate", JsonPath.read(json, "$.reference")); List<NotifyCondition> notifyConditionList = JsonPath.read(json, "$.notifyConditions[*]"); assertEquals(1, notifyConditionList.size()); assertEquals(NotifyConditionEnum.ONTIMEINTERVAL.getLabel(), JsonPath.read(json, "$.notifyConditions[0].type")); List<String> condValues = JsonPath.read(json, "$.notifyConditions[0].condValues[*]"); assertEquals(1, condValues.size()); assertEquals("PT10S", JsonPath.read(json, "$.notifyConditions[0].condValues[0]")); }
From source file:com.orange.ngsi.model.RegisterContextModelTest.java
@Test public void serializationSimpleRegisterContext() throws URISyntaxException, JsonProcessingException { RegisterContext registerContext = createRegisterContextTemperature(); ObjectMapper mapper = new ObjectMapper(); ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter()); String json = writer.writeValueAsString(registerContext); List<ContextRegistration> contextRegistrationList = JsonPath.read(json, "$.contextRegistrations[*]"); assertEquals(1, contextRegistrationList.size()); List<EntityId> entityIdList = JsonPath.read(json, "$.contextRegistrations[0].entities[*]"); assertEquals(1, entityIdList.size()); assertEquals("Room*", JsonPath.read(json, "$.contextRegistrations[0].entities[0].id")); assertEquals("Room", JsonPath.read(json, "$.contextRegistrations[0].entities[0].type")); assertEquals(true, JsonPath.read(json, "$.contextRegistrations[0].entities[0].isPattern")); List<ContextRegistrationAttribute> attributes = JsonPath.read(json, "$.contextRegistrations[0].attributes[*]"); assertEquals(1, attributes.size());//from www . j a va 2 s . co m assertEquals("temperature", JsonPath.read(json, "$.contextRegistrations[0].attributes[0].name")); assertEquals("float", JsonPath.read(json, "$.contextRegistrations[0].attributes[0].type")); assertEquals(false, JsonPath.read(json, "$.contextRegistrations[0].attributes[0].isDomain")); assertEquals("http://localhost:1028/accumulate", JsonPath.read(json, "$.contextRegistrations[0].providingApplication")); assertEquals("PT10S", JsonPath.read(json, "$.duration")); }