org.json.CDL Java Examples

The following examples show how to use org.json.CDL. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JSONObjectTest.java    From JSON-Java-unit-test with Apache License 2.0 6 votes vote down vote up
/**
 * JSONObjects can be built from a Map<String, Object>. 
 * In this test the map entries are not valid JSON types.
 * The actual conversion is kind of interesting.
 */
@Test
public void jsonObjectByMapWithUnsupportedValues() {
    Map<String, Object> jsonMap = new HashMap<String, Object>();
    // Just insert some random objects
    jsonMap.put("key1", new CDL());
    jsonMap.put("key2", new Exception());

    JSONObject jsonObject = new JSONObject(jsonMap);

    // validate JSON
    Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
    assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
    assertTrue("expected 0 key1 items", ((Map<?,?>)(JsonPath.read(doc, "$.key1"))).size() == 0);
    assertTrue("expected \"key2\":java.lang.Exception","java.lang.Exception".equals(jsonObject.query("/key2")));
}
 
Example #2
Source File: CDLTest.java    From JSON-Java-unit-test with Apache License 2.0 6 votes vote down vote up
/**
 * Given a JSONArray that was not built by CDL, some chars may be
 * found that would otherwise be filtered out by CDL.
 */
@Test
public void checkSpecialChars() {
    JSONArray jsonArray = new JSONArray();
    JSONObject jsonObject = new JSONObject();
    jsonArray.put(jsonObject);
    // \r will be filtered from name
    jsonObject.put("Col \r1", "V1");
    // \r will be filtered from value
    jsonObject.put("Col 2", "V2\r");
    assertTrue("expected length should be 1",jsonArray.length() == 1);
    String cdlStr = CDL.toString(jsonArray);
    jsonObject = jsonArray.getJSONObject(0);
    assertTrue(cdlStr.contains("\"Col 1\""));
    assertTrue(cdlStr.contains("Col 2"));
    assertTrue(cdlStr.contains("V1"));
    assertTrue(cdlStr.contains("\"V2\""));
}
 
Example #3
Source File: CDLTest.java    From JSON-Java-unit-test with Apache License 2.0 6 votes vote down vote up
/**
 * Attempt to create a JSONArray with an escape quote and no enclosing quotes.
 * Expects a JSONException. 
 */
@Test
public void badEscapedQuote(){
	       String badLine = "Col1, Col2\nVal1, \"\"Val2";
	       
	       try {
               CDL.toJSONArray(badLine);
               fail("Expecting an exception");
           } catch (JSONException e) {
        	   System.out.println("Message" + e.getMessage());
               assertEquals("Expecting an exception message",
                       "Bad character 'V' (86). at 20 [character 9 line 2]",
                       e.getMessage());
               
           }
           
}
 
Example #4
Source File: CDLTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * Create a JSONArray from string containing only whitespace and commas
 */
@Test
public void emptyLinesToJSONArray() {
    String str = " , , , \n , , , ";
    JSONArray jsonArray = CDL.toJSONArray(str);
    assertNull("JSONArray should be null for no content",
            jsonArray);
}
 
Example #5
Source File: CDLIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenCommaDelimitedText_thenGetJSONArrayOfJSONObjects2() {
    JSONArray ja = new JSONArray();
    ja.put("name");
    ja.put("city");
    ja.put("age");
     
    String string = 
      "john, chicago, 22 \n" +
      "gary, florida, 35 \n" +
      "sal, vegas, 18";
     
    JSONArray result = CDL.toJSONArray(ja, string);
    assertEquals("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]", result.toString());
}
 
Example #6
Source File: CDLIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenCommaDelimitedText_thenGetJSONArrayOfJSONObjects() {
    String string = 
      "name, city, age \n" +
      "john, chicago, 22 \n" +
      "gary, florida, 35 \n" +
      "sal, vegas, 18";
     
    JSONArray result = CDL.toJSONArray(string);
    assertEquals("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]", result.toString());
}
 
Example #7
Source File: CDLDemo.java    From tutorials with MIT License 5 votes vote down vote up
public static void jaOfJOFromCDT2() {
    JSONArray ja = new JSONArray();
    ja.put("name");
    ja.put("city");
    ja.put("age");
     
    String string = 
      "john, chicago, 22 \n" +
      "gary, florida, 35 \n" +
      "sal, vegas, 18";
     
    JSONArray result = CDL.toJSONArray(ja, string);
    System.out.println(result.toString());
}
 
