Java Json fromDictionary(Map dictionary)

Here you can find the source of fromDictionary(Map dictionary)

Description

from Dictionary

License

Open Source License

Declaration

public static String fromDictionary(Map<String, Object> dictionary) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.List;
import java.util.Map;
import javax.json.Json;

import javax.json.JsonArrayBuilder;
import javax.json.JsonBuilderFactory;

import javax.json.JsonObjectBuilder;

public class Main {
    private static JsonBuilderFactory jsonFactory = Json.createBuilderFactory(null);

    public static String fromDictionary(Map<String, Object> dictionary) {
        if (dictionary == null) {
            return null;
        }// w  w w  .ja  v a2 s  . c  o m
        JsonObjectBuilder root = jsonFactory.createObjectBuilder();
        fill_map(root, dictionary);
        return root.build().toString();
    }

    private static void fill_map(JsonObjectBuilder jsonObject, Map<String, Object> map) {
        if (jsonObject == null) {
            return;
        }
        for (String key : map.keySet()) {
            Object value = map.get(key);
            fill_key_value(jsonObject, key, value);
        }
    }

    private static void fill_key_value(JsonObjectBuilder jsonObject, String key, Object value) {
        if (jsonObject == null || key == null) {
            return;
        }

        if (value == null) {
            jsonObject.addNull(key);
        } else if (value instanceof String) {
            jsonObject.add(key, (String) value);
        } else if (value instanceof Integer) {
            jsonObject.add(key, ((Integer) value).intValue());
        } else if (value instanceof Long) {
            jsonObject.add(key, ((Long) value).longValue());
        } else if (value instanceof Boolean) {
            jsonObject.add(key, ((Boolean) value).booleanValue());
        } else if (value instanceof List) {
            fill_list(jsonObject, key, (List<Object>) value);
        } else if (value instanceof Map) {
            JsonObjectBuilder entryObj = jsonFactory.createObjectBuilder();
            fill_map(entryObj, (Map<String, Object>) value);
            jsonObject.add(key, entryObj);
        }
    }

    private static void fill_list(JsonObjectBuilder jsonObject, String key, List<Object> list) {
        if (jsonObject == null || key == null) {
            return;
        }
        JsonArrayBuilder arrayBuilder = jsonFactory.createArrayBuilder();
        for (Object entry : list) {
            if (entry instanceof Map) {
                JsonObjectBuilder entryObj = jsonFactory.createObjectBuilder();
                fill_map(entryObj, (Map<String, Object>) entry);
                arrayBuilder.add(entryObj);
            }
        }
        jsonObject.add(key, arrayBuilder);
    }
}

Related

  1. convertJsonValue(Object jsonValue, Class desiredType)
  2. escape(String text)
  3. fill_dictionary(Map dictionary, JsonObject jsonObject)
  4. fill_key_value(JsonObjectBuilder jsonObject, String key, Object value)
  5. fill_list(JsonObjectBuilder jsonObject, String key, List list)
  6. getAsJSONArray(Object obj)
  7. getFromGson(String json, Class clazz)
  8. getJSONDate(long l)
  9. getJsonMapper()

  10. HOME | Copyright © www.java2s.com 2016