如何阻止Grape从JSON编码输出两次?

时间:2014-09-03 11:18:43

标签: json ruby-on-rails-4 grape rails-roar

我目前正在实施基于JSON的Grape API。我已经集成了用于JSON serailization和反序列化的咆哮。执行以下代码时,似乎JSON被编码两次,从而产生带斜杠的响应。无论如何,在Grape上有必要在需要时抑制JSON解码吗?

json = UserRepresenter.new(user).to_json { status: StatusResponse::VALID, message: json }

1 个答案:

答案 0 :(得分:0)

Grape会自动将输出编码为JSON(指定format :json时),因此代码中的to_json调用是多余的。

尝试将代码更改为:

{ status: StatusResponse::VALID, message: UserRepresenter.new(user) }