CustomSerializer
を使用できます。 あなたはデシリアライザとして空PartialFunction
を提供することにより、唯一のシリアル化をカスタマイズすることができます。
trait Ignore
case class A(a: Int) extends Ignore
case class B(f: String, a: A, d: Int)
implicit val formats = DefaultFormats +
new CustomSerializer[Ignore](formats => (
PartialFunction.empty,
{ case _: Ignore => JNothing }
))
// prints "{"f":"x","d":2}"
println(Serialization.write(B("x", A(1), 2)))
// deserializes to B(x,A(1),2)
println(Serialization.read[B]("""{"f":"x","a":{"a":1},"d":2}"""))
編集:(3.3.0)json4sネイティブを書いている時点では間違ってシリアライズされたオブジェクトをレンダリングバグを持っている最初の場合フィールドは、例えばcase class B(a: A, ...)
(無視される問題は、まずExtraction.decompose
を使用して
を解決し、Serialization
後、問題を解決するように見えるされるまで、私はjson4sジャクソンを使用することをお勧めし:。
trait Ignore
case class A(a: Int) extends Ignore
case class B(a: A, d: Int)
implicit val formats = DefaultFormats +
new CustomSerializer[Ignore](formats => (
PartialFunction.empty,
{ case _: Ignore => JNothing }
))
// prints {,"d":2}
println(Serialization.write(B(A(1), 2)))
// prints {"d":2}
println(Serialization.write(Extraction.decompose(B(A(1), 2))))
はい、これはうまくいきますが、Bのフィールドの先頭にフィールドを置くと、無効なjsonが生成されます。例えば、 'case class B(a:A、d:Int)'は '' {、 "d":2} "'を生成します。 – maks
@maksそれは私のために働く: '{" d ":2}'。あなたは古いjson4sバージョンを使用していますか?私はonです3.3.0 –
@maks [OK]を私はチェックしていないが、私はjson4sジャクソンを使用しているため、私のコードが動作すると思う。ネイティブを使っていますか? –