Documentation
¶
Overview ¶
Package jbuilder is a pure-Go (CGO-free) reimplementation of Ruby's jbuilder gem — the DSL that builds JSON with a method_missing builder: `json.name "x"` yields {"name":"x"}, nested blocks yield nested objects, and json.array! yields arrays. The gem drives everything through method_missing on a Jbuilder receiver; Go has no method_missing, so this package exposes the same behaviour as explicit driver methods (Set, Block, Array, Extract, Merge, Nil, Child, …) that the go-embedded-ruby host binds json.<name> onto. The result — obtained with (*Jbuilder).Encode / TargetJSON — is the exact compact JSON string the gem's target! returns for the same structure.
The value model is deliberately small so a host can map its own object graph to and from it: nil, bool, integers (including *big.Int), float32/float64, string, Symbol, []any (arrays), *Jbuilder / *object (nested objects), and Go maps. Blocks are ordinary Go closures the host supplies.
Index ¶
- func Encode(fn func(*Jbuilder)) string
- type Jbuilder
- func (j *Jbuilder) Array(items []any, fn func(*Jbuilder, any))
- func (j *Jbuilder) Block(key string, fn func(*Jbuilder))
- func (j *Jbuilder) Child(fn func(*Jbuilder))
- func (j *Jbuilder) Encode() string
- func (j *Jbuilder) Extract(pairs []Pair)
- func (j *Jbuilder) IgnoreNil(on ...bool)
- func (j *Jbuilder) KeyFormat(ops ...KeyOp)
- func (j *Jbuilder) Merge(pairs []Pair)
- func (j *Jbuilder) MergeArray(elems []any)
- func (j *Jbuilder) Nil()
- func (j *Jbuilder) Set(key string, value any)
- func (j *Jbuilder) SetKey(key Symbol, value any)
- func (j *Jbuilder) TargetJSON() string
- type KeyOp
- type Pair
- type Symbol
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Jbuilder ¶
type Jbuilder struct {
// contains filtered or unexported fields
}
Jbuilder is the JSON builder. It starts undecided and becomes an object the first time a key is set, an array when Array/Child is used, or a bare value when Nil clears it — mirroring how the gem's @attributes morphs. An empty builder renders as {} (jbuilder's Jbuilder.new.target! default).
func (*Jbuilder) Array ¶
Array turns the whole builder into a JSON array by mapping fn over items (json.array! collection do |x| … end). For each element fn is called with a fresh child builder and the element; the child's rendered value becomes that array entry. Passing a nil fn emits the elements verbatim (json.array! collection), so scalar collections pass straight through.
func (*Jbuilder) Block ¶
Block builds a nested object under key from fn (json.<key> do … end). fn receives a child builder that inherits the parent's key_format! and ignore_nil! settings; whatever it produces becomes the value at key. A block that sets nothing yields {}.
func (*Jbuilder) Child ¶
Child appends one element built by fn to the builder's array (json.child! { … }), turning the builder into an array on first use. It is the incremental form of Array.
func (*Jbuilder) Encode ¶
Encode renders the builder to its compact JSON string — the exact bytes the gem's json.target! returns.
func (*Jbuilder) Extract ¶
Extract copies the named attributes from a host-resolved attribute list into the object (json.extract!(obj, :a, :b) / json.call(obj, :a, :b)). The host resolves obj.a / obj[:a] to values and passes them as ordered pairs; Extract sets each under its key (with key_format! applied). A missing attribute the host resolves to nil is written as null unless IgnoreNil is active.
func (*Jbuilder) IgnoreNil ¶
IgnoreNil enables (or, given false, disables) dropping keys whose value is nil (json.ignore_nil!). The setting is inherited by child blocks created after it.
func (*Jbuilder) KeyFormat ¶
KeyFormat sets the active key transform (json.key_format! camelize: :lower, etc.). Passing no ops clears formatting (json.key_format! with no arguments). The transform applies to keys set afterwards and is inherited by child blocks.
func (*Jbuilder) Merge ¶
Merge folds pairs into the current object (json.merge!(hash)). Like the gem's merge!, it does not de-duplicate against existing keys via the ordered index — it appends the pairs in the given order — so a host reproducing jbuilder's literal merge! passes an ordered []Pair. Merging onto a fresh builder makes it an object.
func (*Jbuilder) MergeArray ¶
MergeArray concatenates elems onto the builder's array (json.merge! with an array argument), turning the builder into an array if it was undecided.
func (*Jbuilder) Nil ¶
func (j *Jbuilder) Nil()
Nil sets the whole target to null (json.nil! / json.null!), discarding any object or array built so far, exactly as the gem does.
func (*Jbuilder) Set ¶
Set assigns value to key (json.<key> value / json.set!(key, value)). A nil value under an active IgnoreNil is dropped, matching json.ignore_nil!. Setting a key that already exists overwrites it in place (last write wins).
func (*Jbuilder) SetKey ¶
SetKey mirrors Set for a Symbol key (json.set! :sym, value), stringifying the symbol name as Ruby does when it becomes a JSON key.
func (*Jbuilder) TargetJSON ¶
TargetJSON is an alias for Encode named after the gem's target! for hosts that bind the Ruby method name directly.
type KeyOp ¶
KeyOp is a single key_format! transform (camelize/dasherize/underscore). The gem's json.key_format! takes a chain of these; they compose left to right.
func Camelize ¶
Camelize returns the camelize KeyOp. upper selects UpperCamelCase (json.key_format! camelize: :upper); false selects lowerCamelCase (camelize: :lower), matching ActiveSupport::Inflector.camelize with the default (empty) acronym table.
func Dasherize ¶
func Dasherize() KeyOp
Dasherize returns the dasherize KeyOp (json.key_format! :dasherize): ActiveSupport's String#dasherize, which simply swaps `_` for `-`.
func Underscore ¶
func Underscore() KeyOp
Underscore returns the underscore KeyOp (json.key_format! :underscore): ActiveSupport::Inflector.underscore with the default acronym table.
