Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6194705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:20:28+00:00 2026-05-24T03:20:28+00:00

In my code I have a domain object with an Enum type in it.

  • 0

In my code I have a domain object with an Enum type in it. That enum type saves and retrieves fine from the database. I am converting a list of domain objects into json cells. All fields except the enum are either a string or a long. When the json gets picked up ‘on the other side’ it displays [Object object] for the column, instead of the enum name or enum value. Is there something on either the domain or json side that would help with this? Code/example below

Domain Class

class MyDomain {
long id
long otherValue
MyEnum enu  //Nullable per constraints
//Mapping and constraints are not special. 
}

Enum

enum MyEnum {
ENUM1("Value1"),ENUM2("Value2")
//constructor ommitted
String myValue
String toString() { myValue }

Json cell creation

def jsonCells = domainList.collect
    {
        [cell: [
                it.id,
                it.otherValue,
                it.enu?.value
            ],
            id: it.id]
    }

it.enu?.value works. However, is there somehow a better way to do this where I don’t have to call the value every time and can rely on the object? I would have assumed that overriding the toString method would have taken care of it, but apparently I was wrong. I know it seems like a minor issue, but its easier to forget “.value”, especially as the same enum will be used in many domain objects. Ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:20:29+00:00Added an answer on May 24, 2026 at 3:20 am

    Since you’re simply putting it in a List when building the object, it will treat it as an Object unless you specifically do something else with it (like you’re doing currently).

    One alternative would be to use it.enu as String or something similar, but that still probably doesn’t achieve what you’re trying to achieve.

    Another (maybe overengineered?) way would be to create a method on the domain itself that returns the value, and then use that method when building JSON:

    class MyDomain {
        MyEnum enu
    
        def enuVal() {
            enu?.value
        }
    }
    

    with

    def jsonCells = domainList.collect {
        [cell: [
            it.enuVal()
        ],
        ...]
    

    As for me, I guess I’d just prefer to use it.enu?.val in the JSON. Write a test for your JSON-rendering methods and make sure the value is what you expect it to be, so that you don’t forget.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a library function that returns a list of domain objects from a
I have the following code in a class that is called from a web
I have the current basic structure for each domain object that I need to
have a Grails domain object that has a custom static function to grab data
I have a piece of code that 'updates' an entry in the database by
I have a domain object, Expense, that has a field called initialFields . It's
HI, I have code below but getting error object does not match target type
I have a domain model object which has properties of type System.DateTimeOffset. I'm using
We have a simple method that gets all of a specific domain object where
I have a grails project that contains a few domain objects. I am using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.