<commit_before>package net.liftweb.json.scalaz

import scalaz._
import Scalaz._
import net.liftweb.json._
import net.liftweb.json.JsonAST._

import org.specs.Specification
import org.specs.runner.{Runner, JUnit}

class ExampleTest extends Runner(Example) with JUnit
object Example extends Specification {
  import JsonScalaz._

  case class Address(street: String, zipCode: String)
  case class Person(name: String, age: Int, address: Address)
  
  "Parse address in Applicative style" in {
    val json = JsonParser.parse(""" {"street": "Manhattan 2", "zip": "00223" } """)
    val a = field[String](json, "zip") <*> (field[String](json, "street") map Address.curried)
    a mustEqual Success(Address("Manhattan 2", "00223"))
  }

  "Failed address parsing" in {
    val json = JsonParser.parse(""" {"street": "Manhattan 2", "zip": "00223" } """)
    val a = field[String](json, "zip") <*> (field[String](json, "streets") map Address.curried)
    a mustEqual Failure("no such field 'streets'")
  }

  "Parse Person with Address" in {
    implicit def addrJSON: JSON[Address] = new JSON[Address] {
      def read(json: JValue) = 
        field[String](json, "zip") <*> (field[String](json, "street") map Address.curried)

      def write(value: Address) = error("fixme")
    }

    val p = JsonParser.parse(""" {"name":"joe","age":34,"address":{"street": "Manhattan 2", "zip": "00223" }} """)
    val person = field[Address](p, "address") <*> (field[Int](p, "age") <*> (field[String](p, "name") map Person.curried))
    person mustEqual Success(Person("joe", 34, Address("Manhattan 2", "00223")))
  }
}<commit_msg>Slightly better ap syntax<commit_after>package net.liftweb.json.scalaz

import scalaz._
import Scalaz._
import net.liftweb.json._
import net.liftweb.json.JsonAST._

import org.specs.Specification
import org.specs.runner.{Runner, JUnit}

class ExampleTest extends Runner(Example) with JUnit
object Example extends Specification {
  import JsonScalaz._

  case class Address(street: String, zipCode: String)
  case class Person(name: String, age: Int, address: Address)
  
  "Parse address in Applicative style" in {
    val json = JsonParser.parse(""" {"street": "Manhattan 2", "zip": "00223" } """)
    val a1 = field[String](json, "zip") <*> (field[String](json, "street") map Address.curried)
    val a2 = (field[String](json, "street") |@| field[String](json, "zip")) { Address }
    a1 mustEqual Success(Address("Manhattan 2", "00223"))
    a2 mustEqual a1
  }

  "Failed address parsing" in {
    val json = JsonParser.parse(""" {"street": "Manhattan 2", "zip": "00223" } """)
    val a = (field[String](json, "streets") |@| field[String](json, "zip")) { Address }
    a mustEqual Failure("no such field 'streets'")
  }

  "Parse Person with Address" in {
    implicit def addrJSON: JSON[Address] = new JSON[Address] {
      def read(json: JValue) = 
        (field[String](json, "street") |@| field[String](json, "zip")) { Address }

      def write(value: Address) = error("fixme")
    }

    val p = JsonParser.parse(""" {"name":"joe","age":34,"address":{"street": "Manhattan 2", "zip": "00223" }} """)
    val person = (field[String](p, "name") |@| field[Int](p, "age") |@| field[Address](p, "address")) { Person }
    person mustEqual Success(Person("joe", 34, Address("Manhattan 2", "00223")))
  }
}
{'fraction_non_alphanumeric': 0.1759927797833935, 'fraction_numerical': 0.021961492178098677, 'mean_word_length': 4.46875, 'pattern_counts': {'":': 18, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6816893', 'n_tokens_mistral': 1166, 'n_tokens_neox': 1080, 'n_words': 296}