[ 
https://issues.apache.org/jira/browse/GROOVY-10208?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17406667#comment-17406667
 ] 

A. Lepe commented on GROOVY-10208:
----------------------------------

I found the way to add support to those classes, so I will close this request.

Example:


{code:java}
JsonBuilder jb = new JsonBuilder(object , new JsonGenerator.Options()
                .addConverter(LocalDateTime, { it.format("yyyy-MM-DD HH:mm:ss") 
})
                .addConverter(LocalDate, { it.format("yyyy-MM-DD")})
                .addConverter(LocalTime, { it.format("HH:mm:ss") })
                .build()
{code}


> Add LocalDate/LocalTime/LocalDateTime support in JsonBuilder
> ------------------------------------------------------------
>
>                 Key: GROOVY-10208
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10208
>             Project: Groovy
>          Issue Type: Improvement
>    Affects Versions: 3.0.8
>            Reporter: A. Lepe
>            Priority: Trivial
>              Labels: json, jsonBuilder
>
> 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:
> {code:java}
> // 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]]
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to