Grails
  1. Grails
  2. GRAILS-4881

make converters able to parse back strings to dates

    Details

    • Type: Wish Wish
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Won't Fix
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:
      Grails 1.1.1

      Description

      In Grails 1.1.1, if I wish to parse JSON or XML and create a new domain object from it, there is no automatic conversion from java.lang.string to Java.util.Date, unless you use JSON and set the old way of exporting dates on the config such as grails.converters.json.date = 'javascript' .

        Activity

        Hide
        Siegfried Puchbauer added a comment -

        There is no easy way to solve this problem, since scanning every parsed string if it matches the date format wouldn't be acceptable performance-wise. The only way I can think of, is to use a DataBinder that parses the JSON/XML Date Format when creating the domain class instance. See http://stackoverflow.com/questions/963922/grails-date-unmarshalling/964083#964083 for an example on how to register such a DataBinder.

        You should be able to use this way, once the CustomDateBinder is registered:
        def book = new Book(JSON.parse(...))

        Show
        Siegfried Puchbauer added a comment - There is no easy way to solve this problem, since scanning every parsed string if it matches the date format wouldn't be acceptable performance-wise. The only way I can think of, is to use a DataBinder that parses the JSON/XML Date Format when creating the domain class instance. See http://stackoverflow.com/questions/963922/grails-date-unmarshalling/964083#964083 for an example on how to register such a DataBinder. You should be able to use this way, once the CustomDateBinder is registered: def book = new Book(JSON.parse(...))
        Hide
        Martin Fuchs-Lautensack added a comment -

        That is a very helpful pointer, thank you, and certainly solves the issue.

        On the other hand, from my point of view it would be acceptable to have automatic conversion on certain fields only that the programmer has to name. E.g., my date related stuff all has 'date' somehow in its names.

        Show
        Martin Fuchs-Lautensack added a comment - That is a very helpful pointer, thank you, and certainly solves the issue. On the other hand, from my point of view it would be acceptable to have automatic conversion on certain fields only that the programmer has to name. E.g., my date related stuff all has 'date' somehow in its names.
        Hide
        Siegfried Puchbauer added a comment -

        IMO it would make more sense to post-process the converted results by hand. Smth like:

        def result = JSON.parse(...)

        result.each

        { key, value -> if(key.toLowerCase().indexOf("date") != -1) result[key] = new SimpleDateFormat("...").parse(value); }
        Show
        Siegfried Puchbauer added a comment - IMO it would make more sense to post-process the converted results by hand. Smth like: def result = JSON.parse(...) result.each { key, value -> if(key.toLowerCase().indexOf("date") != -1) result[key] = new SimpleDateFormat("...").parse(value); }
        Hide
        Martin Fuchs-Lautensack added a comment -

        Just tried your suggestions. I can't get key value pairs out of the map interface:

        def list = JSON.parse(new FileInputStream("emissions.jsn"), "UTF-8")

        list.each

        { key, value -> ...do stuff }

        groovy.lang.MissingMethodException: No signature of method: DataTransferController$_closure5_closure10.doCall() is applicable for argument types: (org.codehaus.groovy.grails.web.json.JSONObject) values:....

        Also tried it.key, same problem. Something must be broken.
        I know the syntax is correct for maps. If I just replace list with some map of strings it works.

        Show
        Martin Fuchs-Lautensack added a comment - Just tried your suggestions. I can't get key value pairs out of the map interface: def list = JSON.parse(new FileInputStream("emissions.jsn"), "UTF-8") list.each { key, value -> ...do stuff } groovy.lang.MissingMethodException: No signature of method: DataTransferController$_closure5_closure10.doCall() is applicable for argument types: (org.codehaus.groovy.grails.web.json.JSONObject) values:.... Also tried it.key, same problem. Something must be broken. I know the syntax is correct for maps. If I just replace list with some map of strings it works.

          People

          • Assignee:
            Siegfried Puchbauer
            Reporter:
            Martin Fuchs-Lautensack
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development