question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when generating/serializing keys with multilines and colon

See original GitHub issue

Hello there

I wanted to report that a key containing a colon : followed by new line chars \n does not seem to be parsing correctly. For example the following test case reproduces the error

    @Test(expected = JsonParseException.class)
    public void testYamlMultiLineNotCanonical() throws Exception {
        String key = "SomeKey:\n\nOtherLine";
        ObjectMapper jsonMapper = new ObjectMapper();
        ObjectNode jsonRoot = jsonMapper.createObjectNode();
        jsonRoot.put(key, 302);

        YAMLFactory yamlFactory = new YAMLFactory();
        ObjectMapper yamlObjectMapper = new ObjectMapper(yamlFactory);
        yamlObjectMapper.configure(JsonParser.Feature.STRICT_DUPLICATE_DETECTION, true);
        StringWriter swriter = new StringWriter();

        yamlObjectMapper.writeValue(swriter, jsonRoot);

        ObjectMapper readMapper =  new ObjectMapper(yamlFactory);
        ObjectNode readJsonRoot = (ObjectNode) readMapper.readTree(swriter.toString());
        Assert.assertEquals(readJsonRoot.get(key).asInt(),302 );
    }

I would have expected the parser to quote the key as \n and : are supported yaml content.

There is work around for this: Force canonical output.

        yamlFactory.configure(YAMLGenerator.Feature.CANONICAL_OUTPUT, true);

That however could have other side effects. Maybe I am missing some other configuration that would do the same?

Thoughts, comments are welcomed.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
cowtowncodercommented, Feb 1, 2022

I added a simple failing test like so:

    public void testComplexName() throws Exception
    {
        final String key = "SomeKey:\nOtherLine";
        Map<?,?> input = Collections.singletonMap(key, 302);
        final String doc = MAPPER.writeValueAsString(input);
        Map<?,?> actual = MAPPER.readValue(doc, Map.class);
        assertEquals(input, actual);
    }

and will try to see if I can figure out how to resolve this, since SnakeYAML should be able to handle it properly.

1reaction
cowtowncodercommented, Feb 1, 2022

@asomov Will do if need be, thanks!

Fix is in, for 2.13.2 release. Covers linefeed case but I suspect there may be other things that require quoting.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to repair a serialized string which has been corrupted by ...
This solved my problem in Laravel 5. I changed the column definition from string() to binary().
Read more >
Struggling to deserialize JSON with escaped double quotes
I'll try to illustrate this. This is valid JSON. { "errors": [ { "params": { "password": "size must be between 4 and 30",...
Read more >
Serialization - Archive Exceptions - Boost C++ Libraries
This exception is thrown when it is detected that the serialization of the same type has been instantiated more than once. This might...
Read more >
Database Engine events and errors - SQL Server
Consult this MSSQL error code list to find explanations for error messages for SQL Server database engine events.
Read more >
Binary Object Serialization using Template ... - YouTube
AbstractThere are many mechanisms for a program to persist data. Most are slow, bulky, and inflexible, that typically store flat data, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found