com.khubla.cbean.serializer.impl.json.JSONArrayFieldSerializer.java Source code

Java tutorial

Introduction

Here is the source code for com.khubla.cbean.serializer.impl.json.JSONArrayFieldSerializer.java

Source

/*
 * cBean Copyright 2016, Tom Everett <tom@khubla.com>
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.khubla.cbean.serializer.impl.json;

import java.lang.reflect.Array;
import java.lang.reflect.Field;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.json.JSONArray;

import com.khubla.cbean.CBean;
import com.khubla.cbean.CBeanKey;
import com.khubla.cbean.CBeanServer;
import com.khubla.cbean.annotation.Property;
import com.khubla.cbean.serializer.FieldSerializer;
import com.khubla.cbean.serializer.SerializerException;

public class JSONArrayFieldSerializer implements FieldSerializer {
    public JSONArrayFieldSerializer() {
    }

    @Override
    public boolean applies(Field field) {
        return field.getType().isArray();
    }

    @Override
    public void delete(Object o, Field field) throws SerializerException {
        final Property property = field.getAnnotation(Property.class);
        if ((null != property) && (property.cascadeDelete() == true)) {
            throw new SerializerException("Not implemented");
        }
    }

    @Override
    public void deserialize(Object o, Field field, String value) throws SerializerException {
        try {
            final JSONArray jsonArray = new JSONArray(value);
            final Object od = Array.newInstance(field.getType().getComponentType(), jsonArray.length());
            PropertyUtils.setProperty(o, field.getName(), od);
            final Class<?> componentType = field.getType().getComponentType();
            if (componentType.isPrimitive()) {
                for (int i = 0; i < jsonArray.length(); i++) {
                    final String v = jsonArray.getString(i);
                    PropertyUtils.setIndexedProperty(o, field.getName(), i,
                            ConvertUtils.convert(v, field.getType().getComponentType()));
                }
            } else {
                final CBean<Object> cBean = CBeanServer.getInstance().getCBean(componentType);
                for (int i = 0; i < jsonArray.length(); i++) {
                    final Object co = cBean.load(new CBeanKey(jsonArray.getString(i)));
                    PropertyUtils.setIndexedProperty(o, field.getName(), i,
                            ConvertUtils.convert(co, field.getType().getComponentType()));
                }
            }
        } catch (final Exception e) {
            throw new SerializerException(e);
        }
    }

    @Override
    public String serialize(Object o, Field field) throws SerializerException {
        try {
            final Class<?> componentType = field.getType().getComponentType();
            if (componentType.isPrimitive()) {
                final String[] values = BeanUtils.getArrayProperty(o, field.getName());
                final JSONArray jsonArray = new JSONArray(values);
                return jsonArray.toString();
            } else {
                final CBean<Object> cBean = CBeanServer.getInstance().getCBean(componentType);
                final Object[] values = (Object[]) PropertyUtils.getProperty(o, field.getName());
                final JSONArray jsonArray = new JSONArray();
                if (null != values) {
                    for (int i = 0; i < values.length; i++) {
                        cBean.save(values[i]);
                        jsonArray.put(i, cBean.getId(values[i]));
                    }
                }
                return jsonArray.toString();
            }
        } catch (final Exception e) {
            throw new SerializerException(e);
        }
    }
}