Modules such as json_field or jsonb allows entities to have fields holding arbitrary JSON data.

When retrieved through JSONAPI, theses fields' value is a string containing the data as encoded JSON.

A new enhancer could decode the string on the fly so the data would be a real JSON object inside the response payload.

Comments

garphy created an issue. See original summary.

garphy’s picture

Status: Active » Needs review
StatusFileSize
new2.1 KB

First attempt of doing this.

garphy’s picture

StatusFileSize
new2.23 KB
new1.46 KB

This is an updated patch for 2.x branch.

Status: Needs review » Needs work

The last submitted patch, 4: jsonapi_extras-2906753-4.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

garphy’s picture

Version: 8.x-1.x-dev » 8.x-2.x-dev
Status: Needs work » Needs review
e0ipso’s picture

Kicking off tests.

logickal’s picture

We're doing the exact same thing in a custom enhancer - +1 for including this functionality in the upstream module. One thing of note, at least in our case, the json could also be an array so for this to be one-size-fits-all, I'd suggest adding that to the getOutputJsonSchema.

logickal’s picture

@garphy @e0ipso Another question - is there a general preference to using \Symfony\Component\Serializer\SerializerInterface over Drupal\Component\serialization\Json?

sime’s picture

I upgraded to the latest jsonapi_extras and the enhancers changed a bit - I got stuck on validator config. So I moved the my enhancer into a standalone module, so I could nurse it along and see how this issue progresses.

https://gist.github.com/simesy/fb44c48fcab0725b019b0fd6dc7e1fb2 in case anyone needs something urgently.

garphy’s picture

@logickal, I chose to go with SerializerInterface as it seemed to be the coherent with what jsonapi mostly uses itself but it seems that it also use Json::decode() directly in some places.

@e0ipso, any opinion ?

logickal’s picture

Here's a quick re-roll to support arrays.

logickal’s picture

And for the sake of discussion, here is the code that we are running on our end in our custom enhancer, rolled onto the original patch.

e0ipso’s picture

+++ b/src/Plugin/jsonapi/FieldEnhancer/JSONFieldEnhancer.php
@@ -0,0 +1,88 @@
+ * Perform additional manipulations to date fields.

This is a c&p error.


I will fix this issue on commit. Thanks everyone for the contribution!

  • e0ipso committed 88db704 on 8.x-2.x authored by logickal
    Issue #2906753 by logickal, garphy, e0ipso, sime, jamesmalvi: Add a...
  • e0ipso committed cfc69ac on 8.x-2.x authored by garphy
    Issue #2906753 by logickal, garphy, e0ipso, sime, jamesmalvi: Add a...
e0ipso’s picture

Status: Needs review » Fixed

Thanks for your relentless contributions.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.