Inline Response...
On Mon, Sep 3, 2018 at 3:38 AM Lukas Eder <[email protected]> wrote:
> Hi Samir,
>
> I'm guessing that the problem might be in your overridden sql() method:
>
>
> @Override
> public void sql(BindingSQLContext<JsonNode> ctx) throws SQLException {
>
> // Rendering a bind variable for the binding context's value and
> casting it to the json type
> //Allows for null values to be stored in DB
> if (ctx.convert(converter()).value() == null) {
> ctx.render().visit(DSL.val(ctx.convert(converter()).value(),
> JsonNode.class));
> } else {
>
> ctx.render().visit(DSL.val(ctx.convert(converter()).value())).sql("::json");
> }
>
>
My override is my attempt at getting it to write out the null value. My
original code was essentially.
ctx.render().visit(DSL.val(ctx.convert(converter()).value())).sql("::json");
for the sql method above.
> In particular, there's not really a point in the if branch of your if-else
> statement - why would you re-delegate the generation of your SQL string to
> another instance of some JsonNode binding?
>
if the value is null, then null::json causes errors *I believe*, at least
it was puking out on me before, I updated the converter to return null when
it sees null rather then an empty instance of JSON Node. I'll post that
code below as you're talking about that particular code further down.
> Do note, if you don't explicitly specify the binding on a DSL.val() bind
> variable, jOOQ will internally pick one from a static data type registry,
> i.e. the first one jOOQ has ever seen on the class path for the
> JsonNode.class
>
> This might be an entirely different implementation from the one you're
> using here.
>
> Another thing in PostgresJsonBinding.converter():
>
> @Override
> public Object to(JsonNode u) {
> return u == null || u ==
> JsonNodeFactory.instance.objectNode() ? null : u.toString();
> }
>
> Are you sure about that object identity comparison? I mean, looking at the
> implementation, the objectNode() method returns a new instance every time,
> so identity comparison doesn't really make sense.
>
My original version of that code above was as follows.
@Override
public JsonNode from(Object t) {
try {
return t == null ? JsonNodeFactory.instance.objectNode() : new
ObjectMapper().readTree(t.toString());
} catch (IOException e) {
return JsonNodeFactory.instance.objectNode();
}
}
So if the object was being set to null, rather then retuning null, it was
creating a new empty instance of a JsonNode. I changed it to check for
null and return null when it encounters it. Then the code you mentioned
earlier.
@Override
public void sql(BindingSQLContext<JsonNode> ctx) throws SQLException {
// Rendering a bind variable for the binding context's value
and casting it to the json type
//Allows for null values to be stored in DB
if (ctx.convert(converter()).value() == null) {
ctx.render().visit(DSL.val(ctx.convert(converter()).value(),
JsonNode.class));
} else {
ctx.render().visit(DSL.val(ctx.convert(converter()).value())).sql("::json");
}
was an attempt to capture the null value and actually persist it as null.
If that makes sense?
> Of course, there could be entirely different reasons why your binding
> might not even be applied to the insert statement. I can only be really
> sure if I have an MCVE:
> https://stackoverflow.com/help/mcve
>
I can try to provide an MVCE but that might be difficult since so many of
the artifacts are internal. I'll see what I can figure out that I can
share.
Thanks again, Lukas and everyone else on the list.
--
Samir Faci
>
>
> Thanks,
> Lukas
>
> On Thu, Aug 30, 2018 at 6:43 PM Samir Faci <[email protected]> wrote:
>
>> Attached are the two files I've used to allow me to read JSON postgres
>> fields in jooq.
>>
>> The use case i'm running into is where I want to explicitly set the
>> field to null but the Jooq (or my implementation ) stores it as an empty
>> JSON object.
>>
>> My implementation is roughly based on:
>> https://www.jooq.org/doc/latest/manual/code-generation/custom-data-type-bindings/
>> but using Jackson JSON library
>> instead of GSON.
>>
>> I've tried to adjust the behavior as you can see from the snippet in
>> PostgresJsonBinding but that isn't working as expected. The code gets
>> invoked
>> but i'm still seeing '{}' being stored rather then nulls.
>>
>> What am I missing here?
>>
>>
>> --
>> Thank you
>> Samir Faci
>> https://keybase.io/csgeek
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "jOOQ User Group" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> For more options, visit https://groups.google.com/d/optout.
>>
> --
> You received this message because you are subscribed to the Google Groups
> "jOOQ User Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
Thank you
Samir Faci
https://keybase.io/csgeek
--
You received this message because you are subscribed to the Google Groups "jOOQ
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.