Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ObjectMapper.readValue() from java.io.File fails to parse single quoted string (json5) #3301

Closed
wcarmon opened this issue Oct 13, 2021 · 3 comments
Labels
to-evaluate Issue that has been received but not yet evaluated

Comments

@wcarmon
Copy link

wcarmon commented Oct 13, 2021

Describe the bug
com.fasterxml.jackson.databind.ObjectMapper.readValue drops double quotes when parsing *.json5 file.
(specifically, when parsing a single quoted string value)

Version information
2.12.4

To Reproduce

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import java.nio.file.Files;
import java.nio.file.Path;
import static java.nio.charset.StandardCharsets.UTF_8;

public class Foo {

  @JsonProperty("bar")
  String bar;

  public Foo() {
  }

  public static void main(String[] args) throws Exception {
    ObjectMapper objectMapper = JsonMapper.builder()
        .enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES)
        .enable(JsonReadFeature.ALLOW_SINGLE_QUOTES)
        .build();

    final String json = "{ \"bar\": '\"\"'}";    // <--- single quoted string here (json5)

    // -- Write the same json to file
    Path tempFile = Files.createTempFile("foo", ".json5");
    Files.write(tempFile, json.getBytes(UTF_8));

    // Succeeds when reading from string
    Foo success = objectMapper.readValue(
        json,
        Foo.class);

    // Bug: Parses incorrectly when reading from file
    Foo failure = objectMapper.readValue(
        tempFile.toFile(),   // <--- this is the difference
        Foo.class);

    System.out.println("success: " + success.bar);  // prints both
    System.out.println("failure: " + failure.bar);  // prints only one "   <--- BUG symptom
  }
}

Expected behavior
Expected both double quotes to be parsed from *.json5 file

(Works correctly when parsing string, fails when parsing *.json5 file)

Additional context

  • oracle jvm 11
  • same issue happens in kotlin
@wcarmon wcarmon added the to-evaluate Issue that has been received but not yet evaluated label Oct 13, 2021
@cowtowncoder cowtowncoder changed the title ObjectMapper.readValue fails to parse single quoted string (json5) ObjectMapper.readValue() from java.io.File fails to parse single quoted string (json5) Oct 13, 2021
@cowtowncoder
Copy link
Member

Hmmmh. That is unexpected... I suspect it might not necessarily be due to File but use of byte-backed source. Will play with this.

Thank you for reporting this issue!

@cowtowncoder
Copy link
Member

Created FasterXML/jackson-core#721 and I can reproduce this with byte-backed source (and DataInput it seems)

@cowtowncoder
Copy link
Member

So; fix will be in 2.13.1 (or, if I ever do 2.12.6).

In the meantime one way to work around this would be to force use of Reader internally; this is possible by explicitly constructing InputStreamReader on FileInputStream.
Writing is not related to the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to-evaluate Issue that has been received but not yet evaluated
Projects
None yet
Development

No branches or pull requests

2 participants