+ // Preparation for adding possibly missing content to the context
+ if (!empty($json['@context']) && is_string($json['@context'])) {
+ $json['@context'] = [$json['@context']];
+ }
+
+ if (!empty($json['@context']) && is_array($json['@context'])) {
+ // Remove empty entries from the context (a problem with WriteFreely)
+ $json['@context'] = array_filter($json['@context']);
+
+ // Workaround for servers with missing context
+ // See issue https://github.com/nextcloud/social/issues/330
+ if (!in_array('https://w3id.org/security/v1', $json['@context'])) {
+ $json['@context'][] = 'https://w3id.org/security/v1';
+ }
+
+ // Issue 12419: Workaround for GoToSocial
+ $pos = array_search('http://joinmastodon.org/ns', $json['@context']);
+ if (is_int($pos)) {
+ $json['@context'][$pos] = ['toot' => 'http://joinmastodon.org/ns#'];
+ }
+ }
+
+ // Bookwyrm transmits "id" fields with "null", which isn't allowed.
+ array_walk_recursive($json, function (&$value, $key) {
+ if ($key == 'id' && is_null($value)) {
+ $value = '';
+ }
+ });
+
+ $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));