<s> import json <EOL> from nativeconfig . exceptions import DeserializationError , ValidationError , InitializationError <EOL> from nativeconfig . options . base_option import BaseOption <EOL> class DictOption ( BaseOption ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , value_option = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( ) . __init__ ( name , getter = '<STR_LIT>' , setter = '<STR_LIT>' , ** kwargs ) <EOL> if value_option : <EOL> from nativeconfig . options import ArrayOption <EOL> if isinstance ( value_option , BaseOption ) and not isinstance ( value_option , ArrayOption ) and not isinstance ( value_option , DictOption ) : <EOL> self . _value_option = value_option <EOL> else : <EOL> raise InitializationError ( "<STR_LIT>" ) <EOL> else : <EOL> self . _value_option = None <EOL> def serialize ( self , value ) : <EOL> serializable_dict = { } <EOL> if isinstance ( self . _value_option , BaseOption ) : <EOL> for k , v in value . items ( ) : <EOL> serialized_v = self . _value_option . serialize ( v ) <EOL> serializable_dict . update ( { k : serialized_v } ) <EOL> return serializable_dict <EOL> else : <EOL> return value <EOL> def deserialize ( self , raw_value ) : <EOL> try : <EOL> if isinstance ( self . _value_option , BaseOption ) : <EOL> deserialized_dict = { } <EOL> for k , v in raw_value . items ( ) : <EOL> deserialized_dict . update ( { k : self . _value_option . deserialize ( v ) } ) <EOL> value = deserialized_dict <EOL> else : <EOL> value = raw_value <EOL> except DeserializationError : <EOL> raise DeserializationError ( "<STR_LIT>" . format ( raw_value , self . name ) , raw_value , self . name ) <EOL> else : <EOL> return value <EOL> def serialize_json ( self , value ) : <EOL> if value is None : <EOL> return json . dumps ( None ) <EOL> elif self . _value_option : <EOL> return '<STR_LIT:{>' + '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' . format ( json . dumps ( k ) , self . _value_option . serialize_json ( v ) ) for k , v in value . items ( ) ] ) + '<STR_LIT:}>' <EOL> else : <EOL> return json . dumps ( value ) <EOL> def deserialize_json ( self , json_value ) : <EOL> try : <EOL> value = json . loads ( json_value ) <EOL> except ValueError : <EOL> raise DeserializationError ( "<STR_LIT>" . format ( self . name , json_value ) , json_value , self . name ) <EOL> else : <EOL> if value is not None : <EOL> if not isinstance ( value , dict ) : <EOL> raise DeserializationError ( "<STR_LIT>" . format ( json_value ) , json_value , self . name ) <EOL> else : <EOL> if self . _value_option : <EOL> return { k : self . _value_option . deserialize_json ( json . dumps ( v ) ) for k , v in value . items ( ) } <EOL> else : <EOL> return value <EOL> else : <EOL> return None <EOL> def validate ( self , value ) : <EOL> super ( ) . validate ( value ) <EOL> if not isinstance ( value , dict ) : <EOL> raise ValidationError ( "<STR_LIT>" . format ( value , self . name ) , value , self . name ) </s>
