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

  • SEARCH
  • Home
  • 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 8344509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:23:50+00:00 2026-06-09T06:23:50+00:00

I am working on how to serialised a POJO into a json string. I

  • 0

I am working on how to serialised a POJO into a json string. I am using the jackson library and have run into a problem.

A value in the json object can either be a string or a string array like so…

{"mimeTypes":"all"}

or

 {"mimeTypes":["application/pdf", "application/msword"]}

This is my approach at the moment

@JsonIgnore
private String mimeTypes;
@JsonIgnore
private String[] mimeTypesArray;
@JsonRawValue
@JsonProperty("integration/enabled-mime-types")
private String mimeType;

public void setMimeTypes(String mimeTypes) {
    this.mimeTypes = mimeTypes;
    mimeType = mimeTypes;
}

public void setMimeTypes(String[] mimeTypes) {
    this.mimeTypesArray = mimeTypes;
    try {
        JSONArray jsonArray = new JSONArray(mimeTypes);
        this.mimeType = jsonArray.toString();
    } catch (JSONException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
}

Basically I have a field for the string, a field for the string array, and a field for the string I want to add to the json object. I serialise on the setter so the object mapper doesn’t have to choose which field to use.

This feels like a hacky way of doing things. Would there be a better solution? I have been reading about using jackson views but I am not sure if this is possible for my problem.

  • 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-06-09T06:23:51+00:00Added an answer on June 9, 2026 at 6:23 am

    I haven’t tested it, but I’d try adding a dedicated method for computing what should be serialized:

    class MyMimeTypes {
        @JsonIgnore
        private String[] mimeTypesArray;
    
        /** A convenience method for setting just one item. */
        public void setMimeType(String mimeType) {
            this.mimeTypesArray = new String[] { mimeType };
        }
    
        public void setMimeTypes(String[] mimeTypes) {
            this.mimeTypesArray = mimeTypes;
        }
    
        // The JSON-specific part:
    
        @JsonProperty("mimeTypes")
        public Object jsonMimeTypes() {
            if (mimeTypesArray.length == 1)
                return mimeTypesArray[0];
            else
                return mimeTypesArray;
        }
    }
    

    This way, if you have just one item in the array, the serialized value will be just the item. Otherwise the whole array will be serialized.

    Note that it is a bad practise having two setters with the same name and different signatures. AFAIK that violates the Java Beans specification.

    Perhaps you could use a vararg method here to replace the two setters (but I don’t know how this goes together with Java Beans specification):

    public void setMimeTypes(String... mimeTypes) {
        this.mimeTypesArray = mimeTypes;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with serialization in C# right now and I've run into a problem
I'm working with a large data frame, and have run up against RAM limits.
I've been working in CakePHP and I got the following problem. I have a
I have an Object of my POJO Class and I saved it on my
While working with ActiveRecord I have a table which stores a serialized array of
I'm working on a problem where I need to store serialized hierarchies of Ruby
Working on a small game using an HTML5 canvas, and javascript. And it's working
Working with an undisclosed API, I found a function that can set the number
Working with Reporting Services 2008 r2. So here's my issue: We have 5 reports
I'm working on getting my .NET object serialized / deserialized. As a requirement for

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.