Three class definitions with a combined relationship
A
D =
C {...}
...
}
...
}
Each class has Loadfromjson and Writeasjson methods. In the past few years, three class member variables have been added, and the database has a lot of JSON string type of old data, in order to be able to correctly convert them to class, you need to judge some fields, for example.
There is a JSON string in the DB, which is the Name:xins, age:24
And a few years later C added a new domain, company
Then Loadfromjson to add judgment, if the JSON does not have this key, it is necessary to write Company:null
Now, to rewrite A, B, C. The goal is that the code as little as possible, extensibility is better, do not if/else judge the existence of the domain.
There are not enough fields for JSON in old data, and there may be problems when you go to class.
The class goes to JSON to add some fields, but this may not matter, because the front end may only take the key,val that it is interested in from the JSON
Two JSON libraries, Spray.json and Jackson_scala_module, were tested below.
Results:
Jackson_scala_module can accept domain-missing issues when converting from string JSON to type. Like what
Case class = Mapper.readvalue ("{}", Classof[demo]) println (Demo)
Demo (false,0,null,0.0)
In case of Spray.json occurrence key missing, it will exception
And for the domain key there is a redundant situation
Jackson Error
Val demo1 = Mapper.readvalue ("{\" x\ ": true, \" y\ ": 1100, \" c\ ": \" str\ ", \" d\ ": \" Str\ "}", CLA Ssof[demo])
Spray.json is no problem.
There is no relationship between the order of the variables, and two libraries can correctly handle the case where the key sequence is incorrect.
The new program is written in Scala, spray framework. The first thing to think about is Spray.json. From other people's blog to see Spray.json speed is very slow, but spray to Spray.json built up a lot of support, such as post data can go through Entity.as[person] directly to the person object. Considering the amount of data that the program faces is not too big, spray.json slow point actually does not matter, but many key situation it cannot handle, cannot use.
Spray JSON, Jackson go to type when multiple key and less key comparison