Java Map to Json toJSON(Map map)

Here you can find the source of toJSON(Map map)

Description

to JSON

License

Apache License

Declaration

public static String toJSON(Map<String, String> map) 

Method Source Code

//package com.java2s;
/*//from w w  w  . j a  va 2  s .  c o m
 * Copyright (C) 2014 William JIANG
 * Created on May 18, 2015
 *
 * 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.
 */

import java.util.Map;

public class Main {
    public static final String JSON_OBJECT_START = "{";
    public static final String JSON_OBJECT_END = "}";
    public static final String JSON_ARRAY_START = "[";
    public static final String JSON_ARRAY_END = "]";
    public static final String JSON_SPLITTER = ", ";
    public static final String JSON_PAIR_SPLITTER = JSON_SPLITTER + "\n";
    public static final String JSON_NAME_VALUE_FORMAT = "\"%s\":\"%s\"" + JSON_PAIR_SPLITTER;
    public static final String JSON_NAME_ARRAY_FORMAT = "\"%s\":[%s]" + JSON_PAIR_SPLITTER;

    public static String toJSON(Map<String, String> map) {
        if (map == null)
            return null;
        else if (map.size() == 0)
            return "{}";

        // Append the object name if it is identified by key of an empty String
        StringBuilder jsonBuilder = new StringBuilder(map.containsKey("") ? map.get("") : "");
        jsonBuilder.append(JSON_OBJECT_START);
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (key.length() != 0) {
                // Simply guess by detecting of "[" and "]"
                // TODO: More robust way
                String format = (value.startsWith(JSON_ARRAY_START) && value.endsWith(JSON_ARRAY_END))
                        ? JSON_NAME_ARRAY_FORMAT
                        : JSON_NAME_VALUE_FORMAT;

                jsonBuilder.append(String.format(format, key, entry.getValue()));
            }
        }

        // Remove the last JSON_PAIR_SPLITTER
        if (jsonBuilder.indexOf(JSON_PAIR_SPLITTER) != -1) {
            jsonBuilder.setLength(jsonBuilder.length() - JSON_PAIR_SPLITTER.length());
        }

        jsonBuilder.append(JSON_OBJECT_END);
        return jsonBuilder.toString();
    }

    public static String toJSON(String objectName, Object[] objects) {
        if (objectName == null || objects == null)
            return null;

        String json = String.format(JSON_NAME_VALUE_FORMAT, objectName, toJSON(objects));

        return json;
    }

    public static String toJSON(Object[] objects) {
        StringBuilder jsonBuilder = new StringBuilder();
        int length = objects.length;
        for (int i = 0; i < length; i++) {
            jsonBuilder.append(String.format("\"%s\"", objects[i].toString()));
            if (i != length - 1) {
                jsonBuilder.append(JSON_SPLITTER);
            }
        }

        return jsonBuilder.toString();
    }
}

Related

  1. mapToJson(Map map)
  2. mapToJson(Map map, boolean pretty, String indent)
  3. mapToJson(Map parameters)
  4. mapToJson(Map map)
  5. toJson(Map map)
  6. toJSON(Map map)
  7. toJSON(Map map)
  8. toJSONString(Map map, int indentLevel)