-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63ecbb8
commit 1b89f30
Showing
12 changed files
with
798 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...a/ru/olegcherednik/json/jackson/datetime/serializers/key/JacksonInstantKeySerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package ru.olegcherednik.json.jackson.datetime.serializers.key; | ||
|
||
import ru.olegcherednik.json.api.ZoneModifier; | ||
|
||
import java.time.Instant; | ||
import java.time.ZoneId; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.function.UnaryOperator; | ||
|
||
/** | ||
* @author Oleg Cherednik | ||
* @since 25.12.2023 | ||
*/ | ||
public class JacksonInstantKeySerializer extends JacksonKeySerializer<Instant> { | ||
|
||
private static final long serialVersionUID = 154759679223502525L; | ||
|
||
public JacksonInstantKeySerializer(DateTimeFormatter df) { | ||
super(Instant.class, df, ZoneModifier.USE_ORIGINAL, | ||
Instant::toEpochMilli, Instant::getEpochSecond, Instant::getNano); | ||
} | ||
|
||
} |
123 changes: 123 additions & 0 deletions
123
...ain/java/ru/olegcherednik/json/jackson/datetime/serializers/key/JacksonKeySerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package ru.olegcherednik.json.jackson.datetime.serializers.key; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
import com.fasterxml.jackson.datatype.jsr310.DecimalUtils; | ||
import ru.olegcherednik.json.api.JsonSettings; | ||
|
||
import java.io.IOException; | ||
import java.time.ZoneId; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.temporal.Temporal; | ||
import java.util.function.ToIntFunction; | ||
import java.util.function.ToLongFunction; | ||
import java.util.function.UnaryOperator; | ||
|
||
/** | ||
* @author Oleg Cherednik | ||
* @since 25.12.2023 | ||
*/ | ||
public abstract class JacksonKeySerializer<T extends Temporal> extends StdSerializer<T> { | ||
|
||
private static final long serialVersionUID = 2020854544464342989L; | ||
|
||
protected final DateTimeFormatter df; | ||
protected final UnaryOperator<ZoneId> zoneModifier; | ||
protected final ToLongFunction<T> getEpochMillis; | ||
protected final ToLongFunction<T> getEpochSeconds; | ||
protected final ToIntFunction<T> getNanoseconds; | ||
|
||
protected JacksonKeySerializer(Class<T> supportedType, | ||
DateTimeFormatter df, | ||
UnaryOperator<ZoneId> zoneModifier, | ||
ToLongFunction<T> getEpochMillis, | ||
ToLongFunction<T> getEpochSeconds, | ||
ToIntFunction<T> getNanoseconds) { | ||
super(supportedType); | ||
this.df = df; | ||
this.zoneModifier = zoneModifier; | ||
this.getEpochMillis = getEpochMillis; | ||
this.getEpochSeconds = getEpochSeconds; | ||
this.getNanoseconds = getNanoseconds; | ||
} | ||
|
||
@Override | ||
public void serialize(T value, JsonGenerator gen, SerializerProvider provider) throws IOException { | ||
String fieldName = useTimestamp(provider) ? getTimestampFieldName(value, gen, provider) | ||
: getStringFieldName(value, gen, provider); | ||
|
||
gen.writeFieldName(fieldName); | ||
} | ||
|
||
protected String getTimestampFieldName(T value, JsonGenerator gen, SerializerProvider provider) { | ||
if (useNanoseconds(provider)) { | ||
long seconds = getEpochSeconds.applyAsLong(value); | ||
int nanoseconds = getNanoseconds.applyAsInt(value); | ||
return String.valueOf(DecimalUtils.toBigDecimal(seconds, nanoseconds)); | ||
} | ||
|
||
return String.valueOf(getEpochMillis.applyAsLong(value)); | ||
} | ||
|
||
protected String getStringFieldName(T value, JsonGenerator gen, SerializerProvider provider) { | ||
if (df == null) | ||
return value.toString(); | ||
|
||
ZoneId zoneId = getZoneId(value, provider); | ||
zoneId = zoneModifier.apply(zoneId); | ||
|
||
return zoneId == null ? df.format(value) : df.withZone(zoneId).format(value); | ||
} | ||
|
||
protected ZoneId getZoneId(T value, SerializerProvider provider) { | ||
ZoneId zoneId = df.getZone(); | ||
zoneId = zoneId == null ? getContextZoneId(provider) : zoneId; | ||
return zoneId; | ||
} | ||
|
||
protected ZoneId getContextZoneId(SerializerProvider provider) { | ||
if (!useContextTimeZone(provider)) | ||
return null; | ||
if (!provider.getConfig().hasExplicitTimeZone()) | ||
return null; | ||
return provider.getTimeZone().toZoneId(); | ||
} | ||
|
||
protected boolean useContextTimeZone(SerializerProvider provider) { | ||
return isEnabled(provider, SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE); | ||
} | ||
|
||
protected boolean useTimestamp(SerializerProvider provider) { | ||
return df == null && isEnabled(provider, SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); | ||
} | ||
|
||
protected boolean useNanoseconds(SerializerProvider provider) { | ||
return isEnabled(provider, SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS); | ||
} | ||
|
||
protected static boolean isEnabled(SerializerProvider provider, SerializationFeature feature) { | ||
return provider.isEnabled(feature); | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
src/test/java/ru/olegcherednik/json/jackson/LocalTimeZone.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package ru.olegcherednik.json.jackson; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import sun.util.calendar.ZoneInfo; | ||
|
||
import java.time.ZoneId; | ||
import java.util.TimeZone; | ||
|
||
/** | ||
* @author Oleg Cherednik | ||
* @since 30.12.2023 | ||
*/ | ||
@SuppressWarnings("PMD.ClassNamingConventions") | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public final class LocalTimeZone { | ||
|
||
public static final TimeZone ASIA_SINGAPORE = ZoneInfo.getTimeZone(LocalZoneId.ASIA_SINGAPORE); | ||
public static final TimeZone AUSTRALIA_SYDNEY = ZoneInfo.getTimeZone(LocalZoneId.AUSTRALIA_SYDNEY); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.