gpt4 book ai didi

ruby - 关于在 ruby​​ sinatra 应用程序中使用 JSON 的方法的困惑

转载 作者:太空宇宙 更新时间:2023-11-03 16:53:11 24 4
gpt4 key购买 nike

我正在制作一个使用 mongomapper 的 Ruby Sinatra 应用程序,我的大部分响应都将采用 JSON 格式。

困惑

现在我遇到了许多与 JSON 相关的不同事情。

  1. Std-lib 1.9.3 JSON 类:http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html
  2. JSON gem :http://flori.github.io/json/
  3. ActiveSupport JSON http://api.rubyonrails.org/classes/ActiveSupport/JSON.html因为我使用的是 MongoMapper,它使用 ActiveSupport

什么有效

我使用单一方法来处理响应:

def handleResponse(data, haml_path, haml_locals)
case true
when request.accept.include?("application/json") #JSON requested
return data.to_json
when request.accept.include?("text/html") #HTML requested
return haml(haml_path.to_sym, :locals => haml_locals, :layout => !request.xhr?)
else # Unknown/unsupported type requested
return 406 # Not acceptable
end
end

线路:

return data.to_json

data 是我的 MongoMapper 模型类之一的实例时工作:

class DeviceType

include MongoMapper::Document
plugin MongoMapper::Plugins::IdentityMap

connection Mongo::Connection.new($_DB_SERVER_CNC)
set_database_name $_DB_NAME

key :name, String, :required => true, :unique => true

timestamps!

end

怀疑在这种情况下,to_json 方法来自ActiveSupport 的某处,并在mongomapper 中进一步实现框架。

什么不起作用

我也使用相同的方法来处理错误。我使用的错误类是我自己的错误类之一:

# Superclass for all CRUD errors on a specific entity.
class EntityCrudError < StandardError
attr_reader :action # :create, :update, :read or :delete
attr_reader :model # Model class
attr_reader :entity # Entity on which the error occured, or an ID for which no entity was found.
def initialize(action, model, entity = nil)
@action = action
@model = model
@entity = entity
end
end

当然,在这个类的实例上调用to_json时,是不行的。不是以一种完全合理的方式:显然这个方法实际上是定义的。我不知道它会从哪里来。从堆栈跟踪来看,显然是 activesupport:

 Unexpected error while processing request: object references itself
object references itself
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/json/encoding.rb:75:in `check_for_circular_references'
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/json/encoding.rb:46:in `encode'
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/json/encoding.rb:246:in `block in encode_json'
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/json/encoding.rb:246:in `each'
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/json/encoding.rb:246:in `map'
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/json/encoding.rb:246:in `encode_json'

但是这个方法实际上是在我的类中的什么地方定义的呢?

问题

我需要像这样重写我的类中的方法:

# Superclass for all CRUD errors on a specific entity.
class EntityCrudError < StandardError

def to_json
#fields to json
end

end

但我不知道如何进行。鉴于顶部提到的 3 种方式,哪种方式最适合我?

最佳答案

事实证明,我不需要做任何特别的事情。

我并没有很快怀疑到这一点,但问题是:

class EntityCrudError < StandardError
..
attr_reader :model # Model class
..
end

该字段包含有效模型类:

class DeviceType
..
..
end

这让循环引用。我现在只用 name 类替换它,这将满足我的目的。现在 to_json 不再提示了,我也很高兴:)

我仍然想知道所有这些 JSON 实现之间有什么区别。

关于ruby - 关于在 ruby​​ sinatra 应用程序中使用 JSON 的方法的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16419075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com