Джексон: десериализация карты - PullRequest
9 голосов
/ 03 декабря 2011

Я использую Джексона для десериализации файла JSON (который я сериализовал с Джексоном). JSON - это простой HashMap, но картограф жалуется, когда я пытаюсь десериализовать.

org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.HashMap out of START_ARRAY token

Моя сериализованная строка HashMap .....

{
"0f861a9a-0a3e-40a7-8ff3-0b83d8070876" : {
"name" : "BAR.xml",
"filePath" : "/FOO/repo/BAR.xml"
},
"f3cbb32e-b7b8-4af1-b48b-7ea393de7971" : {
"name" : "BLAH.xml",
"filePath" : "/FOO/repo/BLAH.xml"
},
"012009b6-26e9-4bc1-9050-2a4ac9546c7e" : {
"name" : "Check System.xml",
"filePath" : "/FOO/repo/Check System.xml"
}
}

Я пробовал 2 разных способа заставить это работать, и оба не удаются ...

//doesn't work    
cache = (Map<String,UUIDInfo>) mapper.readValue(bytes.toString(), new TypeReference<HashMap<String,UUIDInfo>>(){});

//doesn't work.
cache = (Map<String,UUIDInfo>) mapper.readValue(bytes.toString(), TypeFactory.mapType(HashMap.class, String.class, UUIDInfo.class));

Полная трассировка стека

org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token
 at [Source: java.io.StringReader@32b9bd47; line: 1, column: 1]
    at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
    at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:198)
    at org.codehaus.jackson.map.deser.MapDeserializer.deserialize(MapDeserializer.java:151)
    at org.codehaus.jackson.map.deser.MapDeserializer.deserialize(MapDeserializer.java:25)
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2395)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1602)
    at com.hp.oo.studio.shared.UUIDRegistry.UUIDRegistry.<init>(UUIDRegistry.java:63)
    at com.hp.oo.studio.shared.UUIDRegistry.UUIDRegistry.<clinit>(UUIDRegistry.java:37)
    at com.hp.oo.studio.shared.StudioShared.loadUUIDRegistry(StudioShared.java:93)
    at com.hp.oo.studio.shared.StudioShared.start(StudioShared.java:42)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683)
    at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381)
    at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:299)
    at org.eclipse.osgi.framework.util.SecureAction.start(SecureAction.java:440)
    at org.eclipse.osgi.internal.loader.BundleLoader.setLazyTrigger(BundleLoader.java:268)
    at org.eclipse.core.runtime.internal.adaptor.EclipseLazyStarter.postFindLocalClass(EclipseLazyStarter.java:107)
    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findLocalClass(ClasspathManager.java:462)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.findLocalClass(DefaultClassLoader.java:216)
    at org.eclipse.osgi.internal.loader.BundleLoader.findLocalClass(BundleLoader.java:400)
    at org.eclipse.osgi.internal.loader.SingleSourcePackage.loadClass(SingleSourcePackage.java:35)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:473)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:429)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:417)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at com.hp.oo.studio.StudioUI.registerFile(StudioUI.java:133)
    at com.hp.oo.studio.StudioUI.findFilesInDirectory(StudioUI.java:125)
    at com.hp.oo.studio.StudioUI.findFilesInDirectory(StudioUI.java:120)
    at com.hp.oo.studio.StudioUI.start(StudioUI.java:75)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683)
    at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381)
    at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:299)
    at org.eclipse.osgi.framework.util.SecureAction.start(SecureAction.java:440)
    at org.eclipse.osgi.internal.loader.BundleLoader.setLazyTrigger(BundleLoader.java:268)
    at org.eclipse.core.runtime.internal.adaptor.EclipseLazyStarter.postFindLocalClass(EclipseLazyStarter.java:107)
    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findLocalClass(ClasspathManager.java:462)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.findLocalClass(DefaultClassLoader.java:216)
    at org.eclipse.osgi.internal.loader.BundleLoader.findLocalClass(BundleLoader.java:400)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:476)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:429)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:417)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at org.eclipse.osgi.internal.loader.BundleLoader.loadClass(BundleLoader.java:345)
    at org.eclipse.osgi.framework.internal.core.BundleHost.loadClass(BundleHost.java:229)
    at org.eclipse.osgi.framework.internal.core.AbstractBundle.loadClass(AbstractBundle.java:1207)
    at org.eclipse.core.internal.registry.osgi.RegistryStrategyOSGI.createExecutableExtension(RegistryStrategyOSGI.java:174)
    at org.eclipse.core.internal.registry.ExtensionRegistry.createExecutableExtension(ExtensionRegistry.java:905)
    at org.eclipse.core.internal.registry.ConfigurationElement.createExecutableExtension(ConfigurationElement.java:243)
    at org.eclipse.core.internal.registry.ConfigurationElementHandle.createExecutableExtension(ConfigurationElementHandle.java:55)
    at org.eclipse.ui.internal.WorkbenchPlugin$1.run(WorkbenchPlugin.java:268)
    at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
    at org.eclipse.ui.internal.WorkbenchPlugin.createExtension(WorkbenchPlugin.java:264)
    at org.eclipse.ui.internal.registry.EditorDescriptor.createEditor(EditorDescriptor.java:235)
    at org.eclipse.ui.internal.EditorManager.createPart(EditorManager.java:875)
    at org.eclipse.ui.internal.EditorReference.createPartHelper(EditorReference.java:609)
    at org.eclipse.ui.internal.EditorReference.createPart(EditorReference.java:465)
    at org.eclipse.ui.internal.WorkbenchPartReference.getPart(WorkbenchPartReference.java:595)
    at org.eclipse.ui.internal.EditorAreaHelper.setVisibleEditor(EditorAreaHelper.java:271)
    at org.eclipse.ui.internal.EditorManager.setVisibleEditor(EditorManager.java:1459)
    at org.eclipse.ui.internal.EditorManager$5.runWithException(EditorManager.java:972)
    at org.eclipse.ui.internal.StartupThreading$StartupRunnable.run(StartupThreading.java:31)
    at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
    at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
    at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:3563)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3212)
    at org.eclipse.ui.application.WorkbenchAdvisor.openWindows(WorkbenchAdvisor.java:803)
    at org.eclipse.ui.internal.Workbench$33.runWithException(Workbench.java:1595)
    at org.eclipse.ui.internal.StartupThreading$StartupRunnable.run(StartupThreading.java:31)
    at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
    at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
        at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:3563)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3212)
    at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
    at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494)
    at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:123)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1386)

