OOro

Serialization

API-safe serialization, hidden fields, loaded relations, nullable values, and JSON behavior.

Oro does not replace Go’s JSON package. It only provides an optional ORM-aware serialization layer for hidden fields and loaded relations.

Normal JSON

A model is still a Go struct. encoding/json works as usual and follows your JSON tags.

bytes, err := json.Marshal(product)

Oro serialization

payload := oro.Serialize(product)

Serialize applies ORM metadata:

  • fields marked Hidden() are omitted by default;
  • loaded relations can be included;
  • oro.Null[T] serializes naturally as a value or null;
  • virtual fields can be included when present.

Hidden fields

func (User) Define(s *oro.SchemaBuilder) {
    s.Field("PasswordHash").String().Hidden()
}
payload := oro.Serialize(user)
payloadWithHidden := oro.Serialize(user, oro.ShowHidden())

Use ShowHidden only for internal tooling.

Relations

Relations are not embedded in the struct, so relation serialization reads the loaded relation state:

article, err := db.Use[Article]().With(Article{}.Comments()).First(ctx)
payload := oro.Serialize(article)

If a relation was not loaded, serialization does not query it implicitly.

DTOs

For public APIs with strict shape requirements, DTOs are still the best boundary:

type ProductResponse struct {
    ID    uint64 `json:"id"`
    Code  string `json:"code"`
    Price uint   `json:"price"`
}

Use Serialize for convenient ORM-aware output, and DTOs for stable external contracts.

Edit this page