All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.centurylink.mdw.common.translator.impl.StringStringMapTranslator Maven / Gradle / Ivy

There is a newer version: 6.1.39
Show newest version
/*
 * Copyright (C) 2019 CenturyLink, Inc.
 *
 * 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 com.centurylink.mdw.common.translator.impl;

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

import org.json.JSONException;
import org.json.JSONObject;

import com.centurylink.mdw.model.JsonObject;
import com.centurylink.mdw.translator.DocumentReferenceTranslator;
import com.centurylink.mdw.translator.TranslationException;

public class StringStringMapTranslator extends DocumentReferenceTranslator {

    @Override
    public Object realToObject(String json) throws TranslationException {
        try {
            Map stringMap = new HashMap<>();
            JSONObject jsonObject = new JsonObject(json);
            String[] stringNames = JSONObject.getNames(jsonObject);
            if (stringNames != null) {
                for (int i = 0; i < stringNames.length; i++) {
                    stringMap.put(stringNames[i], jsonObject.optString(stringNames[i], null));
                }
            }
            return stringMap;
        }
        catch (JSONException ex) {
            throw new TranslationException(ex.getMessage(), ex);
        }
    }

    @SuppressWarnings("unchecked")
    public String realToString(Object object) throws TranslationException {
        Map stringMap = (Map)object;
        JSONObject jsonObject = new JsonObject();
        Iterator it = stringMap.keySet().iterator();
        try {
            while (it.hasNext()) {
                String name = it.next();
                String val = stringMap.get(name);
                jsonObject.put(name, val == null ? JSONObject.NULL : val);
            }
        return jsonObject.toString(2);
        }
        catch (JSONException e) {
            throw new TranslationException(e.getMessage(), e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy