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

Java tutorial

Introduction

Here is the source code for org.gwt.json.serialization.InternalSerializerGenerator.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.*;
import com.google.gwt.user.rebind.SourceWriter;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

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

    private JClassType jClassType;

    private Set<JArrayType> jArrayTypes;

    public InternalSerializerGenerator(JClassType jClassType, Set<JArrayType> jArrayTypes) {
        this.jClassType = jClassType;
        this.jArrayTypes = jArrayTypes;
    }

    public String generateSerializers(SourceWriter writer) throws NotFoundException {

        Set<JField> candidates = AccessorResolveUtil.resolveFieldCandidates(jClassType);

        List<FieldInfo> resolvedFields = new ArrayList<FieldInfo>();
        for (JField jField : candidates) {
            resolvedFields.add(AccessorResolveUtil.resolveAccessor(jField, jClassType));
        }

        String serializerName = jClassType.getName().replaceAll("\\.", "\\$") + "_SerializableImpl";
        writer.println("public class " + serializerName + " extends JsonSimpleTypeSerializer<"
                + jClassType.getQualifiedSourceName() + "> { ");
        writer.indent();
        writer.println("public " + serializerName + "(){}");

        writer.println("public JSONValue serialize(" + jClassType.getQualifiedSourceName() + " obj) {");
        writer.indent();
        writer.println("JSONObject jsonObj = new JSONObject();");
        writer.println("String type;");
        writer.println("String fieldName;");
        for (FieldInfo fieldInfo : resolvedFields) {

            JParameterizedType parametrizedType = fieldInfo.getjField().getType().isParameterized();
            generateGenericTypeSerializer(fieldInfo,
                    parametrizedType != null ? parametrizedType.getTypeArgs() : null, writer);
        }
        writer.println("return jsonObj;");
        writer.outdent();
        writer.println("} ");
        writer.println();

        writer.println("public " + jClassType.getQualifiedSourceName()
                + " deSerialize(JSONValue jsonObj) throws JSONException {");
        writer.indent();
        writer.println("JSONObject obj = jsonObj.isObject();");
        writer.println(jClassType.getQualifiedSourceName() + " response = new "
                + jClassType.getQualifiedSourceName() + "();");
        writer.println("String type;");
        writer.println("String fieldName;");
        for (FieldInfo fieldInfo : resolvedFields) {
            JParameterizedType parametrizedType = fieldInfo.getjField().getType().isParameterized();
            generateGenericTypeDeSerializer(fieldInfo,
                    parametrizedType != null ? parametrizedType.getTypeArgs() : null, writer);
        }
        writer.println("return response;");
        writer.outdent();
        writer.println("}");

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

        return serializerName;
    }

    private void generateGenericTypeDeSerializer(FieldInfo fieldInfo, JClassType[] types, SourceWriter writer)
            throws NotFoundException {
        String fieldType = GenericGenerationUtils.resolveType(fieldInfo.getjField().getType());
        writer.println("type = \"" + fieldType + "\";");
        writer.println("fieldName = \"" + fieldInfo.getjField().getName() + "\";");
        writer.println("JsonGenericTypeSerializer<" + fieldType + "> " + fieldInfo.getjField().getName()
                + "Serializer = getSerializerForType(type);");
        writer.println("response." + fieldInfo.getSetAccessorName() + "(" + fieldInfo.getjField().getName()
                + "Serializer.deSerialize(obj.get(fieldName), "
                + GenericGenerationUtils.getGenericTypesParam(fieldInfo.getjField().getType(), types) + "));");
        if (fieldInfo.getjField().getType().isArray() != null) {
            registerArraySerializer(fieldInfo.getjField().getType().isArray());
        }
    }

    private void generateGenericTypeSerializer(FieldInfo fieldInfo, JClassType[] types, SourceWriter writer)
            throws NotFoundException {
        /*
        * example:
        * type = "java.lang.String";
        * fieldName = "name";
        * JsonGenericTypeSerializer<java.lang.String> nameSerializer = getSerializerForType(type);
        * jsonObj.put(fieldName, nameSerializer.serialize(obj.getName(), null));
        *
        */
        String fieldType = GenericGenerationUtils.resolveType(fieldInfo.getjField().getType());
        writer.println("type = \"" + fieldType + "\";");
        writer.println("fieldName = \"" + fieldInfo.getjField().getName() + "\";");
        writer.println("JsonGenericTypeSerializer<" + fieldType + "> " + fieldInfo.getjField().getName()
                + "Serializer = getSerializerForType(type);");
        writer.println("jsonObj.put(fieldName, " + fieldInfo.getjField().getName() + "Serializer.serialize(obj."
                + fieldInfo.getGetAccessorName() + "(), "
                + GenericGenerationUtils.getGenericTypesParam(fieldInfo.getjField().getType(), types) + "));");

        if (fieldInfo.getjField().getType().isArray() != null) {
            registerArraySerializer(fieldInfo.getjField().getType().isArray());
        }
    }

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

}