SORU
16 Temmuz 2010, Cuma


Nasıl Jackson JSON işlemciye sahip Joda DateTime seri hale getirmek için?

Nasıl Jackson benim Joda seri hale getirmek için DateTime basit bir örneğe göre nesne muyum ("dd-MM-yyyy" gibi mi?

Denedim:

@JsonSerialize(using=DateTimeSerializer.class)
private final DateTime date;

Denedim:

ObjectMapper mapper = new ObjectMapper()
    .getSerializationConfig()
    .setDateFormat(df);

Teşekkürler!

CEVAP
17 Temmuz 2010, CUMARTESİ


Nesne eşleme:

@JsonSerialize(using = CustomDateSerializer.class)
public DateTime getDate() { ... }

CustomDateSerializer

public class CustomDateSerializer extends JsonSerializer<DateTime> {

    private static DateTimeFormatter formatter = 
        DateTimeFormat.forPattern("dd-MM-yyyy");

    @Override
    public void serialize(DateTime value, JsonGenerator gen, 
                          SerializerProvider arg2)
        throws IOException, JsonProcessingException {

        gen.writeString(formatter.print(value));
    }
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Ralph Phillips

    Ralph Philli

    5 Aralık 2006
  • SalGames2016

    SalGames2016

    17 Kasım 2012
  • UnboxTechnology

    UnboxTechnol

    20 Ocak 2011