Java Code Examples for org.codehaus.jackson.map.ObjectWriter#writeValueAsString()

The following examples show how to use org.codehaus.jackson.map.ObjectWriter#writeValueAsString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedisPublisher.java    From onboard with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: StateTransitionThrottleConfig.java    From helix with Apache License 2.0 6 votes vote down vote up
/**
 * Generate the JSON String for StateTransitionThrottleConfig.
 * @return Json String for this config.
 */
public String toJSON() {
  Map<String, String> configMap = new HashMap<>();
  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;
}
 
Example 3
Source File: StringCodec.java    From Bats with Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example 4
Source File: TbContentControllerTests.java    From spring-boot-samples with Apache License 2.0 5 votes vote down vote up
/**
 * 新增资源
 */
@Test
public void testInsert() throws Exception {
    // 由于请求参数使用了 @RequestBody 注解,故需要将参数封装成 JSON 格式
    TbContent tbContent = new TbContent();
    tbContent.setCategoryId(89L);
    tbContent.setTitle("来自 SpringMock 的新增测试");
    ObjectMapper objectMapper = new ObjectMapper();
    ObjectWriter objectWriter = objectMapper.writer().withDefaultPrettyPrinter();
    String jsonParams = objectWriter.writeValueAsString(tbContent);

    // 模拟请求
    int status = this.mockMvc
            .perform(MockMvcRequestBuilders
                    .post("/insert")
                    .header("Authorization", "Bearer 91317816-5036-4b76-86b7-05d96d55774d")
                    // 设置使用 JSON 方式传参
                    .contentType(MediaType.APPLICATION_JSON)
                    // 设置 JSON 参数内容
                    .content(jsonParams))
            .andDo(MockMvcResultHandlers.print())
            .andReturn().getResponse().getStatus();
    if (status == 200) {
        log.info("请求成功");
    } else {
        log.info("请求失败,状态码为:{}", status);
    }

    Assert.assertEquals(status, 200);
}
 
Example 5
Source File: TbContentControllerTests.java    From spring-boot-samples with Apache License 2.0 5 votes vote down vote up
/**
 * 更新资源
 *
 * @throws Exception
 */
@Test
public void testUpdate() throws Exception {
    // 由于请求参数使用了 @RequestBody 注解,故需要将参数封装成 JSON 格式
    TbContent tbContent = new TbContent();
    tbContent.setId(43L);
    tbContent.setCategoryId(89L);
    tbContent.setTitle("来自 SpringMock 的编辑测试");
    ObjectMapper objectMapper = new ObjectMapper();
    ObjectWriter objectWriter = objectMapper.writer().withDefaultPrettyPrinter();
    String jsonParams = objectWriter.writeValueAsString(tbContent);

    // 模拟请求
    int status = this.mockMvc
            .perform(MockMvcRequestBuilders
                    .put("/update")
                    .header("Authorization", "Bearer 91317816-5036-4b76-86b7-05d96d55774d")
                    // 设置使用 JSON 方式传参
                    .contentType(MediaType.APPLICATION_JSON)
                    // 设置 JSON 参数内容
                    .content(jsonParams))
            .andDo(MockMvcResultHandlers.print())
            .andReturn().getResponse().getStatus();
    if (status == 200) {
        log.info("请求成功");
    } else {
        log.info("请求失败,状态码为:{}", status);
    }

    Assert.assertEquals(status, 200);
}
 
Example 6
Source File: PrettifyJSONExample.java    From Examples with MIT License 5 votes vote down vote up
public static void main(String[] args) {
    try {
        String json = "{\"candidate\" : \"Vicky Thakor\", \"expertise\" : [\"Core Java\", \"J2EE\", \"Design Pattern\"]}";
        System.out.println("Unformatter json:" + json);
        /* Create required objects */
        ObjectMapper objectMapper = new ObjectMapper();
        Object object = objectMapper.readValue(json, Object.class);
        ObjectWriter objectWriter = objectMapper.writer().withDefaultPrettyPrinter();
        /* Get the formatted json */
        String formattedJSON = objectWriter.writeValueAsString(object);
        System.out.println("Formatter json:\n"+formattedJSON);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
 
Example 7
Source File: StringCodec.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example 8
Source File: WebSocketServiceImpl.java    From onboard with Apache License 2.0 5 votes vote down vote up
/**
 * 根据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;
}