Example #8
Source File: CDLDemo.java    From tutorials with MIT License 5 votes vote down vote up
public static void jaOfJOFromCDT() {
    String string = 
      "name, city, age \n" +
      "john, chicago, 22 \n" +
      "gary, florida, 35 \n" +
      "sal, vegas, 18";
     
    JSONArray result = CDL.toJSONArray(string);
    System.out.println(result.toString());
}
 
Example #9
Source File: CDLTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * Create a JSONArray from a string of lines,
 * then convert to string and then back to JSONArray
 */
@Test
public void textToJSONArrayAndBackToString() {
    JSONArray jsonArray = CDL.toJSONArray(this.lines);
    String jsonStr = CDL.toString(jsonArray);
    JSONArray finalJsonArray = CDL.toJSONArray(jsonStr);
    JSONArray expectedJsonArray = new JSONArray(this.expectedLines);
    Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
}
 
Example #10
Source File: CDLTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * Create a JSONArray from a JSONArray of titles and a 
 * string of value lines
 */
@Test
public void jsonArrayToJSONArray() {
    String nameArrayStr = "[Col1, Col2]";
    String values = "V1, V2";
    JSONArray nameJSONArray = new JSONArray(nameArrayStr);
    JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values);
    JSONArray expectedJsonArray = new JSONArray("[{Col1:V1,Col2:V2}]");
    Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
}
 
Example #11
Source File: CDLTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * Create a JSONArray from a string of lines
 */
@Test
public void textToJSONArray() {
    JSONArray jsonArray = CDL.toJSONArray(this.lines);
    JSONArray expectedJsonArray = new JSONArray(this.expectedLines);
    Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
}
 
Example #12
Source File: CDLTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * call toString with a null arrays for names and values
 */
@Test
public void nullJSONArraysToString() {
    String str = CDL.toString(null, null);
    assertNull("CDL should return null for toString(null)",
            str);
}
 
Example #13
Source File: CDLTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * call toString with a null array
 */
@Test
public void emptyJSONArrayToString() {
    JSONArray jsonArray = new JSONArray();
    String str = CDL.toString(jsonArray);
    assertNull("CDL should return null for toString(null)",
            str);
}
 
Example #14
Source File: JSONUtil.java    From qaf with MIT License 5 votes vote down vote up
/**
 * 
 * @param csv
 * @return
 */
public static JSONArray getJsonArrayFromCsvOrNull(String csv){
	try {
		return CDL.rowToJSONArray(new JSONTokener(csv));
	} catch (JSONException e) {
		e.printStackTrace();
		return null;
	}
}
 
Example #15
Source File: CDLTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * Create a JSONArray with only 1 row
 */
@Test
public void onlyColumnNames() {
    String columnNameStr = "col1, col2, col3";
    JSONArray jsonArray = CDL.toJSONArray(columnNameStr);
    assertNull("CDL should return null when only 1 row is given",
            jsonArray);
}
 
Example #16
Source File: CDLTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * Create a JSONArray from an empty string
 */
@Test
public void emptyString() {
    String emptyStr = "";
    JSONArray jsonArray = CDL.toJSONArray(emptyStr);
    assertTrue("CDL should return null when the input string is empty",
            jsonArray == null);
}
 
Example #17
Source File: CDLTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * Assert that there is no error for a single escaped quote within a properly
 * embedded quote when not the last value.
 */
@Test
public void singleEscapedQuoteMiddleString(){
           String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\"\nVal 3,Val 4";
           JSONArray jsonArray = CDL.toJSONArray(singleEscape);
           
           String cdlStr = CDL.toString(jsonArray);
           assertTrue(cdlStr.contains("Col1"));
           assertTrue(cdlStr.contains("Col2"));
           assertTrue(cdlStr.contains("Val1"));
           assertTrue(cdlStr.contains("\"Val2"));
}
 
Example #18
Source File: CDLTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * Assert that there is no error for a single escaped quote within a properly embedded quote.
 */
@Test
public void singleEscapedQuote(){
           String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\"";
           JSONArray jsonArray = CDL.toJSONArray(singleEscape);
           
           String cdlStr = CDL.toString(jsonArray);
           assertTrue(cdlStr.contains("Col1"));
           assertTrue(cdlStr.contains("Col2"));
           assertTrue(cdlStr.contains("Val1"));
           assertTrue(cdlStr.contains("\"Val2"));
}
 
Example #19
Source File: CDLTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * Attempt to create a JSONArray with unbalanced quotes and a properly escaped doubled quote.
 * Expects a JSONException. 
 */
