[英]Serialize and de-serialize metadata-extractor-dotnet
我正在从上传的图像序列化元数据,以便能够将其持久保存在数据库中。
可以使用Newtonsoft(JSON.NET)的Custom JsonConverter序列化数据-但是反序列化失败:
(IReadOnlyList<MetadataExtractor.Directory>)JsonConvert.DeserializeObject(metadata)
例外情况:
An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code
Additional information: Could not create an instance of type MetadataExtractor.Directory. Type is an interface or abstract class and cannot be instantiated. Path '[0].Name', line 1, position 9.
由于目录列表可能会因特定对象的不同而有所差异,因此我怀疑分别对目录进行序列化/反序列化是微不足道的。
有什么简单的建议,例如如何仅将上载图像的元数据部分保存到可以稍后重用的表单中?
尽管目前正在讨论的Java版本上有一个活跃的问题 ,但Metadata Extractor不支持序列化。
那里的问题的一部分在这里也适用-一切都取决于您为什么要序列化数据。 如果您完全需要它,那么它比只想保存/恢复一些属性描述的工作量大得多。
您可以使用类似以下内容的XML形式编写描述:
var doc = new XDocument(
new XElement("Metadata",
directories.Select(directory =>
new XElement("Directory",
new XAttribute("Name", directory.Name),
directory.Tags.Select(tag =>
new XElement("Tag",
new XAttribute("Id", tag.Type.ToString("X"),
new XAttribute("Name", tag.Name),
tag.Description))))));
这将产生类似于以下内容的XML:
<Metadata>
<Directory Name="Exif IFD0">
<Tag Id="10F" Name="Make">NIKON</Tag>
<Tag Id="110" Name="Model">COOLPIX P340</Tag>
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.