web-dev-qa-db-ja.com

ジャクソン:マップの逆シリアル化

私はJacksonを使用してjsonファイル(jacksonでシリアル化したもの)を逆シリアル化します。 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)
9
user1078261
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を使用しています。

それがあなたを助けることができることを願っています。

7
Jason

このようにしてください:

ArrayList<LinkedHashMap<?, ?>> companymap = mapper.readValue(jsonCompany, ArrayList.class);
3
Avinash

元の質問のJSONは、Jackson1.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);
  }
}
1

問題は、 Jackson polymorphic deserialization Wikiページの5.5にリストされている問題だと思います。基本的に、それはJava型消去がシリアル化するとき、および逆シリアル化タイプが明示的に利用可能になるとき、そしてその不一致が痛いです。

1
StaxMan

マップを別のオブジェクトにラップしてから、マーシャリングします。例えば:

_class MyObject{

   private Map<String,UUIDInfo> map;

}
_

そして、単に行う:mappper.readValue(myInstance, MyObject.class)

0
Kfir Bloch