@Test
public void unbalancedEscapedQuote(){
	   String badLine = "Col1, Col2\n\"Val1, \"\"Val2\"\"";
       try {
           CDL.toJSONArray(badLine);
           fail("Expecting an exception");
       } catch (JSONException e) {
           assertEquals("Expecting an exception message",
                   "Missing close quote '\"'. at 26 [character 15 line 2]",
                   e.getMessage());
           
       }
}
 
Example #20
Source File: CDLTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to create a JSONArray from a string with null char
 * in column title line. Expects a JSONException.
 */
@Test
public void nullInName() {
    String badLine = "C\0ol1, Col2\nVal1, Val2";
    try {
        CDL.toJSONArray(badLine);
        fail("Expecting an exception");
    } catch (JSONException e) {
        assertEquals("Expecting an exception message",
                "Bad character 'o' (111). at 2 [character 3 line 1]",
                e.getMessage());
        
    }
}
 
Example #21
Source File: CDLTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to create a JSONArray from a string with unbalanced quotes
 * in value line. Expects a JSONException.
 */
@Test
public void unbalancedQuoteInValue() {
    String badLine = "Col1, Col2\n\"Val1, Val2";
    try {
        CDL.toJSONArray(badLine);
        fail("Expecting an exception");
    } catch (JSONException e) {
        assertEquals("Expecting an exception message",
                "Missing close quote '\"'. at 22 [character 11 line 2]",
                e.getMessage());
        
    }
}
 
Example #22
Source File: CDLTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to create a JSONArray from a string with unbalanced quotes
 * in column title line. Expects a JSONException.
 */
@Test
public void unbalancedQuoteInName() {
    String badLine = "Col1, \"Col2\nVal1, Val2";
    try {
        CDL.toJSONArray(badLine);
        fail("Expecting an exception");
    } catch (JSONException e) {
        assertEquals("Expecting an exception message",
                "Missing close quote '\"'. at 12 [character 0 line 2]",
                e.getMessage());
    }
}
 
Example #23
Source File: StringUtil.java    From qaf with MIT License 5 votes vote down vote up
/**
 * 
 * @param csv
 * @return
 */
private static Object[] getArrayFromCsv(String csv){
	JSONArray obj = CDL.rowToJSONArray(new JSONTokener(csv));

	List<Object> strings = obj.toList();
	Object[] array = new Object[strings.size()];
	for (int i=0; i<strings.size();i++){
		array[i]=toObject((String) strings.get(i));
	}
	return array;
}
 
Example #24
Source File: CDLTest.java    From JSON-Java-unit-test with Apache License 2.0 4 votes vote down vote up
/**
 * call toString with a null array
 */
@Test(expected=NullPointerException.class)
public void nullJSONArrayToString() {
    CDL.toString((JSONArray)null);
}
 
Example #25
Source File: CDLDemo.java    From tutorials with MIT License 4 votes vote down vote up
public static void jsonArrayFromCDT() {
    JSONArray ja = CDL.rowToJSONArray(new JSONTokener("England, USA, Canada"));
    System.out.println(ja);
}
 
Example #26
Source File: CDLDemo.java    From tutorials with MIT License 4 votes vote down vote up
public static void cDTfromJSONArray() {
    JSONArray ja = new JSONArray("[\"England\",\"USA\",\"Canada\"]");
    String cdt = CDL.rowToString(ja);
    System.out.println(cdt);
}
 
Example #27
Source File: CDLIntegrationTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenCommaDelimitedText_thenConvertToJSONArray() {
    JSONArray ja = CDL.rowToJSONArray(new JSONTokener("England, USA, Canada"));
    assertEquals("[\"England\",\"USA\",\"Canada\"]", ja.toString());
}
 
Example #28
Source File: CDLIntegrationTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenJSONArray_thenConvertToCommaDelimitedText() {
    JSONArray ja = new JSONArray("[\"England\",\"USA\",\"Canada\"]");
    String cdt = CDL.rowToString(ja);
    assertEquals("England,USA,Canada", cdt.toString().trim());
}
 
Example #29
Source File: CDLTest.java    From JSON-Java-unit-test with Apache License 2.0 4 votes vote down vote up
/**
 * Attempts to create a JSONArray from a null string.
 * Expect a NullPointerException.
 */
@Test(expected=NullPointerException.class)
public void exceptionOnNullString() {
    String nullStr = null;
    CDL.toJSONArray(nullStr);
}