List of usage examples for com.fasterxml.jackson.databind ObjectMapper writeValue
public void writeValue(Writer w, Object value) throws IOException, JsonGenerationException, JsonMappingException
From source file:org.httpobjects.jackson.JacksonDSL.java
public static Representation JacksonJson(final Object object, final ObjectMapper jackson) { return new Representation() { @Override/*from w w w .j ava2 s . c o m*/ public void write(OutputStream out) { try { jackson.writeValue(out, object); } catch (Exception e) { throw new RuntimeException(e); } } @Override public String contentType() { return "application/json"; } }; }
From source file:com.tectonica.thirdparty.Jackson2.java
public static void toJson(OutputStream os, Object o, ObjectMapper mapper) { try {// w ww . j a v a 2 s . co m mapper.writeValue(os, o); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.tectonica.thirdparty.Jackson2.java
public static void toJson(Writer w, Object o, ObjectMapper mapper) { try {//from ww w . j ava 2 s . c om mapper.writeValue(w, o); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:de.javagl.jgltf.browser.MenuNodesCreator.java
/** * Write the given list of {@link MenuNode} objects as JSON to the * given output stream. The caller is responsible for closing the * given stream.//from ww w . j av a 2 s . c om * * @param menuNodes The menu nodes * @param outputStream The output stream * @throws IOException If an IO error occurs */ private static void write(List<? extends MenuNode> menuNodes, OutputStream outputStream) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setSerializationInclusion(Include.NON_NULL); objectMapper.enable(SerializationFeature.INDENT_OUTPUT); objectMapper.writeValue(outputStream, menuNodes); }
From source file:fr.feedreader.websocket.UpdateFeed.java
public static void notifyUpdateFeed(Map<Feed, List<FeedItem>> newFeedItem, Map<Feed, Long> countUnread) { LogManager.getLogger().info("notification de mise jour de flux"); ObjectMapper mapper = new ObjectMapper(); FeedUpdateWrapper feedUpdateWrapper = new FeedUpdateWrapper(newFeedItem, countUnread); try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { mapper.writeValue(baos, feedUpdateWrapper); String jsonResponse = baos.toString("UTF-8"); availableSession.iterator();/*from www. j av a 2 s. c o m*/ for (Iterator<Session> it = availableSession.iterator(); it.hasNext();) { Session session = it.next(); try { session.getBasicRemote().sendText(jsonResponse); LogManager.getLogger().info("Envoyer \"" + session.getId() + "\""); } catch (ClosedChannelException ex) { LogManager.getLogger().error( "Session \"" + session.getId() + "\" Fermer. Exclusion de la liste des session active"); it.remove(); } catch (IOException ex) { LogManager.getLogger().error("Erreur dans l'envoi la websocket : " + session.getId(), ex); } } } catch (IOException ioe) { LogManager.getLogger().error("Impossible de convertir les flux mis jour en json", ioe); } }
From source file:de.alpharogroup.xml.json.JsonTransformer.java
/** * Creates from the given {@link List} to a json string. * * @param <T>//w ww . j a va 2s. c om * the generic type * @param list * the list * @return the json string. * @throws JsonGenerationException * If an error occurs by writing json string * @throws JsonMappingException * the If an error occurs when mapping the string into Object * @throws IOException * Signals that an I/O exception has occurred. */ public static <T> String toJson(final List<T> list) throws JsonGenerationException, JsonMappingException, IOException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(out, list); final byte[] bytes = out.toByteArray(); out.close(); return new String(bytes); }
From source file:com.msopentech.odatajclient.engine.data.Serializer.java
private static <T extends AbstractPayloadObject> void json(final T obj, final Writer writer) { try {// www . j a va 2 s. co m final ObjectMapper mapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.writeValue(writer, obj); } catch (IOException e) { throw new IllegalArgumentException("While serializing JSON object", e); } }
From source file:com.mac.holdempoker.socket.JsonConverter.java
public static String toJsonString(Object obj) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); //configure Object mapper for pretty print objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true); //writing to console, can write to any output stream such as file StringWriter jsonString = new StringWriter(); objectMapper.writeValue(jsonString, obj); return jsonString.toString(); }
From source file:groovyx.net.http.optional.Jackson.java
/** * Used to encode the request content using the Jackson JSON encoder. * * @param config the configuration//from w w w .jav a 2s. c o m * @param ts the server request content accessor */ @SuppressWarnings("WeakerAccess") public static void encode(final ChainedHttpConfig config, final ToServer ts) { try { if (handleRawUpload(config, ts)) { return; } final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest(); final ObjectMapper mapper = (ObjectMapper) config.actualContext(request.actualContentType(), OBJECT_MAPPER_ID); final StringWriter writer = new StringWriter(); mapper.writeValue(writer, request.actualBody()); ts.toServer(new CharSequenceInputStream(writer.toString(), request.actualCharset())); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.ant.myteam.gcm.POST2GCM.java
public static void post(String apiKey, Content content) { try {//from ww w . j a va 2 s.co m // 1. URL URL url = new URL("https://android.googleapis.com/gcm/send"); // 2. Open connection HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 3. Specify POST method conn.setRequestMethod("POST"); // 4. Set the headers conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Authorization", "key=" + apiKey); conn.setDoOutput(true); // 5. Add JSON data into POST request body //`5.1 Use Jackson object mapper to convert Contnet object into JSON ObjectMapper mapper = new ObjectMapper(); // 5.2 Get connection output stream DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); // 5.3 Copy Content "JSON" into mapper.writeValue(wr, content); // 5.4 Send the request wr.flush(); // 5.5 close wr.close(); // 6. Get the response int responseCode = conn.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 7. Print result System.out.println(response.toString()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }