Why specify fields to loadJSON() when already present in JSON

16 views
Skip to first unread message

Olafur Bragason

unread,
Jun 6, 2019, 12:43:39 PM6/6/19
to jOOQ User Group
When loading a JSON export like:
  jooq.loadInto(table)
    .loadJSON(...)
    .fields(field1, ..., fieldsN)
    .execute();
you need to specify the fields even if the JSON already contains the field information.
This seems unnecessary and brittle.

I made a proof of concept change to jOOQ which seems to work, at least for my test cases.

I added LoaderJSONOptionsStep<R> fieldsFromJSON() to LoaderJSONStep
and an implementation to LoaderImpl that only sets a flag fieldsFromJSON:

  @Override
  public final LoaderImpl<R> fieldsFromJSON() {
    this.fieldsFromJSON = true;
    return this;
  }

The only change needed to LoaderImpl.executeJSON() is to set fields to source if the new fieldsFromJSON flag is set:

  private void executeJSON() throws IOException {
    ...
    source = r.fields();    // old
    if(this.fieldsFromJSON) // new
      fields = source;      // new
    ...
  }

The new JSON load looks like this:
  jooq.loadInto(table)
    .loadJSON(...)
    .
fieldsFromJSON()
    .execute();

Please consider adding this feature to jOOQ.

Lukas Eder

unread,
Jun 7, 2019, 4:48:13 AM6/7/19
to jooq...@googlegroups.com
Hi Olafur,

Thank you very much for your suggestion. You're right, there is no strict requirement to make the fields() call on the LoaderJSONStep API mandatory (same with LoaderCSVStep and LoaderRowsStep). While it may not always be possible to derive field names from input data (the JSON header info is optional also when exporting data), we can definitely use it when it is present. So, either we make this step completely optional, or offer an additional fieldsFromSource() method that indicates that we want to use the source specification. I tend to prefer the latter.

In any case, I have registered a feature request for this:

Thanks again for your suggestion!
Lukas

--
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 jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/0d5172c5-56a6-4f60-b7d0-ddd22f169583%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lukas Eder

unread,
Jun 7, 2019, 4:49:47 AM6/7/19
to jooq...@googlegroups.com
Notice, as a workaround, you can use the LoaderJSONStep.fields(LoaderFieldMapper) method:

for example:

loader.fields(ctx -> ctx.field())
Reply all
Reply to author
Forward
0 new messages