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.

Deserializing to single-value Record from scalar values

See original GitHub issue

Describe the bug I experience a potential regression since 2.12.0 when trying to deserialize a scalar value to a single-value Record. Consider this test case:

import static org.junit.jupiter.api.Assertions.assertEquals;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

class RecordStringDeserializationTest {

  private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

  @Test
  void deserializeMyValueRecordFromPrimitiveValue() throws Exception {
    assertEquals(new MyValueRecord("foo"), OBJECT_MAPPER.readValue("\"foo\"", MyValueRecord.class));
  }

  final record MyValueRecord(@JsonProperty String value) {}
}

This works as expected in 2.11.3, since 2.12.0 a MismatchedInputException is thrown:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `us.byteb.jackson.RecordStringDeserializationTest$MyValueRecord` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('foo')
 at [Source: (String)""foo""; line: 1, column: 1]

This still works as expected if an explicit factory method annotated with @JsonCreator is provided for the Record or for an equivalent Class without explicit @JsonCreator (see additional test cases). I would expect the behavior for Records to be the same as for Classes, it also seems contrary to the brevity of Records to have to define an additional factory method.

Version information 2.12.0

To Reproduce See test case above.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:18 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Dec 17, 2020

@bfncs Yeah, this wasn’t something I had thought of until you reported it; I did not realize Records were already used, although it was mentioned as something that sort of works (when discussing initially addition of explicit support for 2.12).

I will add a note on 2.12 release page: thank you for bringing this up.

1reaction
bfncscommented, Dec 17, 2020

Thanks very much for answering this in great depth 💐 This makes sense and is still easy and consistent to use. I was only confused by the changed heuristic behavior between versions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson JSON - Using @JsonValue to serialize a single value ...
Usually the getter should return a simple scalar type (e.g. String or Number), but it can be any serializable type. This annotation can...
Read more >
Serialize and deserialize singles values in JSON.NET C# ...
I have several single values likes int, string types. I set string value with ~ separator . I serialize the string, save it...
Read more >
Window Functions - Substrait: Cross-Language Serialization ...
Window functions are similar to scalar values to an end user, producing a single value for each input record. However, the consumption visibility...
Read more >
Line-delimited JSON with Jackson - cowtowncoder - Medium
although JSON specification does not consider Scalar values (Strings, Boolean, ... While single-value input and output is sufficient for request/response ...
Read more >
The dynamic data type - Azure Data Explorer | Microsoft Learn
A value of any of the primitive scalar data types: bool , datetime , guid , int , long , real , string...
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