A brief talk on Python Simplejson module

Source: Internet
Author: User
Tags object serialization


First, background knowledge


    • JSON:

      Refer to the encyclopedia described below, specifically, please search the relevant introduction:

JSON (JavaScript Object Notation) is a lightweight data interchange format. It is based on a subset of JavaScript (standard ECMA-262 3rd edition-december 1999). JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, C #, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language. Easy to read and write, but also easy to machine parse and build (network transfer speed).

Representation method:


      • Data in name/value pairs

      • Data is separated by commas

      • Curly braces Save Object

      • Square brackets Save Array

Example:

{"Programmers": [{"FirstName": "Brett", "LastName": "McLaughlin", "email": "AAAA"},{"firstName": "Jason", "LastName": " Hunter "," email ":" bbbb "},{" FirstName ":" Elliotte "," LastName ":" Harold "," email ":" CCCC "}]," authors ": [{" FirstName ":" Isaac "," LastName ":" Asimov "," Genre ":" Sciencefiction "},{" FirstName ":" Tad "," LastName ":" Williams "," Genre ":" Fantasy "},{" FirstName ":" Frank "," LastName ":" Peretti "," Genre ":" Christianfiction "}]}


    • Howto-unicode:

The Unicode standard describes how characters correspond to coded points (code point) and uses 16 binary notation for 00 00.

In Python, basestring derives the Unicode type and str type

A Unicode string is a sequence of encoded points that is represented in memory as a set of bytes (0-255), which is a 8-byte stream.

Unicode strings can be converted to str;str through the Encode function, which can be converted to Unicode by decode. Codec type is generally utf-8

Example:

>>> u "China". Encode (' utf-8 ') ' \xe4\xb8\xad\xe5\x9b\xbd ' #将unicode字符串编码为str >>> ' \xe4\xb8\xad\xe5\x9b \xbd '. Decode (' Utf-8 ') u ' \u4e2d\u56fd ' #将str解码为unicode字符串

the operation of reading and writing files from a file should be the 8-bit byte stream of the operation, and if the Unicode string is written to a file, it needs to be encoded, and if a Unicode string is read from a file, the first read is a 8-bit byte stream that needs to be decoded.

Unicode strings are directly manipulated in the general function code, and the corresponding codec operations are added only when writing data or reading data.


    • Serialization and deserialization

      When two processes are communicating remotely, each other can send various types of data. Regardless of the type of data, it will be two

The form of the binary sequence is transmitted over the network. The sender needs to convert the object to a sequence of bytes to be transmitted over the network, and the receiver needs to revert the byte sequence back to the object.

The process of converting an object to a sequence of bytes is called serialization of an object, such as writing a Dictionary object to a file in some format (JSON), or the process of reverting a sequence of bytes to an object is called the deserialization of an object, such as reading a file of a format (JSON) and constructing a Dictionary object.

According to the knowledge of Howto-unicode, the network can be regarded as a file, the sender needs to encode the data when it is written to the network, and the receiver needs to decode it when reading the data. This means that the serialization is encoded at the same time, and the deserialization is decoded at the same time .

Second, Simplejson

The Simplejson is an extension of the JSON standard module (with the same basic functionality) and is the expansion module provided by PyPI, which needs to be installed separately. However, you can use the JSON library that comes with Python, which is basically the same way of using it (the interface functionality provided is basically consistent). In Python's library document, JSON is classified as a network data control class, which is a good illustration of their use, mainly for network data control, codec and so on. But it also has other uses, such as a read-write module that can be used as a configuration file, simple file manipulation, and so on.

