Package org.json

Examples of org.json.JSONStringer


   public void marshal(ReadResourceModel model, OutputStream outputStream, boolean pretty) throws BindingException
   {
      PrintWriter printWriter = new PrintWriter(outputStream);
      try
      {
         JSONStringer json = new JSONStringer();
         json.object().key("description").value(model.getDescription());
         json.key("children").array();
         for (String child : model.getChildren())
         {
            json.object().key("name").value(child);
            NamedDescription nd = model.getChildDescription(child);
            if (nd != null)
            {
               json.key("description").value(nd.getDescription());
            }
            json.endObject();
         }
         json.endArray().key("operations").array();
         for (NamedDescription nd : model.getOperations())
         {
            json.object().key("operation-name").value(nd.getName()).key("operation-description").value(nd.getDescription()).endObject();
         }
         json.endArray().endObject();

         JSONObject output = new JSONObject(json.toString());
         if (pretty)
         {
            printWriter.write(output.toString(3));
         }
         else
View Full Code Here


            } catch (Exception e) {
              e.printStackTrace();
            }
        }
       
          JSONStringer stringer = new JSONStringer();
          MfGeoJSONWriter builder = new MfGeoJSONWriter(stringer);
          builder.encodeFeatureCollection(new MfFeatureCollection(outputFeatureCollection));
         
          servletIOManager.tryToWriteBackToClient(stringer.toString());
     
    } catch(Throwable t) {
      t.printStackTrace();
    } finally {
      logger.debug("OUT");
View Full Code Here

    }
    return null;
  }

  public static String getSampleJsonString(int numRows) {
    JSONStringer x = new JSONStringer();
    Random rand = new Random(0);
    try {
      JSONWriter top = x.array();
      for (int i = 1; i <= numRows; i++) {
        JSONWriter o = top.object();
        o.key("a").value("a" + i);

        o.key("b").value("b" + i);

        JSONWriter c = o.key("c").array();
        for (int ci = 1; ci <= 10; ci++) {
          JSONWriter co = c.object();
          co.key("c.1").value("c.1_" + ci);
          if (rand.nextBoolean()) {
            co.key("c.2").value("c.2_" + ci);
          }

          if (rand.nextBoolean()) {
            JSONWriter c3a = co.key("c.3").array();

            for (int c3i = 1; c3i < rand.nextInt(10) + 1; c3i++) {
              JSONWriter c3o = c3a.object();
              if (rand.nextBoolean()) {
                co.key("c.3.1").value(
                    "c.3.1_" + c3i + "+" + ci + "+" + i);
              }
              if (rand.nextBoolean()) {
                co.key("c.3.2").value("c.3.2_" + c3i);
              }
              if (rand.nextBoolean()) {
                co.key("c.3.3").value("c.3.3_" + c3i);
              }
              if (rand.nextBoolean()) {
                JSONWriter c34o = co.key("c.3.4").object();
                c34o.key("c.3.4.1").value("c.3.4.1_X");
                c34o.key("c.3.4.2").value("c.3.4.1_Y");
                c34o.endObject();
              }
              c3o.endObject();
            }

            c3a.endArray();
          }

          co.endObject();
        }
        c.endArray();

        // List of primitive values.
        JSONWriter d = o.key("d").array();
        for (int di = 1; di < rand.nextInt(10) + 1; di++) {
          d.value("d" + di);
        }
        d.endArray();

        JSONWriter e = o.key("e").array();
        for (int ei = 1; ei < rand.nextInt(10) + 1; ei++) {
          if (rand.nextBoolean()) {
            e.value("e" + ei + "+" + i);
          } else {
            e.object().key("e.1").value("e.1_" + ei).key("e.2")
                .value("e.2_" + ei).endObject();
          }
        }
        e.endArray();

        o.endObject();
      }
      top.endArray();
    } catch (JSONException e) {
      e.printStackTrace();
      return null;
    }

    return x.toString();
  }
