Search in sources :

Example 1 with JSONParser

use of net.minidev.json.parser.JSONParser in project JsonPath by jayway.

the class IssuesTest method issue_76.

@Test
public void issue_76() throws Exception {
    String json = "{\n" + "    \"cpus\": -8.88178419700125e-16,\n" + "    \"disk\": 0,\n" + "    \"mem\": 0\n" + "}";
    JSONParser parser = new JSONParser(JSONParser.MODE_PERMISSIVE);
    JSONAware jsonModel = (JSONAware) parser.parse(json);
    jsonModel.toJSONString();
}
Also used : JSONParser(net.minidev.json.parser.JSONParser) JSONAware(net.minidev.json.JSONAware) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 2 with JSONParser

use of net.minidev.json.parser.JSONParser in project skype-bot by toomasr.

the class BitBucketHookHandler method handle.

@Override
public Map<String, String[]> handle(Map<String, String[]> parameters, String requestBody) {
    try {
        requestBody = URLDecoder.decode(requestBody, "UTF-8");
        requestBody = requestBody.replaceFirst("payload=", "");
        JSONObject object = (JSONObject) new JSONParser(JSONParser.MODE_PERMISSIVE).parse(requestBody);
        JSONObject repo = (JSONObject) object.get("repository");
        // String repoName = (String) repo.get("name");
        String absoluteUrl = (String) repo.get("absolute_url");
        String repoUrl = "https://bitbucket.org" + absoluteUrl;
        List<String> messages = new ArrayList<String>();
        JSONArray commits = (JSONArray) object.get("commits");
        for (int i = 0; i < commits.size(); i++) {
            JSONObject commit = (JSONObject) commits.get(i);
            String rawAuthor = (String) commit.get("raw_author");
            rawAuthor = rawAuthor.replaceFirst(" <.*>", "");
            String message = (String) commit.get("message");
            String branch = (String) commit.get("branch");
            messages.add(String.format("%s commited '%s' to %s (branch '%s')", rawAuthor, message, repoUrl, branch));
        }
        Map<String, String[]> replies = new HashMap<String, String[]>();
        replies.put(chatName, messages.toArray(new String[0]));
        return replies;
    } catch (Exception e) {
        throw new RuntimeException("Cannot parse json: " + requestBody, e);
    }
}
Also used : JSONObject(net.minidev.json.JSONObject) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONArray(net.minidev.json.JSONArray) JSONParser(net.minidev.json.parser.JSONParser)

Example 3 with JSONParser

use of net.minidev.json.parser.JSONParser in project skype-bot by toomasr.

the class Weather method getKelvinsTemp.

private Double getKelvinsTemp(String cityId) throws Exception, ParseException {
    String url = baseUrl.replaceFirst("CITY_ID", cityId);
    String json = URLConnectionReader.getText(url);
    JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);
    JSONObject o1 = (JSONObject) p.parse(json);
    o1 = (JSONObject) o1.get("main");
    Number kelvins = (Number) o1.get("temp");
    return kelvins.doubleValue();
}
Also used : JSONObject(net.minidev.json.JSONObject) JSONParser(net.minidev.json.parser.JSONParser)

Example 4 with JSONParser

use of net.minidev.json.parser.JSONParser in project jmeter by apache.

the class TestJSONPostProcessor method testBug59609.

@Test
public void testBug59609() throws ParseException {
    JMeterContext context = JMeterContextService.getContext();
    JSONPostProcessor processor = setupProcessor(context, "0", false);
    String innerValue = "{\"a\":\"one\",\"b\":\"two\"}";
    String data = "{\"context\":" + innerValue + "}";
    SampleResult result = new SampleResult();
    result.setResponseData(data.getBytes(StandardCharsets.UTF_8));
    JMeterVariables vars = new JMeterVariables();
    context.setVariables(vars);
    context.setPreviousResult(result);
    processor.setJsonPathExpressions("$.context");
    processor.process();
    JSONParser parser = new JSONParser(0);
    Object expectedValue = parser.parse(innerValue);
    assertThat(parser.parse(vars.get(VAR_NAME)), CoreMatchers.is(expectedValue));
    assertThat(vars.get(VAR_NAME + "_matchNr"), CoreMatchers.is(CoreMatchers.nullValue()));
    assertThat(vars.get(VAR_NAME + "_1"), CoreMatchers.is(CoreMatchers.nullValue()));
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) JSONPostProcessor(org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor) SampleResult(org.apache.jmeter.samplers.SampleResult) JSONParser(net.minidev.json.parser.JSONParser) Test(org.junit.Test)

