ActiveModelSerializers :camel_lower 配置不工作
ActiveModelSerializers :camel_lower configuration not working
我想利用 AMS 创建 json 数据以在我的测试套件中作为 GraphQL 变量传递。显然,它支持 :camel_lower
,它将像 :some_field
这样的散列键转换为 :someField
,但我似乎无法让它工作。这是相关代码:
/config/initializers/active_model_serializers.rb:
ActiveModelSerializers.config.key_transform = :camel_lower
/app/serializers/service_serializer.rb:
class ServiceSerializer < ApplicationSerializer
attributes :id, :name, :time_increment
end
rails 控制台:
ActiveModelSerializers.config.key_transform
=> :camel_lower
s = Service.new(name: 'Test', time_increment: 10)
=> #<Service id: nil, name: "Test", time_increment: 10, created_at: nil, updated_at: nil>
ss = ServiceSerializer.new(s)
=> #<ServiceSerializer:0x00007f3771dd9dc0 @object=#<Service id: nil, name: "Test", time_increment: 10, created_at: nil, updated_at: nil>, @instance_options={}, @root=nil, @scope=nil>
ss.as_json
=> {:id=>nil, :name=>"Test", :time_increment=>10}
我期待的结果是:
=> {:id=>nil, :name=>"Test", :timeIncrement=>10}
ActiveModelSerializers
长时间处于某种维护状态,似乎没有收到任何更新。
我个人的选择是 blueprinter
gem or jsonapi-serializers
。 blueprinter
更接近 AMS。
使用起来非常容易
# Gemfile
gem 'blueprinter'
和通常的
bundle install
创建序列化程序非常简单
# e.g. app/blueprinter/service_blueprint.rb
class ServiceBlueprint < Blueprinter::Base
# identifier :id
fields :id, :name, :time_increment
end
添加一个classLowerCamelTransformer
# e.g. app/blueprinter/lower_camel_transformer.rb
class LowerCamelTransformer < Blueprinter::Transformer
def transform(hash, _object, _options)
hash.transform_keys! { |key| key.to_s.camelize(:lower).to_sym }
end
end
并在 config/initializers/blueprinter.rb
Blueprinter.configure do |config|
config.default_transformers = [LowerCamelTransformer]
end
测试一下
s = Service.find(1)
puts ServiceBlueprint.render(s)
# Will give you a nice output with lower camel case
使用
ActiveModelSerializers.config.adapter = :json
它对我有用
我想利用 AMS 创建 json 数据以在我的测试套件中作为 GraphQL 变量传递。显然,它支持 :camel_lower
,它将像 :some_field
这样的散列键转换为 :someField
,但我似乎无法让它工作。这是相关代码:
/config/initializers/active_model_serializers.rb:
ActiveModelSerializers.config.key_transform = :camel_lower
/app/serializers/service_serializer.rb:
class ServiceSerializer < ApplicationSerializer
attributes :id, :name, :time_increment
end
rails 控制台:
ActiveModelSerializers.config.key_transform
=> :camel_lower
s = Service.new(name: 'Test', time_increment: 10)
=> #<Service id: nil, name: "Test", time_increment: 10, created_at: nil, updated_at: nil>
ss = ServiceSerializer.new(s)
=> #<ServiceSerializer:0x00007f3771dd9dc0 @object=#<Service id: nil, name: "Test", time_increment: 10, created_at: nil, updated_at: nil>, @instance_options={}, @root=nil, @scope=nil>
ss.as_json
=> {:id=>nil, :name=>"Test", :time_increment=>10}
我期待的结果是:
=> {:id=>nil, :name=>"Test", :timeIncrement=>10}
ActiveModelSerializers
长时间处于某种维护状态,似乎没有收到任何更新。
我个人的选择是 blueprinter
gem or jsonapi-serializers
。 blueprinter
更接近 AMS。
使用起来非常容易
# Gemfile
gem 'blueprinter'
和通常的
bundle install
创建序列化程序非常简单
# e.g. app/blueprinter/service_blueprint.rb
class ServiceBlueprint < Blueprinter::Base
# identifier :id
fields :id, :name, :time_increment
end
添加一个classLowerCamelTransformer
# e.g. app/blueprinter/lower_camel_transformer.rb
class LowerCamelTransformer < Blueprinter::Transformer
def transform(hash, _object, _options)
hash.transform_keys! { |key| key.to_s.camelize(:lower).to_sym }
end
end
并在 config/initializers/blueprinter.rb
Blueprinter.configure do |config|
config.default_transformers = [LowerCamelTransformer]
end
测试一下
s = Service.find(1)
puts ServiceBlueprint.render(s)
# Will give you a nice output with lower camel case
使用
ActiveModelSerializers.config.adapter = :json 它对我有用