New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

sbf-dm-serializer

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sbf-dm-serializer

  • 1.4.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

= dm-serializer

== Overview

dm-serializer allows DataMapper models and collections to be serialized to a variety of formats (currently JSON, XML, YAML and CSV).

== How it works

One method is added to each model/collection for each serialization type - to_json, to_xml, to_yaml, and to_csv. With the exception of to_csv, all of these methods share the same interface. to_json will be used for examples. Any method specific behaviour is documented in its own section below.

require 'dm-serializer'

class Cow include DataMapper::Resource

property :id,   Integer, :key => true
property :name, String

def description
  "A Cow"
end

end

cow = Cow.create( :id => 1, :name => "Berta" )

cow.to_json # => { "id": 1, "name": "Berta" } cow.to_json(:only => [:name]) # => { "name": "Berta" } cow.to_json(:exclude => [:id]) # => { "name": "Berta" } cow.to_json(:methods => [:description]) # => { "id": 1, "name": "Berta", "description": "A Cow" }

You can include associations by passing the association accessor the :methods option.

If you want to only load a particular serialization method, that's cool, you can do that:

require 'dm-serializer/to_json'

== to_xml

to_xml supports some extra options to allow you to override the element names

cow.to_xml(:element_name => 'bovine') # => 1Berta cows.to_xml(:collection_element_name => 'kine') # => 1Berta

If you would like a nice speed boost (~5x), require libxml or nokogiri before dm-serializer, and that library will be used rather than REXML.

== to_csv

to_csv currently doesn't support any options yet. It will in the future. It will not support serializing child associations.

== Arrays, Hashes, and other core classes

dm-serializer only adds serialization methods to DataMapper objects and collections, however some libraries used (json, yaml) add methods to core classes, such as Array. Note that passing dm-serializer options (such as :only) to these methods is not supported.

Cow.all.to_a.to_yaml(:only => 'name') # WILL NOT WORK

== Beware

If you go spelunking through the code you will find other undocumented options. Use at your own risk, I plan on removing or changing these in the near future.

FAQs

Package last updated on 04 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc