2

I am using the Jackson library to map POJO to XML. While serializing the OffsetDateTime field I am getting the output in multiple tags enclosed within the parent tag.

compile ('com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.0')

When trying to serialize the OffsetDateTime field, which is given below,

 @JacksonXmlProperty(localName = "InvoiceStatusDate")
 private OffsetDateTime invoiceStatusDate;

I am getting the parsed XML as

<InvoiceStatusDate>
   <offset>
      <totalSeconds>19800</totalSeconds>
      <id>+05:30</id>
      <rules>
         <fixedOffset>true</fixedOffset>
         <transitions />
         <transitionRules />
      </rules>
   </offset>
   <hour>14</hour>
   <minute>48</minute>
   <second>43</second>
   <nano>988195000</nano>
   <monthValue>9</monthValue>
   <year>2019</year>
   <month>SEPTEMBER</month>
   <dayOfMonth>4</dayOfMonth>
   <dayOfWeek>WEDNESDAY</dayOfWeek>
   <dayOfYear>247</dayOfYear>
</InvoiceStatusDate>

But I need something like

 <InvoiceStatusDate>2019-09-05T15:08:53.549+05:30</InvoiceStatusDate>
Gowtham
  • 73
  • 7

2 Answers2

0

This is due to the structure of the OffsetDateTime Type.

public final class OffsetDateTime implements Temporal, TemporalAdjuster, Comparable<OffsetDateTime>, Serializable {
    public static final OffsetDateTime MIN;
    public static final OffsetDateTime MAX;
    private static final long serialVersionUID = 2287754244819255394L;
    private final LocalDateTime dateTime;
    private final ZoneOffset offset;
    /*
      ....
    */
}

It has a LocalDateTime and ZoneOffset as attribute.

Same for LocalDateTime :

public final class LocalDateTime implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable {
        public static final LocalDateTime MIN;
        public static final LocalDateTime MAX;
        private static final long serialVersionUID = 6207766400415563566L;
        private final LocalDate date;
        private final LocalTime time;
        /*
         ...
        */
}

I will let you check ZoneOffset structure. Basically jackson is serializing the object as it is(e.g getting value of property and creating an xml element for it).

What you could do is to modify how your property is rendered

Instead of :

@JacksonXmlProperty(localName = "InvoiceStatusDate")
 private OffsetDateTime invoiceStatusDate;

Do

Change how you property getter would process this field during the serializing process

@JacksonXmlProperty(localName = "InvoiceStatusDate")
public LocalDateTime getInvoceStatusDate() {
    return formatInvoiceDate();
}

private LocalDateTime formatInvoiceDate() {
 // do processing logic here
}

OR

If there is no requirement to keep the InvoiceStatusDate as OffsetDate time keep it as a LocalDateTime

Hassam Abdelillah
  • 2,246
  • 3
  • 16
  • 37
0

There is a similar issue while parsing Dates from java.time to JSON using Jackson library. Basically you need to provide Format information:

@JacksonXmlProperty(localName = "InvoiceStatusDate")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
    public ZonedDateTime getInvoiceStatusDate() {
        return invoiceStatusDate;
    }

For more info look at those two questions: Spring Data JPA - ZonedDateTime format for json serialization and Jackson deserialize date from Twitter to `ZonedDateTime`

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36