Package com.granule.json

Examples of com.granule.json.JSONObject


                logger.logp(Level.FINEST, className, "transform", "Parsing the JSON and a DOM builder.");
            }

            try {
                //Get the JSON from the stream.
                JSONObject jObject = new JSONObject(JSONStream);

                //Create a new document

                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbf.newDocumentBuilder();
View Full Code Here


    public void setOptions(String options) {
        this.options = options;
    }

    public String getJSONString(String id) throws JSONException {
        JSONObject obj = new JSONObject();
        obj.put("id", id);
        obj.put("mime-type", mimeType);
        if (options != null)
            obj.put("options", options);
        if (fragments.size() > 0) {
            JSONArray array = new JSONArray();
            for (FragmentDescriptor fd : fragments) {
                JSONObject o = new JSONObject();
                o.put("type", fd instanceof ExternalFragment ? "file" : "script");
                if (fd instanceof ExternalFragment) o.put("file", ((ExternalFragment) fd).getFilePath());
                else o.put("text", ((InternalFragment) fd).getText());
                array.put(o);
            }
            obj.put("fragments", array);
        }
        if (dependentFragments != null && dependentFragments.size() > 0) {
            JSONArray array = new JSONArray();
            for (FragmentDescriptor fd : dependentFragments) {
                JSONObject o = new JSONObject();
                o.put("file", fd.toString());
                array.put(o);
            }
            obj.put("dependency", array);
        }
        return obj.toString() + "\n";
View Full Code Here

        else options = null;
        fragments = new ArrayList<FragmentDescriptor>();
        if (obj.has("fragments")) {
            JSONArray array = obj.getJSONArray("fragments");
            for (int i = 0; i < array.length(); i++) {
                JSONObject o = (JSONObject) array.get(i);
                FragmentDescriptor fd;
                if (o.getString("type").equals("file")) {
                    fd = new ExternalFragment(o.getString("file"));
                } else {
                    fd = new InternalFragment(o.getString("text"));
                }
                fragments.add(fd);
            }
        }
        dependentFragments.clear();
        if (obj.has("dependency")) {
            JSONArray array = obj.getJSONArray("dependency");
            for (int i = 0; i < array.length(); i++) {
                JSONObject o = (JSONObject) array.get(i);
                FragmentDescriptor fd = new ExternalFragment(o.getString("file"));
                dependentFragments.add(fd);
            }
        }

        String filename = cacheFolder + "/" + obj.getString("id") + ".gzip." + (isScript() ? "js" : "css");
View Full Code Here

            } else if (JSONObject.class.isAssignableFrom(clazz)) {
                ja = (JSONObject)obj;
            } else if (JSONArray.class.isAssignableFrom(clazz)) {
                ja = (JSONArray)obj;
            } else if (Map.class.isAssignableFrom(clazz)) {
                ja = new JSONObject((Map)obj);
            } else if (Collection.class.isAssignableFrom(clazz)) {
                ja = new JSONArray((Collection)obj);
            } else if (clazz.isArray()) {
                ja = new JSONArray((Object[])obj);
            }
View Full Code Here

     * @param includeSuperclass Boolean indicating if superclass properties should be included in the output JSON.                      
     * @param parsedObjects An array list of objects traversed to try and avoid loops in graphs
     * @throws com.granule.json.JSONException Thrown if a JSON conversion error occurs.
     */
    private static JSONArtifact introspectBean(Object obj, boolean includeSuperclass, ArrayList parsedObjects) throws JSONException {
        JSONObject ja = null;
        boolean found = false;
        for (int i = 0; i < parsedObjects.size(); i++) {
            // Check and try to avoid graphs by parsing the same
            // object multiple times, which may indicate a cycle.
            Object possibleObj = parsedObjects.get(i);
            if (possibleObj != null && obj == possibleObj) {
                found = true;
                break;
            }
        }

        if (!found) {
            parsedObjects.add(obj);
            ja = new JSONObject();

            Class clazz = obj.getClass();

            ja.put("_type", "JavaClass");
            ja.put("_classname", clazz.getName());

            // Fetch all the methods, based on including superclass or not.
            Method[] methods = null;
            if (includeSuperclass) {
                methods = clazz.getMethods();
            } else {
                methods = clazz.getDeclaredMethods();
            }

            if (methods != null && methods.length > 0) {
                for (int i = 0; i < methods.length; i++) {
                    Method m = methods[i];
                    // Include all superclass methods if requested,
                    // or only those that are part of the actual declaring class.
                    String mName = m.getName();
                    Class[] types = m.getParameterTypes();

                    // Getter, so we can assume this accesses a field.
                    if (mName.startsWith("get") && mName.length() > 3 && (types == null || types.length == 0)) {
                        String attr = mName.substring(3, mName.length());
                        attr = Character.toLowerCase(attr.charAt(0)) + attr.substring(1, attr.length());
                        try {
                            Object val = m.invoke(obj, (Object[])null);
                            if (val == null) {
                                ja.put(attr, (Object)null);
                            } else {
                                Class vClazz = val.getClass();
                                if (String.class == vClazz) {
                                    ja.put(attr, val);
                                } else if (Boolean.class == vClazz) {
                                    ja.put(attr, val);
                                } else if (Class.class == vClazz) {
                                    ja.put(attr, ((Class)val).getName());
                                } else if (Number.class.isAssignableFrom(vClazz)) {
                                    ja.put(attr, val);
                                } else if (JSONObject.class.isAssignableFrom(vClazz)) {
                                    ja.put(attr, val);
                                } else if (JSONArray.class.isAssignableFrom(vClazz)) {
                                    ja.put(attr, val);
                                } else if (Map.class.isAssignableFrom(vClazz)) {
                                    ja.put(attr, new JSONObject((Map)val));
                                } else if (Collection.class.isAssignableFrom(vClazz)) {
                                    ja.put(attr, new JSONArray((Collection)obj));
                                } else {
                                    if (val != obj) {
                                        // Try to avoid processing references to itself.
                                        ja.put(attr, introspectBean(val, includeSuperclass, parsedObjects));
                                    }
                                }
                            }
                        } catch (Exception ex) {
                            ja.put(attr, (Object)null);
                        }
                    }
                }
            }
        }
View Full Code Here

                                                    }
                                                }
                                            }
                                        } else if (JSONObject.class.isAssignableFrom(vClazz)) {
                                            // Handle determining a map type to set, if there is one.
                                            JSONObject jObj = (JSONObject)val;
                                            Class vC = val.getClass();
                                            if(jObj.get("_classname") != null && "JavaClass".equals(jObj.get("_type"))){
                                                val = fromJson(jObj);
                                                vC = val.getClass();
                                            }

                                            // Handle locating a boolean method.
View Full Code Here

     * @throws JSONException Thrown if an IO error occurs during parse, such as a malformed JSON object.
     */
    public JSONObject parseObject(boolean ordered, JSONObject rootObject) throws JSONException {

        try {
            JSONObject result = null;
            if (rootObject != null) {
                result = rootObject;
            } else {
                if (!ordered) {
                    result = new JSONObject();
                } else {
                    result = new OrderedJSONObject();
                }
            }

            if (lastToken != Token.TokenBraceL) throw new JSONException("Expecting '{' " + tokenizer.onLineCol() + " instead, obtained token: '" + lastToken + "'");
            lastToken = tokenizer.next();

            while (true) {
                if (lastToken == Token.TokenEOF) throw new JSONException("Unterminated object " + tokenizer.onLineCol());

                if (lastToken == Token.TokenBraceR) {
                    lastToken = tokenizer.next();
                    break;
                }

                if (!lastToken.isString()) throw new JSONException("Expecting string key " + tokenizer.onLineCol());
                String key = lastToken.getString();

                lastToken = tokenizer.next();
                if (lastToken != Token.TokenColon) throw new JSONException("Expecting colon " + tokenizer.onLineCol());

                lastToken = tokenizer.next();
                Object val = parseValue(ordered);

                result.put(key, val);

                if (lastToken == Token.TokenComma) {
                    lastToken = tokenizer.next();
                }

View Full Code Here

    try {
      String line;
      needRebuildCache = false;
      while ((line = reader.readLine()) != null) {
        try {
          JSONObject obj = new JSONObject(line);
          String id = obj.getString("id");
          if (bundles.containsKey(id)) {
            logger.warn("Rebuilding from dublicate id");
            needRebuildCache = true;
          }
          CachedBundle cs = new CachedBundle();
View Full Code Here

TOP

Related Classes of com.granule.json.JSONObject

Copyright © 2015 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.