简体   繁体   English

当序列化已被注释更改时,如何从 Java 类中提取 Jackson 属性名称?

[英]How do I extract the Jackson property names from a java class when the serialization has been altered by annotation?

I have a somewhat odd problem.我有一个有点奇怪的问题。 I have a system of data objects fo significant size, with a wide variety of properties.我有一个很大的数据对象系统,具有各种各样的属性。 They're being serialized and deserialized with JSON, and in many cases, the field names have been altered by annotation.它们正在使用 JSON 进行序列化和反序列化,并且在许多情况下,字段名称已被注释更改。 I have users who interact with that JSON and could not care less what the underlying code does, as long as they know what to put int eh JSON to make it work right.我有与该 JSON 交互的用户,并且他们并不关心底层代码的作用,只要他们知道如何放入 int eh JSON 以使其正常工作。 I'd like to provide them with an endpoint they can go to, where they can identify a class, and have it spit back the field name (as it would appear in the serialized JSON).我想为他们提供一个可以访问的端点,在那里他们可以识别一个类,并让它返回字段名称(就像它出现在序列化的 JSON 中一样)。 I have the class object and have some ability in working with reflection.我有类对象并且有一些处理反射的能力。

I have considered a few options.我已经考虑了几个选项。 My initial plan was that I was just going to handle everything with reflection, but I realized that that would require me to chase around all of the Jackson-affecting annotations myself to try to get the logic right.我最初的计划是我只是用反射来处理所有事情,但我意识到这需要我自己追逐所有影响杰克逊的注释,以试图使逻辑正确。 That sounds like a huge amount of unnecessary effort that would almost certainly generate a few ugly and well-hidden bugs as I wound up getting the logic not-quite-right.这听起来像是大量不必要的努力,几乎肯定会产生一些丑陋且隐藏得很好的错误,因为我最终得到的逻辑不太正确。 Jackson already has this logic, after all.毕竟杰克逊已经有了这个逻辑。 It seems like I should be able to harness it in some way.似乎我应该能够以某种方式利用它。

I have considered making a dummy version of the class, serializing it, and reading the field names off of the JSON that results, but there are a large number of classes here, many of them are complicated, and many of them have properties that point at one another.我曾考虑制作该类的虚拟版本,对其进行序列化,然后从生成的 JSON 中读取字段名称,但是这里有大量的类,其中很多都很复杂,而且其中很多都有指向彼此。 Under those conditions, making the sort of field auto-populate that would make that work right... well, that also sounds like a great deal of hopefully-unnecessary, bug-generating work.在这些条件下,使那种字段自动填充可以使其正常工作......好吧,这听起来也像是大量不必要的、会产生错误的工作。

There is the logic in Jackson somewhere that knows how to identify these field names (specifically the serialized field names for the fields that actually are being serialized). Jackson 的某个地方有一个逻辑,它知道如何识别这些字段名称(特别是实际正在序列化的字段的序列化字段名称)。 It seems like it should be possible to determine just with the ObjectMapper and the class that I want the information on.似乎应该可以仅使用 ObjectMapper 和我想要信息的类来确定。 Is it possible?是否可以? How should I do it?我该怎么做? I haven't been able to find instructions online (fuzzed-out by all the articles on how to change the name in the first place) and just reading the class files in Jackson isn't doing the trick either.我一直无法在网上找到说明(首先被所有关于如何更改名称的文章模糊了),并且仅阅读 Jackson 中的类文件也无法解决问题。 (The comments are relatively terse, and what I'm looking for is very specific. Even if I did find it, I don't know how I would make sure that it was actually giving me the thing I need, rather than some other, very similar thing.) (评论相对简洁,我正在寻找的内容非常具体。即使我找到了,我也不知道如何确保它确实给了我我需要的东西,而不是其他一些东西,非常相似的事情。)

As a bonus, if there were some way to know which java class the field had as its value, that would be even better, but I have at least a few workarounds for that that I think I might be able to make workable.作为奖励,如果有某种方法可以知道该字段具有哪个 java 类作为其值,那就更好了,但是我至少有一些解决方法,我认为我可以使其可行。 Getting a precise field names is more important.获得精确的字段名称更为重要。

Well, I managed to find my answer.好吧,我设法找到了答案。

// We're using the jacksObjectMapper to grab the serializer, so that
// we can get the list of logical properties from said serializer.
// The point of this process is that we want to give the user the
// data that corresponds with what said user is looking at.  That
// means we specifically want the fields that jackson will serialize
// and deserialize, with the names that jackson will use.  This is
// the way to get that.  It was not as easy to find as you might think.

SerializerProvider serProv = jacksonObjectMapper.getSerializerProviderInstance();
JsonSerializer serializer = serProv.findValueSerializer(inputClass);

Iterator<PropertyWriter> logicalProperties = serializer.properties();

// Once we have the logical properties, we grab the field names and 
// value classes from that, and use that to populate everything else.
while (logicalProperties.hasNext()) {

    PropertyWriter propw = logicalProperties.next();
    String jsonFieldName = propw.getName();
    ...
}

Now past that point, it was still somewhat interesting to line it up with the java reflection fields that the properties were spawned from, so that I could actually get those value classes, but that was a different issue, and one with a solution far more dependent on the individual codebase.现在过了那个点,将它与产生属性的 java 反射字段对齐仍然有点有趣,这样我就可以实际获得这些值类,但这是一个不同的问题,一个有更多解决方案的问题取决于个人代码库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jackson 序列化如何不显示类和字段名称 - Jackson serialization how to not show class and fields names 如何从java中从C#REST服务检索的字符串中提取值 - How do I extract a value from a string in java that has been retrieved from a C# REST service 类包含泛型时的Jackson序列化错误 - Jackson serialization error when class has generics in it 如何在Java的LocalDateTime和LocalDate上配置Jackson序列化? - How do I configure Jackson Serialization on LocalDateTime and LocalDate for Java? Java 注释处理器:如何从注释中提取 class 值 - Java Annotation Processor: how to extract class values from an annotation 杰克逊(Jackson)-当某些属性符合特定条件时,注释要从序列化中排除的类 - Jackson - annotate the class to be excluded from serialization when some property meets specific criteria 使用Java Jackson的不同序列化/反序列化名称 - Different serialization/deserialization names with java jackson 如何检查已调用哪个 java 实例方法以对属性应用适当的折扣? - How do I check which java instance method has been called to apply appropriate discount to the property? 如何从现有的Java项目中提取主类? - How do I extract a main class from an existing Java project? 杰克逊序列化,是否有注释指示用于属性的子属性? - jackson serialization, is there an annotation that indicates what sub-properties to use for a property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM