All Projects → lagmoellertim → Pyon

lagmoellertim / Pyon

Licence: mit
The Pythonic way to use JSON - with native objects 🛠 and path support 📁

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Pyon

Jc
CLI tool and python library that converts the output of popular command-line tools and file-types to JSON or Dictionaries. This allows piping of output to tools like jq and simplifying automation scripts.
Stars: ✭ 967 (+998.86%)
Mutual labels:  json, convert
Xresloader
跨平台Excel导表工具(Excel=>protobuf/msgpack/lua/javascript/json/xml)
Stars: ✭ 161 (+82.95%)
Mutual labels:  json, convert
Gelatin
Transform text files to XML, JSON, or YAML
Stars: ✭ 150 (+70.45%)
Mutual labels:  json, convert
X2struct
Convert between json string and c++ object. json字符串和c++结构体之间互相转换
Stars: ✭ 251 (+185.23%)
Mutual labels:  json, convert
Canmatrix
Converting Can (Controller Area Network) Database Formats .arxml .dbc .dbf .kcd ...
Stars: ✭ 486 (+452.27%)
Mutual labels:  json, convert
Gravity
Gravity Programming Language
Stars: ✭ 3,968 (+4409.09%)
Mutual labels:  json, object-oriented
Zson
专为测试人员打造的JSON解析器
Stars: ✭ 181 (+105.68%)
Mutual labels:  json, path
Ason
[DEPRECATED]: Prefer Moshi, Jackson, Gson, or LoganSquare
Stars: ✭ 777 (+782.95%)
Mutual labels:  json, convert
Jsoncsv
a command tool easily convert json file to csv or xlsx
Stars: ✭ 43 (-51.14%)
Mutual labels:  json, convert
Blooddy crypto
ActionScript (AS3) library for processing binary data. This library contains MD5, SHA-1, SHA-2 ( SHA-224 и SHA-256 ), Base64, CRC32 algorithms, JSON encoder & decoder as well as PNG and JPEG encoders.
Stars: ✭ 83 (-5.68%)
Mutual labels:  json
Dbwebapi
(Migrated from CodePlex) DbWebApi is a .Net library that implement an entirely generic Web API (RESTful) for HTTP clients to call database (Oracle & SQL Server) stored procedures or functions in a managed way out-of-the-box without any configuration or coding.
Stars: ✭ 84 (-4.55%)
Mutual labels:  json
Internettools
XPath/XQuery 3.1 interpreter for Pascal with compatibility modes for XPath 2.0/XQuery 1.0/3.0, custom and JSONiq extensions, XML/HTML parsers and classes for HTTP/S requests
Stars: ✭ 82 (-6.82%)
Mutual labels:  json
Redisjson
RedisJSON - a JSON data type for Redis
Stars: ✭ 1,239 (+1307.95%)
Mutual labels:  json
Diagram Vue
A editable SVG-based diagram component for Vue
Stars: ✭ 86 (-2.27%)
Mutual labels:  json
Js2excel
😌 😃 👿 A simple module for excel and json converts each other, which works in the browser.
Stars: ✭ 83 (-5.68%)
Mutual labels:  json
Openfintech
Opensource FinTech standards & payment provider data
Stars: ✭ 87 (-1.14%)
Mutual labels:  json
Mongoose Patch History
Mongoose plugin that saves a history of JSON patch operations for all documents belonging to a schema in an associated 'patches' collection
Stars: ✭ 82 (-6.82%)
Mutual labels:  json
Entityframework.commontools
Extensions, Auditing, Concurrency Checks, JSON properties and Transaction Logs for EntityFramework and EFCore
Stars: ✭ 82 (-6.82%)
Mutual labels:  json
Superdelete
SuperDelete is a Windows command line application (.NET) that can be used to delete files and directories with very long paths - longer than 260 characters.
Stars: ✭ 87 (-1.14%)
Mutual labels:  path
Pager Api
Easy API pagination for Rails
Stars: ✭ 86 (-2.27%)
Mutual labels:  json

Pyon

Introduction

Pyon (Pythonic JSON) is a Python library which allows you to easily convert native objects into JSON objects.

It also supports filesystem-like path-structure, which allows you to easily construct you JSON objects just the way you like it.

Additionally, it uses recursion in order to also convert every connected object into a usable form.

Prerequisites

  • Python >= 3.2

Installation

The installation via pip is as easy as typing

pip install pyon-lib

If you want to install the newest version manually, you can also do this:

git clone https://github.com/lagmoellertim/pyon.git

cd pyon

python3 setup.py install

Build

