Json-to-Dart-Model
Json-to-Dart-Model copied to clipboard
[BUG] <title>
Is there an existing issue for this?
- [x] I have searched the existing issues
Current Behavior
Hi, Firstly thank you so much for this super package/extension, its very useful.
I think I came across a strange bug when using r@ annotation in the .jsonc file.
When trying to create a class that has a required value object the script fails (does not generate the corresponding file) and return the following error:
Strangely when trying to create a class that has a required list of objects it works flawlessly.
Please find the attached screenshots of some trials that I did:
![Screenshot 2022-03-18 at 10 03 13](https://user-images.githubusercontent.com/45605051/158954648-be0e8d6f-52e8-4d0f-be3b-4cdfbc30f0b8.png)
![Screenshot 2022-03-18 at 10 16 04](https://user-images.githubusercontent.com/45605051/158954635-4e0151e6-94e7-47e0-a6b5-8febaad9f3f5.png)
![Screenshot 2022-03-18 at 10 01 12](https://user-images.githubusercontent.com/45605051/158954642-0da6e34e-38f0-4cb7-9fe0-9714f8982770.png)
![Screenshot 2022-03-18 at 10 05 09](https://user-images.githubusercontent.com/45605051/158954622-33edc548-c165-4f21-923f-4191f0584ee4.png)
Expected Behavior
that the required object files are generated
Steps To Reproduce
I am using Json Serialisable.
Version
latest
Relevant JSON syntax
{
"__className": "location", // <- The base class name of the object.
"__path": "/lib/models/", // <- override default path with a new one by adding '__path' key.
"r@latitude": 0.1,
"r@longitude": 0.1
},
{
"__className": "customer_shop", // <- The base class name of the object.
"__path": "/lib/models/", // <- override default path with a new one by adding '__path' key.
"r@shopId": 5,
"r@name": "shopName",
"[email protected]": {},
"phoneNumber": "0787383510",
"r@shopCategory": "shopcategory"
},
{
"__className": "customer_profile", // <- The base class name of the object.
"__path": "/lib/models/", // <- override default path with a new one by adding '__path' key.
"r@applicationUserId": 18,
"r@block": false,
"r@username": "+962700000000",
"r@name": "TESTUSER",
"r@phoneNumber": "+962700000000",
"[email protected]_shop": [
{}
]
}
Anything else?
No response
ps. I tried changing the name of the file from "[email protected]": {}
to "[email protected]": {}
just in case it was confusing the script and it still resulted in the same error.
If it is useful here is the generated CustomerShop class whenever the error happens:
class CustomerShop {
CustomerShop();
factory CustomerShop.fromJson(Map<String, dynamic> json) {
// TODO: implement fromJson
throw UnimplementedError('CustomerShop.fromJson($json) is not implemented');
}
Map<String, dynamic> toJson() {
// TODO: implement toJson
throw UnimplementedError();
}
}
@madatr Hi 👋 and thank you for your very detailed report.
By working with multiple JSON in one file you need to be careful. I see some bad practice. Given your example, you need to wrap your JSON to the list 👇
[ // <- Start of the list body.
{
"__className": "customer_shop",
//...
},
{
"__className": "customer_profile",
//...
}
] // // <- End of the list body.
Don't do that 👇
/** BAD */
[
{
"__className": "location", // <- Location object
"__path": "/lib/models/",
"laitidude": 0.1,
"longitude": 0.1
},
{
"__className": "customer_profile",
"__path": "/lib/models/",
"location": {} // <- same Location object in the same map.
//...
}
]
/** GOOD */
[
{
"__className": "customer_profile",
"__path": "/lib/models/",
"location": { // <- put here instead Location.
"laitidude": 0.1,
"longitude": 0.1
}
//...
}
]
Object naming 👇
/** BAD */
[
{
"__className": "customer_profile", // <- Required class name because the JSON object does not have a keyword.
"location": { // <- This object has a keyword.
"__className": "location", // <- Do not add a class name to an object with keys.
"laitidude": 0.1,
"longitude": 0.1
}
//...
}
]
/** GOOD */
[
{
"__className": "customer_profile", // <- There's only one name per object.
"location": {
"laitidude": 0.1,
"longitude": 0.1
}
//...
}
]
Try to upgrade your JSON and tell us about the problems you are facing :wink: I will check the required annotation for objects.
Hi @iamarnas thank you for the quick reply and for the nice tips, I just started using your package and in fact jsonc only today. If I find the time I may contribute some time to add to the documentation of this package for newbies like me.
I followed your recommendations for the json but it still gives me the error and not generate any class file whenever I set the location to required. (I did try both ways) so I guess it may be a bug after all.
![Screenshot 2022-03-18 at 14 00 14](https://user-images.githubusercontent.com/45605051/158992108-6bb22987-15e2-40ac-8cf4-0267047ace80.png)
Here is the snippet if you'd like to try and replicate the issue:
[{
"__className": "customer_profile", // <- The base class name of the object.
"__path": "/lib/models/", // <- override default path with a new one by adding '__path' key.
"r@applicationUserId": 18,
"r@block": false,
"r@username": "+962700000000",
"r@name": "TESTUSER",
"r@phoneNumber": "+962700000000",
"[email protected]_shop": [
{
"r@shopId": 5,
"r@name": "shopName",
"r@location": {
"r@latitude": 0.1,
"r@longitude": 0.1
},
"phoneNumber": "0787383510",
"r@shopCategory": "shopcategory"
}
]
}]
@madatr I will look at this when I will find a time. For now you can generate without r@
and add manually required
keyword.
Take your time, already did :)