Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's one solution with <a href="https://github.com/lift/lift/tree/master/framework/lift-base/lift-json" rel="nofollow">Lift JSON</a>.</p> <pre><code>import java.util.UUID import net.liftweb.json._ import net.liftweb.json.JsonAST._ import net.liftweb.json.JsonDSL._ import net.liftweb.json.Serialization._ sealed abstract class Parent { def uuid: UUID } case class Foo(uuid: UUID, name: String) extends Parent object UUIDTest extends Application { implicit val formats = Serialization.formats(NoTypeHints) + new UUIDSerializer val f = Foo(UUID.randomUUID, "foo") val ser = write(f) println(ser) val f2 = read[Foo](ser) assert(f == f2) // Special serializer for UUID type class UUIDSerializer extends Serializer[UUID] { private val Class = classOf[UUID] def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), UUID] = { case (TypeInfo(Class, _), json) =&gt; json match { case JObject(JField("mostSig", JInt(m)) :: JField("leastSig", JInt(l)) :: Nil) =&gt; new UUID(m.longValue, l.longValue) case x =&gt; throw new MappingException("Can't convert " + x + " to UUID") } } def serialize(implicit format: Formats): PartialFunction[Any, JValue] = { case x: UUID =&gt; ("mostSig" -&gt; x.getMostSignificantBits) ~ ("leastSig" -&gt; x.getLeastSignificantBits) } } } </code></pre> <p>It prints:</p> <pre><code>{"uuid":{"mostSig":-8054689529719995935,"leastSig":-5722404370736228056},"name":"foo"}' </code></pre> <p>Another solution which uses a custom serializer for Parent type.</p> <pre><code>sealed abstract class Parent { var uuid: UUID = UUID.randomUUID } case class Foo(name: String) extends Parent object UUIDTest extends Application { implicit val formats = Serialization.formats(NoTypeHints) + new ParentSerializer val f = Foo("foo") val ser = write(f) println(ser) val f2 = read[Foo](ser) assert(f == f2) // Special serializer for Parent type class ParentSerializer extends Serializer[Parent] { def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), Parent] = { case (t@TypeInfo(cl, _), json) if (classOf[Parent].isAssignableFrom(cl)) =&gt; val x = Extraction.extract(json, t)(DefaultFormats).asInstanceOf[Parent] x.uuid = (for { JField("mostSig", JInt(m)) &lt;- json JField("leastSig", JInt(l)) &lt;- json } yield new UUID(m.longValue, l.longValue)).head x } def serialize(implicit format: Formats): PartialFunction[Any, JValue] = { case x: Parent =&gt; Extraction.decompose(x)(DefaultFormats) ++ JField("mostSig", x.uuid.getMostSignificantBits) ++ JField("leastSig", x.uuid.getLeastSignificantBits) } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload