Git Product home page Git Product logo

selfusepy's Introduction

Self-Use Python Lib

PyPI image image image

DirTree

image

Json To Object

Usage

more info in json_test_cases

import selfusepy
obj: One = selfusepy.parse_json(jsonStr, One())

Notice

Because Python is not a strongly-typed language, so you must
assign a value when you define a variable in class, 
otherwise the parser can not get the right type of each variable, 
just like examples below 

e.g. 1

Python Class

from selfusepy.jsonparse import BaseJsonObject
class One(BaseJsonObject):

  def __init__(self):
    self.x: str = ''  # have to be init
    self.two: One.Two = One.Two()  # have to be init

  class Two(BaseJsonObject):
    def __init__(self):
      self.y: str = ''
      self.three: One.Two.Three = One.Two.Three()

    class Three(BaseJsonObject):
      def __init__(self):
        self.z: str = ''

Json str

{
  "x": "x",
  "two": {
    "y": "y",
    "three": {
      "z": "z"
    }
  }
}

e.g. 2

Python Class

from selfusepy.jsonparse import BaseJsonObject
from typing import List
class One1(BaseJsonObject):

  def __init__(self):
    self.x: str = ''
    self.two: List[One1.Two] = [One1.Two()]

  class Two(BaseJsonObject):
    def __init__(self):
      self.y: str = ''

Json str

{
  "x": "x",
  "two": [
    {
      "y": "y1"
    },
    {
      "y": "y2"
    }
  ]
}

e.g. 4

from selfusepy.jsonparse import DeserializeConfig, BaseJsonObject
from selfusepy.utils import override_str
@override_str
@DeserializeConfig({'x--': 'x'})
class One2(BaseJsonObject):

  def __init__(self):
    self.x: str = ''
    self.two: One2.Two = One2.Two()

  @override_str
  @DeserializeConfig({'y--': 'y'})
  class Two(BaseJsonObject):
    def __init__(self):
      self.y: str = ''
      self.three: One2.Two.Three = One2.Two.Three()

    @override_str
    @DeserializeConfig({'z--': 'z'})
    class Three(BaseJsonObject):
      def __init__(self):
        self.z: str = ''

Json str

{
  "x--": "x",
  "two": {
    "y--": "y",
    "three": {
      "z--": "z"
    }
  }
}

e.g. 5

def handle(x):
  return datetime.fromisoformat(x)

@DeserializeConfig({"date": JsonField(func = handle)})
class Obj(BaseJsonObject):

  def __init__(self):
    self.date: datetime = datetime.now()

selfusepy's People

Contributors

luomingxu avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.