ResourceBundle的序列化[解决方法] [英] Serialization [Workaround] for ResourceBundle

查看:334
本文介绍了ResourceBundle的序列化[解决方法]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的问题如下:
对于我需要编写的应用程序,我必须实现将一些DTO存储到磁盘以便以后重用的能力(采用JSON格式)。只是为了给你一个广泛的参考框架:DTO包含过程/数据模型以及它们的图形表示。

So my problem is the following: For an application that I need to write I have to implement the ability to store some DTOs to disk to be reused later on (in JSON format). Just to give you a broad frame of reference: The DTOs contain process/data models and also their graphical representation.

要获得所需的JSON文件,我目前使用Jackson。这对于最大的部分来说效果很好,但是,在一个需要保存的对象中我使用ResourceBundle(用于本地化不同语言的程序)。这正是问题出现的地方,因为杰克逊似乎无法序列化ResourceBundle对象(知道两者都是尝试它,而且我到目前为止所做的研究基本上都告诉了我同样的事情)。

To obtain the desired JSON files I currently use Jackson. This works out fine for the largest part, however, in one object that needs to be saved I use a ResourceBundle (to localize the program for different languages). And this is exactly where the problem comes in, as Jackson seems to be unable to serialize ResourceBundle objects (know that both from trying it, but also the research I have done so far basically told me the same).

所以我想问你是否知道如何使它工作,或者你是否找到了一些奇特的解决方法。

So I would like to ask you whether you might have an idea how to make it work, or whether you might have found some fancy workaround.

为了进一步说明,我将附加一些不是来自相关项目的示例代码,因为我是为其他人做的,我不确定他是否会欣赏他的代码的发布。

For further illustration I will append some sample code which is not from the project in question, since I do this for someone else and I am not sure whether he would appreciate the release of his code.

public class SomeClass {
  private String name;
  private ResourceBundle bundle;

  public SomeClass(String name, ResourceBundle bundle) {
    this.name = name;
    this.bundle = bundle;
  }

  public String getName() {
    return this.name;
  }

  public ResourceBundle getBundle() {
    return this.bundle;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void setBundle(ResourceBundle bundle) {
    this.bundle = bundle;
  }

  /*
    Here one could imagine some additional functionality making use of the 
    given ResourceBundle (something that has to be printed depending on the
    used language etc.). 
  */
}



import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.ResourceBundle;

public class Saver {

  public static void main(String[] args) {
    Saver saver = new Saver();
    saver.run();
  }

  public void run() {
    ObjectMapper om = new ObjectMapper();
    ResourceBundle rb = ResourceBundle.getBundle("test");
    SomeClass sc = new SomeClass("SomeClass", rb);
    try {
      om.writeValue(new File("test.json"), sc);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

生成的堆栈跟踪看起来像如下:

The resulting Stack Trace looks as follows:

com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class sun.util.ResourceBundleEnumeration and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: SomeClass["bundle"]->java.util.PropertyResourceBundle["keys"])
  at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:230)
  at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:68)
  at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:32)
  at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:672)
  at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678)
  at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
  at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:672)
  at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678)
  at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
  at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:130)
  at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3613)
  at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:2929)
  at Saver.run(Saver.java:22)
  at Saver.main(Saver.java:14)


推荐答案

您应该检查您的JSON Java类,如果它包含可能会破坏您的应用程序的递归循环lication。如果是这种情况,请将 @JsonIgnore 添加到相关属性以打破循环。

You should check your JSON Java class, if it contains a recursive cycle that may blow up your application. If this is the case add @JsonIgnore to the relevant attribute to break the cycle.

这篇关于ResourceBundle的序列化[解决方法]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