> For the complete documentation index, see [llms.txt](https://clear.gitbook.io/project/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://clear.gitbook.io/project/model/column-types/converters.md).

# Converters

Any type from PostgreSQL can be converted using converter objects. By default, Clear converts already the main type of PostgreSQL.

However, custom type may not be supported yet. Clear offers you the possibility to add a custom converter.

The example demonstrated below is also available in `spec/model/converters/custom_converter_spec`

## Declare a new converter

The example below with a converter for a `Color` structure should be straight-forward:

```ruby
require "./base"

struct MyApp::Color
  property r : UInt8 = 0
  property g : UInt8 = 0
  property b : UInt8 = 0
  property a : UInt8 = 0

  def to_s
    # ...
  end

  def self.from_string(x : String)
    # ...
  end

  def self.from_slice(x : Slice(UInt8))
    # ...
  end
end

class MyApp::ColorConverter
  def self.to_column(x) : MyApp::Color?
    case x
    when Nil
      nil
    when Slice(UInt8)
      MyApp::Color.from_slice(x)
    when String
      MyApp::Color.from_string(x)
    else
      raise "Cannot convert from #{x.class} to MyApp::Color"
    end
  end

  def self.to_db(x : MyApp::Color?)
    x.to_s #< css style output, e.g. #12345400
  end
end

Clear::Model::Converter.add_converter("MyApp::Color", MyApp::ColorConverter)
```

Then you can use your mapped type in your model:

```ruby
class MyApp::MyModel
  include Clear::Model
  #...
  column color : Color #< Automatically get the converter
end
```

## `converter` option

Optionally, you may want to use a converter which is not related to the type itself. To do so, you can pass the converter name as optional argument in the `column` declaration:

```ruby
class MyApp::MyModel
  include Clear::Model
  #...
  column s : String, converter: "my_custom_converter"
end
```

By convention, converters which map struct and class directly are named using CamelCase, while converters which are not automatic should be named using the underscore notation.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://clear.gitbook.io/project/model/column-types/converters.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
