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
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
}