mirror of
				https://github.com/kovidgoyal/calibre.git
				synced 2025-11-04 03:27:00 -05:00 
			
		
		
		
	Persist QByteArray as python bytearrays in JSONConfig
This commit is contained in:
		
							parent
							
								
									4a967a28c3
								
							
						
					
					
						commit
						869c2bf89e
					
				@ -386,19 +386,23 @@ class XMLConfig(dict):
 | 
				
			|||||||
def to_json(obj):
 | 
					def to_json(obj):
 | 
				
			||||||
    if isinstance(obj, bytearray):
 | 
					    if isinstance(obj, bytearray):
 | 
				
			||||||
        return {'__class__': 'bytearray',
 | 
					        return {'__class__': 'bytearray',
 | 
				
			||||||
                '__value__': base64.standard_b64encode(bytes(obj))}
 | 
					                '__value__': base64.standard_b64encode(bytes(obj)).decode('ascii')}
 | 
				
			||||||
    if isinstance(obj, datetime.datetime):
 | 
					    if isinstance(obj, datetime.datetime):
 | 
				
			||||||
        from calibre.utils.date import isoformat
 | 
					        from calibre.utils.date import isoformat
 | 
				
			||||||
        return {'__class__': 'datetime.datetime',
 | 
					        return {'__class__': 'datetime.datetime',
 | 
				
			||||||
                '__value__': isoformat(obj, as_utc=True)}
 | 
					                '__value__': isoformat(obj, as_utc=True)}
 | 
				
			||||||
 | 
					    if hasattr(obj, 'toBase64'):
 | 
				
			||||||
 | 
					        return {'__class__': 'bytearray',
 | 
				
			||||||
 | 
					                '__value__': bytes(obj.toBase64()).decode('ascii')}
 | 
				
			||||||
    raise TypeError(repr(obj) + ' is not JSON serializable')
 | 
					    raise TypeError(repr(obj) + ' is not JSON serializable')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def from_json(obj):
 | 
					def from_json(obj):
 | 
				
			||||||
    if '__class__' in obj:
 | 
					    custom = obj.get('__class__')
 | 
				
			||||||
        if obj['__class__'] == 'bytearray':
 | 
					    if custom is not None:
 | 
				
			||||||
 | 
					        if custom == 'bytearray':
 | 
				
			||||||
            return bytearray(base64.standard_b64decode(obj['__value__']))
 | 
					            return bytearray(base64.standard_b64decode(obj['__value__']))
 | 
				
			||||||
        if obj['__class__'] == 'datetime.datetime':
 | 
					        if custom == 'datetime.datetime':
 | 
				
			||||||
            from calibre.utils.iso8601 import parse_iso8601
 | 
					            from calibre.utils.iso8601 import parse_iso8601
 | 
				
			||||||
            return parse_iso8601(obj['__value__'], assume_utc=True)
 | 
					            return parse_iso8601(obj['__value__'], assume_utc=True)
 | 
				
			||||||
    return obj
 | 
					    return obj
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user