Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10208

Add LocalDate/LocalTime/LocalDateTime support in JsonBuilder

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Trivial
    • Resolution: Fixed
    • 3.0.8
    • None
    • None

    Description

      Json classes in `groovy.json` do not support `LocalDate`, `LocalTime`, `LocalDateTime`, etc classes.

      Classes like `Date`, `Calendar` and `SimpleDateFormat` are used, which (as you know) are no longer recommended. It would be nice to replace those classes with the recommended ones.

      Affected classes:

      • DefaultJsonGenerator
      • JsonGenerator
      • JsonOutput

       

      Example:

      // Setting date and time in JSON:
      
      def map = [
          date : LocalDateTime.now()
      ]
      
      def json = new JsonBuilder(map).toPrettyString()
      println json
      
      // prints:
      
      {
          "date": {
              "dayOfYear": 242,
              "month": "AUGUST",
              "second": 56,
              "hour": 15,
              "nano": 337326000,
              "year": 2021,
              "dayOfMonth": 30,
              "minute": 51,
              "monthValue": 8,
              "dayOfWeek": "MONDAY",
              "chronology": {
                  "calendarType": "iso8601",
                  "id": "ISO"
              }
          }
      }
      
      // Parsing that object with JsonSlurper:
      def importMap = new JsonSlurper().parseText(json)
      
      println importMap.date
      
      // prints:
      
      [dayOfYear:242, month:AUGUST, second:56, hour:15, nano:337326000, year:2021, dayOfMonth:30, minute:51, monthValue:8, dayOfWeek:MONDAY, chronology:[calendarType:iso8601, id:ISO]]
      
      
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            lepe A. Lepe
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: