stas / jsonapi.rb

Lightweight, simple and maintained JSON:API support for your next Ruby HTTP API.
MIT License
261 stars 57 forks source link

When using OJ, colons prepended to keys in the response #65

Closed Pod-2-Ji-Woong-You closed 2 years ago

Pod-2-Ji-Woong-You commented 3 years ago

Hello, first of all thank you very much for your dedication in maintaining this project. I'm trying to hook up OJ, by following the example shown in jsonapi-serializer repository, but for some reason the response looks weird. Could you please point me in the right direction?

Expected Behavior

{
  "data": {
    "id": "999",
    "type": "user"
  }
}

Actual Behavior

{
  ":data": {
    ":id": "999",
    ":type": "user"
  }
}

Steps to Reproduce the Problem

  1. Extend from BaseSerializer (https://github.com/jsonapi-serializer/jsonapi-serializer/blob/master/docs/json_serialization.md)
  2. Invoke render jsonapi: model

Specifications

Pod-2-Ji-Woong-You commented 3 years ago

Please disregard this issue. I digged into oj documentation, and found a solution. What I had to do was add mode: :compat (Oj.dump(hash, mode: :compat)

For reference: https://github.com/ohler55/oj/issues/54

stas commented 3 years ago

Consider adding an initializer with:`

# config/initializers/oj.rb
require 'oj'

Oj.optimize_rails()

# Disable encoding of HTML entities in JSON.
ActiveSupport.escape_html_entities_in_json = false
stas commented 2 years ago

@Ji-You I'll close this for now, please feel free to reopen if it's still an issue. Thank you!