Example 5 with JSONParser

use of net.minidev.json.parser.JSONParser in project ddf by codice.

the class TestLogoutService method testLogout.

@Test
public void testLogout() throws IOException, ParseException, SecurityServiceException {
    KarafLogoutAction karafLogoutActionProvider = new KarafLogoutAction();
    LdapLogoutAction ldapLogoutActionProvider = new LdapLogoutAction();
    Action karafLogoutAction = karafLogoutActionProvider.getAction(null);
    Action ldapLogoutAction = ldapLogoutActionProvider.getAction(null);
    LogoutService logoutService = new LogoutService();
    logoutService.setHttpSessionFactory(sessionFactory);
    logoutService.setSecurityManager(sm);
    logoutService.setLogoutActionProviders(Arrays.asList(karafLogoutActionProvider, ldapLogoutActionProvider));
    String responseMessage = IOUtils.toString((ByteArrayInputStream) logoutService.getActionProviders(null).getEntity());
    JSONArray actionProperties = (JSONArray) new JSONParser().parse(responseMessage);
    assertEquals(2, actionProperties.size());
    JSONObject karafActionProperty = ((JSONObject) actionProperties.get(0));
    assertEquals(karafActionProperty.get("description"), karafLogoutAction.getDescription());
    assertEquals(karafActionProperty.get("realm"), karafLogoutAction.getId().substring(karafLogoutAction.getId().lastIndexOf(".") + 1));
    assertEquals(karafActionProperty.get("title"), karafLogoutAction.getTitle());
    assertEquals(karafActionProperty.get("url"), karafLogoutAction.getUrl().toString());
    JSONObject ldapActionProperty = ((JSONObject) actionProperties.get(1));
    assertEquals(ldapActionProperty.get("description"), ldapLogoutAction.getDescription());
    assertEquals(ldapActionProperty.get("realm"), ldapLogoutAction.getId().substring(ldapLogoutAction.getId().lastIndexOf(".") + 1));
    assertEquals(ldapActionProperty.get("title"), ldapLogoutAction.getTitle());
    assertEquals(ldapActionProperty.get("url"), ldapLogoutAction.getUrl().toString());
}
Also used : Action(ddf.action.Action) JSONObject(net.minidev.json.JSONObject) JSONArray(net.minidev.json.JSONArray) JSONParser(net.minidev.json.parser.JSONParser) Test(org.junit.Test)

Aggregations

JSONParser (net.minidev.json.parser.JSONParser)8 JSONObject (net.minidev.json.JSONObject)5 JSONArray (net.minidev.json.JSONArray)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 BaseTest (com.jayway.jsonpath.BaseTest)1 Action (ddf.action.Action)1 CatalogFramework (ddf.catalog.CatalogFramework)1 ContentType (ddf.catalog.data.ContentType)1 ContentTypeImpl (ddf.catalog.data.impl.ContentTypeImpl)1 QueryResponse (ddf.catalog.operation.QueryResponse)1 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)1 SourceInfoRequestEnterprise (ddf.catalog.operation.impl.SourceInfoRequestEnterprise)1 SourceInfoResponseImpl (ddf.catalog.operation.impl.SourceInfoResponseImpl)1 SourceDescriptor (ddf.catalog.source.SourceDescriptor)1 SourceDescriptorImpl (ddf.catalog.source.impl.SourceDescriptorImpl)1 InputStreamReader (java.io.InputStreamReader)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1