Skip to main content

Python libraries to convert class to json or Tool to convert Class, object and dict to Json and Json to Object

Project description

PyPI version PyPI version PyPI version

PyPI install PyPI version PyPI download

  • BigQuery | Google cloud <==>

Downloads Downloads Downloads

Python libraries to convert class to json, object and dict to Json and Json to Object (SmartJson)

SmartJson is a simple tool to convert any class or dict to JSON and convert json to Object.

Documentation

Features:

version (2.0.2)

  • Fix script
  • Add script support enumeration (enum)
  • Already support type : * class * date * datetime * set * OrderedDict * deque * list * int * float * bool * complex * tuple * str * dict * bytes * None
  • ex :

    from enum import Enum, IntEnum
    from scripts import SmartJson
    
    class LoggerLevel(Enum):
      CRITICAL = 'CRITICAL'
      ERROR = 'ERROR'
      WARNING = 'WARNING'
      INFO = 'INFO'
      DEBUG = 'DEBUG'
      NOTSET = "NOTSET"
    
    
    class Status(IntEnum):
      success = 0
      failure = 1
    
    print(SmartJson({'Log': LoggerLevel, 'Stat': Status}).serialize())
    
  • output :

    {
      "Log": [
        {
          "CRITICAL": "CRITICAL",
          "DEBUG": "DEBUG",
          "ERROR": "ERROR",
          "INFO": "INFO",
          "NOTSET": "data",
          "WARNING": "WARNING"
        }
      ],
      "Stat": [
        {
          "failure": 1,
          "success": 0
        }
      ]
    }
    

version (2.0.1)

  • Fix script

  • update complex serialization

  • Add new method (serializeToJsonFile) convert your class to json file

  • dict : Default parameter directory="outpout", filename="smart.json"

  • class : Default parameter directory="outpout", filename="className.json"

  • ex :

    • SmartJson(Test()).serializeToJsonFile(directory="yourPath", filename="MyFileName.json")

    • SmartJson(Test()).serializeToJsonFile() :=> (output) :=> outpout/Test.json

version (2.0.0)

Support :

  • Class
  • date
  • datetime
  • set
  • OrderedDict
  • deque
  • list
  • int
  • float
  • bool
  • complex
  • tuple
  • str
  • dict
  • bytes
  • None

Install

smartjson is released on PyPI, so all you need is:

$ pip install smartjson

To upgrade to latest version:

$ pip install --upgrade smartjson

Usage

Requirements

Python >= 2.7 must be installed.

Parameters

  • class or object or dict: you want to convert to json

Project structure:

  • scripts - source code of a package
  • example.py - working examples

Contribute

  1. If unsure, open an issue for a discussion
  2. Create a fork
  3. Make your change
  4. Make a pull request
  5. Happy contribution!

EXAMPLE

Class

import datetime
from collections import deque, OrderedDict
from scripts.__smart_json__ import SmartJson


class Test:
    def __init__(self):
        self.test = "none"
        self.id = 2
        self.date = datetime.datetime.now()
        self.tuple = [((1, 'a'), (2, 'b'))]


data = {
    "int": 1,
    "str": "SmartJson",
    "bytes": "pip install smartjson".encode("utf-8"),
    "date": datetime.date(2010, 1, 1),
    "datetime": datetime.datetime(2020, 1, 1, 18, 30, 0, 500),
    "pull": Test(),
    "set": (["1", 12, datetime.datetime.now()]),
    "list": [datetime.datetime.now(), Test()],
    "ordereddict": OrderedDict([
        ("b", OrderedDict([("b", Test()), ("a", datetime.datetime.now())])),
        ("a", OrderedDict([("b", 1), ("a", [((1, 'a'), (datetime.datetime.now(), 'b'))])])),
    ]),
    "deque": deque([
        deque([1, 2]),
        deque([3, 4]),
    ])
}


