Ejemplo n.º 1
0
 /**
  * 生成 JSON Description Array String
  *
  * @param description Description String
  * @return JSON String
  */
 @SuppressLint("DefaultLocale")
 public static String GenerateJSONDescription(String description) {
   if (description != null) {
     if (description.length() > 0 && !description.contains("null")) {
       JSONStringer jsonStringer = new JSONStringer();
       try {
         jsonStringer.array();
         for (String item : description.split("#")) {
           String[] part = item.split(":");
           jsonStringer.object();
           jsonStringer
               .key("id")
               .value(
                   part.length > 0
                       ? part[0].replaceAll("(?i)bit", "").replaceFirst("^0+(?!$)", "")
                       : "");
           jsonStringer.key("value").value(part.length > 1 ? part[1] : "");
           jsonStringer.endObject();
         }
         jsonStringer.endArray();
       } catch (JSONException e) {
         e.printStackTrace();
       }
       return jsonStringer.toString();
     } else {
       return "";
     }
   } else {
     return "";
   }
 }
Ejemplo n.º 2
0
 private void encodeBoundingBox(Extent env) throws JSONException {
   builder.key("bbox");
   builder.array();
   builder.value(env.getMinX());
   builder.value(env.getMinY());
   builder.value(env.getMaxX());
   builder.value(env.getMaxY());
   builder.endArray();
 }
