Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on May 17th, 2012  |  syntax: None  |  size: 10.53 KB  |  views: 460  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Jackson: deserialization of Map
  2. org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.HashMap out of START_ARRAY token
  3.        
  4. {
  5. "0f861a9a-0a3e-40a7-8ff3-0b83d8070876" : {
  6. "name" : "BAR.xml",
  7. "filePath" : "/FOO/repo/BAR.xml"
  8. },
  9. "f3cbb32e-b7b8-4af1-b48b-7ea393de7971" : {
  10. "name" : "BLAH.xml",
  11. "filePath" : "/FOO/repo/BLAH.xml"
  12. },
  13. "012009b6-26e9-4bc1-9050-2a4ac9546c7e" : {
  14. "name" : "Check System.xml",
  15. "filePath" : "/FOO/repo/Check System.xml"
  16. }
  17. }
  18.        
  19. //doesn't work    
  20. cache = (Map<String,UUIDInfo>) mapper.readValue(bytes.toString(), new TypeReference<HashMap<String,UUIDInfo>>(){});
  21.  
  22. //doesn't work.
  23. cache = (Map<String,UUIDInfo>) mapper.readValue(bytes.toString(), TypeFactory.mapType(HashMap.class, String.class, UUIDInfo.class));
  24.        
  25. org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token
  26.  at [Source: java.io.StringReader@32b9bd47; line: 1, column: 1]
  27.     at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
  28.     at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:198)
  29.     at org.codehaus.jackson.map.deser.MapDeserializer.deserialize(MapDeserializer.java:151)
  30.     at org.codehaus.jackson.map.deser.MapDeserializer.deserialize(MapDeserializer.java:25)
  31.     at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2395)
  32.     at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1602)
  33.     at com.hp.oo.studio.shared.UUIDRegistry.UUIDRegistry.<init>(UUIDRegistry.java:63)
  34.     at com.hp.oo.studio.shared.UUIDRegistry.UUIDRegistry.<clinit>(UUIDRegistry.java:37)
  35.     at com.hp.oo.studio.shared.StudioShared.loadUUIDRegistry(StudioShared.java:93)
  36.     at com.hp.oo.studio.shared.StudioShared.start(StudioShared.java:42)
  37.     at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711)
  38.     at java.security.AccessController.doPrivileged(Native Method)
  39.     at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702)
  40.     at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683)
  41.     at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381)
  42.     at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:299)
  43.     at org.eclipse.osgi.framework.util.SecureAction.start(SecureAction.java:440)
  44.     at org.eclipse.osgi.internal.loader.BundleLoader.setLazyTrigger(BundleLoader.java:268)
  45.     at org.eclipse.core.runtime.internal.adaptor.EclipseLazyStarter.postFindLocalClass(EclipseLazyStarter.java:107)
  46.     at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findLocalClass(ClasspathManager.java:462)
  47.     at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.findLocalClass(DefaultClassLoader.java:216)
  48.     at org.eclipse.osgi.internal.loader.BundleLoader.findLocalClass(BundleLoader.java:400)
  49.     at org.eclipse.osgi.internal.loader.SingleSourcePackage.loadClass(SingleSourcePackage.java:35)
  50.     at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:473)
  51.     at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:429)
  52.     at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:417)
  53.     at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
  54.     at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
  55.     at com.hp.oo.studio.StudioUI.registerFile(StudioUI.java:133)
  56.     at com.hp.oo.studio.StudioUI.findFilesInDirectory(StudioUI.java:125)
  57.     at com.hp.oo.studio.StudioUI.findFilesInDirectory(StudioUI.java:120)
  58.     at com.hp.oo.studio.StudioUI.start(StudioUI.java:75)
  59.     at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711)
  60.     at java.security.AccessController.doPrivileged(Native Method)
  61.     at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702)
  62.     at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683)
  63.     at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381)
  64.     at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:299)
  65.     at org.eclipse.osgi.framework.util.SecureAction.start(SecureAction.java:440)
  66.     at org.eclipse.osgi.internal.loader.BundleLoader.setLazyTrigger(BundleLoader.java:268)
  67.     at org.eclipse.core.runtime.internal.adaptor.EclipseLazyStarter.postFindLocalClass(EclipseLazyStarter.java:107)
  68.     at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findLocalClass(ClasspathManager.java:462)
  69.     at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.findLocalClass(DefaultClassLoader.java:216)
  70.     at org.eclipse.osgi.internal.loader.BundleLoader.findLocalClass(BundleLoader.java:400)
  71.     at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:476)
  72.     at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:429)
  73.     at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:417)
  74.     at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
  75.     at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
  76.     at org.eclipse.osgi.internal.loader.BundleLoader.loadClass(BundleLoader.java:345)
  77.     at org.eclipse.osgi.framework.internal.core.BundleHost.loadClass(BundleHost.java:229)
  78.     at org.eclipse.osgi.framework.internal.core.AbstractBundle.loadClass(AbstractBundle.java:1207)
  79.     at org.eclipse.core.internal.registry.osgi.RegistryStrategyOSGI.createExecutableExtension(RegistryStrategyOSGI.java:174)
  80.     at org.eclipse.core.internal.registry.ExtensionRegistry.createExecutableExtension(ExtensionRegistry.java:905)
  81.     at org.eclipse.core.internal.registry.ConfigurationElement.createExecutableExtension(ConfigurationElement.java:243)
  82.     at org.eclipse.core.internal.registry.ConfigurationElementHandle.createExecutableExtension(ConfigurationElementHandle.java:55)
  83.     at org.eclipse.ui.internal.WorkbenchPlugin$1.run(WorkbenchPlugin.java:268)
  84.     at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
  85.     at org.eclipse.ui.internal.WorkbenchPlugin.createExtension(WorkbenchPlugin.java:264)
  86.     at org.eclipse.ui.internal.registry.EditorDescriptor.createEditor(EditorDescriptor.java:235)
  87.     at org.eclipse.ui.internal.EditorManager.createPart(EditorManager.java:875)
  88.     at org.eclipse.ui.internal.EditorReference.createPartHelper(EditorReference.java:609)
  89.     at org.eclipse.ui.internal.EditorReference.createPart(EditorReference.java:465)
  90.     at org.eclipse.ui.internal.WorkbenchPartReference.getPart(WorkbenchPartReference.java:595)
  91.     at org.eclipse.ui.internal.EditorAreaHelper.setVisibleEditor(EditorAreaHelper.java:271)
  92.     at org.eclipse.ui.internal.EditorManager.setVisibleEditor(EditorManager.java:1459)
  93.     at org.eclipse.ui.internal.EditorManager$5.runWithException(EditorManager.java:972)
  94.     at org.eclipse.ui.internal.StartupThreading$StartupRunnable.run(StartupThreading.java:31)
  95.     at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
  96.     at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
  97.     at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:3563)
  98.     at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3212)
  99.     at org.eclipse.ui.application.WorkbenchAdvisor.openWindows(WorkbenchAdvisor.java:803)
  100.     at org.eclipse.ui.internal.Workbench$33.runWithException(Workbench.java:1595)
  101.     at org.eclipse.ui.internal.StartupThreading$StartupRunnable.run(StartupThreading.java:31)
  102.     at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
  103.     at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
  104.         at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:3563)
  105.     at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3212)
  106.     at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
  107.     at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494)
  108.     at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674)
  109.     at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
  110.     at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667)
  111.     at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
  112.     at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:123)
  113.     at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
  114.     at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
  115.     at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
  116.     at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
  117.     at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
  118.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  119.     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  120.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  121.     at java.lang.reflect.Method.invoke(Method.java:601)
  122.     at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
  123.     at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
  124.     at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
  125.     at org.eclipse.equinox.launcher.Main.main(Main.java:1386)
  126.        
  127. import java.io.File;
  128. import java.util.HashMap;
  129. import java.util.Map;
  130.  
  131. import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
  132. import org.codehaus.jackson.annotate.JsonMethod;
  133. import org.codehaus.jackson.map.ObjectMapper;
  134. import org.codehaus.jackson.type.TypeReference;
  135.  
  136. public class JacksonFoo
  137. {
  138.   public static void main(String[] args) throws Exception
  139.   {
  140.     ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
  141.     Map<String, UUIDInfo> cache =
  142.       mapper.readValue(new File("input.json"), new TypeReference<HashMap<String, UUIDInfo>>() {});
  143.     System.out.println(cache);
  144.     // output:
  145.     // {0f861a9a-0a3e-40a7-8ff3-0b83d8070876=UUIDInfo: name=BAR.xml, filePath=/FOO/repo/BAR.xml,
  146.     //  f3cbb32e-b7b8-4af1-b48b-7ea393de7971=UUIDInfo: name=BLAH.xml, filePath=/FOO/repo/BLAH.xml,
  147.     //  012009b6-26e9-4bc1-9050-2a4ac9546c7e=UUIDInfo: name=Check System.xml, filePath=/FOO/repo/Check System.xml}
  148.   }
  149. }
  150.  
  151. class UUIDInfo
  152. {
  153.   String name;
  154.   String filePath;
  155.  
  156.   @Override
  157.   public String toString()
  158.   {
  159.     return String.format("UUIDInfo: name=%s, filePath=%s", name, filePath);
  160.   }
  161. }