class Pull:
    def __init__(self):
        self.id = 2
        self.title = "Iam pull"
        self.author = "Joel O."
        self.subPull = Pull.SubPull()
        self.data = data
        self.date = datetime.datetime.now()
        self.list = [1, datetime.datetime.now(), Pull.SubPull()]

    class SubPull:
        def __init__(self):
            self.subId = 3
            self.subTitle = "I am sub title"
            self.subAuthor = "OKJ."
            self.date = datetime.date(2010, 1, 1)

# Example
my_json = SmartJson(data).serialize()
print(my_json)

Output:

{
  "bytes": "pip install smartjson",
  "date": "2010-01-01",
  "datetime": "2020-01-01 18:30:00.000500",
  "deque": {
    "1": 2,
    "3": 4
  },
  "int": 1,
  "list": [
    "2019-10-01 19:39:01.916122",
    {
      "date": "2019-10-01 19:39:01.916122",
      "id": 2,
      "test": "none",
      "tuple": [
        [
          [
            1,
            "a"
          ],
          [
            2,
            "b"
          ]
        ]
      ]
    }
  ],
  "ordereddict": {
    "a": {
      "a": [
        [
          [
            1,
            "a"
          ],
          [
            "2019-10-01 19:39:01.916122",
            "b"
          ]
        ]
      ],
      "b": 1
    },
    "b": {
      "a": "2019-10-01 19:39:01.916122",
      "b": {
        "date": "2019-10-01 19:39:01.916122",
        "id": 2,
        "test": "none",
        "tuple": [
          [
            [
              1,
              "a"
            ],
            [
              2,
              "b"
            ]
          ]
        ]
      }
    }
  },
  "pull": {
    "date": "2019-10-01 19:39:01.916122",
    "id": 2,
    "test": "none",
    "tuple": [
      [
        [
          1,
          "a"
        ],
        [
          2,
          "b"
        ]
      ]
    ]
  },
  "set": [
    "1",
    12,
    "2019-10-01 19:39:01.916122"
  ],
  "str": "SmartJson"
}

Json to Object

objFromFile = smart_json.toObjectFromFile("jobs.json")
obj = smart_json.toObject('{"people":[{"name":"Scott", "website":"stackabuse.com", "from":"Nebraska"}]}')
obj2 = smart_json.toObject({'item': 'Beer', 'cost': '£4.00'})

print(obj2.item, obj2.cost)
print(objFromFile.job.item.another.precision)
print(obj.people[0].name, obj.people[0].website)

# Beer £4.00
# 99.56
# Scott stackabuse.com

For support or coffee :)

screenshot

Author : Koffi Joel O.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smartjson-2.0.2.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

smartjson-2.0.2-py2-none-any.whl (7.6 kB view details)

Uploaded Python 2

File details

Details for the file smartjson-2.0.2.tar.gz.

File metadata

  • Download URL: smartjson-2.0.2.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/2.7.16

File hashes

Hashes for smartjson-2.0.2.tar.gz
Algorithm Hash digest
SHA256 15f537357e56da1a8cf87dd875ae3e4f76b6af3d3b6fa78ee1c3179fa71092a4
MD5 31e48177630e61fe6dfad739d75f43f9
BLAKE2b-256 6bf7ee24345e65c7cb05ba5697d3ebc1e39b7faae7d49ceaccb450752bc0d958

See more details on using hashes here.

File details

Details for the file smartjson-2.0.2-py2-none-any.whl.

File metadata

  • Download URL: smartjson-2.0.2-py2-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/2.7.16

File hashes

Hashes for smartjson-2.0.2-py2-none-any.whl
Algorithm Hash digest
SHA256 419847d6cddd0c40dd7d80ee04a289c2791711c00c135b93c4bc96a8770fbc20
MD5 33057865dba77ac3d2eeb72324a39176
BLAKE2b-256 9df476b103b515d6df2b95b6c50f7b71f7f7f7f8fa23ccae5bfe764e22fd4187

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page