/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ecbeta.common.util;

import com.ecbeta.common.core.viewer.bean.MapJSONBean;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.ArrayList;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Ronghai Wei <ronghai.wei@outlook.com>
 */
public class JSONUtils {

    @SuppressWarnings("unchecked")
    public static <T> Collection<T> toCollection(Object json, String key, Class<T> clazz) {
        if (json instanceof JSONArray) {
            return JSONArray.toCollection((JSONArray) json, clazz);
        } else if (json instanceof JSONObject) {
            JSONArray array = (JSONArray) ((JSONObject) json).get(key);
            return JSONArray.toCollection(array, clazz);
        }
        return null;
    }

    public static JSONObject toJSON(HttpServletRequest request) {
        MapJSONBean json = new MapJSONBean();
        for (Object s : request.getParameterMap().keySet()) {
            for (String value : request.getParameterValues(s.toString())) {
                json.add(s.toString(), value);
            }
        }

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
            String line;
            String data = "";
            while ((line = br.readLine()) != null) {
                data += line;
            }
            data = URLDecoder.decode(data, "UTF-8");
            try {
                JSONObject oo = JSONObject.fromObject(data);
                return oo;
            }
            catch (Exception ex) {
                Map<String, List<String>> params = parseParameters(data);
                for (Map.Entry<String, List<String>> entry : params.entrySet()) {
                    for (String value : entry.getValue()) {
                        json.add(entry.getKey(), value);
                    }
                }
            }

        }
        catch (IOException ex) {
            Logger.getLogger(JSONUtils.class.getName()).log(Level.SEVERE, null, ex);
        }

        return json.toJson();
    }

    private static Map<String, List<String>> parseParameters(String url) {
        Map<String, List<String>> params = new HashMap<>();
        String query = url;
        for (String param : query.split("&")) {
            String pair[] = param.split("=");
            String key = pair[0];
            String value = "";
            if (pair.length > 1) {
                value = pair[1];
            }
            List<String> values = params.get(key);
            if (values == null) {
                values = new ArrayList<>();
                params.put(key, values);
            }
            values.add(value);
        }
        return params;
    }
}

@@ -509,38 +509,8 @@
 on;%0A
-import java.util.Collections;%0A
 impo
@@ -645,72 +645,8 @@
 er;%0A
-import java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0A
 impo
@@ -2482,32 +2482,85 @@
 ex);%0A        %7D%0A%0A
+        System.out.println(%22json:%22+json.toString());%0A
         return j
@@ -3238,10 +3238,643 @@
 ;%0A    %7D%0A
+     public static void expectOne(JSONObject json, String key)%7B%0A        if(json.has(key))%7B%0A            Object o = json.get(key);%0A            if(o instanceof JSONArray)%7B%0A                json.put(key, ((JSONArray)o ).get(0));%0A            %7D%0A        %7D%0A          %0A    %7D%0A     public static void expectMore(JSONObject json, String key)%7B%0A        if(json.has(key))%7B%0A            Object o = json.get(key);%0A            if(o instanceof JSONArray)%7B%0A               %0A            %7Delse%7B%0A                List%3CObject%3E list = new ArrayList%3C%3E(1);%0A                list.add(o);%0A                json.put(key, list);%0A            %7D%0A        %7D%0A          %0A    %7D%0A
 %7D%0A

add morr utils to jsonutils