Ejemplo n.º 3
0
 /**
  * 序列化集合
  *
  * @param js json对象
  * @param collection 集合
  */
 private static void serializeCollect(JSONStringer js, Collection<?> collection) {
   try {
     js.array();
     for (Object o : collection) {
       serialize(js, o);
     }
     js.endArray();
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 4
0
 /**
  * 序列化数组
  *
  * @param js json对象
  * @param array 数组
  */
 private static void serializeArray(JSONStringer js, Object array) {
   try {
     js.array();
     for (int i = 0; i < Array.getLength(array); ++i) {
       Object o = Array.get(array, i);
       serialize(js, o);
     }
     js.endArray();
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
  private String inventoryToJson(Inventory inventory) throws JSONException {
    JSONStringer json = new JSONStringer().object();

    json.key("purchaseMap").array();
    for (Map.Entry<String, Purchase> entry : inventory.getPurchaseMap().entrySet()) {
      json.array();
      json.value(entry.getKey());
      json.value(purchaseToJson(entry.getValue()));
      json.endArray();
    }
    json.endArray();

    json.key("skuMap").array();
    for (Map.Entry<String, SkuDetails> entry : inventory.getSkuMap().entrySet()) {
      json.array();
      json.value(entry.getKey());
      json.value(skuDetailsToJson(entry.getValue()));
      json.endArray();
    }
    json.endArray();

    json.endObject();
    return json.toString();
  }
Ejemplo n.º 6
0
  private void encodeFeatureCollection(FeatureCollection c) throws JSONException {
    builder.object();
    builder.key("type").value("FeatureCollection");
    builder.key("features");
    builder.array();

    Enumeration e = c.getFeatures().elements();

    while (e.hasMoreElements()) {
      Feature f = (Feature) e.nextElement();
      encodeFeature(f);
    }

    builder.endArray();
    builder.endObject();
  }
Ejemplo n.º 7
0
  private void encodeGeometry(IGeometry g) throws JSONException {

    builder.object();
    builder.key("type");
    String name = getGeometryName(g);
    if (name != null) {
      builder.value(name);
      if (name.compareTo("Point") == 0) {
        builder.key("coordinates");
        encodeCoordinate((Point) g);
      } else if (name.compareTo("MultiPoint") == 0) {
        MultiPoint mp = (MultiPoint) g;
        builder.key("coordinates");
        builder.array();
        for (int i = 0; i < mp.getNumPoints(); i++) {
          encodeCoordinate(((MultiPoint) g).getPoint(i));
        }
        builder.endArray();
      }
    }
    builder.endObject();
  }
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String q = request.getParameter("q");
    String simple = request.getParameter("simple");
    String test = request.getParameter("test");
    boolean isTest = false;
    if (test != null && test.equals("true")) {
      isTest = true;
    }
    PrintWriter w = response.getWriter();
    if (q != null) {
      // 지원사항을 알려준다.
      response.setCharacterEncoding("utf-8");
      response.setStatus(HttpServletResponse.SC_OK);
      response.setContentType("application/json;");

      String[] collectionNameList = service.getCollectionNameList();
      boolean[] status = service.getCollectionStatus();

      JSONStringer stringer = new JSONStringer();
      try {
        stringer.array();
        if (collectionNameList != null) {
          for (int i = 0; i < collectionNameList.length; i++) {
            stringer.object().key("name").value(collectionNameList[i]);
            if (isTest) {
              stringer.key("status").value(true);
            } else {
              stringer.key("status").value(status[i]);
            }
            stringer.endObject();
          }
        }
        stringer.endArray();
        //    			logger.debug("stringer = "+stringer);
        w.println(stringer.toString());
      } catch (JSONException e) {
        throw new ServletException("JSONException 발생", e);
      }
    } else {
      String callback = request.getParameter("jsoncallback");

      response.setCharacterEncoding("utf-8");
      response.setStatus(HttpServletResponse.SC_OK);

      if (RESULT_TYPE == JSONP_TYPE) {
        response.setContentType("text/javascript;");
      } else {
        response.setContentType("application/json;");
      }
      String[] collectionNameList = service.getCollectionNameList();
      boolean[] status = service.getCollectionStatus();
      RealTimeCollectionStatistics[] statistics = service.getCollectionStatisticsList();
      RealTimeCollectionStatistics globalCollectionStatistics =
          service.getGlobalCollectionStatistics();
      // 결과생성
      JSONStringer stringer = new JSONStringer();
      try {
        //
        // 간략정보
        //
        if (simple != null) {
          if (isTest) {
            stringer.object();
            int hit = r.nextInt(30) + 1;
            stringer.key("h").value(r.nextInt(30) + 1);
            stringer.key("fh").value(r.nextInt(hit > 5 ? 5 : hit));
            int ta = r.nextInt(50) + 1;
            stringer.key("ta").value(ta + 1);
            stringer.key("tx").value(ta + r.nextInt(20) + 1);
            stringer.endObject();
          } else {
            // 컬렉션 통합 정보를 리턴한다.
            //	    				if(collectionNameList != null){
            //	    					int hit = 0;
            //	    					int failHit = 0;
            //	    					int avgTime = 0;
            //	    					int maxTime = 0;
            //	    					int count = 0;
            //	    					for (int i = 0; i < collectionNameList.length; i++) {
            //	    						if(status[i]){
            //	    							count++;
            //	    							hit += statistics[i].getHitPerUnitTime();
            //	    							failHit += statistics[i].getFailHitPerUnitTime();
            //	    							avgTime += statistics[i].getMeanResponseTime();
            //	    							if(statistics[i].getMaxResponseTime() > maxTime){
            //	    								maxTime = statistics[i].getMaxResponseTime();
            //	    							}
            //	    						}
            //	    					}
            //	    				}
            stringer.object();
            stringer.key("h").value(globalCollectionStatistics.getHitPerUnitTime());
            stringer.key("fh").value(globalCollectionStatistics.getFailHitPerUnitTime());
            stringer.key("ach").value(globalCollectionStatistics.getAccumulatedHit());
            stringer.key("acfh").value(globalCollectionStatistics.getAccumulatedFailHit());
            stringer.key("ta").value(globalCollectionStatistics.getMeanResponseTime());
            stringer.key("tx").value(globalCollectionStatistics.getMaxResponseTime());
            stringer.endObject();
          }
        } else {
          //
          // 컬렉션별 상세정보
          //
          stringer.array();
          if (collectionNameList != null) {
            for (int i = 0; i < collectionNameList.length; i++) {
              if (status[i]) {
                stringer.object().key("c").value(collectionNameList[i]);
                if (isTest) {
                  int hit = r.nextInt(10) + 1;
                  stringer.key("h").value(hit);
                  stringer.key("fh").value(r.nextInt(hit > 3 ? 3 : hit));
                  stringer.key("ta").value(r.nextInt(100) + 1);
                  stringer.key("tx").value(r.nextInt(50) + r.nextInt(20) + 1);
                } else {
                  stringer.key("h").value(statistics[i].getHitPerUnitTime());
                  stringer.key("fh").value(statistics[i].getFailHitPerUnitTime());
                  stringer.key("ach").value(statistics[i].getAccumulatedHit());
                  stringer.key("acfh").value(statistics[i].getAccumulatedFailHit());
                  stringer.key("ta").value(statistics[i].getMeanResponseTime());
                  stringer.key("tx").value(statistics[i].getMaxResponseTime());
                }
                stringer.endObject();
              }
            }
          }
          stringer.endArray();
        }
      } catch (JSONException e) {
        throw new ServletException("JSONException 발생", e);
      }

      // JSONP는 데이터 앞뒤로 function명으로 감싸준다.
      if (RESULT_TYPE == JSONP_TYPE) {
        w.write(callback + "(");
      }

      // 정보를 보내준다.
      w.write(stringer.toString());

      if (RESULT_TYPE == JSONP_TYPE) {
        w.write(");");
      }
    }
    w.close();
  }
Ejemplo n.º 9
0
 private void encodeCoordinate(Point coord) throws JSONException {
   builder.array();
   builder.value(coord.getX());
   builder.value(coord.getY());
   builder.endArray();
 }