git clone https://github.com/lagmoellertim/pyon.git

cd pyon

python3 setup.py sdist bdist_wheel

Usage

Import Pyon

from pyon import PyonObject

Create a basic Object

class Test(PyonObject):
    def __init__(self):
        self.var1 = "Variable 1"
        self.var2 = 5.5

Convert an object into JSON

test = Test()
json = test.dump()

And this is the output:

{'var1': 'Variable 1', 'var2': 5.5}

Write the JSON object to a file

test = Test()
json = test.dump(file_object=open("test.json","w+"), output_format="json")

Hide certain variables

Let's say your Test-Class only needs the variable var2 for it's own calculation, but you don't want it to end up in your final JSON object. You can avoid this by adding the prefix "_"

class Test(PyonObject):
    def __init__(self):
        self.var1 = "Variable 1"
        self._var2 = 5.5
        
test = Test()
json = test.dump()

And this is the output:

{'var1': 'Variable 1'}

Use multiple objects / classes

As an example, I use the concept of a store with products

class Store(PyonObject):
    def __init__(self, store_name):
        self.store_name = store_name
        self.products = [
            Product(0, "Smartphone"),
            Product(1, "Laptop")
        ]
        
class Product(PyonObject):
    def __init__(self, article_id, name):
        self.article_id = article_id
        self.name = name
        
store = Store("Generic Store")
json = store.dump()

And this is the output:

{
    'store_name': 'Generic Store',
    'products': 
        [
            {'article_id': 0, 'name': 'Smartphone'},
            {'article_id': 1, 'name': 'Laptop'}
        ]
}

Specifying object paths

This time, the JSON structure should not be based on the class structure, but rather on the path string that we supply.

Path Strings are very similar to your filesystem paths. You can navigate inside the JSON object using these Path Strings.

Here are some example:

path1 = "/store/"

path2 = "/test/../store/./" # Identical to path1 since ../ means one layer up and ./ can be ignored

path3 = "/store/products/*" #The star symbol tells pyon to create a list instead of a dict in this location

Let's modify our Store / Products Class in order for it to use custom paths

class Store(PyonObject):
    def __init__(self, store_name):
        super().__init__("/stores/*")
        self.store_name = store_name
        self.products = [
            Product(0, "Smartphone"),
            Product(1, "Laptop")
        ]
        
class Product(PyonObject):
    def __init__(self, article_id, name):
        super().__init__("products/*")
        self.article_id = article_id
        self.name = name
        
store = Store("Generic Store")
json = store.dump()

And this is the output:

{
    'stores':
    [
        {'store_name': 'Generic Store', 'products': 
            [
                {'article_id': 0, 'name': 'Smartphone'},
                {'article_id': 1, 'name': 'Laptop'}
            ]
        }
    ]
}

As you can see in the example above, relative paths are also possible as long as the object has a parent.

Use object variables for the Path String

To use object variables inside of you Path String, simply specify them in the String using brackets like this: {var1}

The value of {var1} is self.var1 , so you just leave the 'self.' away.

class Test(PyonObject):
    def __init__(self):
        super().__init__("/test/{var1}-{_var2}/")
        self.var1 = "Variable 1"
        self._var2 = 5.5
        
test = Test()
json = test.dump()

And this is the output:

{
    'test':
    {
        'Variable 1-5.5': {'var1': 'Variable 1'}
    }
}

Different dump methods

Pyon supports different methods for dumping the PyonObject. You can use the normal python dictionary ('json'), 'xml' or 'yaml'. Here are some usage examples:

json = test.dump(output_format="json") # JSON is default, so you can also leave it empty

xml = test.dump(output_format="xml")

yaml = test.dump(output_format="yaml")

Allow Overwrite

Finally, there is Overwrite Protection. Since Paths allow you to freely choose the location where the object should end up, it is possible for them to overlap. To allow / stop overwriting, you can do this:

json = test.dump(allow_overwrite=True)

The default value for allow_overwrite is False

Documentation

If you get stuck at some point while trying to use the API, take a look the code. It is fully commented and well-labeled, which should help you understand what's going on.

Contributing

If you are missing a feature or have new idea, go for it! That is what open-source is for!

Author

Tim-Luca Lagmöller (@lagmoellertim)

Donations / Sponsors

I'm part of the official GitHub Sponsors program where you can support me on a monthly basis.

GitHub Sponsors

You can also contribute by buying me a coffee (this is a one-time donation).

Ko-Fi Sponsors

Thank you for your support!

License

MIT License

Copyright © 2019-present, Tim-Luca Lagmöller

Have fun 🎉

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].