OkaeriPoland / okaeri-configs

Simple Java/POJO config library written with love and Lombok
MIT License
77 stars 11 forks source link

JSON Gson configs do not encode special characters #24

Closed oskarscot closed 2 years ago

oskarscot commented 2 years ago

Describe the bug JsonGsonConfigurer does not encode characters like ó, ę, ś, ą, ń, ć, », &

To Reproduce Steps to reproduce the behavior: Create a normal config using the JsonGsonConfigurer

Expected behavior Characters should be encoded normally instead they're replaced as ? or an escaped java unicode character like \u0026

Library version json gson configs ver 3.4.2

Config classes and setup routine coreConfig = ConfigManager.create(CoreConfig.class, (it) -> it.withConfigurer(new JsonGsonConfigurer()) .withBindFile(new File(this.getDataFolder(), "config.json")) .saveDefaults() .load(true));

dasavick commented 2 years ago

My best guess is that the missing encoding in the OutputStreamWriter provided to the Gson#toJson method may cause this in some cases. Please check if the issue persists, whilst using okaeri-configs version 4.0.0-beta4.

oskarscot commented 2 years ago

My best guess is that the missing encoding in the OutputStreamWriter provided to the Gson#toJson method may cause this in some cases. Please check if the issue persists, whilst using okaeri-configs version 4.0.0-beta4.

No, just tested it. The issue is still there

dasavick commented 2 years ago

Seems like I missed the second part about the \u0026. The fix would enforce the correct encoding, but this one is not related.

Gson is just leaning on the safer side with its defaults: https://stackoverflow.com/questions/4147012/can-you-avoid-gson-converting-and-into-unicode-escape-sequences

Fixed in 4.0.0-beta6 by adding #disableHtmlEscaping() call to the default GsonBuilder sequence. Can be also enforced in the older versions using JsonGsonConfigurer(Gson) constructor:

new JsonGsonConfigurer(new GsonBuilder()
    .disableHtmlEscaping()
    .setPrettyPrinting()
    .create())