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

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

    Details

    • Type: Bug
    • Status: Open
    • Priority: Normal
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: General
    • Labels:
      None

      Description

      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?

        Activity

        Hide
        fse fse added a comment -

        of course, example should be Classes.coerce("1.0", Integer.class)

        Show
        fse fse added a comment - of course, example should be Classes.coerce("1.0", Integer.class)

          People

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

            Dates

            • Created:
              Updated: