Package net.sf.json

Examples of net.sf.json.JSONObject


   */
  public JSONContentRepository(String[] attr, String encoding, String[] options) {

    super(attr, encoding, options);
    // this.setResponseEncoding(encoding);
    rootObject = new JSONObject();
  }
View Full Code Here


   */
  public void respondWithError(OutputStream stream, CRException ex, boolean isDebug) {
    // Clear the root element and set the status
    this.rootObject.clear();
    this.rootObject.element("status", "error");
    JSONObject errElement = new JSONObject();

    errElement.element("type", ex.getType());
    errElement.element("message", ex.getMessage());
    if (isDebug) {
      // if in debug mode, add stack trace
      errElement.element("stacktrace", ex.getStringStackTrace());
    }
    this.rootObject.element("Error", errElement);

    // Write stream
    try {
View Full Code Here

      for (Iterator<CRResolvableBean> it = this.resolvableColl.iterator(); it.hasNext();) {

        CRResolvableBean crBean = it.next();

        JSONObject objElement = processElement(crBean);

        jsArray.add(objElement);

      }
      this.rootObject.element("Objects", jsArray);
View Full Code Here

  }

  @SuppressWarnings("unchecked")
  private JSONObject processElement(CRResolvableBean crBean) {
    JSONObject objElement = new JSONObject();

    objElement.element("contentid", "" + crBean.getContentid());
    objElement.element("obj_id", "" + crBean.getObj_id());
    objElement.element("obj_type", "" + crBean.getObj_type());
    objElement.element("mother_id", ((crBean.getMother_id() == null) ? "" : "" + crBean.getMother_id()));
    objElement.element("mother_type", ((crBean.getMother_type() == null) ? "" : "" + crBean.getMother_type()));

    if (crBean.getAttrMap() != null && (!crBean.getAttrMap().isEmpty())) {
      JSONObject attrContainer = new JSONObject();

      Iterator<String> bit = crBean.getAttrMap().keySet().iterator();
      while (bit.hasNext()) {

        String entry = bit.next();
        // Element attrElement = doc.createElement(entry);
        Object bValue = crBean.getAttrMap().get(entry);

        if (bValue != null) {
          // deal with multivalue attributes
          if ((bValue.getClass().isArray() && !(bValue instanceof byte[])) || bValue instanceof List) {
            JSONArray value = new JSONArray();
            List<Object> arr;
            if (bValue instanceof List) {
              arr = new ArrayList<Object>();
              List<Object> ob_arr = (List<Object>) bValue;
              for (Object obj : ob_arr) {
                String v = "";
                v = convertToString(obj);
                arr.add(v);
              }
            } else {
              arr = new ArrayList<Object>();
              Object[] ob_arr = (Object[]) bValue;
              for (Object obj : ob_arr) {
                String v = "";
                v = convertToString(obj);
                arr.add(v);
              }
            }
            value.addAll(arr);
            attrContainer.element(entry, value);
          } else if (bValue instanceof Map<?, ?>) {
            attrContainer.element(entry, bValue);
          }
          // dealt with single value attributes (String, Number,
          // ByteArray)
          else {
            String value = "";
            value = convertToString(bValue);
            attrContainer.element(entry, value);
          }
        }
      }
      objElement.element("attributes", attrContainer);
    }
    if (crBean.getChildRepository() != null && crBean.getChildRepository().size() > 0) {
      // JSONObject childContainer = new JSONObject();
      JSONArray childContainer = new JSONArray();
      for (Iterator it = crBean.getChildRepository().iterator(); it.hasNext();) {

        CRResolvableBean chBean = (CRResolvableBean) it.next();

        JSONObject chElement = processElement(chBean);
        childContainer.add(chElement);
        // childContainer.element(chBean.getContentid(),chElement);
      }
      objElement.element("children", childContainer);
    }
View Full Code Here

          throw new CRException("Cannot find the given json file.", e);
        }
      } else {
        objectString = config.getString("objects", "{objects:[]}");
      }
      JSONObject json = JSONObject.fromObject(objectString);
      JSONArray jsonObjects = json.getJSONArray("objects");
      for (Object object : jsonObjects) {
        if (object instanceof JSONObject) {
          objects.add(createCRResolvableBean((JSONObject) object));
        }
      }
