Spray JSON, Jackson go to type when multiple key and less key comparison

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.