Problem with Reading in HOCON Data

Problem: Giving the provided HOCON reader (DataFormats.HOCON.read(str)) strings that are > 4096 chars results in java.io.IOException: Mark invalid.

Further explanation: When the HOCON reader is given this string (which is ~4700 chars long) the above error is displayed. I have given the reader a string of ~1800 chars and have not gotten the above error, which is what leads me to believe strings longer than 4096 chars long result in the error.

Not sure if the calling code would be useful, but just in case:

try {
    DataContainer dataContainer = DataFormats.HOCON.read(item);
    Optional<Animation> optionalAnimation = new Animation.Builder().build(dataContainer);
    return optionalAnimation.orElse(null);
} catch (IOException e){
    e.printStackTrace();
}

I was assuming this was more of an issue with ninja.leaping.configurate. I posted an issue on @zml 's Github page, but I haven’t gotten any responses. So I am now turning to the Sponge forums to see if anyone either knows about the problem I’m encountering or has an effective workaround.

The seemingly obvious solution would just be to break up the data, but I fear that solution would require more DB work that I don’t want to do… (but will if necessary…)

Other Details:
SpongeAPI: 6.1.0
SpongeVanilla: 1.11.2-6.1.0-BETA-17
Configurate-HOCON: 3.2

I guess you have no line breaks in those chars right?

I know that for me it went again when calling setPreservesHeader(true) on the configuration builder.

There are no newlines in the string, however, I’m not sure where in the process I would call setPreservesHeader(). To my knowledge, I never have access to the HoconConfigurationLoader class.

You need to give yourself access. You can’t use DataFormats here. Instead go through the hassle of setting up a configuration loader manually. Configuration Loaders — Sponge 7.2.0 documentation

IMO, a better solution in this case is just not storing your data without newlines (what I resorted to after a while).

Here’s a simple utility method to convert a HOCON string to a DataContainer without the header issue


public static DataContainer hoconToContainer(String hoconString) throws InvalidDataException, IOException {
    HoconConfigurationLoader loader = HoconConfigurationLoader.builder()
            .setHeaderMode(HeaderMode.NONE)
            .setSource(() -> new BufferedReader(new StringReader(hoconString)))
            .build();
    return DataTranslators.CONFIGURATION_NODE.translate(loader.load());
}

setPreservesHeader is deprecated so you should use setHeaderMode instead

2 Likes

@simon816 You sir have saved me a lot of pain. Thank you for providing this helper method to me, it seems to have fixed the issue I was having.

I think, DataTranslator to ConfigurationNode will be better than direct HOCON serialization.