Json-to-Dart-Model icon indicating copy to clipboard operation
Json-to-Dart-Model copied to clipboard

[BUG] <title>

Open madatr opened this issue 2 years ago • 5 comments

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: Screenshot 2022-03-18 at 10 00 59

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 Screenshot 2022-03-18 at 10 16 04 Screenshot 2022-03-18 at 10 01 12 Screenshot 2022-03-18 at 10 05 09

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

madatr avatar Mar 18 '22 07:03 madatr

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 avatar Mar 18 '22 07:03 madatr

@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.

iamarnas avatar Mar 18 '22 09:03 iamarnas

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

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 avatar Mar 18 '22 11:03 madatr

@madatr I will look at this when I will find a time. For now you can generate without r@ and add manually required keyword.

iamarnas avatar Mar 18 '22 11:03 iamarnas

Take your time, already did :)

madatr avatar Mar 18 '22 11:03 madatr