35 lines
1.3 KiB
Java
35 lines
1.3 KiB
Java
/*
|
|
* Copyright 2025 Google. This software is provided as-is, without warranty or representation for any use or purpose.
|
|
* Your use of it is subject to your agreement with Google.
|
|
*/
|
|
|
|
package com.example.util;
|
|
|
|
import com.fasterxml.jackson.core.JsonGenerator;
|
|
import com.fasterxml.jackson.databind.JsonSerializer;
|
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
|
import com.google.cloud.Timestamp;
|
|
import java.io.IOException;
|
|
|
|
/**
|
|
* Custom Jackson Serializer for com.google.cloud.Timestamp.
|
|
* This is crucial for ObjectMapper.convertValue to correctly handle Timestamp objects
|
|
* when they are encountered in a Map<String, Object> and need to be internally
|
|
* serialized before deserialization into a DTO. It converts Timestamp into a
|
|
* simple JSON object with "seconds" and "nanos" fields.
|
|
*/
|
|
public class FirestoreTimestampSerializer extends JsonSerializer<Timestamp> {
|
|
|
|
@Override
|
|
public void serialize(Timestamp value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
|
if (value == null) {
|
|
gen.writeNull();
|
|
} else {
|
|
// Write Timestamp as a JSON object with seconds and nanos
|
|
gen.writeStartObject();
|
|
gen.writeNumberField("seconds", value.getSeconds());
|
|
gen.writeNumberField("nanos", value.getNanos());
|
|
gen.writeEndObject();
|
|
}
|
|
}
|
|
} |