Exemplo n.º 1
0
  // This only exists to support demarshalling of maps using Jackson. The Jackson payload doesn't
  // contain our
  // EMBEDDED_JSON or any type information, so we have to convert the key (which is always a String)
  // to it's actual
  // type. We only need to support primitive wrapper types and enums as key types. Other types
  // require a custom
  // Key(De)Serializer in Jackson anyway which would be unknown to Errai.
  private Object convertKey(final String toType, final String key, final MarshallingSession ctx) {
    if (key == null) return null;

    Marshaller<?> keyMarshaller = ctx.getMarshallerInstance(toType);
    if (toType.equals(Integer.class.getName())) {
      return Integer.parseInt(key);
    } else if (toType.equals(Long.class.getName())) {
      return Long.parseLong(key);
    } else if (toType.equals(Float.class.getName())) {
      return Float.parseFloat(key);
    } else if (toType.equals(Double.class.getName())) {
      return Double.parseDouble(key);
    } else if (toType.equals(Short.class.getName())) {
      return Short.parseShort(key);
    } else if (toType.equals(Boolean.class.getName())) {
      return Boolean.parseBoolean(key);
    } else if (toType.equals(Date.class.getName())) {
      return new Date(Long.parseLong(key));
    } else if (toType.equals(Character.class.getName())) {
      return new Character(key.charAt(0));
    } else if (toType.equals(String.class.getName())) {
      return key;
    } else if (keyMarshaller != null) {
      EJArray eja = ParserFactory.get().parse("[\"" + key + "\"]").isArray();
      if (eja != null && eja.size() == 1) {
        return keyMarshaller.demarshall(eja.get(0), ctx);
      }
    }
    return key;
  }
Exemplo n.º 2
0
  @Override
  public String marshall(final T o, final MarshallingSession ctx) {
    final StringBuilder buf = new StringBuilder();
    buf.append("{");
    int i = 0;
    for (final Map.Entry<Object, Object> entry : o.entrySet()) {
      if (i++ > 0) {
        buf.append(",");
      }

      final Marshaller<Object> keyMarshaller;
      final Marshaller<Object> valueMarshaller;
      if (entry.getKey() instanceof String) {
        buf.append("\"").append(entry.getKey()).append("\"");
      } else if (entry.getKey() != null) {
        if ((entry.getKey() instanceof Number
                && !(entry.getKey() instanceof BigInteger || entry.getKey() instanceof BigDecimal))
            || entry.getKey() instanceof Boolean
            || entry.getKey() instanceof Character) {
          keyMarshaller = MarshallUtil.getQualifiedNumberMarshaller(entry.getKey());
        } else {
          keyMarshaller = MarshallUtil.getMarshaller(entry.getKey(), ctx);
        }

        buf.append(("\"" + SerializationParts.EMBEDDED_JSON))
            .append(
                MarshallUtil.jsonStringEscape(
                    keyMarshaller.marshall(MarshallUtil.maybeUnwrap(entry.getKey()), ctx)))
            .append("\"");
      } else {
        buf.append("\"" + SerializationParts.NULL_VALUE + "\"");
      }

      buf.append(":");

      if (entry.getValue() == null) {
        buf.append("null");
      } else {
        if ((entry.getValue() instanceof Number
                && !(entry.getValue() instanceof BigInteger
                    || entry.getValue() instanceof BigDecimal))
            || entry.getValue() instanceof Boolean
            || entry.getValue() instanceof Character) {

          valueMarshaller = MarshallUtil.getQualifiedNumberMarshaller(entry.getValue());
        } else {
          valueMarshaller = MarshallUtil.getMarshaller(entry.getValue(), ctx);
        }
        buf.append(valueMarshaller.marshall(MarshallUtil.maybeUnwrap(entry.getValue()), ctx));
      }
    }

    return buf.append("}").toString();
  }