我的应用程序支持protobuf和JSON序列化.对于我使用的JSON序列化com.trueaccord.scalapb.json.JsonFormat
,我的dtos是从proto定义生成的.
该com.trueaccord
串行包装选项类型,这是造成某些客户端的问题,所以我希望能够支持JSON对象org.json4s
没有制动现有的客户.
我希望能够根据名为JFORMAT的自定义http头选择一个序列化程序.我的想法是,如果发送此标头,我将使用json4s,否则我将使用trueaccord序列化器.
我设法创建一个Unmarshaller,它可以根据标头值选择一个请求序列化器:
Unmarshaller.withMaterializer[HttpRequest, T](_ => implicit mat => {
case request: HttpRequest =>
val entity = request.entity
entity.dataBytes.runFold(ByteString.empty)(_ ++ _).map(data => {
entity.contentType match {
case `applicationJsonContentType` =>
val jsFormat = {
val header = request.headers.find(h => h.name() == jsonFormatHeaderName)
if (header.isEmpty) "1.0" else header.get.value()
}
val charBuffer = Unmarshaller.bestUnmarshallingCharsetFor(entity)
val jsonText = data.decodeString(charBuffer.nioCharset().name())
val dto = if(jsFormat == "2.0") {
write[T](value)(formats) // New Formatter
} else {
JsonFormat.fromJsonString[T](jsonText) // Old Formatter …
Run Code Online (Sandbox Code Playgroud) 我的应用程序是在同一个存储桶中存储多种文档类型.我知道这不是一个好习惯,但我对我可以在我的服务器上创建多少桶有限制,目前还没有解决方法.文档的前缀是它们的类型,所以当我得到一个文档时,我只需要连接前缀和id来获取密钥,我就可以进行密钥查找.
我需要创建一个报告,从多个文档类型中提取信息.
我的地图看起来像这样:
function(doc, meta) {
var getStep = function(stepName, exit, mapper) {
if (meta.id.indexOf(stepName) !== -1) {
var hotelId = parseInt(meta.id.replace(stepName + '_', ''));
if (hotelId > 0) {
var result = {
hotelId: hotelId,
exit: exit
};
if (mapper !== undefined) {
mapper(result);
}
return result;
}
}
return null;
};
var photos = getStep('PHOTOS', 7);
if (photos != null) {
emit(photos.hotelId, photos);
}
var pricing = getStep('PICR', 5);
if (pricing != null) {
emit(pricing.hotelId, pricing);
}
var …
Run Code Online (Sandbox Code Playgroud)