8000 SupportedTypes · salat/salat Wiki · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Noah Zucker edited this page Sep 7, 2016 · 6 revisions

Supported types

Salat support for case classes and enums

Salat provides built-in support for serializing and deserializing:

Options

Any supported type except collections are also supported as an Option.

Instead of an Option that contains a collection...

  • Did you try using a default argument?
    • Is it a List? Use Nil.
    • For anything else, use an empty instance of the collection - Seq.empty, Map.empty, etc.
  • Stop denormalizing the relationship into your model object. Instead, create a projection on a parent-child relationship.
  • Are you contorting your model to compensate for a JSON library which doesn't support default arguments? Try using Salat's JSON support instead.

Collections

See collection support for a complete list of supported collection types.

Arrays are not currently supported.

Maps are represented as DBObject; all other collections turn into DBList.

BSON support

Salat delegates serialization for most common types to Casbah's BSON support:

  • String
  • Boolean
  • Int
  • Double
  • Long
  • ObjectID
  • Date
  • UUID

Make sure that Casbah's BSON encoding hooks are in scope:

com.mongodb.casbah.commons.conversions.scala.RegisterConversionHelpers()

DateTime support

org.joda.time.DateTime support requires registering Casbah's DateTime encoding hook in addition to Casbah's other conversion helpers:

com.mongodb.casbah.commons.conversions.scala.RegisterJodaTimeConversionHelpers()

Salat extensions to BSON support

Salat provides support for converting the following types to something BSON serializes natively:

  • Char is serialized as String
  • Float is serialized as Double
  • BigDecimal is serialized as Double
    • precision and rounding mode are preserved - see the section on BigDecimal handling in custom Salat contexts for details.
  • BigInt is serialized as Long

Support your own types

You can support other types by creating custom BSON hooks.

Refer to com.mongodb.casbah.commons.conversions.scala.JodaDateTimeHelpers to get started.

Unsupported types

  • Nested inner classes (as used in Cake pattern)
  • A class typed at the top-level to a trait or an abstract superclass
  • com.mongodb.DBRef
  • Any type of Map whose key is not a String
    • any type of map whose key is a String containing . or $
  • Either
  • type aliases
  • varargs

Troubleshooting serialization problems

Salat tries to provide the benefit of the doubt by passing things it doesn't understand down to Casbah.

This means that your object can be serialized but you will encounter an exception from Salat when you try to deserialize. This is actually good news because the Salat exception should give you everything you need to post on the mailing list and get the problem fixed.

First, update to the latest snapshot of salat-core and try again.

If that doesn't fix the problem, post to the mailing list.

Clone this wiki locally
0