Grails 5 DeepDomainClassMarshaller JSON failing for Circular reference and for null values

Hey folks i trying to use Grails JSON feature for generating json from a domain class.
And i am facing many issues in this one after another.

If someone can help me here it would be great. I have following grails configuration

grailsVersion=5.3.2
grailsGradlePluginVersion=5.3.0
groovyVersion=3.0.11
gorm.version=7.3.3

I have following 3 domain classes.

Company.groovy

class Company{

    String name

    String contactName

    String agentId // If businessType = Accountant
    String status  = "Active"


    User owner
    User lastUpdatedBy


    static hasMany = [apps: ZApp]
    static embedded = ['address']
}

User.groovy

class User {

    Long id

    String fullName
    String username
    User owner
    User lastUpdatedBy
}

And ** ZApp.groovy**

class ZucoduApp{

    String appName

    String appDescription
}

I have added following line in my application.groovy file for deep domain marshaller.

grails.converters.json.default.deep = true

And than i tried to generate json for company instance as following

company as JSON

I am getting following error

Caused by: org.grails.web.converters.exceptions.ConverterException: java.lang.StringIndexOutOfBoundsException: start 0, end -1, length 0

When i check error trails it giving me error in JSON.java file in following line

enter image description here

I thought this should handle it automatically gracefully. but no, than i adding following line in application.groovy

grails.converters.json.circular.reference.behaviour=’PATH’

And error solved but now error comes whenever any field has null value. In my case agentId of company class is null

Caused by: org.grails.web.converters.exceptions.ConverterException: org.grails.web.json.JSONException: Value out of sequence: expected mode to be OBJECT or ARRAY
when writing ‘org.grails.web.json.JSONWriter$NullWritable@1a1fd03d’ but was INIT

Error coming at following code from DomainClassMarshaller.java

if ( !(property instanceof Association) ) {
                // Write non-relation property
                Object val = beanWrapper.getPropertyValue(property.getName());
                json.convertAnother(val); // **here it throws error**
            }

To By pass this error i created my own CustomDomainMarshaller class and in place of

json.convertAnother(val); i used

if (val == null) {
   writer.value(null)
} else {
   json.convertAnother(val);
}

And it worked.
But now i got another error for apps (Hasmany relation for ZApp)

Caused by: java.lang.ClassCastException: class org.grails.web.json.PathCapturingJSONWriterWrapper$PropertyElement cannot be cast to class org.grails.web.json.PathCapturingJSONWriterWrapper$IndexElement (org.grails.web.json.PathCapturingJSONWriterWrapper$PropertyElement and org.grails.web.json.PathCapturingJSONWriterWrapper$IndexElement are in unnamed module of loader ‘app’)

This error is coming when we have 2 item in apps. and when call second item.
Error is coming in following code.

    @SuppressWarnings(["unchecked", "rawtypes"])
    public void marshalObject(Object value, JSON json) throws ConverterException {
        JSONWriter writer = json.getWriter();
        value = proxyHandler.unwrapIfProxy(value);
        Class<?> clazz = value.getClass();

        List<String> excludes = json.getExcludes(clazz);
        List<String> includes = json.getIncludes(clazz);
        IncludeExcludeSupport<String> includeExcludeSupport = new IncludeExcludeSupport<String>();

        BeanWrapper beanWrapper = new BeanWrapperImpl(value);

        writer.object(); // **here it throws error**

And in postmen following json response coming.

{
    "data": [
        {
            "id": 72050630615,
            "lastUpdatedBy": {
                "id": 59396476865
            },
            "dateCreated": "2024-03-30 18:07:42",
            "contactName": "Chetan",
            "lastUpdated": "2024-05-22 18:54:21",
            "agentId": null,
            "apps": [
                {
                    "id": 2,
                    "appName": "ZUCODU MTD SERVICES",
                    "appDescription": "Use this app to file you tax using Zucodu MTD app"
                }{
                    "message": "Internal server error",
                    "error": 500
                }

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật