杰克逊:地图的反序列化
Posted
技术标签:
【中文标题】杰克逊:地图的反序列化【英文标题】:Jackson: deserialization of Map 【发布时间】:2012-01-11 20:54:24 【问题描述】:我正在使用 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"
我尝试了两种不同的方法来完成这项工作,但都失败了......
//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)
【问题讨论】:
请完整的异常堆栈跟踪 为您添加了堆栈跟踪。 【参考方案1】:将您的地图包装到另一个对象中,然后编组。 例如:
class MyObject
private Map<String,UUIDInfo> map;
只需这样做:mappper.readValue(myInstance, MyObject.class)
【讨论】:
我试过了,但效果不佳.... "cache" : "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" org .codehaus.jackson.map.JsonMappingException:无法从 START_ARRAY 令牌中反序列化 com.hp.oo.studio.shared.UUIDRegistry.UUIDMap 的实例 解决方案错误,因为 Map 的声明应该是 Map我认为问题是Jackson polymorphic deserialization Wiki 页面上 5.5 下列出的问题。 基本上是Java类型擦除在序列化时咬你;当反序列化类型明确可用时;这种差异很痛苦。
【讨论】:
我已经将@JsonTypeInfo 添加到我的pojos 中,并且那些被写入json,但它仍然不起作用。 :( org.codehaus.jackson.map.JsonMappingException: Unexpected token (START_ARRAY), expected START_OBJECT: need JSON Object to contain As.PROPERTY type information (for class com.hp.oo.studio.shared.UUIDRegistry.UUIDMap)跨度> 这里的奥秘在于,目前的 JSON 在任何地方都没有 START_ARRAY 标记 ("["),所以我很难相信 JSON 被用于反序列化。顺便说一句,这是杰克逊的哪个版本?【参考方案3】:原始问题中的 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);
【讨论】:
【参考方案4】:这样做:
ArrayList<LinkedHashMap<?, ?>> companymap = mapper.readValue(jsonCompany, ArrayList.class);
【讨论】:
我也建议避免放置实现类: List A.马森:如果我避免这种情况,那么问题将无法解决。没有其他答案适用于此..我已经对其进行了测试并且工作正常。【参考方案5】: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>();
我正在使用 Jackson 2.4.2。
希望对你有帮助。
【讨论】:
以上是关于杰克逊:地图的反序列化的主要内容,如果未能解决你的问题,请参考以下文章