A little harvest of Python json, a custom serialization method

Source: Internet
Author: User
Tags compact data structures serialization

from:http://blog.csdn.net/jlnuboy/article/details/5732196

Pymotw:json Module: JSON purpose: JavaScript Object Format Serializer Python version: 2.6

The JSON module provides an API interface similar to that used in pickle to convert a Python object in memory to a sequence representation ("JavaScript object Notation"). Unlike Pickle, however, JSON has a corresponding implementation (especially in JavaScript) in many other languages, making it more appropriate for communication between internal applications. In an AJAX application, JSON may be the most widely used communication between Web servers and clients, but it is not limited to such applications.

encoding and decoding of simple data types

The encoder defaults to support Python's native types (such as int, float, list, tuple, dict).

[python] view plain copy import JSON data = [{' A ': ' A ', ' B ':(2, 4), ' C ': 3.0}] print ' Data: ', repr (data) data_string = json.dumps (data) print ' JSON: ', data_string

The value after the encoder processing is similar to the output value of the Python repr ().  [python] view plain copy $ python json_simple_types.py DATA: [{' A]: ' A ', ' C ': 3.0, ' B ': (2, 4)}] JSON: [{"A]: "A", "C": 3.0, "B": [2, 4]}]

The value obtained by decoding after encoding may not be exactly the same as the original object. [python] view plain copy import JSON data = [{' A ': ' A ', ' B ':(2, 4), ' C ': 3.0}] data_string = Json.dumps (dat A) print ' encoded: ', data_string decoded = Json.loads (data_string) print ' decoded: ', decoded print ' ORIGINAL: ', Ty PE (data[0][' B ']) print ' decoded: ', type (decoded[0][' B '))

For example, a tuple is converted to a list of JSON. [python] view plain copy $ python json_simple_types_decode.py encoded: [{"A": "A", "C": 3.0, "B": [2, 4]}] DE coded: [{u ' a ': U ' a ', U ' C ': 3.0, U ' B ': [2, 4]}] ORIGINAL: <type ' tuple ' > Decoded: <type ' list ' >

Human-use vs compact output

Another point of JSON's superiority over pickle is the readability of its results. The dumps () function receives multiple parameters for a better output structure. For example. The Sort_keys parameter tells the encoder to output the key value of the dictionary in order, rather than randomly unordered.   [python] view plain copy import JSON data = [{' A ': ' A ', ' B ':(2, 4), ' C ': 3.0}] print ' Data: ', repr (data) unsorted = json.dumps (data) print ' JSON: ', json.dumps (data) print ' SORT: ', json.dumps (data, sort_keys=true)   Json.dumps (data, sort_keys=true) Second = json.dumps (data, sort_keys=true) print ' unsorted MATCH: ', unsorted = = print ' SORTED MATCH: ', A/= Second

Sorting makes it easier to see the results and makes it possible to compare the JSON output.

[python] view plain copy $ python json_sort_keys.py DATA: [{' A]: ' A ', ' C ': 3.0, ' B ': (2, 4)}] JSON: [{"A": "A "," C ": 3.0," B ": [2, 4]}] SORT: [{" A ":" A "," B ": [2, 4]," C ": 3.0}] unsorted match:false SORTED match:true

For highly nested data structures, you will want to increase the indentation in the output to better display its format.   [python] view plain copy import JSON data = [{' A ': ' A ', ' B ':(2, 4), ' C ': 3.0}] print ' Data: ', repr (data) print ' NORMAL: ', json.dumps (data, sort_keys=true) print ' INDENT: ', json.dumps (data, sort_keys=true, indent=2)

When the indent parameter is a nonnegative integer, the output structure is closer to the pprint and there are leading spaces on each indentation level. [Python] View plain copy $ python json_indent.py   data: [{' A ':  ' a ',  ' C ':  3.0,   ' B ':  (2, 4)}]   normal: [{"A":  "a",  "B": [2, 4],  "C":  3.0}]   indent: [                 {                 " A ": " a ",                " B ":  [                          2,                          4                           ],                 "C": 3.0                 }  ]    

Data such as this type of output takes up more bytes during the transfer process, but it is not necessary to use indented formatting in the actual production environment. In fact, you can set the delimiter of the data to make the result more compact. [python] view plain copy import json   data = [ {  ' A ': ' A ',   ' B ':(2, 4),  ' C ':3.0 } ]   print  ' data: ', &NBSP;REPR (data)    print  ' repr (data)  : ',  len (repr (data))    print  ' dumps (data)  : ',  len ( Json.dumps (data))    print  ' dumps (data, indent=2)  : ',  len (Json.dumps (data,  indent=2))    print  ' Dumps (data, separators): ',  len (Json.dumps (' , ', ': '))     

The separators parameter of the dumps () function is a tuple that contains a string that separates the items of the list and the dictionary key values. The default is (', ', ': '). The space in the latter can be removed, and we can get a more compact output. [python] view plain copy $ python json_compact_encoding.py DATA: [{' A]: ' A ', ' C ': 3.0, ' B ': (2, 4)}] repr (dat A): Dumps (data): Dumps (data, indent=2): Dumps (data, separators): 29

Coded dictionary

In the

JSON format, the dictionary key is restricted to a string type. If the key in the dictionary is a different type, a TypeError exception is generated when the object is encoded. One way to resolve this limitation is to skip all non-string-type keys using the Skipkeys parameter when encoding. Strong>[python] View plain copy import json   data = [ {  ' A ': ' A ',  ' B ':(2, 4),  ' C ':3.0,  (' d ',): ' D tuple '  } ]   print  ' first  Attempt '    try:       print json.dumps (data)    except  TypeError, err:       print  ' ERROR: ', err   print    print  ' second attempt '    print json.dumps (data, skipkeys=true)      

A key that is not a string type is ignored without throwing an exception. [python] view plain copy $ Python json_skipkeys.py the attempt error:key (' d ',) is not a string Second a Ttempt [{"A": "A", "C": 3.0, "B": [2, 4]}]

handling of custom types

All of the examples above are examples of Python's built-in types, because they are all supported by JSON itself. Of course, custom types also often need to be encoded correctly. Here are two things:

First, for the encoding of a class: [Python] view plain Copy class MyObj (object): Def __init__ (self, s): Self.s = S def __repr__ (self): Return ' <myobj (%s) > '% SELF.S

The simplest way to encode a MyObj object is to define a transformation function that converts a position type out of a known type. You don't have to encode it yourself, and you just need to convert one object to another.[Python] View plain copy import json   import json_myobj   obj = json_myobj. MyObj (' Instance value goes here ')    print  ' first attempt '    try:        print json.dumps (obj)    except typeerror, err:        print  ' ERROR: ', err   def convert_to_builtin_type ( obj):       print  ' Default (',  repr (obj),  ') '         # Convert objects to a dictionary of their  representation       d = {  ' __class__ ': obj.__class__.__name__,               ' __module__ ':obj.__module__,            }       d.update (obj.__dict __)        return d   print   print  ' with default '    print  Json.dumps (obj, default=convert_to_builtin_type)     

In the Convert_to_builtin_type () function, a class object that is not recognized by JSON is converted to a dictionary information that contains enough to reconstruct the object. [python] view plain copy $ Python json_dump_default.py-A-attempt ERROR: <myobj (instance value goes her E) > is isn't JSON serializable with default default (<myobj (instance value goes here) >) {"s": "Instance Valu E goes Here "," __module__ ":" Json_myobj "," __class__ ":" MyObj "}

In order to decode the result data and create a myobj instance, we need to match the decoder so that the class can be imported from the module and the instance is created. We use the Object_hook parameter in the loads () function.

In the input stream, each dictionary obtained for decoding invokes Object_hook, which converts the dictionary to other types of objects. The hook function returns the object that is required by the calling program, not the dictionary. [python] view plain copy import json   Def dict_to_object (d):       if  ' __class__ '  in d:            class_name = d.pop (' __class__ ')             module_name = d.pop (' __module__ ')             module = __import__ (module_name)            print   ' MODULE: ', module           class_ =  GetAttr (module, class_name)   <

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.