List of utility methods to do Object to Json
String | obj2Json(Object obj) obj Json try { ObjectMapper objectMapper = new ObjectMapper(); ObjectWriter ow = objectMapper.writer().withDefaultPrettyPrinter(); return ow.writeValueAsString(obj); } catch (Exception e) { e.printStackTrace(); return null; |
String | object2Jason(Object obj) object Jason try { return objectMapper.writeValueAsString(obj); } catch (JsonProcessingException e) { return null; |
boolean | objectToString(StringBuilder bld, ObjectNode node, int depth, boolean newLine) object To String if (newLine) { indent(bld, depth); newLine = false; if (node.size() > 0) { bld.append("{\n"); newLine = true; boolean first = true; for (Iterator<Map.Entry<String, JsonNode>> itr = node.fields(); itr.hasNext();) { if (first) { first = false; } else { if (newLine) { indent(bld, depth); newLine = false; bld.append(','); bld.append('\n'); newLine = true; Map.Entry<String, JsonNode> entry = itr.next(); indent(bld, depth); bld.append('\"'); bld.append(entry.getKey()); bld.append('\"'); bld.append(':'); newLine = toString(bld, entry.getValue(), depth + 1, false); if (newLine) { indent(bld, depth); newLine = false; if (node.size() > 0) { bld.append('\n'); newLine = true; if (newLine) { indent(bld, depth); newLine = false; bld.append('}'); return false; |
JsonNode | objToJson(Object obj) Converts a generic Java object to a JsoNode object return mapper.valueToTree(obj);
|
String | toJson(List to Json try { return new ObjectMapper().writeValueAsString(list); } catch (JsonProcessingException e) { e.printStackTrace(); return ""; |
String | toJson(Object dto) to Json ObjectMapper mapper = createObjectMapper();
return mapper.writeValueAsString(dto);
|
String | toJson(Object message) to Json return mapper.writeValueAsString(message);
|
String | toJson(Object o) to Json try { return getObjectMapper().writeValueAsString(o); } catch (Exception e) { e.printStackTrace(); return "<exception>"; |
String | toJson(Object o) to Json return JSON_MAPPER.writeValueAsString(o);
|
String | toJSON(Object obj) to JSON try { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); return objectMapper.writeValueAsString(obj); } catch (JsonProcessingException e) { e.printStackTrace(); return "{error: " + e.getMessage() + "}"; ... |