From fd0bd240143e43d23380d1f72789ca19f6b1b92b Mon Sep 17 00:00:00 2001 From: smlng Date: Sat, 10 Jul 2021 12:14:57 +0200 Subject: [PATCH] docs: add JSON encoding example to README --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index 25cb03c..e4f9a75 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,35 @@ frame = LppFrame().from_bytes(buffer) print(frame) ``` +***JSON Encoding*** + +The LppUtil class provides helper function for proper JSON encoding of +PyCayenneLpp types, i.e. LppFrame, LppData and LppType. + +```python +import json + +from cayennelpp import LppFrame, LppUtil + +# create empty frame +frame = LppFrame() +# add some sensor data +frame.add_temperature(0, -1.2) +frame.add_humidity(6, 34.5) +# json encoding +print(json.dumps(frame, default=LppUtil.json_encode, indent=2)) +``` + +There are two wrapper functions to explicitly encode the LPP type as a +number or string, number being default for `LppUtil.json_encode` (see above): + +```python +# type as number +print(json.dumps(frame, default=LppUtil.json_encode_type_int, indent=2)) +# type as string +print(json.dumps(frame, default=LppUtil.json_encode_type_str, indent=2)) +``` + ## Contributing Contributing to a free open source software project can take place in many