Encoding and decoding of "language contrast" json

Source: Internet
Author: User

The processing of JSON in JAVA, Python, and C + + can be done through a third-party library, which is not supported by the language itself. Here's a comparison of the advantages and disadvantages of these three languages in processing JSON in a common scenario. The following is a representation of the Java Jackson Library, the Python Simplejson Library, and the C + + Jsoncpp library as their respective languages


1. Convert the object to a JSON string

S1 class instances that are well defined for their respective languages

Java:

Objectmapper objmapper = new Objectmapper ();

Objmapper.writevalueasstring (S1)


Python:

If you want to convert a custom object to a JSON string, you also need to define a function in Python that converts the object to the Dict type:

def convert_student (obj):

Dict = {}

Dict.update (obj.__dict__)

Return Dict


Json.dump (obj,default=convert_student)


C++:

Jsoncpp does not support functions that serialize objects directly into JSON strings, it can only implement JSON serialization of objects by manually building JSON nodes

Json::value Root;

root["name"]= "XXX"

root["Age"]=12

Root.tostyledstring ()


From the above three different languages can be seen, because Java itself has the concept of Java beans, that is, there is a common way to get object properties, so in Java can easily convert objects into JSON strings, and C + + obviously cannot provide this generic concept, So the developer can only convert the class to a defined Json::value, and then Jsoncpp from Jsonvalue. Because of the built-in Dict data type, this data type is naturally suitable for processing JSON transformations, so as long as the class-to-dict data type conversion function is implemented, the conversion to the JSON string can be accomplished.


2. Convert the JSON string to a class

Java:

Student RT = Objmapper.reader (Student.class). ReadValue (JSONSTR);

Python:

def rebuild_student (Pairs):

Stu = Student ()

stu.__dict__ = Pairs

Return Stu

Json.loads (Jsonstr, object_hook=rebuild_student)

C++:

Json::reader Reader;

Json::value Value;

Reader.parser (Str,value)


From the class to the JSON string analysis, we are not difficult to derive from the JSON string to the class conversion, the same Java can be converted directly, Python needs to rely on the dict type, and C + +, only honest themselves from the json::value to convert.


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.