Menu

Ignoring Iterable

Help
2015-05-14
2015-05-14
  • Robert Liberatore

    I have a class that implements Iterable. When using FlexJSON, the iterator gets used serialize the class by default. For this specific class, I need it to be serialized like a normal class; specifically, there's a float[] of x,y coordinates that need to be saved. The iterator loops through this float array, but outputs them as a convenience class XPoint. This results in FlexJSON saving serializing the class as an array of XPoints rather than a float[].

    I've tried using all the ways of setting the transformer to a ClassTransformer, but this ends up causing ObjectTransformer to throw an exception. I also tried extending ArrayTransformer so that it will deliberately pass the desired array from the object, to no avail:

    public class PointListTransformer extends ArrayTransformer {
        @Override
        public void transform(Object object) {
            if(object instanceof PointList)
                super.transform( ((PointList) object).getArray() );
            else super.transform(object);
        }
    }
    

    ​Same error, don't really know if it made sense to begin with anyways.

    I'll include the code for the class below, but I'm assuming there's something obvious I'm missing to do this. I know I could just not implement Iterable, but that seems like a pretty bad solution.

    public class PointList implements Iterable<XPoint> {
        private float[] array;
    
        public PointList() {
            array = null;
        }
    
        public PointList(float[] array) {
            this.array = array.clone();
        }
    
        public float getX(int index) {
            return array[index*2-2];
        }
    
        public float getY(int index) {
            return array[index*2-1];
        }
        @Override
        public Iterator<XPoint> iterator() {
            Iterator<XPoint> it = new Iterator<XPoint>() {
    
                private int currentIndex = 0;
    
                @Override
                public boolean hasNext() {
                    return (currentIndex < count());
                }
    
                @Override
                public XPoint next() {
                    XPoint newXPoint;
                    newXPoint = getXPoint(++currentIndex);
                    return newXPoint;
                }
    
                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
    
        return it;
        }
    
        public int count() {
            return array.length / 2;
        }
    
        @JSON(include = false)
        public XPoint getXPoint(int index) {
            return new XPoint(array[index*2-2], array[index*2-1]);
        }
    
        @JSON(include = true)
        public float[] getArray() {
            return array;
        }
    
    }
    

    Example JSON from FlexJSON:

    ...
        "vertices": [
        {
        "x": 146.21454,
        "y": 400.45535
        },
        {
        "x": 301.21454,
        "y": 400.45535
        },
        {
        "x": 223.71454,
        "y": 266.2214
        }
        ]
    ...
    
     

    Last edit: Robert Liberatore 2015-05-14
  • Robert Liberatore

    Well, guess I should have waited five more minutes before posting. Solved the issue with a better understanding of custom transformers:

    ~~~~~~
    public class PointListTransformer extends AbstractTransformer {
    @Override
    public void transform(Object object) {
    final float[] array = ((PointList) object).getArray();
    getContext().transform(array);
    }
    }
    ~~~~~~~

    with .transform(new PointListTransformer(), PointList.class) in the serializer. Here's hoping it works with the deserializer as well....

     

Log in to post a comment.

MongoDB Logo MongoDB