Documentation
¶
Overview ¶
Package jsonhelpers provides helper functions for JSON marshaling and unmarshaling with support for extension fields (x-* properties) in OpenAPI specifications.
This package reduces boilerplate code in custom JSON marshal/unmarshal implementations while preserving extension fields that are not part of the OpenAPI schema.
Index ¶
- func ExtractExtensions(data []byte) map[string]any
- func MarshalWithExtras(base map[string]any, extras map[string]any) ([]byte, error)
- func SetIfMapNotEmpty[K comparable, V any](m map[string]any, key string, value map[K]V)
- func SetIfNotEmpty(m map[string]any, key string, value string)
- func SetIfNotNil(m map[string]any, key string, value any)
- func SetIfSliceNotEmpty[T any](m map[string]any, key string, value []T)
- func SetIfSliceNotNil[T any](m map[string]any, key string, value []T)
- func SetIfTrue(m map[string]any, key string, value bool)
- func SetOAS2PrimitiveFields(m map[string]any, f OAS2PrimitiveFields)
- func SetSchemaConstraints(m map[string]any, c SchemaConstraints)
- type OAS2PrimitiveFields
- type SchemaConstraints
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractExtensions ¶ added in v1.28.4
ExtractExtensions extracts specification extension fields (x-* properties) from JSON data. This is the common pattern used in all UnmarshalJSON methods to capture extension fields.
Returns nil if no extensions are found or if the data cannot be parsed. This function never returns an error - parsing failures result in nil extensions.
Performance: Uses a streaming scan to check for the "x- pattern before parsing. This avoids expensive JSON parsing when no extensions exist, which is the common case for most OpenAPI objects. Benchmarks show 2-18x speedup for objects without extensions, with no regression when extensions are present.
Example:
func (c *Contact) UnmarshalJSON(data []byte) error {
type Alias Contact
if err := json.Unmarshal(data, (*Alias)(c)); err != nil {
return err
}
c.Extra = jsonhelpers.ExtractExtensions(data)
return nil
}
func MarshalWithExtras ¶
MarshalWithExtras marshals a base map while merging in extension fields. This is used in custom MarshalJSON implementations to combine known fields with unknown extension fields (typically x-* properties).
Example:
func (s *Schema) MarshalJSON() ([]byte, error) {
base := map[string]any{
"type": s.Type,
"format": s.Format,
}
return jsonhelpers.MarshalWithExtras(base, s.Extra)
}
func SetIfMapNotEmpty ¶
func SetIfMapNotEmpty[K comparable, V any](m map[string]any, key string, value map[K]V)
SetIfMapNotEmpty sets a map field in the map only if the map has length > 0. This is useful for MarshalJSON to avoid adding empty map fields. Note: In Go, both nil maps and empty maps should be omitted from JSON output.
func SetIfNotEmpty ¶
SetIfNotEmpty sets a field in the map only if the value is not empty. This is useful for MarshalJSON to avoid adding empty fields to JSON output.
func SetIfNotNil ¶
SetIfNotNil sets a field in the map only if the value is not nil. This is useful for MarshalJSON to avoid adding nil fields to JSON output.
func SetIfSliceNotEmpty ¶
SetIfSliceNotEmpty sets a slice field in the map only if the slice has length > 0. This is useful for MarshalJSON to avoid adding empty slice fields. Note: In Go, both nil slices and empty slices should be omitted from JSON output.
func SetIfSliceNotNil ¶ added in v1.53.1
SetIfSliceNotNil sets a slice field in the map only if the slice is non-nil. Unlike SetIfSliceNotEmpty, this preserves empty (non-nil) slices, serializing them as []. This is important for fields like operation-level security where an empty slice has semantic meaning (disable inherited security requirements).
func SetIfTrue ¶
SetIfTrue sets a boolean field in the map only if the value is true. This is useful for MarshalJSON to avoid adding false boolean fields.
func SetOAS2PrimitiveFields ¶ added in v1.35.0
func SetOAS2PrimitiveFields(m map[string]any, f OAS2PrimitiveFields)
SetOAS2PrimitiveFields adds OAS 2.0 primitive type fields to a map. This is used by Parameter, Items, and Header MarshalJSON to reduce duplication. Note: For Items, Type should be set separately as a required field.
func SetSchemaConstraints ¶ added in v1.35.0
func SetSchemaConstraints(m map[string]any, c SchemaConstraints)
SetSchemaConstraints adds JSON Schema validation constraint fields to a map. This is used by Parameter, Items, and Header MarshalJSON to reduce duplication.
Types ¶
type OAS2PrimitiveFields ¶ added in v1.35.0
type OAS2PrimitiveFields struct {
Type string
Format string
Items any
CollectionFormat string
Default any
}
OAS2PrimitiveFields holds OAS 2.0 primitive type fields shared across Parameter, Items, and Header types in Swagger 2.0 specifications.
type SchemaConstraints ¶ added in v1.35.0
type SchemaConstraints struct {
Maximum *float64
ExclusiveMaximum bool
Minimum *float64
ExclusiveMinimum bool
MaxLength *int
MinLength *int
Pattern string
MaxItems *int
MinItems *int
UniqueItems bool
Enum []any
MultipleOf *float64
}
SchemaConstraints holds JSON Schema validation constraint fields. This is used for shared marshaling of constraint fields across Parameter, Items, and Header types.