1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
#pike __REAL_VERSION__ 
 
//! Serialization interface. 
//! 
//! This module contains APIs to simplify 
//! serialization and deserialization of 
//! objects. 
//! 
//! @seealso 
//!   @[serialize()], @[deserialize()] 
 
constant Serializable = Builtin.Serializable; 
constant serialize = Builtin.serialize; 
constant deserialize = Builtin.deserialize; 
 
 
//! Simple base for an object that can be serialized by encode_value. 
//! Also supports decoding. 
//! 
//! Uses @[Serializable] as it's base. 
//! 
//! Simply inherit this class in the classes you want to have encoded 
//! and decoded. 
//! 
//! Note that it's not automatically recursive, objects assigned to 
//! variables in this object have to be Encodeable on their own for 
//! encode to work. 
//! 
//! When decoding only variables that existed at the time the object 
//! was encoded are assigned, that is, if the class now has more 
//! variables they new variables will be set to 0. 
class Encodeable 
{ 
    inherit Serializable; 
 
    //! Callback for encoding the object. Returns a mapping from 
    //! variable name to value. 
    //! 
    //! Called automatically by @[encode_value], not normally called 
    //! manually. 
    mapping(string:mixed) _encode() 
    { 
        mapping res = ([]);; 
        _serialize( this, lambda( mixed val, string name, mixed type ) 
                          { 
                              res[name] = val; 
                          }); 
        return res; 
    } 
 
    // Override the one from Serializable. 
    void _deserialize_variable(function deserializer, 
                               function(mixed:void) setter, 
                               string symbol, 
                               mixed symbol_type) 
    { 
        deserializer(setter,symbol,symbol_type); 
    } 
 
    //! Callback for decoding the object. Sets variables in the object 
    //! from the values in the mapping. 
    //! 
    //! Called automatically by @[decode_value], not normally called 
    //! manually. 
    void _decode(mapping(string:mixed) m) 
    { 
        _deserialize( this, lambda( function(mixed:void) set, 
                                    string name, mixed type ) 
                            { 
                                set( m[name] ); 
                            } ); 
    } 
}