Package org.json

Examples of org.json.JSONWriter


            HttpUtilities.respond(response, "error", "Not authorized");
            return;
        }
       
        Writer w = response.getWriter();
        JSONWriter writer = new JSONWriter(w);
        try {
            writer.object();
            writer.key("documents");
            writer.array();
           
            try {
                listSpreadsheets(GDataExtension.getDocsService(token), writer);
                listFusionTables(GDataExtension.getFusionTablesGoogleService(token), writer);
            } catch (ServiceException e) {
                e.printStackTrace();
            }
           
            writer.endArray();
            writer.endObject();
        } catch (JSONException e) {
            throw new ServletException(e);
        } finally {
            w.flush();
            w.close();
View Full Code Here


                optionObj,
                exceptions
            );
           
            Writer w = response.getWriter();
            JSONWriter writer = new JSONWriter(w);
            try {
                writer.object();
                if (exceptions.size() == 0) {
                    job.project.update(); // update all internal models, indexes, caches, etc.
                   
                    writer.key("status"); writer.value("ok");
                } else {
                    writer.key("status"); writer.value("error");
                   
                    writer.key("errors");
                    writer.array();
                    DefaultImportingController.writeErrors(writer, exceptions);
                    writer.endArray();
                }
                writer.endObject();
            } catch (JSONException e) {
                throw new ServletException(e);
            } finally {
                w.flush();
                w.close();
View Full Code Here

           
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Type", "application/json");
           
            Writer w = response.getWriter();
            JSONWriter writer = new JSONWriter(w);
            try {
                writer.object();
               
                List<Exception> exceptions = new LinkedList<Exception>();
                String url = upload(project, engine, params, token, name, exceptions);
                if (url != null) {
                    writer.key("status"); writer.value("ok");
                    writer.key("url"); writer.value(url);
                } else if (exceptions.size() == 0) {
                    writer.key("status"); writer.value("error");
                    writer.key("message"); writer.value("No such format");
                } else {
                    for (Exception e : exceptions) {
                        logger.warn(e.getLocalizedMessage(), e);
                    }
                    writer.key("status"); writer.value("error");
                    writer.key("message"); writer.value(exceptions.get(0).getLocalizedMessage());
                }
            } catch (Exception e) {
                e.printStackTrace();
                writer.key("status"); writer.value("error");
                writer.key("message"); writer.value(e.getMessage());
            } finally {
                writer.endObject();
                w.flush();
                w.close();
            }
        } catch (Exception e) {
            throw new ServletException(e);
View Full Code Here

    }
   
    @Override
    public void flush() throws IOException {
        try {
            JSONWriter jsonWriter = new JSONWriter(writer);
           
            jsonWriter.array();
            for (JSONObject obj : rootObjects) {
                jsonWriter.value(obj);
            }
            jsonWriter.endArray();
           
        } catch (JSONException e) {
            e.printStackTrace();
        }
        writer.flush();
View Full Code Here

    }

    private void renderResult(final PrintWriter pw, final Component component)
        throws IOException
    {
        final JSONWriter jw = new JSONWriter(pw);
        try
        {
            jw.object();

            final ScrService scrService = getScrService();
            if (scrService == null)
            {
                jw.key("status"); //$NON-NLS-1$
                jw.value(-1);
            }
            else
            {
                final Component[] components = scrService.getComponents();

                if (components == null || components.length == 0)
                {
                    jw.key("status"); //$NON-NLS-1$
                    jw.value(0);
                }
                else
                {
                    // order components by name
                    sortComponents(components);

                    final StringBuffer buffer = new StringBuffer();
                    buffer.append(components.length);
                    buffer.append(" component"); //$NON-NLS-1$
                    if (components.length != 1)
                    {
                        buffer.append('s');
                    }
                    buffer.append(" installed."); //$NON-NLS-1$
                    jw.key("status"); //$NON-NLS-1$
                    jw.value(components.length);

                    // render components
                    jw.key("data"); //$NON-NLS-1$
                    jw.array();
                    if (component != null)
                    {
                        component(jw, component, true);
                    }
                    else
                    {
                        for (int i = 0; i < components.length; i++)
                        {
                            component(jw, components[i], false);
                        }
                    }
                    jw.endArray();
                }
            }

            jw.endObject();
        }
        catch (JSONException je)
        {
            throw new IOException(je.toString());
        }
View Full Code Here

  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

  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

  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();
View Full Code Here

  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

  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

TOP

Related Classes of org.json.JSONWriter

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.