示例#1
0
  public static void main(String[] args) {

    List<Map<Long, Long>> anchors = new ArrayList<Map<Long, Long>>();
    Map<Long, Long> uid2type1 = new HashMap<>();
    uid2type1.put(124L, 4L);
    anchors.add(uid2type1);

    Map<Long, Long> uid2type2 = new HashMap<>();
    uid2type2.put(324L, 24L);
    anchors.add(uid2type2);

    Map<Long, Long> uid2type3 = new HashMap<>();
    uid2type3.put(524L, 34L);
    anchors.add(uid2type3);

    System.out.println("anchors size:" + anchors.size());

    objectMapper = new ObjectMapper();
    String rtn = null;

    List<Map<Long, Long>> anchors2 = new ArrayList<Map<Long, Long>>();
    try {
      rtn = objectMapper.writeValueAsString(anchors);

      // anchors2 = objectMapper.readValue(rtn, List< Map<Long, Long> >);
    } catch (JsonProcessingException e) {
      e.printStackTrace();
    }

    System.out.println(rtn);
  }
 public static String tagsToJson(ArrayList<Tag> tags) throws CloudFormationException {
   try {
     return mapper.writeValueAsString(tags == null ? Lists.<Tag>newArrayList() : tags);
   } catch (JsonProcessingException e) {
     throw new ValidationErrorException(e.getMessage());
   }
 }
  @Test
  public void testgoodInput() {
    FinishTurnInput input = new FinishTurnInput(0);
    Player testPlayer = model.getPlayer(new PlayerID(0));
    try {
      // (int soldier, int monument, int monopoly, int yearOfPlenty, int roadBuild)
      testPlayer.getPlayerBank().setDC(new DevelopmentHand(0, 0, 0, 0, 0));
      testPlayer.getPlayerBank().addNewDC(new DevelopmentHand(1, 1, 1, 1, 1));
    } catch (BankException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    FinishTurnCommand command = new FinishTurnCommand();
    command.setGameModel(model);
    try {
      model = (GameModel) command.execute(new ObjectMapper().writeValueAsString(input));
    } catch (JsonProcessingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    assertEquals(testPlayer.getPlayerBank().getMonopoly().getQuantity(), 1);
    assertEquals(testPlayer.getPlayerBank().getMonument().getQuantity(), 1);
    assertEquals(testPlayer.getPlayerBank().getRoadBuild().getQuantity(), 1);
    assertEquals(testPlayer.getPlayerBank().getSoldier().getQuantity(), 1);
    assertEquals(testPlayer.getPlayerBank().getYearOfPlenty().getQuantity(), 1);
    assertEquals(testPlayer.getPlayerBank().getNewMonopoly().getQuantity(), 0);
    assertEquals(testPlayer.getPlayerBank().getNewMonument().getQuantity(), 0);
    assertEquals(testPlayer.getPlayerBank().getNewRoadBuild().getQuantity(), 0);
    assertEquals(testPlayer.getPlayerBank().getNewSoldier().getQuantity(), 0);
    assertEquals(testPlayer.getPlayerBank().getNewYearOfPlenty().getQuantity(), 0);

    assertEquals(testPlayer.getPlayerFacade().canBeRobbed(), true);
  }
  /**
   * Parse a JSON response to extract an entity document.
   *
   * <p>TODO This method currently contains code to work around Wikibase issue
   * https://phabricator.wikimedia.org/T73349. This should be removed once the issue is fixed.
   *
   * @param entityNode the JSON node that should contain the entity document data
   * @return the entitiy document, or null if there were unrecoverable errors
   * @throws IOException
   * @throws JsonProcessingException
   */
  private EntityDocument parseJsonResponse(JsonNode entityNode)
      throws JsonProcessingException, IOException {
    try {
      JacksonTermedStatementDocument ed =
          mapper.treeToValue(entityNode, JacksonTermedStatementDocument.class);
      ed.setSiteIri(this.siteIri);

      return ed;
    } catch (JsonProcessingException e) {
      logger.warn(
          "Error when reading JSON for entity "
              + entityNode.path("id").asText("UNKNOWN")
              + ": "
              + e.toString()
              + "\nTrying to manually fix issue https://phabricator.wikimedia.org/T73349.");
      String jsonString = entityNode.toString();
      jsonString =
          jsonString
              .replace("\"sitelinks\":[]", "\"sitelinks\":{}")
              .replace("\"labels\":[]", "\"labels\":{}")
              .replace("\"aliases\":[]", "\"aliases\":{}")
              .replace("\"claims\":[]", "\"claims\":{}")
              .replace("\"descriptions\":[]", "\"descriptions\":{}");

      ObjectReader documentReader = this.mapper.reader(JacksonTermedStatementDocument.class);

      JacksonTermedStatementDocument ed;
      ed = documentReader.readValue(jsonString);
      ed.setSiteIri(this.siteIri);
      return ed;
    }
  }
 public String objectAsJson(Object value) throws BankException {
   try {
     return mObjectMapper.writeValueAsString(value);
   } catch (JsonProcessingException e) {
     throw new BankException(e.getMessage(), e);
   }
 }
  public static JsonNode build(final JsonProcessingException e, final boolean crlf) {
    final JsonLocation location = e.getLocation();
    final ObjectNode ret = JsonNodeFactory.instance.objectNode();

    /*
     * Unfortunately, for some reason, Jackson botches the column number in
     * its JsonPosition -- I cannot figure out why exactly. However, it does
     * have a correct offset into the buffer.
     *
     * The problem is that if the input has CR/LF line terminators, its
     * offset will be "off" by the number of lines minus 1 with regards to
     * what JavaScript sees as positions in text areas. Make the necessary
     * adjustments so that the caret jumps at the correct position in this
     * case.
     */
    final int lineNr = location.getLineNr();
    int offset = (int) location.getCharOffset();
    if (crlf) offset = offset - lineNr + 1;
    ret.put(LINE, lineNr);
    ret.put(OFFSET, offset);

    // Finally, put the message
    ret.put(MESSAGE, e.getOriginalMessage());
    return ret;
  }
示例#7
0
 /**
  * <br>
  * <b>功能:</b>输出JSON<br>
  * <b>作者:</b>肖财高<br>
  * <b>日期:</b> 2013-4-24 <br>
  *
  * @param response
  * @param type 0=成功 其他=失败
  * @param msg
  */
 public void toJsonMsg(HttpServletResponse response, int type, String msg) {
   Map<String, Object> map = new HashMap<String, Object>();
   ObjectMapper mapper = new ObjectMapper();
   map.put("state", type);
   if (type == 0) {
     map.put("success", true);
     if (msg == null) {
       map.put("msg", "成功");
     } else {
       map.put("msg", msg);
     }
   } else {
     map.put("success", false);
     if (msg == null) {
       map.put("msg", "失败");
     } else {
       map.put("msg", msg);
     }
   }
   try {
     this.toJsonPrint(response, mapper.writeValueAsString(map));
   } catch (JsonProcessingException e) {
     e.printStackTrace();
   }
 }
 public String objectAsJson(Object value) {
   try {
     return mObjectMapper.writeValueAsString(value);
   } catch (JsonProcessingException e) {
     e.printStackTrace();
   }
   return null;
 }
示例#9
0
 private static void gravarDTOCompraSessao(FormularioCompra dto) {
   try {
     ObjectMapper mapper = new ObjectMapper();
     session("compra", mapper.writeValueAsString(dto));
   } catch (JsonProcessingException e) {
     e.printStackTrace();
   }
 }
 public static String parametersToJson(ArrayList<StackEntity.Parameter> parameters)
     throws CloudFormationException {
   try {
     return mapper.writeValueAsString(
         parameters == null ? Lists.<StackEntity.Parameter>newArrayList() : parameters);
   } catch (JsonProcessingException e) {
     throw new ValidationErrorException(e.getMessage());
   }
 }
示例#11
0
 @Test
 public void test() {
   User user = userService.getUserByAccount("shihai");
   try {
     System.out.println(objectMapper.writeValueAsString(user));
   } catch (JsonProcessingException e) {
     e.printStackTrace();
   }
 }
 public static String conditionMapToJson(Map<String, Boolean> conditionMap)
     throws CloudFormationException {
   try {
     return mapper.writeValueAsString(
         conditionMap == null ? Maps.<String, Boolean>newLinkedHashMap() : conditionMap);
   } catch (JsonProcessingException e) {
     throw new ValidationErrorException(e.getMessage());
   }
 }
 public static String notificationARNsToJson(ArrayList<String> notificationARNs)
     throws CloudFormationException {
   try {
     return mapper.writeValueAsString(
         notificationARNs == null ? Lists.<String>newArrayList() : notificationARNs);
   } catch (JsonProcessingException e) {
     throw new ValidationErrorException(e.getMessage());
   }
 }
示例#14
0
 private String toJson(final Object object) {
   final ObjectMapper objectMapper = new ObjectMapper();
   try {
     return objectMapper.writeValueAsString(object);
   } catch (JsonProcessingException e) {
     e.printStackTrace();
   }
   return null;
 }
 public static String getJson(Map<String, Object> map) {
   try {
     return objMap.writeValueAsString(map);
   } catch (JsonProcessingException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     throw new IllegalArgumentException(e);
   }
 }
示例#16
0
 @Override
 public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType)
     throws SQLException {
   try {
     ps.setString(i, objectMapper.writeValueAsString(parameter));
   } catch (JsonProcessingException e) {
     logger.error(e.getMessage(), e);
   }
 }
示例#17
0
 public String toJSON() {
   String result = null;
   try {
     result = new ObjectMapper().writeValueAsString(this);
   } catch (JsonProcessingException e) {
     e.printStackTrace();
   }
   return result;
 }
 public static String outputsToJson(ArrayList<StackEntity.Output> outputs)
     throws CloudFormationException {
   try {
     return mapper.writeValueAsString(
         outputs == null ? Lists.<StackEntity.Output>newArrayList() : outputs);
   } catch (JsonProcessingException e) {
     throw new ValidationErrorException(e.getMessage());
   }
 }
示例#19
0
 @Override
 public String toString() {
   try {
     return new ObjectMapper().writeValueAsString(this);
   } catch (JsonProcessingException e) {
     e.printStackTrace();
     return null;
   }
 }
示例#20
0
 public static String Object2Json(Object o) {
   ObjectMapper objectMapper = new ObjectMapper();
   String jsonStr = null;
   try {
     jsonStr = objectMapper.writeValueAsString(o);
   } catch (JsonProcessingException e) {
     e.printStackTrace();
   }
   return jsonStr;
 }
 // TODO: Surely this method isn't necessary with Ratpack
 protected String requestToString(JsonRpcRequest request) {
   String result;
   try {
     result = mapper.writeValueAsString(request);
   } catch (JsonProcessingException e) {
     e.printStackTrace();
     result = "proxy jackson error";
   }
   return result;
 }
示例#22
0
 public String writeToJson() {
   String json = "";
   try {
     json = (new ObjectMapper()).writeValueAsString(jo);
   } catch (JsonProcessingException e) {
     // TODO 自動生成された catch ブロック
     e.printStackTrace();
   }
   return json;
 }
 private String createJsonFromEmploy(Object employ) {
   ObjectMapper mapper = new ObjectMapper();
   String empoyJson = "";
   try {
     empoyJson = mapper.writeValueAsString(employ);
   } catch (JsonProcessingException e) {
     e.printStackTrace();
   }
   return empoyJson;
 }
示例#24
0
 /**
  * Print JsonNode using default pretty printer.
  *
  * @param json JSON node to print
  */
 @java.lang.SuppressWarnings("squid:S1148")
 private void printJson(JsonNode json) {
   try {
     print("%s", mapper().writerWithDefaultPrettyPrinter().writeValueAsString(json));
   } catch (JsonProcessingException e) {
     StringWriter sw = new StringWriter();
     e.printStackTrace(new PrintWriter(sw));
     print("[ERROR] %s\n%s", e.getMessage(), sw.toString());
   }
 }
示例#25
0
 private void save(File toFile) {
   try {
     System.out.println(toFile.getAbsolutePath());
     graphInputView.save(toFile.getAbsolutePath());
   } catch (JsonProcessingException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
 @Nullable
 static io.zipkin.Span invert(Span input) {
   try {
     byte[] bytes = scalaCodec.writeValueAsBytes(input);
     return Codec.JSON.readSpan(bytes);
   } catch (JsonProcessingException e) {
     e.printStackTrace();
     return null;
   }
 }
示例#27
0
  @Override
  public StatementsResultLRSResponse saveStatements(List<Statement> statements) {
    StatementsResultLRSResponse lrsResponse = new StatementsResultLRSResponse();
    if (statements.isEmpty()) {
      lrsResponse.setSuccess(true);
      return lrsResponse;
    }

    ArrayNode rootNode = Mapper.getInstance().createArrayNode();
    for (Statement statement : statements) {
      rootNode.add(statement.toJSONNode(getVersion()));
    }

    lrsResponse.setRequest(new HTTPRequest());
    lrsResponse.getRequest().setResource("statements");
    lrsResponse.getRequest().setMethod(HttpMethods.POST);
    lrsResponse.getRequest().setContentType("application/json");
    try {
      lrsResponse
          .getRequest()
          .setContent(Mapper.getWriter(this.usePrettyJSON()).writeValueAsBytes(rootNode));
    } catch (JsonProcessingException ex) {
      lrsResponse.setErrMsg("Exception: " + ex.toString());
      return lrsResponse;
    }

    HTTPResponse response = makeSyncRequest(lrsResponse.getRequest());
    int status = response.getStatus();

    lrsResponse.setResponse(response);

    if (status == 200) {
      lrsResponse.setSuccess(true);
      lrsResponse.setContent(new StatementsResult());
      try {
        Iterator it =
            Mapper.getInstance().readValue(response.getContent(), ArrayNode.class).elements();
        for (int i = 0; it.hasNext(); ++i) {
          lrsResponse.getContent().getStatements().add(statements.get(i));
          lrsResponse
              .getContent()
              .getStatements()
              .get(i)
              .setId(UUID.fromString(((JsonNode) it.next()).textValue()));
        }
      } catch (Exception ex) {
        lrsResponse.setErrMsg("Exception: " + ex.toString());
        lrsResponse.setSuccess(false);
      }
    } else {
      lrsResponse.setSuccess(false);
    }

    return lrsResponse;
  }
  public static final String toJson(Object object) {
    String json = null;
    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
    try {
      json = ow.writeValueAsString(object);
    } catch (JsonProcessingException e) {
      e.printStackTrace();
    }

    return json;
  }
示例#29
0
 public static String toJson(Object o) {
   if (o == null) {
     return null;
   }
   try {
     return MAPPER.writeValueAsString(o);
   } catch (JsonProcessingException e) {
     logger.error(e.getMessage(), e);
     return null;
   }
 }
 public static String mappingToJson(Map<String, Map<String, Map<String, String>>> mapping)
     throws CloudFormationException {
   try {
     return mapper.writeValueAsString(
         mapping == null
             ? Maps.<String, Map<String, Map<String, String>>>newLinkedHashMap()
             : mapping);
   } catch (JsonProcessingException e) {
     throw new ValidationErrorException(e.getMessage());
   }
 }