Ejemplo n.º 1
0
  @Override
  public Object apply(WarpScriptStack stack) throws WarpScriptException {
    Object o = stack.pop();

    JsonSerializer parser = BOON_SERIALIZER_FACTORY.create();

    //
    // Only allow the serialization of simple lists and maps, otherwise JSON might
    // expose internals
    //

    if (o instanceof List) {
      for (Object elt : (List) o) {
        if (!(elt instanceof Number) && !(elt instanceof String) && !(elt instanceof Boolean)) {
          throw new WarpScriptException(
              getName() + " can only handle numeric, boolean and string types.");
        }
      }
    } else if (o instanceof Map) {
      for (Entry<Object, Object> entry : ((Map<Object, Object>) o).entrySet()) {
        Object elt = entry.getKey();
        if (!(elt instanceof Number) && !(elt instanceof String) && !(elt instanceof Boolean)) {
          throw new WarpScriptException(
              getName() + " can only handle numeric, boolean and string types.");
        }
        elt = entry.getValue();
        if (!(elt instanceof Number) && !(elt instanceof String) && !(elt instanceof Boolean)) {
          throw new WarpScriptException(
              getName() + " can only handle numeric, boolean and string types.");
        }
      }
    } else {
      throw new WarpScriptException(getName() + " can only handle simple lists/maps.");
    }

    String json = parser.serialize(o).toString();

    stack.push(json);

    return stack;
  }
 @Override
 public Object serialize(Object data) {
   return serializer.serialize(data);
 }