Exemple #1
0
 /**
  * Returns a JSON object (as {@link Map}<String, Object>) of the wrapped erlang value.
  *
  * @return the converted value
  * @throws ClassCastException if thrown if a conversion is not possible, i.e. the type is not
  *     supported
  */
 public Map<String, Object> jsonValue() throws ClassCastException {
   /*
    * object(): {struct, [{key::string() | atom(), value()}]}
    * array():  {array, [value()]}
    * value():  number(), string(), object(), array(), 'true', 'false', 'null'
    *
    * first term must be an object!
    */
   final OtpErlangTuple value_tpl = (OtpErlangTuple) value;
   if ((value_tpl.arity() == 2) && value_tpl.elementAt(0).equals(CommonErlangObjects.structAtom)) {
     final ErlangValueJSONToMap json_converter = new ErlangValueJSONToMap();
     return json_converter.toJava((OtpErlangList) value_tpl.elementAt(1));
   } else {
     throw new ClassCastException("wrong tuple arity");
   }
 }
Exemple #2
0
 /**
  * Converts a (supported) Java type to an {@link OtpErlangObject}.
  *
  * @param <T> the type of the value
  * @param value the value to convert to an erlang type
  * @return the converted value
  * @throws ClassCastException if thrown if a conversion is not possible, i.e. the type is not
  *     supported
  */
 public static <T> OtpErlangObject convertToErlang(final T value) throws ClassCastException {
   if (value instanceof Boolean) {
     return new OtpErlangBoolean((Boolean) value);
   } else if (value instanceof Integer) {
     return new OtpErlangLong((Integer) value);
   } else if (value instanceof Long) {
     return new OtpErlangLong((Long) value);
   } else if (value instanceof BigInteger) {
     return new OtpErlangLong((BigInteger) value);
   } else if (value instanceof Double) {
     return new OtpErlangDouble((Double) value);
   } else if (value instanceof String) {
     return new OtpErlangString((String) value);
   } else if (value instanceof byte[]) {
     return new OtpErlangBinary((byte[]) value);
   } else if (value instanceof List<?>) {
     final List<?> list = (List<?>) value;
     final int listSize = list.size();
     final OtpErlangObject[] erlValue = new OtpErlangObject[listSize];
     int i = 0;
     // TODO: optimise for specific lists?
     for (final Object iter : list) {
       erlValue[i] = convertToErlang(iter);
       ++i;
     }
     return new OtpErlangList(erlValue);
   } else if (value instanceof Map<?, ?>) {
     // map to JSON object notation of Scalaris
     @SuppressWarnings("unchecked")
     final Map<String, Object> map = (Map<String, Object>) value;
     final ErlangValueJSONToMap json_converter = new ErlangValueJSONToMap();
     return json_converter.toScalarisJSON(map);
   } else if (value instanceof ErlangValue) {
     return ((ErlangValue) value).value();
   } else if (value instanceof OtpErlangObject) {
     return (OtpErlangObject) value;
   } else {
     // map to JSON object notation of Scalaris
     @SuppressWarnings("unchecked")
     final ErlangValueJSONToBean<T> json_converter =
         new ErlangValueJSONToBean<T>((Class<T>) value.getClass());
     return json_converter.toScalarisJSON(value);
     //            throw new ClassCastException("Unsupported type (value: " + value.toString() +
     // ")");
   }
 }