Java Json fill_key_value(JsonObjectBuilder jsonObject, String key, Object value)

Here you can find the source of fill_key_value(JsonObjectBuilder jsonObject, String key, Object value)

Description

filkevalue

License

Open Source License

Declaration

private static void fill_key_value(JsonObjectBuilder jsonObject, String key, Object value) 

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);

    private static void fill_key_value(JsonObjectBuilder jsonObject, String key, Object value) {
        if (jsonObject == null || key == null) {
            return;
        }/*from   w w w .  java  2 s .  com*/

        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);
    }

    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);
        }
    }
}

Related

  1. asString(JsonValue value)
  2. compare(JsonValue expected, JsonValue actual, boolean strict)
  3. convertJsonValue(Object jsonValue, Class desiredType)
  4. escape(String text)
  5. fill_dictionary(Map dictionary, JsonObject jsonObject)
  6. fill_list(JsonObjectBuilder jsonObject, String key, List list)
  7. fromDictionary(Map dictionary)
  8. getAsJSONArray(Object obj)
  9. getFromGson(String json, Class clazz)

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