hiranthaR / Json-to-Dart-Model

Json to Dart Model extension can convert JSON objects into Dart data classes. It supports pure Dart class conversion, Flutter-recommended JSON serialization using annotations, Freezed support, Effective Dart:Style, and many more features. Currently, it has more than 135,000 installs.
https://marketplace.visualstudio.com/items?itemName=hirantha.json-to-dart
MIT License
93 stars 18 forks source link

How to reference already exisiting Model from another file #91

Closed globalwebforce closed 2 years ago

globalwebforce commented 2 years ago

Is there an existing issue for this?

Current Behavior

Product Model

{
id: 1,
name: "product-name"
}

Shop Model

{
id: 1,
name: "Jane Doe Shop"
products: [ ]    // I want to use Product Model here without copying again the json structure of Product Model
}

Expected Behavior

No response

Steps To Reproduce

No response

Version

No response

Relevant JSON syntax

No response

Anything else?

No response

iamarnas commented 2 years ago

@globalwebforce Hi 👋 If you have the same JSON objects in the different files you can put them into one and the generator will merge them with the imports path. Otherwise, you need to write import path self. In your case, you don't need two JSON objects just merge them into one 👇

{
    "id": 1,
    "name": "Jane Doe Shop",
    "products": [{
    "id": 1,
    "name": "product-name"
    }]
}
globalwebforce commented 2 years ago

Thank you for your response but I still don't get it sorry :( In my case, I have 3 JsonObjects A,B,C that needs JsonObject Z (a very big json data). How can I tell to the 3 JsonObjects to use JsonObject Z (must be required)?

JsonObject Z

{
id: 1
title: "I am Z"
}

JsonObject A

{
id: 1
z : //How?
}

JsonObject B

{
id: 1
z : //How?
}

JsonObject C

{
id: 1
z : //How?
}
iamarnas commented 2 years ago

You can generate multiple JSON to one folder by adding a patch. And generator will remove duplicate files. In your case should be like that 👇

Note: by adding a patch you need to be careful. Because the generator reads your all JSON objects from the all tracked directory from the top to the bottom. In your case I would recommend the z JSON file kept above the a, b, c. But, if all a, b, and c have identical z then no matter where and how you sort your JSON.

{
"__className": "z",
"__path": "/lib/models/multi_json",
"id": 1
"title": "I am Z"
}
{
"__className": "a",
"__path": "/lib/models/multi_json",
"id": 1
"z": {
id: 1
title: "I am Z"
}
}
{
"__className": "b",
"__path": "/lib/models/multi_json",
"id": 1
"z": {
id: 1
title: "I am Z"
}
}
{
"__className": "c",
"__path": "/lib/models/multi_json",
"z": {
id: 1
title: "I am Z"
}
}
// files directory.
./json_models/
|-z.json
|-a.json
|-b.json
|-c.json

@globalwebforce

globalwebforce commented 2 years ago

Oh I get it, I just thought that there could be a shortcut to reference a JsonObect specially if it's very big. Base on your example, you copy-pasted the whole json structure of JsonObject Z to the other JsonObjects. Now I know what I'm doing is right, I can now proceed on my Project, Thank you very much!

iamarnas commented 2 years ago

@globalwebforce You are welcome. Just remember, when you copy and paste JSON Z structure to JSON A don't forget change json key as Z class name in the JSON A.

princ3od commented 2 years ago

I think this cool trick should be noted in the Readme file.