@Override
 public Object deserialize(JsonNode n, ObjectMapper mapper) {
   List list = new ArrayList<>();
   for (int i = 0; i < n.size(); i++) {
     try {
       list.add(parent.deserialize(n.get(i), mapper));
     } catch (Exception e) {
       Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, e);
     }
   }
   Collection collection = PersistentVector.create(list);
   return collection;
 }
    @Override
    public Object deserialize(JsonNode n, ObjectMapper mapper) {
      SimpleEvaluationObject o = null;
      try {
        String expression = null, message = null;
        Object status = null, payload = null;
        Integer progressBar = null;

        if (n.has("expression")) expression = n.get("expression").asText();
        if (n.has("message")) message = n.get("message").asText();
        if (n.has("progressBar")) progressBar = n.get("progressBar").asInt();
        if (n.has("payload")) payload = parent.deserialize(n.get("payload"), mapper);

        o = new SimpleEvaluationObject(expression);
        o.message = message;
        o.status = (EvaluationStatus) status;
        o.progressBar = progressBar;
        o.payload = (EvaluationResult) payload;

        if (n.has("outputdata")) {
          JsonNode ne = n.get("outputdata");
          for (JsonNode d : ne) {
            if (!d.has("type") || !d.has("value")) continue;
            if (d.get("type").asText().equals("out")) {
              EvaluationStdOutput e = o.new EvaluationStdOutput(d.get("value").asText());
              o.outputdata.add(e);
            } else {
              EvaluationStdError e = o.new EvaluationStdError(d.get("value").asText());
              o.outputdata.add(e);
            }
          }
        }

      } catch (Exception e) {
        logger.error("exception deserializing SimpleEvaluationObject ", e);
      }
      return o;
    }
Exemple #3
0
  /**
   * kdb result conversions to standard beaker types.
   *
   * <p>TODO it would be better if this was handled by customizing a serializer module, but I don't
   * see a clean way of doing that.
   */
  private Object convert(Object o) {
    // Convert flips of simple lists into TableDisplays.
    if (c.t(o) == 98)
      to_tabledisplay:
      {
        Flip f = (Flip) o;

        // Make sure each column is an array and a type we can handle.
        List<String> columns = Arrays.asList(f.x);
        List<String> classes = new ArrayList<>();
        List<List<?>> colLists = new ArrayList<>();
        for (Object c : f.y) {
          List<?> values = toList(c);
          if (values == null) {
            break to_tabledisplay;
          }
          String type =
              objConv.convertType(
                  ClassUtils.primitiveToWrapper(c.getClass().getComponentType()).getName());
          if (type == null) {
            break to_tabledisplay;
          }

          // Special case - convert Dates to nanosecond times.
          if (BasicObjectSerializer.TYPE_TIME.equals(type)) {
            List<Long> timestamps = new ArrayList<>(values.size());
            for (Object d : values) {
              timestamps.add(
                  TimeUnit.NANOSECONDS.convert(((Date) d).getTime(), TimeUnit.MILLISECONDS));
            }
            values = timestamps;
          }

          classes.add(type);
          colLists.add(values);
        }

        // Columns to rows.
        int rows = colLists.get(0).size();
        List<List<?>> values = new ArrayList<>();
        for (int row = 0; row < rows; ++row) {
          List<Object> rowValues = new ArrayList<>();
          for (List<?> col : colLists) {
            rowValues.add(col.get(row));
          }
          values.add(rowValues);
        }

        return new TableDisplay(values, columns, classes);
      }

    // Convert Dicts to maps.
    if (c.t(o) == 99)
      to_map:
      {
        Dict f = (Dict) o;

        // Special case - keyed tables are dicts of flips.
        if ((c.t(f.x) == 98) && (c.t(f.y) == 98))
          to_table:
          {
            Flip keys = (Flip) f.x;
            Flip cols = (Flip) f.y;
            return convert(
                new Flip(
                    new Dict(
                        ArrayUtils.addAll(keys.x, cols.x), ArrayUtils.addAll(keys.y, cols.y))));
          }

        List<?> keys = toList(f.x);
        if (keys == null) break to_map;
        List<?> values = toList(f.y);
        if (values == null) break to_map;
        Map<Object, Object> map = new HashMap<>();
        for (int i = 0; i < values.size(); ++i) {
          map.put(keys.get(i), values.get(i));
        }
        return map;
      }

    // No conversion.
    return o;
  }
 public DeSerializer(BeakerObjectConverter p) {
   parent = p;
   parent.addKnownBeakerType("SimpleEvaluationObject");
 }