Java Script dbObjectFromMap(Map from)

Here you can find the source of dbObjectFromMap(Map from)

Description

db Object From Map

License

Open Source License

Declaration

public static BasicDBObject dbObjectFromMap(Map<String, Object> from) 

Method Source Code

//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.util.Map;
import java.util.Map.Entry;

import javax.script.Bindings;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;

import jdk.nashorn.api.scripting.JSObject;

public class Main {
    public static BasicDBObject dbObjectFromMap(Map<String, Object> from) {
        BasicDBObject result = new BasicDBObject();
        for (Entry<String, Object> entry : from.entrySet()) {
            result.append(entry.getKey(), convert(entry.getValue()));
        }//from   w  w  w.  j a v  a2s.  co m
        return result;
    }

    @SuppressWarnings("unchecked")
    private static Object convert(Object from) {
        if (from instanceof JSObject) {
            if (((JSObject) from).isArray()) {
                return convertArray((Bindings) from);
            }
        }
        if (from instanceof Map) {
            return dbObjectFromMap((Map<String, Object>) from);
        }
        return from;
    }

    private static BasicDBList convertArray(Bindings from) {
        BasicDBList list = new BasicDBList();
        for (int i = 0; i < from.size(); i++) {
            list.add(from.get(String.valueOf(i)));
        }
        return list;
    }
}

Related

  1. addAttribute(final ScriptContext context, final String key, final Object value)
  2. adjustKey(String key)
  3. cdInShell(IResource resource)
  4. convert(Object from)
  5. decodeB64_TO_UTF8(String encodedStr)
  6. escapeJavascript(String str)
  7. eval(String expression, Object... arguments)
  8. eval(String name, Map params)