private static void generateSLSLoadFile(String inputFile, String outputFile) throws IOException { Reader input = new FileReader(inputFile); try { Writer output = new FileWriter(outputFile); try { ObjectMapper mapper = new ObjectMapper(); ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter(); Iterator<Map> i = mapper.readValues( new JsonFactory().createJsonParser(input), Map.class); while (i.hasNext()) { Map m = i.next(); output.write(writer.writeValueAsString(createSLSJob(m)) + EOL); } } finally { output.close(); } } finally { input.close(); } }
@SuppressWarnings("unchecked") private static void generateSLSNodeFile(String outputFile) throws IOException { Writer output = new FileWriter(outputFile); try { ObjectMapper mapper = new ObjectMapper(); ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter(); for (Map.Entry<String, Set<String>> entry : rackNodeMap.entrySet()) { Map rack = new LinkedHashMap(); rack.put("rack", entry.getKey()); List nodes = new ArrayList(); for (String name : entry.getValue()) { Map node = new LinkedHashMap(); node.put("node", name); nodes.add(node); } rack.put("nodes", nodes); output.write(writer.writeValueAsString(rack) + EOL); } } finally { output.close(); } }
private static void generateSLSLoadFile(String inputFile, String outputFile) throws IOException { try (Reader input = new InputStreamReader(new FileInputStream(inputFile), "UTF-8")) { try (Writer output = new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8")) { ObjectMapper mapper = new ObjectMapper(); ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter(); Iterator<Map> i = mapper.readValues( new JsonFactory().createJsonParser(input), Map.class); while (i.hasNext()) { Map m = i.next(); output.write(writer.writeValueAsString(createSLSJob(m)) + EOL); } } } }
@SuppressWarnings("unchecked") private static void generateSLSNodeFile(String outputFile) throws IOException { try (Writer output = new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8")) { ObjectMapper mapper = new ObjectMapper(); ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter(); for (Map.Entry<String, Set<String>> entry : rackNodeMap.entrySet()) { Map rack = new LinkedHashMap(); rack.put("rack", entry.getKey()); List nodes = new ArrayList(); for (String name : entry.getValue()) { Map node = new LinkedHashMap(); node.put("node", name); nodes.add(node); } rack.put("nodes", nodes); output.write(writer.writeValueAsString(rack) + EOL); } } }
private static void generateSLSLoadFile(String inputFile, String outputFile) throws IOException { Reader input = new FileReader(inputFile); try { Writer output = new FileWriter(outputFile); try { ObjectMapper mapper = new ObjectMapper(); ObjectWriter writer = mapper.defaultPrettyPrintingWriter(); Iterator<Map> i = mapper.readValues( new JsonFactory().createJsonParser(input), Map.class); while (i.hasNext()) { Map m = i.next(); output.write(writer.writeValueAsString(createSLSJob(m)) + EOL); } } finally { output.close(); } } finally { input.close(); } }
@SuppressWarnings("unchecked") private static void generateSLSNodeFile(String outputFile) throws IOException { Writer output = new FileWriter(outputFile); try { ObjectMapper mapper = new ObjectMapper(); ObjectWriter writer = mapper.defaultPrettyPrintingWriter(); for (Map.Entry<String, Set<String>> entry : rackNodeMap.entrySet()) { Map rack = new LinkedHashMap(); rack.put("rack", entry.getKey()); List nodes = new ArrayList(); for (String name : entry.getValue()) { Map node = new LinkedHashMap(); node.put("node", name); nodes.add(node); } rack.put("nodes", nodes); output.write(writer.writeValueAsString(rack) + EOL); } } finally { output.close(); } }
private void broadcastByActivity(User owner, Activity activity, BaseProjectItem originalItem, BaseProjectItem updatedItem) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL); ObjectWriter ow = objectMapper.writer().withDefaultPrettyPrinter(); activity.setSubscribers(userService.getUserByProjectId(activity.getProjectId())); activity.setAttachObject((BaseProjectItem)identifiableManager.getIdentifiableByTypeAndId(activity.getAttachType(), activity.getAttachId())); String message = ""; try { message = ow.writeValueAsString(activity); } catch (IOException e) { e.printStackTrace(); } template.convertAndSend("channel", message); }
@GET @Produces(MediaType.APPLICATION_JSON) public String list() throws JsonGenerationException, JsonMappingException, IOException { this.logger.info("list()"); ObjectWriter viewWriter; if (this.isAdmin()) { viewWriter = this.mapper.writerWithView(JsonViews.Admin.class); } else { viewWriter = this.mapper.writerWithView(JsonViews.User.class); } List<NewsEntry> allEntries = this.newsEntryDao.findAll(); return viewWriter.writeValueAsString(allEntries); }
/** * jsonToString exclude null data end edit fields * * @param model - model data to String * @return - model data with json format */ public static String jsonToStringIgnoreSpecialFields(RestModel model) { String[] ExcludedFieldsFromView = getExcludedFields(model); ObjectMapper specialMapper = new ObjectMapper(); specialMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); String data = null; FilterProvider filters = new SimpleFilterProvider() .addFilter("exclude fields", SimpleBeanPropertyFilter.serializeAllExcept( (ExcludedFieldsFromView))); ObjectWriter writer = specialMapper.writer(filters); try { data = writer.writeValueAsString(model); } catch (IOException e) { log.debug(e.getMessage()); } return data; }
/** * {@inheritDoc} */ @Override public <T> int serialize(T obj, Class<T> type, OutputStream out) throws IOException { out = new CountingOutputStream(new BufferedOutputStream(out, getBufferSize())); try { ObjectWriter writer = _objectMapper.writerWithType(type); if (isPrettyPrint()) { writer = writer.withDefaultPrettyPrinter(); } writer.writeValue(out, obj); } finally { if (isCloseEnabled()) { out.close(); } } return ((CountingOutputStream)out).getCount(); }
/** * Generate the JSON String for StateTransitionThrottleConfig. * * @return Json String for this config. */ public String toJSON() { Map<String, String> configMap = new HashMap<String, String>(); configMap.put(ConfigProperty.REBALANCE_TYPE.name(), _rebalanceType.name()); configMap.put(ConfigProperty.THROTTLE_SCOPE.name(), _throttleScope.name()); configMap.put(ConfigProperty.MAX_PARTITION_IN_TRANSITION.name(), String.valueOf(_maxPartitionInTransition)); String jsonStr = null; try { ObjectWriter objectWriter = OBJECT_MAPPER.writer(); jsonStr = objectWriter.writeValueAsString(configMap); } catch (IOException e) { logger.error("Failed to convert config map to JSON object! " + configMap); } return jsonStr; }
protected void getToolConsumerProfile(HttpServletRequest request, HttpServletResponse response,String profile_id) { // Map<String,Object> deploy = ltiService.getDeployForConsumerKeyDao(profile_id); Map<String,Object> deploy = null; ToolConsumer consumer = buildToolConsumerProfile(request, deploy, profile_id); ObjectMapper mapper = new ObjectMapper(); try { // http://stackoverflow.com/questions/6176881/how-do-i-make-jackson-pretty-print-the-json-content-it-generates ObjectWriter writer = mapper.defaultPrettyPrintingWriter(); // ***IMPORTANT!!!*** for Jackson 2.x use the line below instead of the one above: // ObjectWriter writer = mapper.writer().withDefaultPrettyPrinter(); // System.out.println(mapper.writeValueAsString(consumer)); response.setContentType(APPLICATION_JSON); PrintWriter out = response.getWriter(); out.println(writer.writeValueAsString(consumer)); // System.out.println(writer.writeValueAsString(consumer)); } catch (Exception e) { e.printStackTrace(); } }
/** * Get an inclusive jackson object writer for writing a set of named fields of an object * * @param includeFieldNames fields that should be serialized * @param filterName filter name. This needs to match the @JsonFilter("filterName") annotation for a bean * * @return a Jackson ObjectWriter * @throws Exception */ public static ObjectWriter getInclusiveObjectWriter(String[] includeFieldNames, String filterName) throws Exception { if (includeFieldNames == null) throw new Exception("includeFieldNames can't be null"); if (filterName == null) throw new Exception("filterName can't be null"); ObjectMapper mapper = new ObjectMapper(); FilterProvider filters = new SimpleFilterProvider() .addFilter(filterName, SimpleBeanPropertyFilter.filterOutAllExcept(includeFieldNames)); return mapper.writer(filters); }
/** * Get an exclusive jackson object writer for serializing an object without a set of named fields * * @param excludeFieldNames fields that should be excluded for serialization * @param filterName filter name. This needs to match the @JsonFilter("filterName") annotation for a bean * * @return a Jackson ObjectWriter * @throws Exception */ public static ObjectWriter getExclusivObjectWriter(String[] excludeFieldNames, String filterName) throws Exception { if (excludeFieldNames == null) throw new Exception("includeFieldNames can't be null"); if (filterName == null) throw new Exception("filterName can't be null"); ObjectMapper mapper = new ObjectMapper(); FilterProvider filters = new SimpleFilterProvider() .addFilter(filterName, SimpleBeanPropertyFilter.serializeAllExcept(excludeFieldNames)); return mapper.writer(filters); }
@GET @Produces(MediaType.APPLICATION_JSON) public String list() throws JsonGenerationException, JsonMappingException, IOException { this.logger.info("list()"); ObjectWriter viewWriter; if (this.isPublisher()) { viewWriter = this.mapper.writerWithView(JsonViews.Publisher.class); } else { viewWriter = this.mapper.writerWithView(JsonViews.User.class); } List<Memo> allEntries = this.basicMemoService.findAll(); return viewWriter.writeValueAsString(allEntries); }
public static String toJSON(Object obj, boolean prettyPrint) { ObjectMapper mapper = new ObjectMapper(); try { if (prettyPrint) { ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter(); return writer.writeValueAsString(obj); } return mapper.writeValueAsString(obj); } catch (Exception e) { throw new RuntimeException(e); } }
public static void toJSON(Object obj, OutputStream stream, boolean prettyPrint) { ObjectMapper mapper = new ObjectMapper(); try { if (prettyPrint) { ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter(); writer.writeValue(stream, obj); return; } mapper.writeValue(stream, obj); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public String toString(T pojo) { try { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); ObjectWriter writer = mapper.writer(); return writer.writeValueAsString(pojo); } catch (IOException e) { throw Throwables.propagate(e); } }
JacksonInstanceSerializer(ObjectReader objectReader, ObjectWriter objectWriter, TypeReference<ServiceInstance<T>> typeRef) { this.objectReader = objectReader; this.objectWriter = objectWriter; this.typeRef = typeRef; }
public static String convertSongListToJSON(Set<SongVO> listOfSong,String query) throws JsonGenerationException, JsonMappingException, IOException{ ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); String json=""; json = ow.writeValueAsString(listOfSong); return "{"+"\""+query+"\":"+json+"}"; }
/** * 根据map生成json * * @param actionMap * @return */ private String getJson(Object activity) { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL); ObjectWriter ow = objectMapper.writer().withDefaultPrettyPrinter(); String message = ""; try { message = ow.writeValueAsString(activity); } catch (IOException e) { logger.info("activity to json failure"); } return message; }
public static Digest computeDigest(Object value) { if (NO_CRYPTO) return new Digest(new byte[10]); try { ObjectWriter writer = mapper.writer(); MessageDigest digest = MessageDigest.getInstance(DIGEST_TYPE); return new Digest(digest.digest(writer.writeValueAsString(value).getBytes())); } catch (NoSuchAlgorithmException | IOException e) { e.printStackTrace(); } return null; }
public static byte[] convertToJsonByteArray(Object message) { ObjectWriter writer = mapper.writer(FIELD_FILTER); try { return writer.writeValueAsString(message).getBytes(); } catch (IOException e) { e.printStackTrace(); } return null; }
protected void processLogMessage(LogMessageDoc m, Element element) { Element encElement = element.getEnclosingElement(); LogMessageCategory category = element.getAnnotation(LogMessageCategory.class); if (category == null) category = encElement.getAnnotation(LogMessageCategory.class); String categoryStr = "Core"; if (category != null) categoryStr = category.value(); LogMessageDocItemData lmdi = new LogMessageDocItemData(); lmdi.className = encElement.asType().toString(); lmdi.category = categoryStr; lmdi.severity = m.level(); lmdi.message = m.message(); lmdi.explanation = m.explanation(); lmdi.recommendation = m.recommendation(); ObjectMapper mapper = new ObjectMapper(); ObjectWriter writer = mapper.defaultPrettyPrintingWriter(); ByteArrayOutputStream os = new ByteArrayOutputStream(); try { writer.writeValue(os, lmdi); out.print(os.toString()); out.flush(); } catch (Exception e) { e.printStackTrace(); } }
@SuppressWarnings("deprecation") public static void main(String[] args) throws IOException { String pattern = FileUtils.readFileToString(new File(PATTERN)); String input = FileUtils.readFileToString(new File(INPUT_FILE)); ObjectMapper objMapper = new ObjectMapper(); String[] tokens = input.split("###"); int i = 1; StringBuilder sb = new StringBuilder(); for (String js : tokens) { if (js.trim().length() == 0) { continue; } System.out.println("Test " + i + " of " + tokens.length); Parser parser = new Parser(js.trim()); ASTRoot root = parser.parse(); ObjectWriter writer = objMapper.defaultPrettyPrintingWriter(); String json = writer.writeValueAsString(root); FileUtils.write(new File(new File(OUTPUT_FOLDER), "ast_expected_" + i + ".json"), json); sb.append(pattern.replace("@@", i + "").replace("###", escapeQuote(js.trim()))); i++; } FileUtils.writeStringToFile( new File(new File(OUTPUT_FOLDER), "out.txt"), sb.toString()); }
public void testBase(String js, String fileName) throws JsonGenerationException, JsonMappingException, IOException, JSONException { Parser parser = new Parser(js); ASTRoot root = parser.parse(); ObjectMapper objMapper = new ObjectMapper(); ObjectWriter writer = objMapper.defaultPrettyPrintingWriter(); String actual = writer.writeValueAsString(root); JSONAssert.assertEquals(getJson(fileName), actual, false); }
@Test public void test_0() throws JsonGenerationException, JsonMappingException, IOException { String js = " function filip(a,b,c,d) {if (a == 5 / c * 54 && a/4==f*3 || ff!=5) {}}"; Parser parser = new Parser(js); ASTRoot root = parser.parse(); ObjectWriter writer = objMapper.defaultPrettyPrintingWriter(); }