Open madatr opened 2 years ago
ps. I tried changing the name of the file from "r@location.location": {}
to "r@location.point": {}
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.
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",
"r@shops.customer_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 :)
Is there an existing issue for this?
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:
Expected Behavior
that the required object files are generated
Steps To Reproduce
I am using Json Serialisable.
Version
Relevant JSON syntax
Anything else?
No response