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.

JsonBinaryType doesn't support String Object without quotes

See original GitHub issue

Using hibernate-types-52 2.9.12 with PostgreSQL

I would like to be able to persist an arbitrary Object member in my entity using JSONB …

CREATE TABLE my_entity (
    id BIGSERIAL PRIMARY KEY,
    data JSONB,
);
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
public class MyEntity {

    @Type(type = "jsonb")
    @Column(columnDefinition = "json")
    private Object data;

    // Getters and setters removed for brevity
}

This works fine for the following cases …

myEntity.setData(List.of("hello"));
myEntity.setData(Map.of("hello", "world"));
myEntity.setData(null);
myEntity.setData(5);

But not for …

myEntity.setData("hello");

JsonParseException is thrown by the call to objectMapper.readTree(…) because instances of String do not get wrapped in quotes

I believe the fix here is to simply remove the instanceof conditional …

else if (Object.class.isAssignableFrom(type)) {
    return (X) objectMapperWrapper.toJsonNode(toString(value));
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vladmihalceacommented, Jun 23, 2020

I’m not discarding this request since JSON is also used to save unvalidated data, hence it should be less strict.

I created the following branch on GitHub. The persist works fine. It’s just that the unwrapping needs to discard the quotes. I’ll have to find a way to fix that. If you have time, you could take a look and see whether there’s a clever way to address it.

0reactions
vladmihalceacommented, Jun 23, 2020

@michaelcowan There are lots of test failures. It’s not as simple as just removing that check. You have to remember that these types have to work on Oracle, SQL Server, PostgreSQL, and MySQL.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Switch from JsonStringType to JsonBinaryType when the ...
It works fine with String column data type but I get an error with JsonNode as you show above - "Unable to build...
Read more >
Unquoting JSON strings; print JSON strings without quotes
This answer is probably preferable since the escaping is probably not limited to adding quotation marks, but also escapes quotation marks in the ......
Read more >
How to read a JSON string without field name quotes in Jackson
We often have to read JSON values where the field names are not quoted, i.e., when enclosed with double ( "" ) or...
Read more >
How to map JSON objects using generic Hibernate Types
MySQL 5.7 adds support for JSON types, which, at the JDBC level, ... Not only that JSON Object(s) are properly transformed from their ......
Read more >
Which is better to use, json/jsonb or string to store ... - Quora
There are a few factors. That you mention it is not frequently used then you may want to go with json. The JSON...
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