Ответы [ 5 ]

7 голосов
/ 05 сентября 2014
public class ArrayMapDeserializer extends JsonDeserializer<Map<String, UUIDInfo>> {

    @Override
    public Map<String, UUIDInfo> deserialize(JsonParser jp, DeserializationContext context)
            throws IOException {
        ObjectMapper mapper = (ObjectMapper) jp.getCodec();
        if (jp.getCurrentToken().equals(JsonToken.START_OBJECT)) {
            return mapper.readValue(jp, new TypeReference<HashMap<String, UUIDInfo>>() {
            });
        } else {
            //consume this stream
            mapper.readTree(jp);
            return new HashMap<String, UUIDInfo>();
        }
    }
}

в классе объявить

@JsonDeserialize(using = ArrayMapDeserializer.class)
private HashMap<String, UUIDInfo> aliases = new HashMap<String, UUIDInfo>();

Я использую Джексон 2.4.2.

Надеюсь, это поможет вам.

2 голосов
/ 29 марта 2013

Сделай так:

ArrayList<LinkedHashMap<?, ?>> companymap = mapper.readValue(jsonCompany, ArrayList.class);
1 голос
/ 03 декабря 2011

Я думаю, что проблема указана в разделе 5.5 на полиморфной десериализации Джексона на вики-странице. По сути, это Java Type Erasure кусает вас при сериализации; и когда десериализация типа становится доступной явно; и это несоответствие вредит.

1 голос
/ 06 декабря 2011

JSON в исходном вопросе отлично десериализуется для меня, используя Jackson 1.9.2.

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.annotate.JsonMethod;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class JacksonFoo
{
  public static void main(String[] args) throws Exception
  {
    ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
    Map<String, UUIDInfo> cache = 
      mapper.readValue(new File("input.json"), new TypeReference<HashMap<String, UUIDInfo>>() {});
    System.out.println(cache);
    // output:
    // {0f861a9a-0a3e-40a7-8ff3-0b83d8070876=UUIDInfo: name=BAR.xml, filePath=/FOO/repo/BAR.xml, 
    //  f3cbb32e-b7b8-4af1-b48b-7ea393de7971=UUIDInfo: name=BLAH.xml, filePath=/FOO/repo/BLAH.xml, 
    //  012009b6-26e9-4bc1-9050-2a4ac9546c7e=UUIDInfo: name=Check System.xml, filePath=/FOO/repo/Check System.xml}
  }
}

class UUIDInfo
{
  String name;
  String filePath;

  @Override
  public String toString()
  {
    return String.format("UUIDInfo: name=%s, filePath=%s", name, filePath);
  }
}
0 голосов
/ 03 декабря 2011

Оберните вашу карту в другой объект, а затем маршал.например:

class MyObject{

   private Map<String,UUIDInfo> map;

}

И просто сделайте: mappper.readValue(myInstance, MyObject.class)

...