View Full Code Here

    return x.toString();
  }

  public static String getJsonForUnitTest1() {
    JSONStringer x = new JSONStringer();
    Random rand = new Random(0);
    try {
      JSONWriter top = x.array();
      for (int i = 1; i <= 2; i++) {
        JSONWriter o = top.object();
        o.key("a").value("a" + i);

        o.key("b").value("b" + i);

        JSONWriter c = o.key("c").array();
        for (int ci = 1; ci <= 10; ci++) {
          JSONWriter co = c.object();
          co.key("c.1").value("c.1_" + ci);
          if (rand.nextBoolean()) {
            co.key("c.2").value("c.2_" + ci);
          }

          if (rand.nextBoolean()) {
            //
          }

          co.endObject();
        }
        c.endArray();

        // List of primitive values.
        JSONWriter d = o.key("d").array();
        for (int di = 1; di < rand.nextInt(10) + 1; di++) {
          d.value("d" + di);
        }
        d.endArray();

        JSONWriter e = o.key("e").array();
        for (int ei = 1; ei < rand.nextInt(10) + 1; ei++) {
          if (rand.nextBoolean()) {
            e.value("e" + ei + "+" + i);
          } else {
            e.object().key("e.1").value("e.1_" + ei).key("e.2")
                .value("e.2_" + ei).endObject();
          }
        }
        e.endArray();

        o.endObject();
      }
      top.endArray();
    } catch (JSONException e) {
      e.printStackTrace();
      return null;
    }

    return x.toString();
  }
View Full Code Here

    return x.toString();
  }

  public static String getJsonForUnitTest4() {
    JSONStringer x = new JSONStringer();
    Random rand = new Random(0);
    try {
      JSONWriter top = x.array();
      for (int i = 1; i <= 1; i++) {
        JSONWriter o = top.object();

        JSONWriter c = o.key("c").array();
        for (int ci = 1; ci <= 3; ci++) {
          JSONWriter co = c.object();
          if (rand.nextBoolean()) {
            co.key("c.2").value("c.2_" + ci);
          }

          if (rand.nextBoolean()) {
            JSONWriter c3a = co.key("c.3").array();

            for (int c3i = 1; c3i < 3; c3i++) {
              JSONWriter c3o = c3a.object();
              if (rand.nextBoolean()) {
                co.key("c.3.1").value(
                    "c.3.1_" + c3i + "+" + ci + "+" + i);
              }
              if (rand.nextBoolean()) {
                co.key("c.3.3").value("c.3.3_" + c3i);
              }
              if (rand.nextBoolean()) {
                JSONWriter c34o = co.key("c.3.4").object();
                c34o.key("c.3.4.2").value("c.3.4.1_Y");
                c34o.endObject();
              }
              c3o.endObject();
            }

            c3a.endArray();
          }

          co.endObject();
        }
        c.endArray();

        o.endObject();
      }
      top.endArray();
    } catch (JSONException e) {
      e.printStackTrace();
      return null;
    }

    return x.toString();
  }
View Full Code Here

    return x.toString();
  }

  public static String getJsonForUnitTest5() {
    JSONStringer x = new JSONStringer();
    Random rand = new Random(0);
    try {
      JSONWriter top = x.array();
      for (int i = 1; i <= 1; i++) {
        JSONWriter o = top.object();

        JSONWriter c = o.key("c").array();
        for (int ci = 1; ci <= 4; ci++) {
          JSONWriter co = c.object();
          co.key("c.1").value("c.1_" + ci);
          if (rand.nextBoolean()) {
            // Need the call to rand to make the test come out
            // right.
          }

          if (rand.nextBoolean()) {
            JSONWriter c3a = co.key("c.3").array();

            for (int c3i = 1; c3i < rand.nextInt(10) + 1; c3i++) {
              JSONWriter c3o = c3a.object();
              if (rand.nextBoolean()) {
                co.key("c.3.1").value(
                    "c.3.1_" + c3i + "+" + ci + "+" + i);
              }
              if (rand.nextBoolean()) {
                co.key("c.3.2").value("c.3.2_" + c3i);
              }
              if (rand.nextBoolean()) {
                co.key("c.3.3").value("c.3.3_" + c3i);
              }
              if (rand.nextBoolean()) {
                JSONWriter c34o = co.key("c.3.4").object();
                c34o.key("c.3.4.1").value("c.3.4.1_X");
                c34o.key("c.3.4.2").value("c.3.4.1_Y");
                c34o.endObject();
              }
              c3o.endObject();
            }

            c3a.endArray();
          }

          co.endObject();
        }
        c.endArray();

        JSONWriter e = o.key("e").array();
        for (int ei = 1; ei < 6; ei++) {
          if (rand.nextBoolean()) {
            e.value("e" + ei + "+" + i);
          } else {
            e.object().key("e.1").value("e.1_" + ei).endObject();
          }
        }
        e.endArray();

        o.endObject();
      }
      top.endArray();
    } catch (JSONException e) {
      e.printStackTrace();
      return null;
    }

    return x.toString();
  }
