我正在序列化的对象包含一个包含Enums键的地图。那些枚举有一个变量。当我使用GSON对其进行序列化时,我希望生成的JSON具有Enum变量而不是默认的Enum名称。我已经尝试创建自定义序列化程序并注册它,但它并没有做到这一点。这是我的代码。
控制器:
@Controller
public class CheckoutClientController {
@Autowired
private Gson gson;
@Autowired
private RequestHelper requestHelper;
@Autowired
private SettingsReader settingsReader;
@InitBinder
public void initBinder(final WebDataBinder binder) {
binder.registerCustomEditor(CheckoutConfigurationDto.class, new JsonDeserializerPropertyEditor<CheckoutConfigurationDto>(gson, CheckoutConfigurationDto.class));
}
/**
* Handles requests to the Checkout Client page, which is the outer wrapper that includes the white label checkout (WLC) iframe. Sets up the configuration
* data needed to pass to the WLC server.
*
* @return the model and view
*/
@RequestMapping(value = "/checkout/checkout-client.ep", method = RequestMethod.GET)
public ModelAndView showPage(HttpServletRequest request) {
CheckoutClientConfigurationDto checkoutClientConfig = new CheckoutClientConfigurationDto();
StringBuilder host = new StringBuilder();
host.append(request.getScheme()).append("://");
host.append(request.getServerName());
host.append(":").append(request.getServerPort());
checkoutClientConfig.setWlcHost(host.toString());
checkoutClientConfig.setClientId("clientId");
checkoutClientConfig.setAppId("appId");
checkoutClientConfig.setId("wlc-widget");
Map<CheckoutClientConfigurationOption, Boolean> options = checkoutClientConfig.getOptions();
options.put(CheckoutClientConfigurationOption.SHOW_ORDER_CONFIRMATION,
Boolean.valueOf(this.settingsReader.getSettingValue(SettingsConstants.SHOW_ORDER_CONFIRMATION).getValue()));
options.put(CheckoutClientConfigurationOption.REMOVE_CART_ITEMS,
Boolean.valueOf(this.settingsReader.getSettingValue(SettingsConstants.REMOVE_CART_ITEMS).getValue()));
return new ModelAndView(ViewConstants.CHECKOUT_CLIENT_TEMPLATE_PATH, "checkoutClientConfig", gson.toJson(checkoutClientConfig));
}
}
CheckoutClientConfigurationDto(减去所有样板getter / setter):
public class CheckoutClientConfigurationDto implements Dto {
private String wlcHost;
private String clientId;
private String appId;
private String id;
private Map<CheckoutClientConfigurationOption, Boolean> options;
public CheckoutClientConfigurationDto() {
products = new ArrayList<ProductDto>();
options = new HashMap<CheckoutClientConfigurationOption, Boolean>();
}
public Map<CheckoutClientConfigurationOption, Boolean> getOptions() {
return options;
}
public void setOptions(final Map<CheckoutClientConfigurationOption, Boolean> options) {
this.options = options;
}
}
CheckoutClientConfigurationOption:
public enum CheckoutClientConfigurationOption {
SHOW_SAVED_ADDRESSES("showSavedAddresses", true),
SHOW_CART_SUMMARY("showCartSummary", true),
REMOVE_CART_ITEMS("removeCartItems", true),
SHOW_DISCOUNT_FIELD("showDiscountField", true),
SHOW_VAT_CODE("showVatCode", true),
SHOW_ORDER_CONFIRMATION("showOrderConfirmation", true),
SHOW_CANCEL_BUTTON("showCancelButton", false),
SINGLE_PAGE_CHECKOUT("singlePageCheckout", false),
SEND_ORDER_CONFIRMATION_EMAIL("sendOrderConfirmationEmail", true),
SEND_SHIPPING_CONFIRMATION_EMAIL("sendShippingConfirmationEmail", true);
private String optionName;
private boolean defaultValue;
private CheckoutClientConfigurationOption(final String optionName, final boolean defaultValue) {
this.optionName = optionName;
this.defaultValue = defaultValue;
}
public boolean getDefautValue() {
return defaultValue;
}
public String getOptionName() {
return optionName;
}
}
我的自定义GSON序列化程序:
public class CheckoutClientConfigurationOptionGsonSerializer implements JsonSerializer<CheckoutClientConfigurationOption> {
@Override
public JsonElement serialize(CheckoutClientConfigurationOption src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.getOptionName());
}
}
我的自定义GSON配置器:
public class GsonConfigurer {
private Map<Class<?>, Object> typeAdapterMap;
public Gson create() {
final GsonBuilder gsonBuilder = new GsonBuilder();
for (final Entry<Class<?>, Object> typeAdapterMapping : typeAdapterMap.entrySet()) {
gsonBuilder.registerTypeAdapter(typeAdapterMapping.getKey(), typeAdapterMapping.getValue());
}
return gsonBuilder.create();
}
protected Map<Class<?>, Object> getTypeAdapterMap() {
return typeAdapterMap;
}
public void setTypeAdapterMap(final Map<Class<?>, Object> typeAdapterMap) {
this.typeAdapterMap = typeAdapterMap;
}
}
XML:
<bean id="gsonConfigurer" class="com.sfweb.gson.GsonConfigurer">
<property name="typeAdapterMap">
<util:map key-type="java.lang.Class">
<entry key="com.sfweb.dto.CheckoutConfigurationOption">
<bean class="com.sfweb.dto.deserializer.CheckoutConfigurationOptionGsonDeserializer" />
</entry>
<entry key="com.sfweb.dto.CheckoutClientConfigurationOption">
<bean class="com.sfweb.dto.serializer.CheckoutClientConfigurationOptionGsonSerializer" />
</entry>
</util:map>
</property>
</bean>
<bean class="com.google.gson.Gson" factory-bean="gsonConfigurer" factory-method="create" />
我还有一个自定义反序列化器,正如您在XML中看到的那样。那一个没有问题。我在调试模式下运行,CheckoutClientConfigurationOptionGsonSerializer中的行永远不会被命中。我检查了我调用toJson()on的gson对象中是否有自定义序列化程序。所以我不确定问题是什么。我有一种感觉,我只是错过了一件。
我希望得到的JSON能说出&#34; showSavedAddresses&#34;,而是说&#34; SHOW_SAVED_ADDRESSES&#34;。在此先感谢您的帮助!
答案 0 :(得分:2)
阅读GsonBuilder#enableComplexMapKeySerialization上的文档,我看到了:
映射序列化的默认实现使用键上的toString()
默认情况下,它不会在地图键上运行TypeAdapter
。我试着简单地调用这个方法并让我的枚举显示为数字字符串。
答案 1 :(得分:0)
你可以很好地学习TypeAdapterFactory's documentation。它包含一个将枚举格式化为小写的示例;你可以根据自己的需要修改它。
public class LowercaseEnumTypeAdapterFactory implements TypeAdapter.Factory {
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Class<T> rawType = (Class<T>) type.getRawType();
if (!rawType.isEnum()) {
return null;
}
final Map<String, T> lowercaseToConstant = new HashMap<String, T>();
for (T constant : rawType.getEnumConstants()) {
lowercaseToConstant.put(toLowercase(constant), constant);
}
return new TypeAdapter<T>() {
public void write(JsonWriter out, T value) throws IOException {
if (value == null) {
out.nullValue();
} else {
out.value(toLowercase(value));
}
}
public T read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
} else {
return lowercaseToConstant.get(reader.nextString());
}
}
};
}
private String toLowercase(Object o) {
return o.toString().toLowerCase(Locale.US);
}
}