Skip to content

Instantly share code, notes, and snippets.

@iron9light
Created July 19, 2012 05:51

Revisions

  1. iron9light created this gist Jul 19, 2012.
    55 changes: 55 additions & 0 deletions MixedTypeArrayJsonSuite.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    package net.liftweb.sandbox

    import org.scalatest.FunSuite
    import net.liftweb.json._
    import org.scalatest.matchers.ShouldMatchers

    /**
    * @author IL
    * @see <a href="https://groups.google.com/forum/?fromgroups#!topic/liftweb/GK5DbzCtWdA">forum</a>
    */
    class MixedTypeArrayJsonSuite extends FunSuite with ShouldMatchers {
    implicit val formats = DefaultFormats + new CustomSerializer[Either[String, Entry]](
    format => ( {
    case JString(s) => Left(s)
    case j => Right(j.extract[Entry](DefaultFormats, manifest[Entry]))
    }, {
    case Left(s: String) => JString(s)
    case Right(e: Entry) => Extraction.decompose(e)(DefaultFormats)
    }
    )
    )

    test("extract mixed type list") {
    val a = parse( """{
    "field1": true,
    "field2": "hi",
    "entries": [
    [
    "blah",
    {
    "fielda": 70615911,
    "fieldb": "4358367001dfbd6",
    }
    ],
    [
    "hello.txt",
    {
    "fielda": 363696459,
    "fieldb": "15ad914b001dfbd6",
    }
    ]
    ]
    }""")

    val obj = a.extract[Root]

    obj should be === Root(true, "hi",
    (Left("blah") :: Right(Entry(70615911, "4358367001dfbd6")) :: Nil) ::
    (Left("hello.txt") :: Right(Entry(363696459, "15ad914b001dfbd6")) :: Nil) :: Nil)
    }
    }

    case class Root(field1: Boolean, field2: String, entries: List[List[Either[String, Entry]]])

    case class Entry(fielda: Int, fieldb: String)