Repository navigation
Add YAML deserialization and serialization support - #173
Closed
AdamMagued wants to merge 1 commit into
Closed
AdamMagued wants to merge 1 commit into
AdamMagued wants to merge 1 commit into
Conversation
Implement FromYAML, MustFromYAML, FromYAMLSlice, MustFromYAMLSlice, and Map.YAML/MustYAML methods (with FromYaml and Map.Yaml aliases) using gopkg.in/yaml.v3. Also extend cleanUp to handle nested map[string]interface{} and []map[interface{}]interface{}.
hanzei
requested changes
Oct 13, 2026
hanzei
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for the PR, @AdamMagued! We're going to decline adding YAML support to objx itself. Keeping objx free of dependencies is intentional (we removed testify in #159 for the same reason). Adding gopkg.in/yaml.v3 would make every objx user pull it in, including testify, and yaml.v3 is no longer maintained.
You can already do this without new APIs:
var m map[string]interface{}
err := yaml.Unmarshal(data, &m)
o := objx.New(m)The one real gap is nested maps with non-string keys (map[interface{}]interface{}), which objx.New can't traverse. A dependency-free fix for that would be welcome. For example, the cleanUp changes from this PR on their own, with tests, so they work no matter which decoder produced the data.
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #135
Objx provides conversion constructors and serialization methods for JSON (
FromJSON,MustFromJSON,FromJSONSlice,MustFromJSONSlice,Map.JSON,Map.MustJSON), Base64, and URL Query strings, but lacked built-in support for YAML documents. Users working with YAML configurations had to manually parse YAML documents outside objx.This PR adds YAML deserialization and serialization support:
FromYAML(yamlString string) (Map, error)andMustFromYAML(yamlString string) MapFromYAMLSlice(yamlString string) ([]Map, error)andMustFromYAMLSlice(yamlString string) []MapMap.YAML() (string, error)andMap.MustYAML() stringFromYaml,MustFromYaml,FromYamlSlice,MustFromYamlSlice,Map.Yaml, andMap.MustYamlmap[string]interface{}and[]map[interface{}]interface{}in recursivecleanUpVerification
map_test.goverifyingFromYAML,MustFromYAML, aliases, error handling, nested YAML mappings, and top-level YAML slices.conversions_test.goverifyingMap.YAML(),MustYAML(), aliases, nested maps with interface keys, and error conditions.go test -race ./...(100% passed).gofmt -d -s *.goandgo vet ./...(clean output).