Scala 2.10,它对JSON库和案例类验证/创建的影响 [英] Scala 2.10, its impact on JSON libraries and case class validation/creation

查看:93
本文介绍了Scala 2.10,它对JSON库和案例类验证/创建的影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Scala 2.10中,显然我们的反射得到了改善.

In Scala 2.10 apparently we're getting improved reflection.

这将如何影响lift-json,jerkson,sjson和朋友?此外,在不久的将来,我们可以期望内置Scala中具有la Groovy出色的GSON功能的内置JSON语言吗?

How will this impact lift-json, jerkson, sjson and friends? Furthermore, can we expect in the not too distant future a built-in JSON language feature a la Groovy's excellent GSON in Scala?

我问的原因是我非常想做:

The reason I ask is that I would dearly love to do:

case class Foo(a: String, b: Int, bar: Bar)
case class Bar(c: Int)
val foo = Foo("hey", 10, Bar(23))
val json = foo.toJson

即使没有任意复杂的对象图,也不会发生箍跳(即样板式的准备工作).也许我问的太多了,但是总可以梦到.请打破我的2.10梦想或启发我,以备受期待的Scala版本打开了哪些新途径.下一个

without hoop jumping (i.e. boilerplate-ish prep work), even with arbitrarily complex object graphs. Perhaps I'm asking way too much, but one can always dream. Please shatter my 2.10 dreams or enlighten me as to what new avenues are opening with the highly anticipated release of Scala.Next

此外,关于案例类,似乎对于验证/创建来说,scalaz验证是首选的武器.作为对象创建的安全代理或错误收集器,这似乎非常妙.但是,作为Scewbie,我发现scalaz颇具挑战性,尽管它具有明显的力量,但我仍在抵抗F-ing的阴暗面;-)

Also, in regard to case classes, it seems for validation/creation, scalaz validation is the go-to weapon of choice. It seems quite wonderful, acting as a safe proxy for object creation or as an error collector. As a Scewbie, however, I find scalaz somewhat challenging, and am resisting the F-ing dark side despite its obvious power ;-)

无论如何,这里的重点是,通过2.10反射,我们应该能够在运行时将诸如表单发布之类的字段绑定到case类的属性,并仅基于属性类型执行基本验证(即不必指定单独的验证逻辑来​​指定属性foo必须是一个String,因为它的类型已经在case类中定义了,我们现在可以正确地反映在其上)

At any rate, the point here is, with 2.10 reflection we should be able to bind at runtime the fields from say, a form post, to the properties of a case class and perform basic validation based on property type alone (i.e. will not have to specify separate validation logic that specifies property foo must be a String since its type is already defined in the case class upon which we can now properly reflect)

那么,勇敢的新世界来了,还是现有的工具是可预见的未来的中坚力量?

So, brave new world cometh, or existing tools are the mainstay for the foreseeable future?

推荐答案

前言

让我给出一个不依赖于任何基于Java的库,而仅依赖于纯Scala库的解决方案.

Forewords

Let me give a different solution that doesn't rely on any Java based library but only a pure Scala one.

实际上,正如@Steve结果的评论中所讨论的,Play 2的scala版本是使用Jerkson将Json解序列化/串行化到领域模型的. Jerkson是一个DSL包装器,围绕着一个很好的Java库来处理Json.

Actually, as discussed in the comments of @Steve's results Play 2's scala version was using Jerkson for de/serializing Json to domain model. Where Jerkson is a DSL wrapper around a very good Java library for handling Json.

以上内容并未回答您的问题,因为您正在询问是否已设想使用Scala 2.10的反射和宏功能来简化此任务!!!通过消除大多数样板.

The above wasn't answering your question, since you were asking if it has been envisioned to used the reflection and the macro features of Scala 2.10 to ease this task!!!! By eliminating most boilerplates.

事实上,这是一个很好的想法,因为从Play 2.1版本开始,Json Scala API不再使用Jerkson,而是它自己的机制.

And it was a very good thought in fact because from the Play 2.1 version, the Json Scala API is not using Jerkson anymore but it's own mechanism.

实际上,此机制通过引入基于两件事的全新API来利用新的Scala 2.10版本的优势:

This mechanism is in fact taking advantages of this new 2.10 version of Scala by introducing a brand new API based on two things:

    一种功能构造(Appliative Builder),适于读取和写入Json或Domain实例.这些构建器用于粘合所有组合器(用于读取或写入),以定义粗粒度结构化的结构(就像我们对Parser组合器所做的一样)
  • 一堆宏,它们能够发现哪些组合器是隐式可用的,并将为 Case类(或至少具有applyunapply方法的类型)构造复杂的宏. /li>
  • a functional construction (Applicative Builder) adapted to be able to Read and Write Json or Domain instances. These builders are used to glue altogether combinators (for both read or write) in order to define coarse grained structured (like we do with Parser Combinators)
  • a bunch of macros that are able to discover which combinators are implicitly available and will construct complex ones for Case Classes (or at least types that have apply and unapply methods).

最终,这是我们可以使用此API进行的操作:

In the end of the day, here is what we could do by using this API:

import play.api.libs.json._
import play.api.libs.functional.syntax._

case class Person(name: String, age: Int, lovesChocolate: Boolean)

implicit val personReads = Json.format[Person] //a format is a Reader and a Writer

//this format will be implicitly used by the following from/toJson functions
val person:JsResult[Person] = Json.fromJson(json) //JsResult is like JsSucces/JsError
val jsObject = Json.toJson(person)

代码已复制并改编自: JSON接收(基于Scala 2.10宏)

注意一点:新的API甚至足够聪明,可以通过累积错误来验证读取" ...

A little note: the new API is even smart enough to be able to validate a "read" by accumulating errors...

我建议从这里推荐@mandubian的一系列博客,因为它们非常有启发性!

There are a series of blogs from @mandubian that I'd recommend from here, because they are very enlightening it!

  • JsPath & Reads Combinators: http://mandubian.com/2012/09/08/unveiling-play-2-dot-1-json-api-part1-jspath-reads-combinators/
  • Writes/Format Combinators: http://mandubian.com/2012/10/01/unveiling-play-2-dot-1-json-api-part2-writes-format-combinators/
  • JSON Transformers: http://mandubian.com/2012/10/29/unveiling-play-2-dot-1-json-api-part3-json-transformers/
  • JSON Inception (Based on Scala 2.10 Macros) : http://mandubian.com/2012/11/11/JSON-inception/

可悲的是,Play 2的模块化...不允许我们单独使用此API!因此,应该从整个播放库中使用它:/ 将来可能会改变...

What is sad is that the modularization of Play 2... doesn't allow us to use this API alone! So, it should be used from the play lib as a whole :/ This might change in the future...

现在,未来越来越近了,因为Pascal有了 repo ,使我们能够使用play-json API.

And the future is getting closer now, since Pascal has this repo enabling us to use the play-json API.

因此,在Play 2.2发行之前,您可以使用此存储库.实际上,此版本将与json或iteratees等几种API完全脱钩,因此,我们将能够直接使用playframework存储库.

So, one can use this repo until Play 2.2 will be released. Indeed, this version will be completely decoupled with several APIs like json or iteratees, and thus, we'll be able to use the playframework repo directly.

这篇关于Scala 2.10,它对JSON库和案例类验证/创建的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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