2014-05-20 3 views
0

문서 케이스 클래스가 있습니다. Json 텍스트에서 직렬화 및 역 직렬화하려면 암시 ​​적 읽기 및 쓰기 개체를 정의했습니다.Play2 Html 객체 직렬화 및 비 직렬화

내 문서 클래스에 Int 및 String 만 있으면 문제가 없습니다. 그러나 Document case 클래스에 Html 타입 값이 있으면 문제가 있습니다.

중첩 직렬화 및 직렬화 해제입니다. Html 용 리더를 만드는 데 문제가 있습니다. Play 2 Html은 사례 클래스가 아닙니다. 그게 문제 야?

는 다음과 같은 코드가 맞다 : 그것은 작동하지 않습니다

implicit object HtmlReads extends play.api.libs.json.Reads[Html] { 
     def reads(json: JsValue) = Html (
      (json \ "text").as[String] 
     ) 
} 

. 어떻게해야합니까? 감사

답변

0

이 나는 ​​자바에서이 문제를 해결하는 방법입니다 (하지만 난 그게 스칼라에서 같은 추측) : 내가 문자열로 클래스를 번역하기 위해 JsonSerializer 클래스를 만든 다음 내가 번역 될 필드를 annote 내 수업과 함께 Json에.

는 날짜를 작동하는 방법을 보여하려는 exemple : (

public class MyClass 
{ 
    @Formats.DateTime(pattern="dd/MM/yyyy") 
    @JsonSerialize(using=JsonDateSerializer.class) 
    public Date  myDate; 
} 

Ainsi, lorsque의 j'utilise의 mapper.writeValueAsString :

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); 
    } 
} 

가 그럼 난 내 클래스와 해당 필드를 annote lst), j'obtiens des date au 형식 : 08-13-2014

소스를 Loiane Groner에서 복사했습니다.

관련 문제