This is how I solved this problem in java (but I guess it is the same in scala):
I create a JsonSerializer class to translate a class into a string and then I annote the fields which will be translate into Json with my class.
An exemple to show you how it work for the date:
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.stereotype.Component;
/**
* Used to serialize Java.util.Date, which is not a common JSON
* type, so we have to create a custom serialize method;.
*
* @author Loiane Groner
*/
@Component
public class JsonDateSerializer extends JsonSerializer<Date>{
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
@Override
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
throws IOException, JsonProcessingException {
String formattedDate = dateFormat.format(date);
gen.writeString(formattedDate);
}
}
Then I annote the corresponding field with my class:
public class MyClass
{
@Formats.DateTime(pattern="dd/MM/yyyy")
@JsonSerialize(using=JsonDateSerializer.class)
public Date myDate;
}
Ainsi, lorsque j'utilise mapper.writeValueAsString(lst), j'obtiens des date au format: 08-13-2014
I copied the sources from Loiane Groner.