Serialize a card that does not have a row as an elevator key

Lift-json seems to be limited to cards that have strings as keys.

What is the best way to get around this limitation?

+5
source share
2 answers

Define your own Serializer[Map[Any, Any]].

import net.liftweb.json._
import ext._

object MapSerializer extends Serializer[Map[Any, Any]] {
  def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
    case m: Map[_, _] => JObject(m.map({
      case (k, v) => JField(
        k match {
          case ks: String => ks
          case ks: Symbol => ks.name
          case ks: Any => ks.toString
        },
        Extraction.decompose(v)
      )
    }).toList)
  }

  def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), Map[Any, Any]] = {
    sys.error("Not interested.")
  }
}

Then add it to the implicit variable Formats.

implicit val formats = DefaultFormats + MapSerializer

It's all.

+5
source

In addition to the previous answer, you can define instead:

def deserialize (implicit format: Formats): PartialFunction [(TypeInfo, JValue), Map [Any, Any]] = {Map ()}

This does not violate any other deserialization of the work card.

0
source

All Articles