From 7a7ab42dd7ed977e0e276a07c210c79fd2955463 Mon Sep 17 00:00:00 2001 From: Peter Hinch Date: Mon, 10 Feb 2020 18:17:37 +0000 Subject: [PATCH] Add Serialisation doc. --- SERIALISATION.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/SERIALISATION.md b/SERIALISATION.md index dae8dec..f64ddf1 100644 --- a/SERIALISATION.md +++ b/SERIALISATION.md @@ -108,7 +108,23 @@ Decoded data (partial): test more ``` Consequently encoded strings may be separated with `'\n'` before saving and -reading may be done using `readline` methods. +reading may be done using `readline` methods as in this code fragment where +`u` is a UART instance: + +```python +s = ujson.dumps(data) +# pickle produces a bytes object whereas ujson produces a string +# In the case of ujson it is probably wise to convert to bytes +u.write(s.encode()) +# Pickle: +# u.write(s) +u.write(b'\n') + +# receiver +s = u.readline() +v = ujson.loads(s) # ujson can cope with bytes object +# ujson and pickle can cope with trailing newline. +``` # 3. ustruct