org.gwt.json.serialization.client.utils.JsonHashMapSerializer.java Source code

Java tutorial

Introduction

Here is the source code for org.gwt.json.serialization.client.utils.JsonHashMapSerializer.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.client.utils;

import com.google.gwt.json.client.JSONException;
import com.google.gwt.json.client.JSONNull;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;
import org.gwt.json.serialization.client.GenericType;
import org.gwt.json.serialization.client.JsonGenericTypeSerializer;
import org.gwt.json.serialization.client.Serializer;

import java.util.HashMap;
import java.util.Map;

/**
 * @author andrii borovyk 30.07.2015
 */
public class JsonHashMapSerializer implements JsonGenericTypeSerializer<HashMap> {

    private Serializer serializer;

    public JsonHashMapSerializer(Serializer serializer) {
        this.serializer = serializer;
    }

    @Override
    public JSONValue serialize(HashMap obj, GenericType[] genericTypes) {
        if (obj == null)
            return JSONNull.getInstance();
        JSONObject jsonObject = new JSONObject();
        GenericType keyType = genericTypes[0];
        GenericType valueType = genericTypes[1];

        for (Object entry : obj.entrySet()) {
            Map.Entry objAsEntry = (Map.Entry) entry;
            JSONValue key = serializer.getSerializerForType(keyType.getName()).serialize(objAsEntry.getKey(),
                    keyType.getTypes());
            JSONValue value = serializer.getSerializerForType(valueType.getName()).serialize(objAsEntry.getValue(),
                    valueType.getTypes());

            //if key is a String, everything is just perfect. But if not, hm... let's use key.toString() for now
            jsonObject.put(key.isString() != null ? key.isString().stringValue() : key.toString(), value);
        }
        return jsonObject;
    }

    @Override
    public HashMap deSerialize(JSONValue jsonObj, GenericType[] genericTypes) throws JSONException {
        if (jsonObj == null || jsonObj.isObject() == null)
            return null;
        HashMap obj = new HashMap();
        GenericType keyType = genericTypes[0];
        GenericType valueType = genericTypes[1];
        JSONObject mapObject = jsonObj.isObject();
        for (String key : mapObject.keySet()) {
            //well, key is always String, in this case...
            obj.put(key, serializer.getSerializerForType(valueType.getName()).deSerialize(mapObject.get(key),
                    valueType.getTypes()));
        }
        return obj;
    }
}