Search in sources :

Example 16 with JSONObject

use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.

the class TranscriptionServiceImpl method startTranscriptionJob.

@Override
public String startTranscriptionJob(InputStream stream, String mimeType) {
    Request request = httpClientFactory.post("/speech-to-text/api/v1/recognitions?continuous=true&timestamps=true").addHeader("Content-Type", mimeType).bodyStream(stream);
    try {
        JSONObject json = httpClientFactory.getExecutor().execute(request).handleResponse(HANDLER);
        log.trace("content: {}", json.toString(2));
        return json.getString("id");
    } catch (Exception e) {
        log.error("error submitting job", e);
        return null;
    }
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) Request(org.apache.http.client.fluent.Request)

Example 17 with JSONObject

use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.

the class ChildrenAsPropertyResourceTest method testAdd_Unsorted.

@Test
public void testAdd_Unsorted() throws Exception {
    valueMap.put("animals", unsortedJSON.toString());
    ValueMap properties = new ValueMapDecorator(new HashMap<String, Object>());
    properties.put("name", "hyena");
    properties.put("sound", "lolz");
    properties.put("jcr:primaryType", "nt:unstructured");
    JSONObject expectedJSON = new JSONObject(unsortedJSON.toString());
    expectedJSON.put("entry-4", new JSONObject(properties));
    childrenAsPropertyResource = new ChildrenAsPropertyResource(resource, "animals");
    childrenAsPropertyResource.create("entry-4", "nt:unstructured", properties);
    childrenAsPropertyResource.persist();
    String actual = resource.getValueMap().get("animals", String.class);
    String expected = expectedJSON.toString();
    Assert.assertEquals(expected, actual);
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) ModifiableValueMapDecorator(org.apache.sling.api.wrappers.ModifiableValueMapDecorator) JSONObject(org.apache.sling.commons.json.JSONObject) Test(org.junit.Test)

Example 18 with JSONObject

use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.

the class ChildrenAsPropertyResourceTest method testAdd_Sorted.

@Test
public void testAdd_Sorted() throws Exception {
    valueMap.put("animals", unsortedJSON.toString());
    ValueMap properties = new ValueMapDecorator(new HashMap<String, Object>());
    properties.put("name", "hyena");
    properties.put("sound", "lolz");
    properties.put("jcr:primaryType", "nt:unstructured");
    JSONObject expectedJSON = new JSONObject(sortedJSON.toString());
    expectedJSON.put("entry-4", new JSONObject(properties));
    childrenAsPropertyResource = new ChildrenAsPropertyResource(resource, "animals", ChildrenAsPropertyResource.RESOURCE_NAME_COMPARATOR);
    childrenAsPropertyResource.create("entry-4", "nt:unstructured", properties);
    childrenAsPropertyResource.persist();
    String actual = resource.getValueMap().get("animals", String.class);
    String expected = expectedJSON.toString();
    Assert.assertEquals(expected, actual);
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) ModifiableValueMapDecorator(org.apache.sling.api.wrappers.ModifiableValueMapDecorator) JSONObject(org.apache.sling.commons.json.JSONObject) Test(org.junit.Test)

Example 19 with JSONObject

use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.

the class ChildrenAsPropertyResourceTest method setUp.

@Before
public void setUp() throws Exception {
    valueMap = new ModifiableValueMapDecorator(new HashMap<String, Object>());
    when(resource.getPath()).thenReturn("/content/test");
    when(resource.getValueMap()).thenReturn(valueMap);
    when(resource.adaptTo(ValueMap.class)).thenReturn(valueMap);
    when(resource.adaptTo(ModifiableValueMap.class)).thenReturn(valueMap);
    when(resource.getResourceResolver()).thenReturn(resourceResolver);
    entry1.put("name", "dog");
    entry1.put("sound", "woof");
    entry1.put("jcr:primaryType", "nt:unstructured");
    entry2.put("name", "cat");
    entry2.put("sound", "meow");
    entry1.put("jcr:primaryType", "nt:unstructured");
    entry3.put("name", "fish");
    entry3.put("sound", "...");
    entry1.put("jcr:primaryType", "nt:unstructured");
    entry100.put("name", "dog");
    entry100.put("sound", "woof");
    entry100.put("double", 20.002D);
    entry100.put("long", 2000L);
    entry100.put("bigdecimal", "10000.000001");
    entry100.put("bigdecimal2", "20000.000002");
    entry100.put("integer", 100);
    entry100.put("integerAsLong", 100L);
    entry100.put("integerAsDouble", 100D);
    Calendar cal = Calendar.getInstance();
    cal.set(2001, 1, 1, 1, 1, 1);
    entry100.put("date", cal.getTime());
    entry100.put("calendar", cal);
    entry100.put("boolean", true);
    entry100.put("booleanAsString", "true");
    entry100.put("strArray", new String[] { "one", "two" });
    entry1.put("jcr:primaryType", "nt:unstructured");
    unsortedJSON.put("entry-2", new JSONObject(entry2));
    unsortedJSON.put("entry-1", new JSONObject(entry1));
    unsortedJSON.put("entry-3", new JSONObject(entry3));
    sortedJSON.put("entry-1", new JSONObject(entry1));
    sortedJSON.put("entry-2", new JSONObject(entry2));
    sortedJSON.put("entry-3", new JSONObject(entry3));
}
Also used : ModifiableValueMapDecorator(org.apache.sling.api.wrappers.ModifiableValueMapDecorator) JSONObject(org.apache.sling.commons.json.JSONObject) HashMap(java.util.HashMap) Calendar(java.util.Calendar) Before(org.junit.Before)

Example 20 with JSONObject

use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.

the class TypeUtilTest method testToMap_withType.

@Test
public void testToMap_withType() throws JSONException {
    final JSONObject json = new JSONObject();
    json.put("one", "uno");
    json.put("two", 2);
    json.put("three", "tres");
    final Map<String, String> expResult = new HashMap<String, String>();
    expResult.put("one", "uno");
    expResult.put("three", "tres");
    final Map<String, String> actual = TypeUtil.toMap(json, String.class);
    assertEquals(expResult, actual);
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

JSONObject (org.apache.sling.commons.json.JSONObject)74 JSONArray (org.apache.sling.commons.json.JSONArray)22 Test (org.junit.Test)20 JSONException (org.apache.sling.commons.json.JSONException)16 HashMap (java.util.HashMap)15 ValueMap (org.apache.sling.api.resource.ValueMap)9 Map (java.util.Map)8 ArrayList (java.util.ArrayList)6 ServletException (javax.servlet.ServletException)6 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)6 Resource (org.apache.sling.api.resource.Resource)6 RepositoryException (javax.jcr.RepositoryException)5 Calendar (java.util.Calendar)4 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)4 Config (com.adobe.acs.commons.workflow.bulk.execution.model.Config)3 IOException (java.io.IOException)3 LinkedHashMap (java.util.LinkedHashMap)3 Session (javax.jcr.Session)3 ModifiableValueMapDecorator (org.apache.sling.api.wrappers.ModifiableValueMapDecorator)3 Form (com.adobe.acs.commons.forms.Form)2