在具有相同结构的案例类之间转换

时间:2017-08-02 14:54:31

标签: json scala playframework playframework-2.0 shapeless

我有两个相同的案例类,比如说

case class JsonOutput(
  creationDate: ZonedDateTime,
  updateDate: ZonedDateTime,
  doctorName: String,
  patientName: String,
  userName: String
)

case class DbOutput(
  creationDate: ZonedDateTime,
  updateDate: ZonedDateTime,
  doctorName: String,
  patientName: String,
  userName: String
)

在结构上是相同的,但没有一些关系(并且它们的相似性将来可能会改变)

我想将我从数据库获取的DbOutput类型转换为JsonOutput,稍后我将用于转换(由Play Json),如

//fullCase is of type JsonOutput
Ok(Json.obj("case" -> fullCase))

我该怎么做?

注意:

如果是嵌套的案例类结构,该怎么做:

case class GeneralInfo(number: Int)

// Nested case class structure
case class JsonOneOutput(text: String)
case class JsonThreeOutput(meta: JsonOneOutput, info: GeneralInfo)

// Nested case class structure    
case class DbOneOutput(text: String)
case class DbThreeOutput(meta: DbOneOutput, info: GeneralInfo)

我再次必须将DbOutput转换为JsonOutput(并且所有类型都相同,除了DbOneOutputJsonOneOutput之类的一些叶节点类型,它们可以在深层次)

1 个答案:

答案 0 :(得分:1)

你似乎想要无形的这样做,所以我建议你看一下documentation。如何实现这一目标的一个例子是:

def candidate():

    for j in range(0,20):

        driver.implicitly_wait(50)   
        employeeList=driver.find_elements_by_xpath("//td[7]/div/div[1]/a")
        employeeList[j].click()
        driver.switch_to_default_content()
        driver.implicitly_wait(50)
        driver.switch_to.frame("detail")
        wait = WebDriverWait(driver,2)

        try:                
            resume = wait.until(EC.presence_of_element_located((By.XPATH,"//div[@id='menubar']/div[1]/div/ul/li[2]/a/span")))
            driver.implicitly_wait(50)
            resume.click()
            download = wait.until(EC.presence_of_element_located((By.XPATH,'//a[@title="Download Resume"]')))

            driver.implicitly_wait(50)
            download.click()
            driver.implicitly_wait(50)
            driver.switch_to.frame("RTFVIEWER_MS")
            msword =  wait.until(EC.presence_of_element_located((By.XPATH,"//div[@id='pagecontainer'>>]/div/a[2]/ul/li[2]")))
            driver.implicitly_wait(50)            
            msword.click()
            print(j)
        except TimeoutException as ex1:
            print("Exception has been thrown"+str(ex1))
            print(j)
            continue

        driver.switch_to_default_content()
        driver.switch_to.frame(0)