It provides very few interfaces, is easy to master, and in most cases uses the default parameters. The official documentation states that the default interface parameters and non-subclasses have better performance. Below we discuss the provided interface, and only show the necessary parameters, the other keyword parameters will be expressed as **kwargs;

  • Simplejson. Dump (obj, fp, **kwargs): Writes Python objects to a file (in JSON format)

  • Simplejson. dumps (obj, **kwargs): Represents a Python object as a string (in JSON format)

  • Simplejson. Load (FP, **kwargs): read from a file (containing a JSON structure) as a Python object

  • Simplejson. loads (S, **kwargs): Read from a string (containing a JSON structure) as a Python object

  • class Simplejson. Jsondecoder:load/loads is called when the JSON format sequence is decoded to a Python object

  • class Simplejson. Jsonencoder: Called when Dump/dumps, encodes a Python object into a JSON-formatted sequence

In connection with the basic knowledge above, we can know that theprocess of dump is actually to write data to the file handle, that is, the process of object serialization, need to encode, but the encoding format is not just Unicode and STR conversion, Instead, the conversion between the Python object type and the JSON object type is more important. Similarly, the process of load is actually reading the data from the file handle, that is, the process of deserializing the generated object, which needs to be decoded, except that the decoded format is not just the conversion of STR and Unicode, but rather the conversion between the more important JSON object type and the Python object type.

The following is the correspondence between the JSON object type and the Python object type:


array Td>str
json python 2 python 3
object dict dict
list list
string Unicode
number (int) int, long int
number (real) float float
true true true
false false false
null None None

     The following example ends this article with a note in the example:

#coding: Utf-8import simplejson as json#simplejson.dump (**kwargs) Fp = open ('./ Text.json ',  ' w+ ') json.dump ([1,2], FP)          ## Serializes the Python array to a file Fp.seek (0) print  "----dump----\ n",  u ' uses dump to save the Python array object in a JSON-formatted file with the contents: \ n ',  fp.read () print fp.close ()           #simplejson. Dumps ( **kwargs) r_dumps = json.dumps ({"China obj":[1,2],  "obj2": [3,4]})    #将python字典进行序列化, Save to string print  "----dumps----\ n",  u ' uses dumps to convert the Python Dictionary object to a string containing JSON format, the result of the string is: \ n ',  r_dumpsprint# Simplejson.load (**kwargs) #如果json文档格式有错误, will throw Jsondecodererror exception Fp = open ('./text.json ',  ' r ') r_ Load = json.load (FP)            # Convert the contents of a file to a Python object print  "----load----\ n",  u "reads a file containing a JSON array format using load and gets a Python object with the type:",  type (R _load) print  #simplejson. Loads (**kwargs) #如果JSON document Format error, will throw Jsondecodererror exception # Converts the contents of a string into a Python object R_loads = json.loads ("' {" {"programmers": [{" FirstName ":" Brett "," LastName ":" McLaughlin "," email ":" AAAA "},{" firstName ":" Jason "," LastName ":" Hunter "," Email ":" BBBB "},{" FirstName ":" Elliotte "," LastName ":" Harold "," email ":" CCCC "}]," authors ": [{" FirstName ":" Isaac "," LastName " : "Asimov", "Genre": "Sciencefiction"},{"FirstName": "Tad", "LastName": "Williams", "Genre": "Fantasy"},{"FirstName": " Frank "," LastName ":" Peretti "," Genre ":" Christianfiction "}]} ') print "----loads----\ n ",  u" When you use loads to read a string that contains a JSON dictionary format, you get a Python object with the type: ",  type (r_loads) print

The results after the run are displayed:

----dump----Use dump to save the Python array object in a JSON-formatted file with the following contents: [1, 2]----dumps----use dumps to convert the Python Dictionary object to a string containing JSON format. The string result is: {"Obj2": [3, 4], "\u4e2d\u56fdobj": [1, 2]}----load----using load to read a file containing the JSON array format, a Python object is obtained, the type is: <type ' List ' >----loads----use loads to read a string containing the JSON dictionary format and get a Python object with the type: <type ' dict ' >


This article is from the "Nameless" blog, please be sure to keep this source http://xdzw608.blog.51cto.com/4812210/1612389

A brief talk on Python Simplejson module

Related Article

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.