@@ -51,6 +51,69 @@ jp.jsonset('baz', Path.rootPath(), 'qaz')
5151jp.execute()
5252```
5353
54+ ## Encoding/Decoding
55+
56+ rejson-py uses Python's [ ` json ` ] ( https://docs.python.org/2/library/json.html ) .
57+ The client can be set to use custom encoders/decoders at creation, or by calling
58+ explicitly the [ ` setEncoder() ` ] ( API.md#setencoder ) and
59+ [ ` setDecoder() ` ] ( API.md#setdecoder ) methods, respectively.
60+
61+ The following shows how to use this for a custom class that's stored as
62+ a JSON string for example:
63+
64+ ``` python
65+ from json import JSONEncoder, JSONDecoder
66+ from rejson import Client
67+
68+ class CustomClass (object ):
69+ " Some non-JSON-serializable"
70+ def __init__ (self , s = None ):
71+ if s is not None :
72+ # deserialize the instance from the serialization
73+ if s.startswith(' CustomClass:' ):
74+ ...
75+ else :
76+ raise Exception (' unknown format' )
77+ else :
78+ # initialize the instance
79+ ...
80+
81+ def __str__ (self ):
82+ _str = ' CustomClass:'
83+ # append the instance's state to the serialization
84+ ...
85+ return _str
86+
87+ ...
88+
89+ class CustomEncoder (JSONEncoder ):
90+ " A custom encoder for the custom class"
91+ def default (self , obj ):
92+ if isinstance (obj, CustomClass):
93+ return str (obj)
94+ return json.JSONEncoder.encode(self , obj)
95+
96+ class TestDecoder (JSONDecoder ):
97+ " A custom decoder for the custom class"
98+ def decode (self , obj ):
99+ d = json.JSONDecoder.decode(self , obj)
100+ if isinstance (d, basestring ) and d.startswith(' CustomClass:' ):
101+ return CustomClass(d)
102+ return d
103+
104+ # Create a new instance of CustomClass
105+ obj = CustomClass()
106+
107+ # Create a new client with the custom encoder and decoder
108+ rj = Client(encoder = CustomEncoder(), decoder = CustomDecoder())
109+
110+ # Store the object
111+ rj.jsonset(' custom' , Path.rootPath(), obj))
112+
113+ # Retrieve it
114+ obj = rj.jsonget(' custom' , Path.rootPath())
115+ ```
116+
54117## Class Client
55118This class subclasses redis-py's ` StrictRedis ` and implements ReJSON's
56119commmands (prefixed with "json").
@@ -68,9 +131,8 @@ def __init__(self, encoder=None, decoder=None, *args, **kwargs)
68131
69132Creates a new ReJSON client.
70133
71-
72- `` encoder `` is an instance of a `` json.JSONEncoder `` class.
73- `` decoder `` is an instance of a `` json.JSONDecoder `` class.
134+ `` encoder `` should be an instance of a `` json.JSONEncoder `` class
135+ `` decoder `` should be an instance of a `` json.JSONDecoder `` class
74136
75137
76138### jsonarrappend
@@ -303,7 +365,8 @@ def setDecoder(self, decoder)
303365
304366
305367
306- Sets the decoder
368+ Sets the client's decoder
369+ `` decoder `` should be an instance of a `` json.JSONDecoder `` class
307370
308371
309372### setEncoder
@@ -315,13 +378,14 @@ def setEncoder(self, encoder)
315378
316379
317380
318- Sets the encoder
381+ Sets the client's encoder
382+ `` encoder `` should be an instance of a `` json.JSONEncoder `` class
319383
320384
321385
322386
323387## Class Path
324- None
388+ This class represents a path in a JSON value
325389### \_\_ init\_\_
326390``` py
327391
@@ -331,5 +395,8 @@ def __init__(self, path)
331395
332396
333397
398+ Make a new path based on the string representation in ` path `
399+
400+
334401
335402
0 commit comments