我是Scala的新手,也许这个问题很容易解决,但我一直在努力理解为什么我会遇到编译错误。
所以我有我的案例课
case class AggregationQuery (
startDate: String,
endDate: String,
aggregates: Aggregates,
value: Value,
options: Options
)
case class Aggregates (aggregate: List[String])
case class Options (excludePredicted: Boolean,excludeZeroMetricValue: Boolean)
case class Value (name: String,metric: String,parameters: Parameters)
case class Parameters (numberFormat: String,bigNumberFormat: String,currency: String)
为我创建的RootJsonFormat扩展名
object OptionsProtocol {
implicit object optionValue extends RootJsonFormat[Options] {
def write(option: Options): JsValue = {
JsObject(
"excludePredicted" -> option.excludePredicted.toJson,
"excludeZeroMetricValue" -> option.excludeZeroMetricValue.toJson
)
}
override def read(json: JsValue): Options = ???
}
}
object ValueProtocol {
implicit object parametersValue extends RootJsonFormat[Value] {
def write(value: Value): JsValue = {
JsObject(
"metric" -> value.metric.toJson,
"name" -> value.name.toJson,
"paramters" -> value.parameters.toJson
).toJson
}
override def read(json: JsValue) = ???
}
}
object AggregatesProtocol {
implicit object optionValue extends RootJsonFormat[Aggregates] {
def write(aggregate: Aggregates): JsValue = {
val list= aggregate.aggregate
JsObject( "aggregates" ->list.toJson )
}
override def read(json: JsValue): Aggregates = ???
}}
然后我又添加了一个RootJsonFormat,我认为它将聚合上述格式化程序
object AggregateQueryProtocol {
implicit object aggregateQueryValue extends RootJsonFormat[AggregationQuery] {
def write(query: AggregationQuery): JsValue = {
JsObject(
"startDate" -> query.startDate.toJson,
"endDate" -> query.endDate.toJson,
"aggregates" -> query.aggregates.toJson,
"value" -> query.value.toJson,
"options" -> query.options.toJson
)
}
override def read(json: JsValue): AggregationQuery = ???
}
}
现在我想创建我的AggregationQuery json
AggregationQuery(
dateRange.startDate,
dateRange.endDate,
filters,
aggregates,
value,
option
).toJson;
但是对于聚集和选项,我的编译错误是没有定义jsonwriter,
Error:(23, 43) not enough arguments for method toJson: (implicit writer: spray.json.JsonWriter[aggreagateQuery.Aggregates])spray.json.JsValue.
Unspecified value parameter writer.
"aggregates" -> query.aggregates.toJson,
Error:(23, 43) Cannot find JsonWriter or JsonFormat type class for aggreagateQuery.Aggregates
"aggregates" -> query.aggregates.toJson,
我不知道我在做什么错,所有导入都被添加了。 这也很奇怪,因为Value类没有错误。基本上,这些类非常相似,并放置在同一包中:/
也许有人可以给我一些提示?
问候 卢卡斯
答案 0 :(得分:0)
以下是对我有用的代码:
import spray.json.DefaultJsonProtocol._
import spray.json.{JsObject, JsValue, RootJsonFormat, _}
object Main extends App {
case class AggregationQuery(startDate: String,
endDate: String,
aggregates: Aggregates,
value: Value,
options: Options)
case class Aggregates(aggregate: List[String])
case class Options(excludePredicted: Boolean, excludeZeroMetricValue: Boolean)
case class Parameters(numberFormat: String, bigNumberFormat: String, currency: String)
case class Value(name: String, metric: String, parameters: Parameters)
implicit object optionsFormat extends RootJsonFormat[Options] {
def write(option: Options): JsValue = {
JsObject(
"excludePredicted" -> option.excludePredicted.toJson,
"excludeZeroMetricValue" -> option.excludeZeroMetricValue.toJson
)
}
override def read(json: JsValue): Options = Options(excludePredicted = true, excludeZeroMetricValue = false)
}
implicit object parametersFormat extends RootJsonFormat[Parameters] {
def write(parameters: Parameters): JsValue = {
JsObject(
"numberFormat" -> parameters.numberFormat.toJson,
"bigNumberFormat" -> parameters.bigNumberFormat.toJson,
"currency" -> parameters.currency.toJson
)
}
override def read(json: JsValue): Parameters = Parameters("", "", "")
}
implicit object valueFormat extends RootJsonFormat[Value] {
def write(value: Value): JsValue = {
JsObject(
"metric" -> value.metric.toJson,
"name" -> value.name.toJson,
"paramters" -> value.parameters.toJson
).toJson
}
override def read(json: JsValue) = Value("", "", Parameters("", "", ""))
}
implicit object aggregatesValue extends RootJsonFormat[Aggregates] {
def write(aggregate: Aggregates): JsValue = {
val list = aggregate.aggregate
JsObject("aggregates" -> list.toJson)
}
override def read(json: JsValue): Aggregates = Aggregates(List())
}
implicit object aggregateQueryValue extends RootJsonFormat[AggregationQuery] {
def write(query: AggregationQuery): JsValue = {
JsObject(
"startDate" -> query.startDate.toJson,
"endDate" -> query.endDate.toJson,
"aggregates" -> query.aggregates.toJson,
"value" -> query.value.toJson,
"options" -> query.options.toJson
)
}
override def read(json: JsValue): AggregationQuery =
AggregationQuery("", "", Aggregates(List()), Value("", "", Parameters("", "", "")), Options(excludePredicted = true, excludeZeroMetricValue = false))
}
AggregationQuery("2016", "2018", Aggregates(List()), Value("", "", Parameters("", "", "")), Options(excludePredicted = true, excludeZeroMetricValue = false)).toJson
}
我基本上添加了parametersFormat并删除了格式化程序周围包裹的对象。
希望这会有所帮助。