为什么在java中你不需要创建这些json读/写? [英] Why do you need to create these json read/write when in java you didn't have to?

查看:136
本文介绍了为什么在java中你不需要创建这些json读/写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我错了,请纠正我,但是当使用Java而不是Spring MVC时,您不必创建这些额外的类来将您的Java类映射到JSON和JSON到类。

Please correct me if I am wrong, but when using Java with say Spring MVC you didn't have to create these extra classes to map your Java class to JSON and JSON to class.

你为什么要在Play with Scala中这样做?它与Scala有关吗?

Why do you have to do this in Play with Scala? Is it something to do with Scala?

case class Location(lat: Double, long: Double)

implicit val locationWrites: Writes[Location] = (
  (JsPath \ "lat").write[Double] and
  (JsPath \ "long").write[Double]
)(unlift(Location.unapply))


implicit val locationReads: Reads[Location] = (
  (JsPath \ "lat").read[Double] and
  (JsPath \ "long").read[Double]
)(Location.apply _)


推荐答案

您必须在Play中执行此操作的原因是框架设计选择,这是一个非常好的选择。

The reason why you have to do it in Play is a framework design choice, and it is a very good one.

在Play中,该机制依赖于 Scala implicits ,这是一个非常强大的功能,可以使机制高度可插入 ,就在你打电话的那一刻:

In Play, the mechanism relies on Scala implicits, which are a very powerful feature leveraged to make the mechanism highly pluggable , in the sense that at the moment you call:

Json.toJson(Location(4.5, 5.3)) 

编译器会看对于匹配所需类型的范围中的隐式 Scala语言规范描述了解决隐含的算法,并且此类算法的设计方式使您可以在有限范围内导入隐式。由于这个功能,在程序的不同部分,您可以看到您的读/写或任何类型类的不同实现。

The compiler will look for an implicit in scope matching the required type. The Scala language specification describes the algorithm to resolve implicits, and such algorithm is designed in a way that you can "import" an implicit in a limited scope. Thanks to this feature, in different part of your program you can make visible a different implementation of your Reads / Writes or any typeclass .

object MyImplicits {

    object ImplicitJson1{
        implicit val write:Write[Location] = "write to json all fields"
    }

    object ImplicitJson2{
        implicit val write:Write[Location] = "skip field a"
    }
}

object MyBusinessCode{

    def f1(location:Location){
        import MyImplicits.ImplicitJson1._
        Json.toJson(location)
    }
    def f2(location:Location){
        import MyImplicits.ImplicitJson2._
        Json.toJson(location)
    }

    def dynamicChoice(location:Location){
        implicit val write = {
            if(location.isEurope)           
                MyImplicits.ImplicitJson1.write
            else
                MyImplicits.ImplicitJson2.write
        }
        Json.toJson(location)
    }

}

相反,在春天这是典型的通过内省和反思来完成。您可能需要使用注释来帮助Spring确定如何从数据模型构建Json。结果是你不能改变它的完成方式,因此灵活性较低。

Instead, in Spring this is typically done through introspection and reflection. You might need to use annotations to help Spring determining how to build your Json from your data model. The consequence is that you cannot change the way it is done, and therefore you have less flexibility.

由于你可能不需要更多的灵活性,许多Scala库/框架为你提供了函数生成所需类型类的默认实现。额外的代码行

Since you might not need more flexibility, many Scala libraries/framework provides you functions to generate default implementation of the typeclass you need. That extra line of code

implicit val fmt = Json.format[Location]

是您必须支付的价格,因为Play json序列化依赖于隐式。

is the price that you have to pay because Play json serialization relies on implicit.

这篇关于为什么在java中你不需要创建这些json读/写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