Skip to main content

A tool to convert any class to json and convert json to object

Project description

PyPI version

Tool to convert Class to Json and Json to Object (SmartJson)

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

Usage

Requirements

Python >= 2.7 must be installed.

How to run

  • use code from github or
  • pip install smartjson

Parameters

  • cls: Class 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 scripts.__smart_json__ import SmartJson

class Pull:
    def __init__(self):
        self.id = 2
        self.title = "Iam pull"
        self.author = "Joel O."
        self.subPull = Pull.SubPull()

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


class Jobs:
    def __init__(self):
        self.name = 'John'
        self.url = "5444"
        self.id = 1
        self.job = Jobs.Job()

    def name(self, set=None):
        if set != None:
            self.name = set

        return self.name

    class Job:
        def __init__(self):
            self.job_name = 'Test'
            self.job_url = "_blank"
            self.date = datetime.datetime.now()
            self.date2 = datetime.datetime.now()
            self.item = Jobs.Item()
            self.pull = Pull()

    class Item:
        def __init__(self):
            self.item_name = 'item 1'
            self.item_boof = datetime.datetime.now()
            self.mylist = [1, 2, 3]
            self.another = Jobs.Item.Another()

        class Another:
            def __init__(self):
                self.age = 26
                self.precision = 99.56
                self.ville = "Lille"
                self.meteo = Jobs.Item.Another.Meteo()

            class Meteo:
                def __init__(self):
                    self.pluie = True
                    self.complex = complex(12, 78)
                    self.tuple = [((1, 'a'), (2, 'b'))]
                    self.none = None

jb = Jobs()
smart_json = SmartJson(jb)

# Disable pretty print
serialize = smart_json.serialize(pretty=False)
print(serialize)

# Use pretty print
pretty = SmartJson(Jobs()).serialize()
print(pretty)

Output:

{
  "id": 1,
  "job": {
    "date": "2019-09-30 13:57:36.340899",
    "date2": "2019-09-30 13:57:36.340899",
    "item": {
      "another": {
        "age": 26,
        "meteo": {
          "complex": "(12+78j)",
          "none": "",
          "pluie": true,
          "tuple": [
            [
              [
                1,
                "a"
              ],
              [
                2,
                "b"
              ]
            ]
          ]
        },
        "precision": 99.56,
        "ville": "Lille"
      },
      "item_boof": "2019-09-30 13:57:36.340899",
      "item_name": "item 1",
      "mylist": [
        1,
        2,
        3
      ]
    },
    "job_name": "Test",
    "job_url": "_blank",
    "pull": {
      "author": "Joel O.",
      "id": 2,
      "subPull": {
        "subAuthor": "OKJ.",
        "subId": 3,
        "subTitle": "I am sub title"
      },
      "title": "Iam pull"
    }
  },
  "name": "John",
  "url": "5444"
}

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

###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-0.0.3.tar.gz (4.2 kB view hashes)

Uploaded Source

Built Distribution

smartjson-0.0.3-py2-none-any.whl (4.9 kB view hashes)

Uploaded Python 2

Supported by

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