View Full Code Here

    return x.toString();
  }

  public static String getJsonForUnitTest6() {
    JSONStringer x = new JSONStringer();
    Random rand = new Random(0);
    try {
      JSONWriter top = x.array();
      for (int i = 1; i <= 1; i++) {
        JSONWriter o = top.object();

        JSONWriter c = o.key("c").array();
        for (int ci = 1; ci <= 1; ci++) {
          JSONWriter co = c.object();
          // co.key("c.1").value("c.1_" + ci);
          if (rand.nextBoolean()) {
            // Need the call to rand to make the test come out
            // right.
          }

          if (rand.nextBoolean()) {
            JSONWriter c3a = co.key("c.3").array();
            for (int c3i = 1; c3i <= 2; c3i++) {
              JSONWriter c3o = c3a.object();
              if (rand.nextBoolean()) {
                co.key("c.3.1").value(
                    "c.3.1_" + c3i + "+" + ci + "+" + i);
              }
              if (rand.nextBoolean()) {
                JSONWriter c34o = co.key("c.3.4").object();
                c34o.key("c.3.4.1").value("c.3.4.1_X");
                c34o.endObject();
              }
              c3o.endObject();
            }

            c3a.endArray();
          }

          co.endObject();
        }
        c.endArray();

        JSONWriter e = o.key("e").array();
        for (int ei = 1; ei <= 3; ei++) {
          e.object().key("e.1").value("e.1_" + ei).endObject();
        }
        e.endArray();

        o.endObject();
      }
      top.endArray();
    } catch (JSONException e) {
      e.printStackTrace();
      return null;
    }

    return x.toString();
  }
View Full Code Here

  }

  public static Worksheet createJsonWithFunnyCharacters(Workspace workspace) {
    try {

      JSONStringer x = new JSONStringer();
      String html = "<ul><li>item 1</li></ul>";
      x.object().key("double quote")
          .value("string with \"double quotes\"")
          .key("single quotes")
          .value("'starts has ' and \"ends\" with '").key("tabs")
          .value("there is a tab between \"x\" and \"y\": x\ty")
          .key("newlines").value("there sho\nuld be a newline")
          .key("HTML").value(html).key("URL").value("http://cnn.com")
          .endObject();

      JsonImport ji = new JsonImport(x.toString(),
          "Funny Characters", workspace, "UTF-8", -1);
      Worksheet w = ji.generateWorksheet();
      return w;
    } catch (JSONException e) {
      e.printStackTrace();
View Full Code Here

  public static Worksheet createSampleJsonWithEmptyNestedTable1(
      Workspace workspace) {
    try {

      JSONStringer x = new JSONStringer();

      JSONWriter topA = x.array();
      topA.object().key("a").value("a_1").key("b").array()
          //
          .object().key("b.1").value("b.1_1").key("b.2")
          .value("b.2_1").endObject()
          //
          // .object().key("b.1").value("b.1_2").key("b.2")
          // .value("b.2_2").endObject()
          //
          .endArray().endObject();

      topA.object().key("a").value("a_2").endObject();

      topA.endArray();

      JsonImport ji = new JsonImport(x.toString(),
          "Empty Nested Table 1", workspace, "UTF-8", -1);
      Worksheet w = ji.generateWorksheet();
      return w;
    } catch (JSONException e) {
      e.printStackTrace();
View Full Code Here

  public static Worksheet createSampleJsonWithEmptyNestedTable2(
      Workspace workspace) {
    try {

      JSONStringer x = new JSONStringer();

      JSONWriter topA = x.array();
      topA.object().key("a").value("a_1").key("b").array()
          //
          .object().key("b.1").value("b.1_1").key("b.2")
          .value("b.2_1").endObject()
          //
          .object().key("b.1").value("b.1_2").endObject()
          //
          .endArray().endObject();

      topA.object().key("a").value("a_2").endObject();

      topA.endArray();

      JsonImport ji = new JsonImport(x.toString(),
          "Empty Nested Table 2", workspace, "UTF-8", -1);
      Worksheet w = ji.generateWorksheet();
      return w;
    } catch (JSONException e) {
      e.printStackTrace();
View Full Code Here

TOP

Related Classes of org.json.JSONStringer

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.