| package org.codehaus.jackson.map; |
| |
| import java.io.*; |
| import java.util.*; |
| |
| import org.codehaus.jackson.*; |
| |
| /** |
| * Unit tests for verifying "old" data binding from Json to JDK objects; |
| * one that only uses core JDK types; wrappers, Maps and Lists. |
| */ |
| public class TestToJavaType |
| extends main.BaseTest |
| { |
| @SuppressWarnings("unchecked") |
| public void testSampleDoc() |
| throws Exception |
| { |
| final String JSON = SAMPLE_DOC_JSON_SPEC; |
| |
| JsonFactory jf = new JsonFactory(); |
| |
| /* To get "untyped" Mapping (to Maps, Lists, instead of beans etc), |
| * we'll specify plain old Object.class as the target. |
| */ |
| Object root = new ObjectMapper().readValue(jf.createJsonParser(new StringReader(JSON)), Object.class); |
| |
| assertType(root, Map.class); |
| Map<?,?> rootMap = (Map<?,?>) root; |
| assertEquals(1, rootMap.size()); |
| Map.Entry<?,?> rootEntry = rootMap.entrySet().iterator().next(); |
| assertEquals("Image", rootEntry.getKey()); |
| Object image = rootEntry.getValue(); |
| assertType(image, Map.class); |
| Map<?,?> imageMap = (Map<?,?>) image; |
| assertEquals(5, imageMap.size()); |
| |
| Object value = imageMap.get("Width"); |
| assertType(value, Integer.class); |
| assertEquals(Integer.valueOf(SAMPLE_SPEC_VALUE_WIDTH), value); |
| |
| value = imageMap.get("Height"); |
| assertType(value, Integer.class); |
| assertEquals(Integer.valueOf(SAMPLE_SPEC_VALUE_HEIGHT), value); |
| |
| assertEquals(SAMPLE_SPEC_VALUE_TITLE, imageMap.get("Title")); |
| |
| // Another Object, "thumbnail" |
| value = imageMap.get("Thumbnail"); |
| assertType(value, Map.class); |
| Map<?,?> tnMap = (Map<?,?>) value; |
| assertEquals(3, tnMap.size()); |
| |
| assertEquals(Integer.valueOf(SAMPLE_SPEC_VALUE_TN_HEIGHT), tnMap.get("Height")); |
| // for some reason, width is textual, not numeric... |
| assertEquals(SAMPLE_SPEC_VALUE_TN_WIDTH, tnMap.get("Width")); |
| assertEquals(SAMPLE_SPEC_VALUE_TN_URL, tnMap.get("Url")); |
| |
| // And then number list, "IDs" |
| value = imageMap.get("IDs"); |
| assertType(value, List.class); |
| List<Object> ids = (List<Object>) value; |
| assertEquals(4, ids.size()); |
| assertEquals(Integer.valueOf(SAMPLE_SPEC_VALUE_TN_ID1), ids.get(0)); |
| assertEquals(Integer.valueOf(SAMPLE_SPEC_VALUE_TN_ID2), ids.get(1)); |
| assertEquals(Integer.valueOf(SAMPLE_SPEC_VALUE_TN_ID3), ids.get(2)); |
| assertEquals(Integer.valueOf(SAMPLE_SPEC_VALUE_TN_ID4), ids.get(3)); |
| |
| // and that's all folks! |
| } |
| } |