2

I need to serialize a couple of objects in my Android app and send them to web service. The model classes for objects have various int fields which need to be converted into meaningful string representations from various arrays before sending to web service.

So, I am assuming that easiest way will be to use gson or xstream (JSON or XML - anything is fine) but with following method: - I'll mark all existing int fields as transient and exclude them from serialization - I'll create new get method per field. The get method will read value of corresponding integer and return its string representation.

But in either of 2 libraries - gson or xstream, I am unable to find way to serialize based on getters instead of fields. Please suggest.

And yes, I DO NOT need to deserialize the data back.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • Why don't you like the String representation of ints, that JSON libraries do? – Andres May 30 '14 at 15:33
  • Try if Jaxb helps. Jaxb supports annotations on getters. refer http://stackoverflow.com/questions/10617267/jaxb-annotations-for-setter-getter – jsjunkie May 30 '14 at 15:34
  • By string representations I mean corresponding string entity from respective arrays. Example: Property: Gender (int) Elsewhere there is an array {"Male", "Female"} Based on Gender's value (1 or 2), I need to write "Male" or "Female" into serialized json/xml. – user3691875 May 30 '14 at 15:34
  • Marking all int fields as transient is a terrible idea. – DwB May 30 '14 at 15:50
  • @DwB True. Agreed. Dropped the idea :P – user3691875 May 31 '14 at 13:19

2 Answers2

0

I think you need a wrapper class.

Consider this:

public class Blammy
{
    private int gender;

    ... imagine the rest of the class.
}

public class BlammyWrapper
{
    private String gender;

    public BlammyWrapper(final Blammy blammy)
    {
        if (blammy.gender == 1)
        {
            gender = "Its a Boy";
        }
        else if (blammy.gender == 2)
        {
            gender = "girl";
        }
        else // always check your boundary conditions.
        {
           throw new InvalidArgumentException("Naughty blammy; unrecognized gender value");
        }

        public String gender()
        {
            return gender;
        }
    }
DwB
  • 37,124
  • 11
  • 56
  • 82
  • Thanks for suggestion. Though I chose to improve my code architecture by changing my model files as explained below in my answer. Thanks nevertheless. Was useful insight. Cheers. – user3691875 May 31 '14 at 13:24
0

Ok, finally, I followed this approach: 1. Removed all resource arrays from my app code 2. Added enums with toString for each current array 3. Changed all int properties to be of corresponding enum type 4. Used XStream 5. Added a custom convertor for XStream for generic enum types where if it finds any property of type enum, then it will marshal it using toString method.

Thanks for all support btw. All your answers and comments atleast made me clear that my current code architecture needed drastic improvement.