Examples of JSONStringer


Examples of org.apache.wink.json4j.compat.JSONStringer

    public void test_WriteObjectComplex() {
        Exception ex = null;
        try{
            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.json4j.compat.impl.ApacheJSONFactory");
            JSONFactory factory = JSONFactory.newInstance();
            JSONStringer jStringer = factory.createJSONStringer();
            jStringer.object();
            jStringer.key("string");
            jStringer.value("String1");
            jStringer.key("bool");
            jStringer.value(false);
            jStringer.key("number");
            jStringer.value(1);

            // Place an object
            jStringer.key("object");
            jStringer.object();
            jStringer.key("string");
            jStringer.value("String2");
            jStringer.endObject();

            // Place an array
            jStringer.key("array");
            jStringer.array();
            jStringer.value(1);
            jStringer.value((double)2);
            jStringer.value((short)3);
            jStringer.endArray();

            //Close top object.
            jStringer.endObject();

            String str = jStringer.toString();

            // Verify it parses.
            JSONObject test = factory.createJSONObject(str);
            assertTrue(str.equals("{\"string\":\"String1\",\"bool\":false,\"number\":1,\"object\":{\"string\":\"String2\"},\"array\":[1,2.0,3]}"));
        }catch(Exception ex1){
View Full Code Here

Examples of org.apache.wink.json4j.compat.JSONStringer

    public void test_WriteArrayComplex() {
        Exception ex = null;
        try{
            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.json4j.compat.impl.ApacheJSONFactory");
            JSONFactory factory = JSONFactory.newInstance();
            JSONStringer jStringer = factory.createJSONStringer();
            jStringer.array();
            jStringer.value("String1");
            jStringer.value(false);
            jStringer.value(1);

            // Place an object
            jStringer.object();
            jStringer.key("string");
            jStringer.value("String2");
            jStringer.endObject();

            // Place an array
            jStringer.array();
            jStringer.value(1);
            jStringer.value((double)2);
            jStringer.value((short)3);
            jStringer.endArray();

            //Close top array.
            jStringer.endArray();

            String str = jStringer.toString();

            // Verify it parses.
            JSONArray test = factory.createJSONArray(str);
            assertTrue(str.equals("[\"String1\",false,1,{\"string\":\"String2\"},[1,2.0,3]]"));
        }catch(Exception ex1){
View Full Code Here

Examples of org.apache.wink.json4j.compat.JSONStringer

    public void test_OptsAfterCloseFail() {
        Exception ex = null;
        try{
            System.setProperty("org.apache.wink.common.model.json.factory.impl", "org.apache.wink.json4j.compat.impl.ApacheJSONFactory");
            JSONFactory factory = JSONFactory.newInstance();
            JSONStringer jStringer = factory.createJSONStringer();
            jStringer.array();
            jStringer.toString();

            // This should die.
            jStringer.endArray();
        }catch(Exception ex1){
            ex = ex1;
        }
        assertTrue(ex instanceof IllegalStateException);
    }
View Full Code Here

Examples of org.codehaus.jettison.json.JSONStringer

  /** Test if toString generates real JSON */
  @Test
  public void testToString() throws JSONException {
    SchemaConfigured sc = new SchemaConfigured(null, TABLE_NAME, CF_NAME);
    JSONStringer json = new JSONStringer();
    json.object();
    json.key("tableName");
    json.value(TABLE_NAME);
    json.key("cfName");
    json.value(CF_NAME);
    json.endObject();
    assertEquals(json.toString(), sc.schemaConfAsJSON());
  }
View Full Code Here

Examples of org.json.JSONStringer

    @Test
    @InSequence(1)
    public void testAddMember() throws Exception {
        HttpPost post = new HttpPost(contextPath.toString() + API_PATH);
        post.setHeader("Content-Type", "application/json");
        String newMemberJSON = new JSONStringer().object()
                .key("name").value(NEW_MEMBER_NAME)
                .key("email").value(NEW_MEMBER_EMAIL)
                .key("phoneNumber").value(NEW_MEMBER_PHONE)
                .endObject().toString();
        post.setEntity(new StringEntity(newMemberJSON));
View Full Code Here

Examples of org.json.JSONStringer

{
    JSONWriter json;

    public JSONWriterSerializer()
    {
        this.json = new JSONStringer();
    }
View Full Code Here

Examples of org.json.JSONStringer

   public void marshal(ReadResourceModel model, OutputStream outputStream) 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();

         printWriter.write(new JSONObject(json.toString()).toString(3));
         printWriter.flush();
      }
      catch (JSONException e)
      {
         throw new BindingException("Could not marshal to JSON format", e);
View Full Code Here

Examples of org.json.JSONStringer

                    result = jsonObject.toString(getIndentingSize());
                } else {
                    result = jsonObject.toString();
                }
            } else if (this.jsonValue instanceof JSONStringer) {
                JSONStringer jsonStringer = (JSONStringer) this.jsonValue;
                result = jsonStringer.toString();
            } else if (this.jsonValue instanceof JSONTokener) {
                JSONTokener jsonTokener = (JSONTokener) this.jsonValue;
                result = jsonTokener.toString();
            }
        } else if (this.jsonRepresentation != null) {
View Full Code Here

Examples of org.json.JSONStringer

        Token lBCT = new Token(lNS, "event");
        lBCT.put("name", "init");

        String lData = null;
        try {
          JSONStringer jsonStringer = new JSONStringer();
          // start main object
          jsonStringer.object();
          // iterate through all items (fields) of the token
          Iterator<String> lIterator = sharedObjects.getKeys().iterator();
          while (lIterator.hasNext()) {
            String lKey = lIterator.next();
            Object lVal = sharedObjects.get(lKey);
            if (lVal instanceof Collection) {
              jsonStringer.key(lKey).array();
              for (Object item : (Collection) lVal) {
                jsonStringer.value(item);
              }
              jsonStringer.endArray();
            } else {
              jsonStringer.key(lKey).value(lVal);
            }
          }
          // end main object
          jsonStringer.endObject();
          lData = jsonStringer.toString();
        } catch (JSONException ex) {
          log.error(ex.getClass().getSimpleName() + ": " + ex.getMessage());
        }
        lBCT.put("value", lData);
        getServer().sendToken(aConnector, lBCT);
View Full Code Here

Examples of org.json.JSONStringer

     * @throws JSONException
     *             When JSON string could not be constructed.
     */
    public String getMidiDeviceInfo() throws JSONException
    {
        final JSONStringer js = new JSONStringer();
        js.array();
        for (final Info info : MidiSystem.getMidiDeviceInfo())
        {
            js.object();
            js.key("name").value(info.getName());
            js.key("description").value(info.getDescription());
            js.key("vendor").value(info.getVendor());
            js.key("version").value(info.getVersion());
            js.endObject();
        }
        js.endArray();
        return js.toString();
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.