美文网首页
[翻译]squbs官网之8 编组和解组

[翻译]squbs官网之8 编组和解组

作者: 乐言笔记 | 来源:发表于2017-10-22 03:03 被阅读18次

概述

在客户端和服务器端都使用编组和解组。在服务器端, 它用于将传入请求映射到 scala对象, 并将 scala对象映射到传出响应。类似,在客户端,它用于编组对象到一个传出HTTP请求和从一个输入的响应解组对象。有许多内容格式用于编组/解组, 常见的是 JSON 和 XML。

Akka HTTP提供了编组/解组工具。而且,还有其它的开源编组和解组工具为Akka HTTP 提供不同格式和使用不同对象实现编组/解组。

squbs 为跨语言环境提供编组工具。例如, 使用同时具有 Scala 样例类和 Java bean 混合对象层次结构时。对于简单、语言的环境, 请直接使用已提供的编组工具或其他开源编组工具。

本章讨论由squbs提供的编组和解组,以及可用于手工调用这些编组和解组的工具。本章不涉及使用作为Akka HTTP路由DSL的编组和解组。

依赖

增加如下依赖到你的build.sbt或者scala构建文件:

"org.squbs" %% "squbs-ext" % squbsVersion,
"de.heikoseeberger" %% "akka-http-json4s" % heikoseebergerAkkaHttpJsonVersion,
"de.heikoseeberger" %% "akka-http-jackson" % heikoseebergerAkkaHttpJsonVersion,
"com.fasterxml.jackson.core" % "jackson-core" % jacksonVersion,
"com.fasterxml.jackson.core" % "jackson-databind" % jacksonVersion

以下是可选的组件, 具体取决于您要使用的编组格式和库。它们可以组合起来支持多种格式。

// To use json4s native...
"org.json4s" %% "json4s-native" % "3.5.0",

// To use json4s jackson...
"org.json4s" %% "json4s-jackson" % "3.5.0",

// For Jackson marshalling of Scala case classes... 
"com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.8.4",

// For Jackson marshalling of immutable Java classes...
"com.fasterxml.jackson.module" % "jackson-module-parameter-names" % jacksonVersion,

用法

JacksonMapperSupport
JacksonMapperSupport 基于广受欢迎的Jackson库提供JSON编组/解组。它允许Jackson ObjectMappers
的全局的和每种类型的配置。

你仅仅需要引入JacksonMapperSupport._,以便将它的隐式成员暴露在编组/解组的使用范围内:

import org.squbs.marshallers.json.JacksonMapperSupport._

自动和手工编组工具都将隐式使用此包提供的编组工具。下面的代码展示了使用 ObjectMapper 配置 DefaultScalaModule 的各种方法:

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import org.squbs.marshallers.json.JacksonMapperSupport

/* To use DefaultScalaModule for all classes.
 */

JacksonMapperSupport.setDefaultMapper(
  new ObjectMapper().registerModule(DefaultScalaModule))


/* To register a 'DefaultScalaModule' for marshalling a
 * specific class, overrides global configuration for
 * this class.
 */

JacksonMapperSupport.register[MyScalaClass](
  new ObjectMapper().registerModule(DefaultScalaModule))

XLangJsonSupport
XLangJsonSupport 通过委派编组和解组来增加跨语言支持:

  • Json4s for Scala classes
  • Jackson for Java classes
    这些通常是每种语言的首选编组工具, 因为它们支持语言特定的约定, 而无需进一步配置。它们通常对不同的约定也有更好的优化。

然而,使用Json4s或Jackson的决定是由传递给编组/解组的对象的类型决定的。 如果您有混合对象层次结构,则可能仍需要配置编组/解组工具以支持不同的约定,如下所示:

  • 引用Java Bean的Scala样例类。由于顶级别对象是Scala样例类, 因此将选择 Json4s。但它不知道如何编组/解组Java Bean。需要将自定义序列化程序添加到 Json4s 以处理这些 Java bean。
  • 引用Scala样例类的Java Bean。由于顶级对象是Java Bean,因此将选择Jackson。默认Jackson不知道如何编组/解组样例类。需要注册DefaultScalaModule到Jackson ObjectMapper来处理这样的样例。

编组/解组混合语言对象层次结构的一般准则: 除非首选 Json4s 优化, 否则更容易通过将 DefaultScalaModule 注册到 ObjectMapper, 来配置Jackson来处理 Scala。

与 JacksonMapperSupport 一样, 它支持编组和解组的每种类型配置。它允许对Json4s 和Jackson都配置。

你仅仅需要引入XLangJsonSupport._,以便将它的隐式成员暴露在编组/解组的使用范围内:

import org.squbs.marshallers.json.XLangJsonSupport._

自动和手工编组工具都将隐式使用此包提供的编组工具。下面的代码提供了XLangJsonSupport配置案例:

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule
import org.json4s.{DefaultFormats, jackson, native}
import org.squbs.marshallers.json.XLangJsonSupport

/* The following configures the default settings
 * for 'XLangJsonSupport'
 */

// Adds ParameterNamesModule to Jackson
XLangJsonSupport.setDefaultMapper(
  new ObjectMapper().registerModule(new ParameterNamesModule())

// Tells Json4s to use native serialization
XLangJsonSupport.setDefaultSerialization(native.Serialization)

// Adds MySerializer to the serializers used by Json4s
XLangJsonSupport.setDefaultFormats(DefaultFormats + MySerializer)


/* The following configures XLangJsonSupport for specific class.
 * Namely, it configures for 'MyClass' and 'MyOtherClass'.
 */

// Use ParameterNamesModule for mashal/unmarshal MyClass
XLangJsonSupport.register[MyClass](new ParameterNamesModule()))

// Use Json4s Jackson serialization for MyOtherClass
XLangJsonSupport.register[MyOtherClass](jackson.Serialization)

// Use MySerializer Json4s serializer for MyOtherClass
XLangJsonSupport.register[MyOtherClass](DefaultFormats + MySerializer)

调用编组/解组

除了使用编组和解组作为Akka HTTP 路由 DSL 的一部分之外, 还经常需要手动调用编组和解组, 以便在服务器端和客户端Flow以及测试中使用。
Akka提供了Scala DSL用于编组和解组。它的使用可以在下面的例子中看到:

import akka.actor.ActorSystem
import akka.http.scaladsl.marshalling.Marshal
import akka.http.scaladsl.model.MessageEntity
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.ActorMaterializer

// We need the ActorSystem and Materializer to marshal/unmarshal
implicit val system = ActorSystem()
implicit val mat = ActorMaterializer()

// Also need the implicit marshallers provided by this import
import org.squbs.marshallers.json.XLangJsonSupport._

// Just call Marshal or Unmarshal as follows:
Marshal(myObject).to[MessageEntity]
Unmarshal(entity).to[MyType]

相关文章

网友评论

      本文标题:[翻译]squbs官网之8 编组和解组

      本文链接:https://www.haomeiwen.com/subject/cttguxtx.html