玩Scala无法从Json读取和写入日期

时间:2015-09-09 16:29:22

标签: json scala playframework reactivemongo

无法使用Scala在Play中阅读日期

在Mongo,日期记录是:“dateAdded”:ISODate(“2015-09-09T04:33:20.740Z”)

object JsonSmightyFormats {
    import play.api.libs.json.Json
    import play.api.libs.json._
    import play.api.libs.json.Format
    import play.api.libs.json.Reads
    import play.api.libs.json.Writes
    import play.api.libs.functional.syntax._
    import org.joda.time.format.ISODateTimeFormat
    import org.joda.time.{LocalDate, DateTimeZone, DateTime, LocalDateTime}
    // import org.joda.time.DateTime
    // import play.api.libs.json.Format
    // import play.api.libs.json.Reads
    // import play.api.libs.json.Writes
    import org.joda.time.format.DateTimeFormat
    import org.joda.time.format.DateTimeFormatterBuilder

   // val dateFormat = "yyyy-MM-dd'T'HH:mm:ssz"
   //val dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
   val dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
    private val dateFmt = ISODateTimeFormat.dateTime()  
   val jodaDateReads = Reads[DateTime] {
   //method 1 tried:
    js =>
         js.validate[String].map[DateTime](dtString =>
         DateTime.parse(dtString,    DateTimeFormat.forPattern(dateFormat)).toDateTime(DateTimeZone.UTC)
        )
    //method 2 tried    

    js =>
         js.validate[Long].map[DateTime]{ dateTime => 
      new DateTime(dateTime)}


    //  (__ \ "$date").read[Long].map { dateTime => 
    //  new DateTime(dateTime) 

    }

    val jodaDateWrites: Writes[DateTime] = new Writes[DateTime] {
    //for write tried both these methods
    //  def writes(d: DateTime): JsValue = JsString(d.toString())
        def writes(dateTime: DateTime): JsValue = Json.obj( 
      "$date" -> dateTime.getMillis) 
    }


val testReads: Reads[Test] = (      
        (JsPath \ "dateAdded").read[DateTime](jodaDateReads)  
    )(test.apply _)

    val testWrites: Writes[Test] = (

        (JsPath \ "dateAdded").write[DateTime](jodaDateWrites) and 

    )(unlift(test.unapply))

任何帮助将不胜感激。我已经阅读了所有网站,并尝试了所有提到但没有任何作用。

在java方面我已成功使用,但同样的东西在scala方面不起作用:

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
public DateTime deserialize(JsonParser jsonparser,
        DeserializationContext deserializationcontext) throws IOException, JsonProcessingException {

    System.out.println("=================inside CustomJsonDateDeserializer.deserialize jsonparser.getText()=" + jsonparser.getText());  

    //String dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
    String dateFormat = "EEE MMM dd HH:mm:ss z yyyy";

    try {


        return DateTime.parse(jsonparser.getText(), DateTimeFormat.forPattern(dateFormat)).toDateTime(DateTimeZone.UTC);
    }   catch (Exception e) {

        throw new RuntimeException(e);
    }

日志中会显示以下错误: lay.api.Application $$ anon $ 1:执行异常[[RuntimeException:JsError(List((/ dateAdded,List(ValidationError(error.expected.jsstring,WrappedArray())))))]]

0 个答案:

没有答案