JS>> Jzon>> 返回
项目作者: miguel3010

项目描述 :
Python Model JSON Serializer
高级语言: Python
项目地址: git://github.com/miguel3010/Jzon.git
创建时间: 2017-12-23T19:20:47Z
项目社区:https://github.com/miguel3010/Jzon

开源协议:

下载


Jzon

Python3 library for serialization and deserialization of python objects/dicts

Encode

  1. from Jzon import jsonify
  2. print(jsonify(a)) # a is a Class object, dict, or list
  1. import datetime
  2. from Jzon import jsonify
  3. class model(object):
  4. def __init__(self):
  5. self.id = 30
  6. self.name = "Miguel Angel Campos"
  7. self.height = 1.80
  8. self.intExample = 45
  9. self.boolExample = True
  10. self.model2 = model2()
  11. self.sex = "M"
  12. self.date = datetime.datetime.today()
  13. self.complexN = 1 + 2j
  14. self.arrayExample = []
  15. self.arrayExample.append(model2())
  16. class model2(object):
  17. def __init__(self):
  18. self.salary = 100.45
  19. self.bo = True
  20. a = model()
  21. print(jsonify(a))
  22. >>>{"boolExample":"true","arrayExample":[{"salary":100.45,"bo":"true"}],"complexN":{"type":"complex","real":1.0,"imag":2.0},"name":"Miguel Angel Campos","id":30,"sex":"M","model2":{"salary":100.45,"bo":"true"},"intExample":45,"height":1.8,"date":"2017-12-23T14:53:42"}

Decode

  1. from Jzon import unJsonify
  2. print(unJsonify(JSON_TXT)) # JSON_TXT is a JSON Formatted Text

```
a = model()
a.id = 0
a.name = “Miguel Ángel Campos”
a.salary = 100.45
a.none = None

a.model1 = model2()
a.model1.salary = 100.45
a.model1.new_employee = True

a.bo = “None”
a.fff = None
a.date = datetime.datetime(2001, 1, 1)
a.c = 1 + 2j
a.g = []
a.g.append(a.model1)

modelresponse = unJsonify(jsonify(a), model())

  1. > ## Parse
  2. Convert dict to python class object (if apply).

model() is the object with desired type

_dict is the dictionary corresponding to model

parse_from_dict(model(),_dict)
```

Authors

  • Miguel Ángel Campos - Software Engineer - Linkedin