Uploaded image for project: 'ZK'
  1. ZK
  2. ZK-4088

Classes.coerce() cannot convert String representations of Numbers to Numbers

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Normal Normal
    • None
    • None
    • General
    • None

      e.g. Classes.coerce("1.0", Number.class) will rise a NumberFormatException.

      Our workaround for this issue is as following:

      {{public static Object coerce(final Class<?> clazz, Object val) throws ClassCastException {
      if (val instanceof String && String.class != clazz) {
      try

      { val = Classes.coerce(Double.class, val); }

      catch (NumberFormatException e) {
      if (Boolean.class == clazz)

      { val = toBoolean((String) val); // "true", "on", "yes", "y" -> True }

      else

      { val = null; }

      }
      }
      return Classes.coerce(clazz, val);
      }}}

      It makes conversions much more robust. Could this be a general approach for ZK core?

            Unassigned Unassigned
            fse fse
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: