json2python-models icon indicating copy to clipboard operation
json2python-models copied to clipboard

Add parameter to add the values from json as default values in the model

Open teodor-t-tenev opened this issue 4 years ago • 1 comments

Describe

It would be great if there is an option to add the values from json as default values in the Model

Reproduce (Optional)

Package version: ...

JSON data:

{
  "name": "John Smith",
  "hometown": {
    "name": "New York",
    "id": 123
  }
}
...

Desired result

A clear and concise description of what you want to happen. Class code:

r"""
generated by json2python-models v0.2.4 at Tue Oct 19 22:08:54 2021
command: /Users/teodor.tenev/PycharmProjects/TestProj/venv/bin/json2models -m Car package.json -f dataclasses
"""
from dataclasses import dataclass, field
from typing import Literal

@dataclass
class Hometown:
    name: Literal["New York"] = 'New York'
    id_: int = 123

@dataclass
class Car:
    hometown: 'Hometown' = Hometown()
    name: Literal["John Smith"] = 'John Smith'
...

teodor-t-tenev avatar Oct 19 '21 19:10 teodor-t-tenev

This project is designed to generate models from large json datasets. So default value could be generated only when there is just one value for given field in whole dataset. In my opinion this is not very common case. Also currently there is no logic to track non-string values. So for now I will not plan to implement it. But PR are always welcome. Just do not break existing tests and logic and provide option to disable this feature.

bogdandm avatar Nov 26 '21 13:11 bogdandm