Created
July 19, 2012 05:51
-
-
Save iron9light/3141040 to your computer and use it in GitHub Desktop.
lift json serializing mixed type array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment