有组合关系的三个class定义

A {

B {

  C {...}

...

}

...

}

每个class都有loadFromJson和writeAsJson方法。过去几年,三个class里的成员变量一直在添加,而数据库里有很多json string类型的老数据,为了能够正确的把它们转化为class,就需要对一些域进行判断,举个例子。

在 db 中有一个json string,是这样的 name: xins, age: 24

而几年后C添加了一个新的域,company

那么loadFromJson就要添加判断,如果json中没有company这个key,就要写上 company: null

现在,要重写A, B, C。目标是,代码尽可能的少,扩展性要好,不要if/else判断域的存在情况。

老数据中json的域不足,转到class时可能会有问题。

class转到json会添加一些域,但这可能没有关系,因为前端可能只从json中拿它感兴趣的key,val

下面测了两种json库,分别是spray.json和jackson_scala_module

结果:

jackson_scala_module在从string json 到 type进行转换时,能够接受域缺失的问题。比如

case class Demo(x: Boolean, y: Int, c: String, d: Double)
val demo = mapper.readValue("{}", classOf[Demo])
println(demo)

Demo(false,0,null,0.0)

其实这个结果是不太理想的,我更希望当一个域不出现时,该域就不要在json中出现。比如false, 0 和 null 的地位是不一样的。false, 0 是个有效的值,而null则明确说明没有值。

对于spray.json发生key缺失的情况,则会exception

而对于域key出现多余的情况

jackson报错

val demo1 = mapper.readValue("{\"x\": true, \"y\": 1100, \"c\": \"str\", \"d\": \"str\"}", classOf[Demo])

spray.json则没有问题

对于变量的顺序则没有关系,两个库都能正确处理key顺序不正确的情况。

新的程序是用scala写的,spray框架。首先想到的就是spray.json。从别人的blog上看到spray.json的速度很慢,但是spray对spray.json内置了很多支持,比如post的数据可以通过entity.as[Person]直接转到Person对象。考虑到程序面对的数据量并不太大,spray.json慢点其实无所谓,但多key的情况它处理不了,就用不了了。

update: json4s

后来我又测试了下 json4s,发现json4s对于多key或者少key的情况都能正确的处理,唯一的问题是case class每个域都要用option来修饰,不太美观

import org.json4s._
import org.json4s.jackson.JsonMethods._ object Json4s extends App {
implicit val formats = DefaultFormats case class Child(name: Option[String], age: Int, birthdate: Option[java.util.Date]) case class Address(street: String, city: String) case class Person(name: Option[String], address: Address, children: Option[List[Child]]) val json = parse( """
{ "name": "joe",
"sss" : "sss",
"ssss" : ,
"address": {
"street": "Bulevard",
"city": "Helsinki"
},
"children": [
{
"name": "Mary",
"age": ,
"birthdate": "2004-09-04T18:06:22Z"
},
{
"name": "Mazy",
"age":
}
]
}
""") println(json.extract[Person])
}

spray json, jackson 转到type时多key和少key的比较的更多相关文章

  1. 解决 Jackson反序列化 Unexpected token ... , expected VALUE_STRING: need JSON String that contains type id (for subtype of ...)

    首先检查是否是 objectMapper.enableDefaultTyping(); 的受害者.优先考虑删除该配置. 使用Jackson把数组的json字符串反序列化为List时候报了个JsonMa ...

  2. Json --- Jackson工具

    一.Jackson简介 Jackson是一个简单基于Java应用库,Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json.xml转换成Java对象.Jackson ...

  3. 对于json对像,怎么遍历json对象的所有key,在使用json对象时,如果无法知道key,怎么通过key变量来获取值

    对于json对像,怎么遍历json对象的所有key,在使用json对象时,如果无法知道key,怎么通过key变量来获取值?请参阅下面的关键代码: <html> <head> & ...

  4. new JSONObject(str)无法解析 报错:org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject

    org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject 解析服务器返回的Jso ...

  5. 【原创】大叔问题定位分享(12)Spark保存文本类型文件(text、csv、json等)到hdfs时为什么是压缩格式的

    问题重现 rdd.repartition(1).write.csv(outPath) 写文件之后发现文件是压缩过的 write时首先会获取hadoopConf,然后从中获取是否压缩以及压缩格式 org ...

  6. ASP.NET MVC another entity of the same type already has the same primary key value

    ASP.NET MVC项目 Repository层中,Update.Delete总是失败 another entity of the same type already has the same pr ...

  7. Github 访问时出现Permission denied (public key)

    一. 发现问题: 使用 git clone 命令时出现Permission denied (public key) . 二. 解决问题: 1.首先尝试重新添加以前生成的key,添加多次,仍然不起作用. ...

  8. Attaching an entity of type 'xxx' failed because another entity of the same type already has the same primary key value.

    问题的详细描述: Attaching an entity of type 'xxxxx' failed because another entity of the same type already ...

  9. json jackson

    1.引入依赖 <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId&g ...

随机推荐

  1. 22 Best Sites To Download Free Sprites

    http://unity3diy.blogspot.com/2014/11/Free-Sprites-Download-For-YourGames.html ————————————————————— ...

  2. WPF ICommandSource Implementations Leak Memory!

    Actually the title of this article should be entitled "How to use WeakEventManager with IComman ...

  3. MYSQL数据库从A表把数据插入B表

    如果2张表的字段一致,并且希望插入全部数据,可以用这种方法: Code: INSERT INTO 目标表 SELECT * FROM 来源表; 比如要将 articles 表插入到 newArticl ...

  4. form表单target的用法

    偶然有一机会发现form表单的target的用法,可以实现当前页表单提交而不进行跳转刷新.代码如下,首页在页面里准备一form表单和一iframe <form action="提交的a ...

  5. 锐捷 ac ap 连接 记录

    需要用到锐捷的ac管理2台ap.记录一下. 参考文档 锐捷WLAN无线产品一本通(V6.0): http://www.ruijie.com.cn/fw/wd/58033 1.确认AC无线交换机和AP是 ...

  6. 使用Photoshop实现雪花飘落的效果

    一.准备工作 软件环境:PhotoshopCS5 实验目的:雪花飘落的效果 二.实验步骤 1,打开素材图片并将原图层复制 2,在菜单栏内选择:滤镜->像素化->点状化,单元格大小选6  提 ...

  7. Zend Framework2 入门教程(转)

    转载自: http://my.oschina.net/lai1362000/blog/201301 重申:这本书作者的截图我都放上去了,没侵权啊. 别问那么多,我只是一个安静的搬砖工. 摘要 Zend ...

  8. e793. 监听JSpinner数据变化

    // Create a nummber spinner JSpinner spinner = new JSpinner(); // Add the listener spinner.addChange ...

  9. e795. 获得和设置JSlider的值

    // To create a slider, see e794 创建JSlider组件 // Get the current value int value = slider.getValue(); ...

  10. css 阻止元素中的文本。双击选中

    //firefox -moz-user-select: none; //chrome.safari -webkit-user-select: none; //ie -ms-user-select: n ...