@Override public final <T> T parse( Class<T> type, byte[] value, Charset charset ) { if ( type==Object.class || type == Map.class || type == List.class || Typ.isBasicType ( type ) ) { return this.basicParser.parse( type, value, charset ); } else { Object object = objectParser.parse( Map.class, value ); return finalExtract( type, object ); } }
@Override public final <T> T parse( Class<T> type, CharSequence value ) { if ( type==Object.class || type == Map.class || type == List.class || Typ.isBasicType ( type ) ) { return basicParser.parse( type, value ); } else { Object object = objectParser.parse( Map.class, value ); return finalExtract( type, object ); } }
@Override public final <T> T parse( Class<T> type, char[] value ) { if ( type==Object.class || type == Map.class || type == List.class || Typ.isBasicType ( type ) ) { return basicParser.parse( type, value ); } else { Object object = objectParser.parse( Map.class, value ); return finalExtract( type, object ); } }
@Override public final <T> T parseAsStream( Class<T> type, byte[] value ) { if (copyBuf==null) { copyBuf = new char[bufSize]; } charBuf = IO.read( new InputStreamReader ( new InMemoryInputStream(value), charset ), charBuf, value.length, copyBuf ); return this.basicParser.parse ( type, charBuf.readForRecycle () ); }
@Test () public void testBooleanParseError () { Object obj = jsonParserAndMapper.parse ( Map.class, " tbone " ); }
public void helper ( String name, String json, Object compareTo ) { System.out.printf ( "%s, %s, %s", name, json, compareTo ); Object obj = jsonParserAndMapper.parse ( json.replace ( '\'', '"' ) ); boolean ok = true; System.out.printf ( "\nNAME=%s \n \t parsed obj=%s\n \t json=%s\n \t compareTo=%s\n", name, obj, json, compareTo ); ok &= compareTo.equals ( obj ) || die ( name + " :: List has items " + json ); }
@Test public void simpleStringReader () { String str = (String) jsonParserAndMapper.parse ( new StringReader ("\"file\"") ); boolean ok = str.equals ( "file" ) || die ( str ); }
@Test public void oddlySpaced2 () { Object obj = jsonParserAndMapper.parse ( lines ( "[ 2 , 1, 0]" ).replace ( '\'', '"' ) ); boolean ok = true; System.out.println ( obj ); }
@Test public void testWithSpaces() { int num = ((Number)jsonParserAndMapper.parse( " 123")).intValue(); int num2 = 123; boolean ok = num == num2 || die ( "" + num); }
@Test public void complianceForUpperCaseNumber () { Map<String, Object> map = ( Map<String, Object> ) jsonParserAndMapper.parse ( lines ( "{ \"v\":\"\\u00C1\\u00E1\"}" ) ); //puts ( map ); boolean ok = map.get ( "v" ).equals ( "Áá" ) || die ( "map " + map.get ( "v" ) ); }
@Test () public void garbageAtEndOfString () { Map<String, Object> map = ( Map<String, Object> ) jsonParserAndMapper.parse ( lines ( "{ \"a\":\"foo.bar\"}#toto" ) ); //puts ( map ); }
@Test public void testArrayOfArrayWithSimpleValuesValue1() { try { List list = (List) jsonParserAndMapper.parse("[,]"); die(); } catch (JsonException jsonException) { } }
@Test public void classic() { Map<String, Object> map = ( Map<String, Object> ) jsonParserAndMapper.parse ( Map.class, lines ( "{ \"nums\": [12, 12345678, 999.999, 123456789.99],\n " + " \"nums2\": [12, 12345678, 999.999, 123456789.99],\n" + " \"nums3\": [12, 12345678, 999.999, 123456789.99]\n" + "}" ) ); }
@Test public void testParserSimpleMapWithBoolean () { Object obj = jsonParserAndMapper.parse ( " { 'foo': true } ".replace ( '\'', '"' ) ); boolean ok = true; ok &= obj instanceof Map || die ( "Object was not a map" ); Map<String, Object> map = ( Map<String, Object> ) obj; System.out.println ( obj ); ok &= idx ( map, "foo" ).equals ( true ) || die ( "I did not find true" ); }
@Test public void testArrayOfArrayWithSimpleValuesValue2() { try { List list = (List) jsonParserAndMapper.parse("["); die(); } catch (JsonException jsonException) { jsonException.printStackTrace(); } }
@Test public void testArrayOfArrayWithSimpleValuesValue5() { try { List list = (List) jsonParserAndMapper.parse("[1"); die(); } catch (JsonException jsonException) { jsonException.printStackTrace (); } }
@Test public void testNullEmptyMalformedPayloads() { List<String> list = Lists.list ( "[", "[a", "{\"key\"", "{\"key\":1", "[\"a\"", "[\"a\", ", "[\"a\", true" ); for (String str : list) { try { parser().parse( str ); die(str); } catch ( Exception ex ) { ex.printStackTrace(); } } }
@Test public void testComment2 () { String testString = " {foo:bar, #hi mom \n" + " foo2:baz } "; Map<String, Object> map = jsonParserAndMapper.parse ( Map.class, testString ); //uts ( "map = " + map ); inspectMap ( map ); boolean ok = idx ( map, "foo" ).equals ( "bar" ) || die ( "I did not find:" + idx ( map, "foo" ) + "#" ); ok = idx ( map, "foo2" ).equals ( "baz" ) || die ( "I did not find:" + idx ( map, "foo2" ) + "#" ); }
@Test public void testWithJSONStringArray() { String[] cats = new String[10]; cats[0] = "Felix"; cats[5] = "Tom"; String sRick = new JsonSimpleSerializerImpl().serialize(cats).toString(); String[] gatos = new String[10]; new JsonParserFactory().create().parse(List.class, sRick).toArray(gatos); Assert.assertArrayEquals(cats, gatos); }