在另一个自定义格式化程序中使用自定义格式化程序 [英] use custom Formatter inside another custom formatter
问题描述
我实现了我的自定义读/写格式化程序,以便将该类序列化为json.
I implement my custom reads/writes formatter in order to serialize the class as json.
This question is continues question for: Define different formatters for same class in the same trait
我想做的是,我在Tenant类中有一个Product类的列表,tenant的格式化程序不是带隐式的,而是自定义格式化程序.
What i wish to do, I have a list of Product class in Tenant class, the formatter for tenant is not with implicit, it is a custom formatter.
val hsmTenantFormat = new Format[Tenant]
{
def writes(tenant: Tenant): JsValue =
{
val items = tenant.items.getOrElse(List[Product]())
Json.obj(
"items" -> tenant.items, //==> This line throw compile error since Product is not with implicit - cannot find formatter
"prefixAndroid" -> tenant.prefixAndroid,
"prefixIOS" -> tenant.prefixIOS,
"er" -> tenant.er,
"erMessage" -> tenant.erMessage
)
}
def reads(json: JsValue): JsResult[Tenant] =
{
val items = (json \ "items").as[Option[List[Product]]]
var itemsReal:Option[List[Product]] = None
if (items.isDefined)
{
itemsReal = Some(items.get)
}
JsSuccess(new Tenant(
itemsReal,
(json \ "prefixAndroid").as[String],
(json \ "prefixIOS").as[String],
(json \ "er").as[Int],
(json \ "erMessage").as[String]
))
}
}}
当Product带有隐式时,这是正常的,但是当我希望使用customProductFormatter时会发生什么-不带隐式的那个,我找不到在哪里设置它.
This is working GOOD when Product is with implicit, but what happen when i wish to use customProductFormatter - a one that is not with implicit, i cant find where to set it.
谢谢!
这是我的产品格式化程序:
This is my product formatter:
implicit val hsmProductFormat = new Format[Product]
{
def writes(item: Product): JsValue =
{
val retailers = item.r.getOrElse(List[Retailer]())
val images = item.images.getOrElse(List[String]())
Json.obj(
"u" -> item.u,
"s" -> item.s,
"z" -> item.z,
"n" -> item.n,
"v" -> item.v,
"vu" -> item.vu,
"t" -> item.t,
"r" -> retailers,
"images" -> images
)
}
def reads(json: JsValue): JsResult[Product] =
{
val retailers = (json \ "r").as[Option[List[Retailer]]]
var retaliersReal:Option[List[Retailer]] = None
if (retailers.isDefined)
{
retaliersReal = Some(retailers.get)
}
val images = (json \ "images").as[Option[List[String]]]
var imagesReal:Option[List[String]] = None
if (images.isDefined)
{
imagesReal = Some(images.get)
}
JsSuccess(new Product(
(json \ "u").as[String],
(json \ "s").as[Int],
(json \ "z").as[Int],
(json \ "n").as[String],
(json \ "v").as[String],
(json \ "vu").as[String],
(json \ "t").as[String],
retailers,
imagesReal
))
}
}
我希望从产品格式声明中删除隐式,但是如果我这样做了,编译器将找不到Product的格式化程序!
I wish to remove the implicit from product format declaration, but if i do it, the compile cannot find formatter for Product !
推荐答案
好的,我希望我很好地理解了你的问题.我认为有两种方法可以解决此问题.
OK, I hope I understood your question well. I think there are two ways you can overcome this issue.
第一个,以租户的格式声明Product
的隐式格式,以便它可以赶上":
First one, declare an implicit format for Product
in your Tenant's format so that it can "catch it up":
def writes(tenant: Tenant): JsValue =
{
val items = tenant.items.getOrElse(List[Product]())
implicit val productWriter = Product.hsmProductFormat // <--- this should work
Json.obj(
"items" -> tenant.items,
"prefixAndroid" -> tenant.prefixAndroid,
"prefixIOS" -> tenant.prefixIOS,
"er" -> tenant.er,
"erMessage" -> tenant.erMessage
)
}
第二种方法是直接引用所需的写入:
The second approach is to reference the needed writes directly:
def writes(tenant: Tenant): JsValue =
{
val items = tenant.items.getOrElse(List[Product]())
Json.obj(
"items" -> Product.hsmProductFormat.writes(tenant.items: _*), //as you have a List[Product] here, just add a ": _*" thing when calling writes
"prefixAndroid" -> tenant.prefixAndroid,
"prefixIOS" -> tenant.prefixIOS,
"er" -> tenant.er,
"erMessage" -> tenant.erMessage
)
}
欢呼
这篇关于在另一个自定义格式化程序中使用自定义格式化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!