1 """
2 Service to handle serialization and deserialization of fit objects.
3
4 Object serialization is useful for long term storage, interlanguage
5 communication and network transmission. In all cases, the process
6 involves an initial encode() followed by a later decode().
7
8 This module provides the following serialization classes::
9
10 CPickle
11 Pickle
12 JSON
13
14 For each serialization class we have two static methods::
15
16 encode(object) -> string
17 decode(string) -> object
18
19 For serial encoding we will use json. The json format is human
20 readable and easily parsed. json itself does not define support
21 of Inf/NaN, though some json tools support it using the native
22 javascript values of Infinity and Nan. Various xml encodings are
23 also possible, though somewhat more difficult to work with.
24 """
25
26 try:
27 import cPickle as pickle
28 except:
29 import pickle
30 try:
31 import simplejson as json
32 except ImportError:
33 import json
34 from park.util.serial import deconstruct,reconstruct
37 """
38 Network transport encoding so that we can switch between
39 pickle, xml, json, google, ... without having to rewrite
40 ServiceManager.
41
42 Codec by itself is the null service, and so can be used
43 for the single node fitter which doesn't need to marshall
44 its parameters.
45 """
46 @staticmethod
48 """network transport encoder"""
49 return s
50 @staticmethod
52 """network transport decoder"""
53 return s
54
56 """
57 Codec for pickle.
58 """
59 @staticmethod
60 - def encode(s): return pickle.dumps(s)
61 @staticmethod
62 - def decode(s): return pickle.loads(s)
63
65 """
66 Codec for JSON
67
68 Basic python types (list, string, dictionary, numbers, boolean, None)
69 are converted directly to the corresponding string representation.
70 tuples and sets are converted to lists, and str is converted to unicode.
71
72 Python objects are represented by::
73
74 {
75 '.class': 'module.classname',
76 '.version': 'versionstring',
77 '.state': { object state }
78 }
79
80 where state comes from the object __getstate__, the object __dict__ or
81 the object __slots__. See the pickle documentation for details.
82
83 Python functions are represented by::
84
85 {
86 '.function': 'module.functionname'
87 }
88
89 """
90 @staticmethod
92 """
93 Convert structure to JSON string.
94 """
95 return json.dumps(deconstruct(s))
96
97 @staticmethod
99 """
100 Convert JSON string to structure.
101 """
102 return reconstruct(json.loads(s))
103
109
110 if __name__ == "__main__": test()
111