我正在从上传的图片中序列化元数据,以便我能够将其保存在数据库中。
可以使用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.
由于目录列表可能因特定对象而异,我怀疑单独序列化/反序列化目录是微不足道的。
有关如何仅将上传图像的元数据部分保存为以后可以重复使用的表单的简单建议吗?
答案 0 :(得分:0)
Metadata Extractor不支持序列化,尽管Java版本上有active issue目前正在讨论此问题。
这里也存在部分问题 - 这完全取决于你想要序列化数据的原因。如果您需要完全保真,那么比您只想保存/恢复一些属性描述要多得多。
您可以使用以下内容将描述编写为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>
...