Package org.json

Examples of org.json.JSONObject


      }

      String metadata_ = GadgetUtil.fetchGagdetMetadata(url);
      try
      {
         JSONObject jsonObj = new JSONObject(metadata_);
         JSONObject obj = jsonObj.getJSONArray("gadgets").getJSONObject(0);
         String token = GadgetUtil.createToken(url, new Long(hashCode()));
         obj.put("secureToken", token);
         metadata_ = jsonObj.toString();
      }
      catch (JSONException e)
      {
         e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
View Full Code Here


    private JSONObject getId(JsonAPI api, HttpServletRequest req)
    {
        String path = req.getRequestURI().substring(req.getContextPath().length() + req.getServletPath().length());
        StringTokenizer tokenizer = new StringTokenizer(path, "/");
        String className = tokenizer.nextToken(); // ignore class name
        JSONObject id = new JSONObject();
        try
        {
            if (tokenizer.hasMoreTokens())
            {
                String token = tokenizer.nextToken();
                if (token == null)
                {
                    return null;
                }
                AbstractClassMetaData cmd = api.getMetaDataManager()
                        .getMetaDataForClass(className, api.getClassLoaderResolver());
                if (cmd != null && cmd.usesSingleFieldIdentityClass())
                {
                    Object value = TypeConversionHelper.convertTo(token,
                        cmd.getMetaDataForMemberAtRelativePosition(cmd.getPKMemberPositions()[0]).getType());
                    id.put("id", value);

                    return id;
                }
                id.put("id", token);

                return id;
            }
            try
            {
                if (req.getContentLength() > 0)
                {
                    char[] buffer = new char[req.getContentLength()];
                    req.getReader().read(buffer);
                    String idtext = new String(buffer);
                    idtext = URLDecoder.decode(idtext, "UTF-8");
                    // if it's a JSONObject
                    JSONObject jsonobj = new JSONObject(idtext);
                    return jsonobj;
                }
            }
            catch (IOException e)
            {
View Full Code Here

                    cmd = api.getMetaDataManager().getMetaDataForClass(className, clr);
                }
            }
            catch (ClassNotResolvedException ex)
            {
                JSONObject error = new JSONObject();
                error.put("exception", ex.getMessage());
                resp.getWriter().write(error.toString());
                resp.setStatus(404);
                resp.setHeader("Content-Type", "application/json");
                return;
            }
            JSONObject id = getId(api, req);
            if (id == null)
            {
                // no id provided
                try
                {
                    // get the whole extent
                    String queryString = "SELECT FROM " + cmd.getFullClassName();
                    if (req.getQueryString() != null)
                    {
                        // query by filter
                        queryString += " WHERE " + URLDecoder.decode(req.getQueryString(), "UTF-8");
                    }
                    try
                    {
                        api.getTransaction().begin();
                        Query query = api.newQuery("JDOQL", queryString);
                        List result = (List)query.execute();
                        JSONArray array = new JSONArray(result);
                        resp.getWriter().write(array.toString());
                        resp.setHeader("Content-Type", "application/json");
                        resp.setStatus(200);
                        api.getTransaction().commit();
                    }
                    finally
                    {
                        if (api.getTransaction().isActive())
                        {
                            api.getTransaction().rollback();
                        }
                        api.close();
                    }
                    return;
                }
                catch (NucleusUserException e)
                {
                    JSONObject error = new JSONObject();
                    error.put("exception", e.getMessage());
                    resp.getWriter().write(error.toString());
                    resp.setStatus(400);
                    resp.setHeader("Content-Type", "application/json");
                    return;
                }
                catch (NucleusException ex)
                {
                    JSONObject error = new JSONObject();
                    error.put("exception", ex.getMessage());
                    resp.getWriter().write(error.toString());
                    resp.setStatus(404);
                    resp.setHeader("Content-Type", "application/json");
                    return;
                }
                catch (RuntimeException ex)
                {
                    ex.printStackTrace();
                    // errors from the google appengine may be raised when running queries
                    JSONObject error = new JSONObject();
                    error.put("exception", ex.getMessage());
                    resp.getWriter().write(error.toString());
                    resp.setStatus(404);
                    resp.setHeader("Content-Type", "application/json");
                    return;
                }
            }
            if (cmd.getIdentityType() != IdentityType.APPLICATION)
            {
                JSONObject error = new JSONObject();
                error.put("exception", "Only application identity types are support.");
                resp.getWriter().write(error.toString());
                resp.setStatus(404);
                resp.setHeader("Content-Type", "application/json");
                return;
            }
            try
            {
                api.getTransaction().begin();
                id.put("class", getClassNameForPath(req));
                Object result = api.findObject(id);
                resp.getWriter().write(result.toString());
                resp.setHeader("Content-Type", "application/json");
                api.getTransaction().commit();
                return;
            }
            catch (NucleusObjectNotFoundException ex)
            {
                resp.setContentLength(0);
                resp.setStatus(404);
                return;
            }
            catch (NucleusException ex)
            {
                JSONObject error = new JSONObject();
                error.put("exception", ex.getMessage());
                resp.getWriter().write(error.toString());
                resp.setStatus(404);
                resp.setHeader("Content-Type", "application/json");
                return;
            }
            finally
            {
                if (api.getTransaction().isActive())
                {
                    api.getTransaction().rollback();
                }
                api.close();
            }

        }
        catch (JSONException e)
        {
            try
            {
                JSONObject error = new JSONObject();
                error.put("exception", e.getMessage());
                resp.getWriter().write(error.toString());
                resp.setStatus(404);
                resp.setHeader("Content-Type", "application/json");
            }
            catch (JSONException e1)
            {
View Full Code Here

        catch (ClassNotResolvedException ex)
        {
            resp.setStatus(404);
            return;
        }
        JSONObject id = getId(api, req);
        if (id == null)
        {
            // no id provided
            try
            {
                // get the whole extent
                String queryString = "SELECT FROM " + cmd.getFullClassName();
                if (req.getQueryString() != null)
                {
                    // query by filter
                    queryString += " WHERE " + URLDecoder.decode(req.getQueryString(), "UTF-8");
                }
                try
                {
                    api.getTransaction().begin();
                    Query query = api.newQuery("JDOQL", queryString);
                    /* Object result = */query.execute();
                    resp.setStatus(200);
                    api.getTransaction().commit();
                }
                finally
                {
                    if (api.getTransaction().isActive())
                    {
                        api.getTransaction().rollback();
                    }
                    api.close();
                }
                return;
            }
            catch (NucleusUserException e)
            {
                resp.setStatus(400);
                return;
            }
            catch (NucleusException ex)
            {
                resp.setStatus(404);
                return;
            }
            catch (RuntimeException ex)
            {
                resp.setStatus(404);
                return;
            }
        }
        if (cmd.getIdentityType() != IdentityType.APPLICATION)
        {
            resp.setStatus(404);
            return;
        }
        try
        {
            api.getTransaction().begin();
            id.put("class", getClassNameForPath(req));
            api.findObject(id);
            resp.setStatus(200);
            api.getTransaction().commit();
            return;
        }
View Full Code Here

            return;
        }
        char[] buffer = new char[req.getContentLength()];
        req.getReader().read(buffer);
        String str = new String(buffer);
        JSONObject jsonobj;
        try
        {
            api.getTransaction().begin();
            jsonobj = new JSONObject(str);
            jsonobj.put("class", getClassNameForPath(req));
            api.persist(jsonobj);
            resp.getWriter().write(jsonobj.toString());
            resp.setHeader("Content-Type", "application/json");
            api.getTransaction().commit();
        }
        catch (ClassNotResolvedException e)
        {
            try
            {
                JSONObject error = new JSONObject();
                error.put("exception", e.getMessage());
                resp.getWriter().write(error.toString());
                resp.setStatus(500);
                resp.setHeader("Content-Type", "application/json");
                LOGGER_REST.error(e.getMessage(), e);
            }
            catch (JSONException e1)
            {
                throw new RuntimeException(e1);
            }
        }
        catch (NucleusUserException e)
        {
            try
            {
                JSONObject error = new JSONObject();
                error.put("exception", e.getMessage());
                resp.getWriter().write(error.toString());
                resp.setStatus(400);
                resp.setHeader("Content-Type", "application/json");
                LOGGER_REST.error(e.getMessage(), e);
            }
            catch (JSONException e1)
            {
                throw new RuntimeException(e1);
            }
        }
        catch (NucleusException e)
        {
            try
            {
                JSONObject error = new JSONObject();
                error.put("exception", e.getMessage());
                resp.getWriter().write(error.toString());
                resp.setStatus(500);
                resp.setHeader("Content-Type", "application/json");
                LOGGER_REST.error(e.getMessage(), e);
            }
            catch (JSONException e1)
            {
                throw new RuntimeException(e1);
            }
        }
        catch (JSONException e)
        {
            try
            {
                JSONObject error = new JSONObject();
                error.put("exception", e.getMessage());
                resp.getWriter().write(error.toString());
                resp.setStatus(500);
                resp.setHeader("Content-Type", "application/json");
                LOGGER_REST.error(e.getMessage(), e);
            }
            catch (JSONException e1)
View Full Code Here

            }
            catch (ClassNotResolvedException ex)
            {
                try
                {
                    JSONObject error = new JSONObject();
                    error.put("exception", ex.getMessage());
                    resp.getWriter().write(error.toString());
                    resp.setStatus(404);
                    resp.setHeader("Content-Type", "application/json");
                }
                catch (JSONException e)
                {
                    // will not happen
                }
                return;
            }

            api.getTransaction().begin();
            JSONObject id = getId(api, req);
            id.put("class", getClassNameForPath(req));
            api.deleteObject(id);
            api.getTransaction().commit();
        }
        catch (NucleusObjectNotFoundException ex)
        {
            try
            {
                JSONObject error = new JSONObject();
                error.put("exception", ex.getMessage());
                resp.getWriter().write(error.toString());
                resp.setStatus(400);
                resp.setHeader("Content-Type", "application/json");
                return;
            }
            catch (JSONException e)
            {
                // will not happen
            }
        }
        catch (NucleusUserException e)
        {
            try
            {
                JSONObject error = new JSONObject();
                error.put("exception", e.getMessage());
                resp.getWriter().write(error.toString());
                resp.setStatus(400);
                resp.setHeader("Content-Type", "application/json");
                return;
            }
            catch (JSONException e1)
            {
                // ignore
            }
        }
        catch (NucleusException e)
        {
            try
            {
                JSONObject error = new JSONObject();
                error.put("exception", e.getMessage());
                resp.getWriter().write(error.toString());
                resp.setStatus(500);
                resp.setHeader("Content-Type", "application/json");
                LOGGER_REST.error(e.getMessage(), e);
            }
            catch (JSONException e1)
View Full Code Here

      // good
    }
  }

  public void testWriteJSONObject() throws Exception {
    JSONObject json = new JSONObject();
    json.put("k1", 1);
    json.put("k2", true);
    json.put("k3", 2.2);
    JavascriptAppender.INSTANCE.append(out, gxpContext, json);
    assertOutputEquals("{\"k3\":2.2,\"k1\":1,\"k2\":true}");

    json = null;
    try {
View Full Code Here

    GeometrySerializer ser = new GeometrySerializer();
    GeometryFactory factory = new GeometryFactory(new PrecisionModel(100.0), 31300);
    CoordinateArraySequence coords = new CoordinateArraySequence(new Coordinate[] {new Coordinate(
        12.3456, 34567.3456)});
    Point p = new Point(coords, factory);
    JSONObject jo = (JSONObject) ser.marshall(null, p);
    assertEquals("Point", jo.get("type").toString());
    assertEquals("31300", jo.get("srid").toString());
    assertEquals("2", jo.get("precision").toString());
    assertEquals("[12.35,34567.35]", jo.get("coordinates").toString());
  }
View Full Code Here

    GeometrySerializer ser = new GeometrySerializer();
    GeometryFactory factory = new GeometryFactory(new PrecisionModel(100.0), 31300);
    CoordinateArraySequence coords = new CoordinateArraySequence(new Coordinate[] {
        new Coordinate(12.0, 34.23), new Coordinate(12.000, 54.555), new Coordinate(-0.01, -0.0)});
    LineString p = new LineString(coords, factory);
    JSONObject jo = (JSONObject) ser.marshall(null, p);
    assertEquals("LineString", jo.get("type").toString());
    assertEquals("31300", jo.get("srid").toString());
    assertEquals("2", jo.get("precision").toString());
    assertEquals("[12,34.23,12,54.56,-0.01,0]", jo.get("coordinates").toString());
  }
View Full Code Here

    CoordinateArraySequence coords = new CoordinateArraySequence(new Coordinate[] {
        new Coordinate(12.0, 34.23), new Coordinate(12.000, 54.555), new Coordinate(7, 8),
        new Coordinate(12.0, 34.23)});
    LinearRing ring = new LinearRing(coords, factory);
    Polygon p = new Polygon(ring, new LinearRing[] {}, factory);
    JSONObject jo = (JSONObject) ser.marshall(null, p);
    assertEquals("Polygon", jo.get("type").toString());
    assertEquals("[]", jo.get("holes").toString());
    assertEquals("31300", jo.get("srid").toString());
    assertEquals("2", jo.get("precision").toString());
    JSONObject shell = jo.getJSONObject("shell");
    assertEquals("LineString", shell.get("type").toString());
    assertEquals("31300", shell.get("srid").toString());
    assertEquals("2", shell.get("precision").toString());
    assertEquals("[12,34.23,12,54.56,7,8,12,34.23]", shell.get("coordinates").toString());
  }
View Full Code Here

TOP

Related Classes of org.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.