我想創建一個可以合并字串記錄和另一個案例類物體的案例類。
例如:
case class Student(
name: String
age: Int
)
case class Example(
[key:String]: Student
)
現在我想使用Example添加多個屬性,其中attribute可能有 N 個元素,但是所有這些屬性的型別將保持為 Student。這是一個例子:
Example(student1 = Student("name",12),student2=Student("name2",13))
我使用 Case 類的原因是我需要使用 UPickle 庫將其轉換為 JSON,所以我想知道實作相同的可行性。
請注意,Example類不僅包含[key:String]: Student屬性型別,還包含以下內容:
case class Example(
[key:String]: Student,
_logOp: Option[Boolean] = false,
queryName: String,
...
)
案例類的轉換結果:
case class Example(
_logOp: String,
variation: String,
[key:String]: FiltersCaseClass
/* This line I have added to simplify and make my problem more understandable. Basically the case class would contain some properties like `_logOp` `variation` and then a lot of keys with their values as another case class `FilterCaseClass`
*/
)
應該是這樣的:
{"_logOp":"AND","variation": "en","ids": {"_logOp": "OR","_expressions": [{"value": "242424"},{"value": "242422"}]}}
其中 FilterCaseClass 是:
case class FilterCaseClass(
_logOp: String,
_expressions: Seq[SingleValueFilter]
)
其中 SingleValueFilter 是另一個包含值的案例類
編輯1:
根據 Dymtro 的回答之一:
case class Example(
m: Map[String, Student],
_logOp: Option[Boolean] = Some(false),
queryName: String
)
object Example {
implicit val rw: ReadWriter[Example] = macroRW
}
write(Example(
Map("student1" -> Student("name",12), "student2" -> Student("name2",13)),
Some(true),
"abc"
))
//{"m":{"student1":{"name":"name","age":12},"student2":{"name":"name2","age":13}},"_logOp":[true],"queryName":"abc"}
我想要的唯一區別是:
{"student1":{"name":"name","age":12},"student2":{"name":"name2","age":13},"_logOp":[true],"queryName":"abc"}
不同之處在于我希望案例類能夠靈活地添加學生類的鍵值對。
uj5u.com熱心網友回復:
你不需要case類Example,在μPickle你可以創建json混合手工構造和case-class構造
import upickle.default.{macroRW, ReadWriter, write} // "com.lihaoyi" %% "ujson" % "0.9.6"
case class Student(
name: String,
age: Int
)
object Student {
implicit val rw: ReadWriter[Student] = macroRW
}
ujson.Obj("student1" -> write(Student("name",12)), "student2" -> write(Student("name2",13)))
//{"student1":"{\"name\":\"name\",\"age\":12}","student2":"{\"name\":\"name2\",\"age\":13}"}
If mean then [key:String]: StudentμPickleMap[String, Student]似乎支持這種開箱即用
case class Example(
m: Map[String, Student],
_logOp: Option[Boolean] = Some(false),
queryName: String
)
object Example {
implicit val rw: ReadWriter[Example] = macroRW
}
write(Example(
Map("student1" -> Student("name",12), "student2" -> Student("name2",13)),
Some(true),
"abc"
))
//{"m":{"student1":{"name":"name","age":12},"student2":{"name":"name2","age":13}},"_logOp":[true],"queryName":"abc"}
它不應該嵌套在
m
您可以使用自定義編解碼器(pickler)來實作這一點
import upickle.default.{ReadWriter, macroRW, readwriter, transform, write, read}
import scala.collection.mutable
case class Example(
m: Map[String, Student],
_logOp: Option[Boolean] = Some(false),
queryName: String
)
object Example {
implicit val rw: ReadWriter[Example] = {
val standardExampleRW = macroRW[Example]
readwriter[ujson.Value].bimap[Example](
example => transform[Example](example)(standardExampleRW).to[ujson.Value] match {
case ujson.Obj(standardMap) =>
val newMap = mutable.LinkedHashMap.empty[String, ujson.Value]
standardMap.remove("m")
newMap.addAll(example.m.map { case (str, stud) => str -> transform[Student](stud).to[ujson.Value]})
.addAll(standardMap)
ujson.Obj(newMap)
},
// if you don't need a reversed transform i.e. from a json to an Example then you can omit this part
// _ => ???
{
case ujson.Obj(newMap) =>
val logOpJson = newMap.remove("_logOp")
val logOp = logOpJson.map(transform[ujson.Value](_).to[Option[Boolean]])
val queryNameJson = newMap.remove("queryName")
val queryName = queryNameJson.map(transform[ujson.Value](_).to[String]).getOrElse("")
val m = newMap.map { case (str, json) => str -> transform[ujson.Value](json).to[Student] }.toMap
logOp.map(Example(m, _, queryName)).getOrElse(Example(m, queryName = queryName))
}
)
}
}
write(Example(
Map("student1" -> Student("name",12), "student2" -> Student("name2",13)),
Some(true),
"abc"
))
//{"student1":{"name":"name","age":12},"student2":{"name":"name2","age":13},"_logOp":[true],"queryName":"abc"}
read[Example](
"""{"student1":{"name":"name","age":12},"student2":{"name":"name2","age":13},"_logOp":[true],"queryName":"abc"}"""
)
//Example(Map(student1 -> Student(name,12), student2 -> Student(name2,13)),Some(true),abc)
因此,基本上您可以在 Scala 中生成案例類,但不需要序列化為 json 格式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/532049.html
上一篇:播放框架重定向到另一臺主機
