コード例 #1
0
ファイル: DefaultFailover.java プロジェクト: nimeia/moco
 private List<Session> restoreSessions(File file) {
   try {
     return mapper.readValue(file, factory.constructCollectionType(List.class, Session.class));
   } catch (JsonMappingException jme) {
     logger.error("exception found", jme);
     return newArrayList();
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
コード例 #2
0
ファイル: JsonRpcServer.java プロジェクト: sdhjob/jsonrpc4j
  /**
   * Invokes the given method on the {@code handler} passing the given params (after converting them
   * to beans\objects) to it.
   *
   * @param m the method to invoke
   * @param params the params to pass to the method
   * @return the return value (or null if no return)
   * @throws IOException on error
   * @throws IllegalAccessException on error
   * @throws InvocationTargetException on error
   */
  protected JsonNode invoke(Method m, List<JsonNode> params)
      throws IOException, IllegalAccessException, InvocationTargetException {

    // debug log
    if (LOGGER.isLoggable(Level.FINE)) {
      LOGGER.log(Level.FINE, "Invoking method: " + m.getName());
    }

    // convert the parameters
    Object[] convertedParams = new Object[params.size()];
    Type[] parameterTypes = m.getGenericParameterTypes();

    for (int i = 0; i < parameterTypes.length; i++) {
      JsonParser paramJsonParser = mapper.treeAsTokens(params.get(i));
      JavaType paramJavaType = TypeFactory.defaultInstance().constructType(parameterTypes[i]);
      convertedParams[i] = mapper.readValue(paramJsonParser, paramJavaType);
    }

    // invoke the method
    Object result = m.invoke(handler, convertedParams);
    Type genericReturnType = m.getGenericReturnType();
    if (genericReturnType != null) {
      if (Collection.class.isInstance(result) && genericReturnType instanceof ParameterizedType) {
        try {
          if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.log(Level.FINE, "attempting custom collection serialization");
          }
          TypeFactory typeFactory = mapper.getTypeFactory();
          Type actualTypeInCollection =
              ((ParameterizedType) genericReturnType).getActualTypeArguments()[0];
          if (actualTypeInCollection
              instanceof TypeVariable) { // collection actually has a generic return type
            actualTypeInCollection = ((TypeVariable) actualTypeInCollection).getBounds()[0];
          }
          JavaType rootType =
              typeFactory.constructCollectionType(
                  Collection.class, typeFactory.constructType(actualTypeInCollection));
          return valueToTree(mapper.writerWithType(rootType), result);
        } catch (Exception e) {
          LOGGER.log(
              Level.WARNING,
              "could not do custom collection serialization falling back to default",
              e);
        }
      }
      return mapper.valueToTree(result);
    } else {
      return null;
    }
    // return (genericReturnType!=null) ? mapper.valueToTree(result) : null;
  }
コード例 #3
0
ファイル: JsonReader.java プロジェクト: cmongis/imagejfx
  /**
   * Parse Json File and create list of {@link WidgetGroup}.
   *
   * @param path
   */
  public void read(String path) {

    ObjectMapper mapper = new ObjectMapper();

    try {
      // JSON from file to Object
      TypeFactory typeFactory = TypeFactory.defaultInstance();
      String json = TextFileUtils.readFileFromJar(path);
      widgetGroupList =
          mapper.readValue(
              json, typeFactory.constructCollectionType(ArrayList.class, DefaultWidgetGroup.class));
    } catch (IOException ex) {
      Logger.getLogger(JsonReader.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
コード例 #4
0
 @Override
 public void process(Exchange exchange) throws Exception {
   String message = exchange.getIn().getBody(String.class);
   ObjectMapper objectMapper = new ObjectMapper();
   TypeFactory typeFactory = objectMapper.getTypeFactory();
   List<Double> values =
       objectMapper.readValue(
           message, typeFactory.constructCollectionType(List.class, Double.class));
   SummaryStatistics summaryStatistics = new SummaryStatistics();
   List<Double> list = new ObjectMapper().readValue(message, List.class);
   for (Double value : list) {
     summaryStatistics.addValue(value);
   }
   String variance = Double.toString(summaryStatistics.getVariance());
   exchange.getOut().setBody(variance);
 }
コード例 #5
0
 public <T> List<T> readValueAsObjectList(String h, Class clazz) {
   List<T> list = null;
   try {
     TypeFactory t = TypeFactory.defaultInstance();
     list =
         Utils.instance
             .getObjectMapper()
             .readValue(h, t.constructCollectionType(ArrayList.class, clazz));
     return list;
   } catch (JsonProcessingException e) {
     e.printStackTrace();
     return list;
   } catch (IOException e) {
     e.printStackTrace();
     return list;
   }
 }
 @SuppressWarnings("rawtypes")
 public static JavaType buildCollectionType(
     final Class<? extends Collection> collection, final Class<?> element) {
   return typeFactory.constructCollectionType(collection, element);
 }