List of usage examples for com.fasterxml.jackson.core.util DefaultPrettyPrinter DefaultPrettyPrinter
public DefaultPrettyPrinter()
From source file:com.ibm.ws.lars.rest.ErrorHandler.java
@Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setStatus(500);//w ww . j av a 2 s .com response.setContentType(MediaType.APPLICATION_JSON); PrintWriter printWriter = response.getWriter(); JsonGenerator frontPageJsonGenerator = new JsonFactory().createGenerator(printWriter); frontPageJsonGenerator.setPrettyPrinter(new DefaultPrettyPrinter()); frontPageJsonGenerator.writeStartObject(); frontPageJsonGenerator.writeStringField("message", "Internal server error, please contact the server administrator"); frontPageJsonGenerator.writeNumberField("statusCode", response.getStatus()); frontPageJsonGenerator.writeEndObject(); frontPageJsonGenerator.flush(); frontPageJsonGenerator.close(); }
From source file:javasnack.snacks.json.PojoEncodeJackson.java
@Override public void run() { ObjectMapper objectMapper = new ObjectMapper(); try {/*from w w w . j a v a 2 s . c o m*/ String jsonout = objectMapper.writeValueAsString(new EncodePojo()); System.out.println("--- simple jackson encode ---"); System.out.println(jsonout); String jsonout2 = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(new EncodePojo()); System.out.println("--- default pretty-print jackson encode ---"); System.out.println(jsonout2); System.out.println("--- streaming jackson encode ---"); JsonFactory jsonFactory = objectMapper.getFactory(); Writer out = new OutputStreamWriter(System.out); JsonGenerator jg = jsonFactory.createGenerator(out); jg.setPrettyPrinter(new DefaultPrettyPrinter()); jg.writeStartObject(); jg.writeStringField("message", "success"); jg.writeNumberField("count", 10); jg.writeArrayFieldStart("records"); for (int i = 0; i < 10; i++) { jg.writeObject(new EncodePojo()); Thread.sleep(100); jg.flush(); } jg.writeEndArray(); jg.writeEndObject(); jg.close(); } catch (JsonProcessingException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
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.ngsi2.model.RegistrationTest.java
@Test public void serializationRegistrationTest() throws JsonProcessingException, MalformedURLException { ObjectWriter writer = Utils.objectMapper.writer(new DefaultPrettyPrinter()); Registration registration = new Registration("abcde", new URL("http://localhost:1234")); registration.setDuration("PT1M"); SubjectEntity subjectEntity = new SubjectEntity(); subjectEntity.setType(Optional.of("Room")); SubjectRegistration subjectRegistration = new SubjectRegistration(Collections.singletonList(subjectEntity), Collections.singletonList("humidity")); registration.setSubject(subjectRegistration); String json = writer.writeValueAsString(registration); String jsonString = "{\n" + " \"id\" : \"abcde\",\n" + " \"subject\" : {\n" + " \"entities\" : [ {\n" + " \"type\" : \"Room\"\n" + " } ],\n" + " \"attributes\" : [ \"humidity\" ]\n" + " },\n" + " \"callback\" : \"http://localhost:1234\",\n" + " \"duration\" : \"PT1M\"\n" + "}"; assertEquals(jsonString, json);//from w w w .j a v a 2 s . co m }
From source file:org.jasig.cas.util.AbstractJacksonBackedJsonSerializer.java
/** * Instantiates a new Registered service json serializer. * Uses the {@link com.fasterxml.jackson.core.util.DefaultPrettyPrinter} for formatting. */// w w w.j ava 2 s. c o m public AbstractJacksonBackedJsonSerializer() { this(new DefaultPrettyPrinter()); }
From source file:com.amazon.feeds.SampleFeedGenerator.java
/** * The method for generating sample feeds. * * @param format The class containing the format specifications. * @param items The number of items to generate. * @param ext File extension./*from w ww. ja v a2s .c om*/ */ public void createSampleFeed(IFeedFormat format, int items, String ext) throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.getFactory().setCharacterEscapes(format.getEscapeRules()); // create output file String out = format.getFeedFormat() + "-" + items + "." + ext; // TODO: add XML support File outFile = new File(SAMPLE_PATH, out); if (!outFile.exists()) { outFile.getParentFile().mkdirs(); } // populate sample feed System.out.println("Generating " + items + (items == 1 ? " item" : " items") + " for " + format.getProvider() + " feed at " + outFile.getAbsolutePath()); format.populate(items); // write JSON to file if (format.usePrettyPrint()) { DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter(" ", DefaultIndenter.SYS_LF); DefaultPrettyPrinter printer = new DefaultPrettyPrinter(); printer.indentObjectsWith(indenter); printer.indentArraysWith(indenter); mapper.writer(printer).writeValue(outFile, format); } else { mapper.writeValue(outFile, format); } }
From source file:com.bossletsplays.frost.utils.config.JsonWrapper.java
public JsonWrapper(String path, int type) throws IOException { this.path = path; this.f = new JsonFactory(); this.type = type; elements = new MultiMap<String, ConfigObject>(); if (type == GENERATE) { mapper = new ObjectMapper(f); g = f.createGenerator(new FileWriter(path)); g.setCodec(mapper);//from ww w . j av a 2s . co m g.setPrettyPrinter(new DefaultPrettyPrinter()); g.writeStartObject(); } else if (type == PARSE) { mapper = new ObjectMapper(f); rootNode = mapper.readTree(new File(path)); } }
From source file:org.ulyssis.ipp.publisher.FileOutput.java
@Override public void outputScore(Score score) { Path tmpFile = null;/*from w ww. j a v a2 s .co m*/ try { if (tmpDir.isPresent()) { tmpFile = Files.createTempFile(tmpDir.get(), "score-", ".json", PosixFilePermissions.asFileAttribute(defaultPerms)); } else { tmpFile = Files.createTempFile("score-", ".json", PosixFilePermissions.asFileAttribute(defaultPerms)); } BufferedWriter writer = Files.newBufferedWriter(tmpFile, StandardCharsets.UTF_8); Serialization.getJsonMapper().writer(new DefaultPrettyPrinter()).writeValue(writer, score); Files.move(tmpFile, filePath, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { LOG.error("Error writing score to file!", e); } finally { try { if (tmpFile != null) Files.deleteIfExists(tmpFile); } catch (IOException ignored) { } } }
From source file:com.orange.ngsi2.model.SubscriptionTest.java
@Test public void serializationSubscriptionTest() throws JsonProcessingException, MalformedURLException { ObjectWriter writer = Utils.objectMapper.writer(new DefaultPrettyPrinter()); SubjectEntity subjectEntity = new SubjectEntity(); subjectEntity.setId(Optional.of("Bcn_Welt")); subjectEntity.setType(Optional.of("Room")); Condition condition = new Condition(); condition.setAttributes(Collections.singletonList("temperature")); condition.setExpression("q", "temperature>40"); SubjectSubscription subjectSubscription = new SubjectSubscription(Collections.singletonList(subjectEntity), condition);/*from www . j a v a 2 s . c o m*/ List<String> attributes = new ArrayList<>(); attributes.add("temperature"); attributes.add("humidity"); Notification notification = new Notification(attributes, new URL("http://localhost:1234")); notification.setThrottling(Optional.of(new Long(5))); notification.setTimesSent(12); notification.setLastNotification(Instant.parse("2015-10-05T16:00:00.10Z")); notification.setHeader("X-MyHeader", "foo"); notification.setQuery("authToken", "bar"); notification.setAttrsFormat(Optional.of(Notification.Format.keyValues)); String json = writer.writeValueAsString(new Subscription("abcdefg", subjectSubscription, notification, Instant.parse("2016-04-05T14:00:00.20Z"), Subscription.Status.active)); assertEquals(jsonString, json); }
From source file:ratpack.jackson.JacksonModule.java
@Provides @Singleton//from w w w . jav a 2 s .co m protected ObjectWriter objectWriter(ObjectMapper objectMapper) { return objectMapper.writer(prettyPrint ? new DefaultPrettyPrinter() : new MinimalPrettyPrinter()); }