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

  • jkimisyellow...ish

    jkimisyellow

    8 Temmuz 2009
  • Paulo Bautista

    Paulo Bautis

    21 Aralık 2008
  • Titan Lee Hai

    Titan Lee Ha

    14 Temmuz 2008