package com.nedap.archie.json;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.nedap.archie.adlparser.treewalkers.TemporalConstraintParser;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAccessor;

/**
 * Created by pieter.bos on 30/09/16.
 */
public class DateTimeSerializer extends JsonSerializer<TemporalAccessor> {

    @Override
    public void serialize(TemporalAccessor temporalAccessor, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        if(temporalAccessor == null) {
            jsonGenerator.writeString("");
        }
        jsonGenerator.writeString(TemporalConstraintParser.ISO_8601_DATE_TIME.format(temporalAccessor));
    }

}

@@ -435,32 +435,164 @@
 .ZonedDateTime;%0A
+import java.time.format.DateTimeFormatter;%0Aimport java.time.format.DateTimeFormatterBuilder;%0Aimport java.time.temporal.ChronoField;%0A
 import java.time
@@ -742,16 +742,1819 @@
 sor%3E %7B%0A%0A
+    private static final DateTimeFormatter ISO_8601_DATE_TIME = new DateTimeFormatterBuilder()%0A        .parseCaseInsensitive()%0A        .appendValue(ChronoField.YEAR)%0A        .appendLiteral('-')%0A        .appendValue(ChronoField.MONTH_OF_YEAR, 2)%0A        .appendLiteral('-')%0A        .appendValue(ChronoField.DAY_OF_MONTH, 2)%0A        .appendLiteral('T')%0A        .appendValue(ChronoField.HOUR_OF_DAY, 2)%0A        .optionalStart()%0A            .appendLiteral(':')%0A            .appendValue(ChronoField.MINUTE_OF_HOUR, 2)%0A            .optionalStart()%0A                .appendLiteral(':')%0A                .appendValue(ChronoField.SECOND_OF_MINUTE, 2)%0A                .optionalStart()%0A                    .appendLiteral(',')%0A                    .appendFraction(ChronoField.MICRO_OF_SECOND, 1, 6, false)%0A                .optionalEnd()%0A            .optionalEnd()%0A        .optionalEnd()%0A        .optionalStart()%0A            .appendOffset(%22+HHMM%22, %22Z%22)%0A        .optionalEnd()%0A        .toFormatter();%0A%0A    public static final DateTimeFormatter ISO_8601_DATE_TIME_WITHOUT_MICROS = new DateTimeFormatterBuilder()%0A        .parseCaseInsensitive()%0A        .appendValue(ChronoField.YEAR)%0A        .appendLiteral('-')%0A        .appendValue(ChronoField.MONTH_OF_YEAR, 2)%0A        .appendLiteral('-')%0A        .appendValue(ChronoField.DAY_OF_MONTH, 2)%0A        .appendLiteral('T')%0A        .appendValue(ChronoField.HOUR_OF_DAY, 2)%0A        .optionalStart()%0A            .appendLiteral(':')%0A            .appendValue(ChronoField.MINUTE_OF_HOUR, 2)%0A            .optionalStart()%0A                .appendLiteral(':')%0A                .appendValue(ChronoField.SECOND_OF_MINUTE, 2)%0A            .optionalEnd()%0A        .optionalEnd()%0A        .optionalStart()%0A            .appendOffset(%22+HHMM%22, %22Z%22)%0A        .optionalEnd()%0A        .toFormatter();%0A%0A%0A
     @Ove
@@ -2826,32 +2826,268 @@
       %7D%0A        
+if(temporalAccessor.isSupported(ChronoField.MICRO_OF_SECOND) && temporalAccessor.get(ChronoField.MICRO_OF_SECOND) != 0l) %7B%0A            jsonGenerator.writeString(ISO_8601_DATE_TIME.format(temporalAccessor));%0A        %7D else %7B%0A            
 jsonGenerator.wr
@@ -3100,33 +3100,8 @@
 ing(
-TemporalConstraintParser.
 ISO_
@@ -3106,32 +3106,47 @@
 O_8601_DATE_TIME
+_WITHOUT_MICROS
 .format(temporal
@@ -3153,21 +3153,31 @@
 Accessor));%0A
+        %7D%0A
     %7D%0A%0A%7D%0A

Fix datetime JSON serialization