View Full Code Here

    public static String jsonResponseFromRequestAttributes(HttpServletRequest request, HttpServletResponse response) {
        // pull out the service response from the request attribute
        Map<String, Object> attrMap = UtilHttp.getJSONAttributeMap(request);

        // create a JSON Object for return
        JSONObject json = JSONObject.fromObject(attrMap);
        writeJSONtoResponse(json, response);

        return "success";
    }
View Full Code Here


    public static String getJSONuiLabelArray(HttpServletRequest request, HttpServletResponse response) {
        String requiredLabels = request.getParameter("requiredLabels");

        JSONObject uiLabelObject = null;
        if (UtilValidate.isNotEmpty(requiredLabels)) {
            // Transform JSON String to Object
            uiLabelObject = (JSONObject) JSONSerializer.toJSON(requiredLabels);
        }

        JSONObject jsonUiLabel = new JSONObject();
        Locale locale = request.getLocale();
        if(!uiLabelObject.isEmpty()) {
            Set<String> resourceSet = UtilGenerics.checkSet(uiLabelObject.keySet());
            // Iterate over the resouce set
            for (String resource : resourceSet) {
                JSONArray labels = uiLabelObject.getJSONArray(resource);
                if (labels.isEmpty() || labels == null) {
                    continue;
                }

                // Iterate over the uiLabel List
                Iterator<String> jsonLabelIterator = UtilGenerics.cast(labels.iterator());
                JSONArray resourceLabelList = new JSONArray();
                while(jsonLabelIterator.hasNext()) {
                    String label = jsonLabelIterator.next();
                    String receivedLabel = UtilProperties.getMessage(resource, label, locale);
                    if (UtilValidate.isNotEmpty(receivedLabel)) {
                        resourceLabelList.add(receivedLabel);
                    }
                }
                jsonUiLabel.element(resource, resourceLabelList);
            }
        }

        writeJSONtoResponse(jsonUiLabel, response);
        return "success";
View Full Code Here

    }

    public static String getJSONuiLabel(HttpServletRequest request, HttpServletResponse response) {
        String requiredLabels = request.getParameter("requiredLabel");

        JSONObject uiLabelObject = null;
        if (UtilValidate.isNotEmpty(requiredLabels)) {
            // Transform JSON String to Object
            uiLabelObject = (JSONObject) JSONSerializer.toJSON(requiredLabels);
        }

        JSONArray jsonUiLabel = new JSONArray();
        Locale locale = request.getLocale();
        if(!uiLabelObject.isEmpty()) {
            Set<String> resourceSet = UtilGenerics.checkSet(uiLabelObject.keySet());
            // Iterate over the resource set
            // here we need a keySet because we don't now which label resource to load
            // the key set should have the size one, if greater or empty error should returned
            if (UtilValidate.isEmpty(resourceSet)) {
                Debug.logError("No resource and labels found", module);
                return "error";
            } else if (resourceSet.size() > 1) {
                Debug.logError("More than one resource found, please use the method: getJSONuiLabelArray", module);
                return "error";
            }

            for (String resource : resourceSet) {
                String label = uiLabelObject.getString(resource);
                if (UtilValidate.isEmail(label)) {
                    continue;
                }

                String receivedLabel = UtilProperties.getMessage(resource, label, locale);
View Full Code Here

            } else {
                JSONArray.toList(jsonArray, target, new JsonConfig());
            }

        } else {
            JSONObject jsonObject = JSONObject.fromObject(sb.toString());
            JSONObject.toBean(jsonObject, target, new JsonConfig());
        }
    }
View Full Code Here

        if (obj != null) {
            if (isArray(obj)) {
                JSONArray jsonArray = JSONArray.fromObject(obj);
                stream.write(jsonArray.toString());
            } else {
                JSONObject jsonObject = JSONObject.fromObject(obj);
                stream.write(jsonObject.toString());
            }
        }
        return null;

View Full Code Here

TOP

Related Classes of net.sf.json.JSONObject

Copyright © 2018 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.