Package com.xmage.ws.util.json

Examples of com.xmage.ws.util.json.JSONParser


*/
public class TestJSONParser {

    @Test
    public void testParse() throws Exception {
        JSONParser parser = new JSONParser();
        parser.parseJSON("{}");
        parser.parseJSON("{\"test\" : 1}");
        parser.parseJSON("{\"test\" : \"test\"}");
        parser.parseJSON("{\"list\" : [\"1\", \"2\", \"3\"]}");
        parser.parseJSON("{test:test}");

        testError(parser, "{");
        testError(parser, "}");
        testError(parser, "{{}");
        testError(parser, "{\"test\" : [}}");
View Full Code Here


        testError(parser, "{\"test\" : [}}");
    }

    @Test
    public void testQueryForInt() throws Exception {
        JSONParser parser = new JSONParser();
        parser.parseJSON("{\"test\" : 1}");
        Assert.assertEquals(1, parser.getInt("test"));

        parser = new JSONParser();
        parser.parseJSON("{test : { internal : {level : 2}}}");
        Assert.assertEquals(2, parser.getInt("test.internal.level"));
        Assert.assertFalse("No cache should have been used", parser.isHitCache());

        Assert.assertEquals(2, parser.getInt("test.internal.level"));
        Assert.assertTrue("Cache should have been used this time!", parser.isHitCache());
    }
View Full Code Here

        Assert.assertTrue("Cache should have been used this time!", parser.isHitCache());
    }

    @Test
    public void testQueryForJSONArray() throws Exception {
        JSONParser parser = new JSONParser();
        parser.parseJSON("{\"test\" : [\"1\", \"2\", \"3\"]}");
        Assert.assertTrue(parser.getJSONArray("test") instanceof JSONArray);
        Assert.assertEquals("1", parser.getJSONArray("test").get(0));

        parser = new JSONParser();
        parser.parseJSON("{\"test\" : [1,2,3]}");
        Assert.assertTrue(parser.getJSONArray("test") instanceof JSONArray);
        Assert.assertFalse(parser.isHitCache());
        Assert.assertEquals(2, parser.getJSONArray("test").get(1));
        Assert.assertTrue(parser.isHitCache());

        Assert.assertTrue(parser.getJSONArray("test") instanceof JSONArray);
        Assert.assertEquals(2, parser.getJSONArray("test").get(1));
        Assert.assertTrue(parser.isHitCache());

        parser = new JSONParser();
        parser.parseJSON("{\"test\" : [{second_level: \"3\"}, {\"third_level\" : 2}]}");
        Assert.assertTrue(parser.getJSONArray("test") instanceof JSONArray);
        Assert.assertTrue(parser.getJSONArray("test").get(0) instanceof JSONObject);
        Assert.assertEquals(2, parser.getInt("test[1].third_level"));
        Assert.assertEquals("3", parser.getString("test[0].second_level"));

        parser = new JSONParser();
        parser.parseJSON("{\"test\" : [{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{2:3},{4:5}]}");
        Assert.assertTrue(parser.getJSONArray("test") instanceof JSONArray);
        Assert.assertEquals(5, parser.getInt("test[10].4"));
    }
View Full Code Here

        //parser.getInt("test.internal.level");
    }

    @Test
    public void testExtendedCache() throws Exception {
        JSONParser parser = new JSONParser();
        parser.parseJSON("{test : { internal : {level : 2}}}");
        Assert.assertEquals(2, parser.getInt("test.internal.level"));
        Assert.assertFalse("No cache should have been used", parser.isHitCache());

        Assert.assertTrue(parser.getJSON("test") instanceof JSONObject);
        Assert.assertFalse("No cache should have been used", parser.isHitCache());
        Assert.assertTrue(parser.getJSON("test.internal") instanceof JSONObject);
        Assert.assertFalse("No cache should have been used", parser.isHitCache());

        parser = new JSONParser();
        parser.parseJSON("{test : { internal : {level : 2}}}");
        parser.setCachePolicy(JSONParser.CachePolicy.CACHE_ALL_LEVELS);
        Assert.assertEquals(2, parser.getInt("test.internal.level"));
        Assert.assertFalse("No cache should have been used", parser.isHitCache());

        Assert.assertTrue(parser.getJSON("test") instanceof JSONObject);
        Assert.assertTrue("Cache should have been used this time!", parser.isHitCache());
        Assert.assertTrue(parser.getJSON("test.internal") instanceof JSONObject);
        Assert.assertTrue("Cache should have been used this time!", parser.isHitCache());
    }
View Full Code Here

        Assert.assertTrue("Cache should have been used this time!", parser.isHitCache());
    }

    @Test
    public void testExtendedIndexes() throws Exception {
        JSONParser parser = new JSONParser();
        parser.parseJSON("{\"test\" : [1,2,3,4,5]}");
        Assert.assertEquals(1, parser.getInt("test[].$first"));
        Assert.assertEquals(2, parser.getInt("test[].$second"));
        Assert.assertEquals(3, parser.getInt("test[].$third"));
        Assert.assertEquals(4, parser.getInt("test[].$fourth"));
        Assert.assertEquals(5, parser.getInt("test[].$fifth"));

        parser = new JSONParser();
        parser.parseJSON("{\"test\" : [{1:1},{2:2},{3:3},{4:4},{5:5}]}");
        Assert.assertEquals(1, parser.getInt("test[].$first.1"));
        Assert.assertEquals(2, parser.getInt("test[].$second.2"));
        Assert.assertEquals(3, parser.getInt("test[].$third.3"));
        Assert.assertEquals(4, parser.getInt("test[].$fourth.4"));
        Assert.assertEquals(5, parser.getInt("test[].$fifth.5"));

        parser = new JSONParser();
        parser.parseJSON("{\"contacts\": {\"phones\": [\n" +
                "   {\"phone\": \"100000\"},\n" +
                "   {\"phone\": \"+7 999 1234567\"}\n" +
                "  ]}}");

        Assert.assertEquals("100000", parser.getString("contacts.phones[].$first.phone"));

    }
View Full Code Here

        XMageStatsService xMageStatsService = new XMageStatsService();

        Response response = xMageStatsService.getAllStats();

        JSONParser parser = new JSONParser();
        parser.parseJSON((String) response.getEntity());

        Assert.assertEquals(DomainErrors.Errors.STATUS_OK.getCode(), parser.getInt("code"));
        System.out.println("response = " + response.getEntity().toString());
    }
View Full Code Here

TOP

Related Classes of com.xmage.ws.util.json.JSONParser

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.