org.gwt.json.serialization.InternalArraySerializerGenerator.java Source code

Java tutorial

Introduction

Here is the source code for org.gwt.json.serialization.InternalArraySerializerGenerator.java

Source

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gwt.json.serialization;

import com.google.gwt.core.ext.typeinfo.JArrayType;
import com.google.gwt.core.ext.typeinfo.JParameterizedType;
import com.google.gwt.core.ext.typeinfo.JType;
import com.google.gwt.core.ext.typeinfo.NotFoundException;
import com.google.gwt.user.rebind.SourceWriter;

import java.util.Set;

/**
 * @author andrii borovyk 28.12.2015
 */
public class InternalArraySerializerGenerator implements InternalSG {

    private JArrayType jArrayType;

    private Set<JArrayType> jArrayTypes;

    public InternalArraySerializerGenerator(JArrayType jArrayType, Set<JArrayType> jArrayTypes) {
        this.jArrayType = jArrayType;
        this.jArrayTypes = jArrayTypes;
    }

    public String generateSerializers(SourceWriter writer) throws NotFoundException {
        String serializerName = jArrayType.getQualifiedSourceName().replaceAll("\\.", "\\$").replaceAll("\\[\\]",
                "_1") + "_SerializableImpl";
        writer.println("public class " + serializerName + " extends JsonSimpleTypeSerializer<"
                + jArrayType.getQualifiedSourceName() + "> { ");
        writer.indent();
        writer.println("public " + serializerName + "(){}");

        writer.println("public JSONValue serialize(" + jArrayType.getQualifiedSourceName() + " obj) {");
        writer.indent();
        writer.println("if (obj == null) return JSONNull.getInstance();");
        writer.println("JSONArray jsonArray = new JSONArray();");
        writer.println("for (int i=0; i< obj.length; i++) {");
        writer.indent();
        JType componentType = jArrayType.getComponentType();
        String fieldType = GenericGenerationUtils.resolveType(componentType);
        if (componentType.isArray() != null) {
            writer.println(
                    "jsonArray.set(i, getSerializerForType(\"" + fieldType + "\").serialize(obj[i], null));");
            registerArraySerializer(componentType.isArray());
        } else {
            JParameterizedType parametrizedType = componentType.isParameterized();
            writer.println("jsonArray.set(i, getSerializerForType(\"" + fieldType + "\").serialize(obj[i], "
                    + GenericGenerationUtils.getGenericTypesParam(componentType,
                            parametrizedType != null ? parametrizedType.getTypeArgs() : null)
                    + "));");
        }
        writer.outdent();
        writer.println("}");

        writer.println("return jsonArray;");
        writer.outdent();
        writer.println("} ");
        writer.println();

        writer.println("public " + jArrayType.getQualifiedSourceName()
                + " deSerialize(JSONValue jsonObj) throws JSONException {");
        writer.indent();

        writer.println("if (jsonObj == null) return null;");

        writer.println("JSONArray arr = jsonObj.isArray();");
        writer.println(jArrayType.getQualifiedSourceName() + " response = new "
                + insertAsFirstArray(jArrayType.getComponentType().getQualifiedSourceName(), "[arr.size()]") + ";");
        writer.println("JsonGenericTypeSerializer<" + fieldType + "> serializer = getSerializerForType(\""
                + fieldType + "\");");
        writer.println("for (int i=0; i<arr.size(); i++) {");
        writer.indent();
        if (componentType.isArray() != null) {
            writer.print("response[i] = serializer.deSerialize(arr.get(i), null);");
            registerArraySerializer(componentType.isArray());
        } else {
            JParameterizedType parametrizedType = componentType.isParameterized();
            writer.print("response[i] = serializer.deSerialize(arr.get(i), "
                    + GenericGenerationUtils.getGenericTypesParam(componentType,
                            parametrizedType != null ? parametrizedType.getTypeArgs() : null)
                    + ");");
        }
        writer.outdent();
        writer.println("}");
        writer.println("return response;");
        writer.outdent();
        writer.println("}");
        writer.outdent();
        writer.println("}");
        return serializerName;
    }

    protected void registerArraySerializer(JArrayType type) {
        jArrayTypes.add(type);
    }

    protected String insertAsFirstArray(String qName, String str) {
        int firstArr = qName.indexOf("[");
        if (firstArr > -1) {
            return qName.substring(0, firstArr) + str + qName.substring(firstArr);
        } else {
            return qName + str;
        }
    }

}