Skip to content

Add support for RawMessage, similar to json.RawMessage#790

Merged
goccy merged 2 commits intogoccy:masterfrom
thanethomson:thane/425-rawmessage
Nov 29, 2025
Merged

Add support for RawMessage, similar to json.RawMessage#790
goccy merged 2 commits intogoccy:masterfrom
thanethomson:thane/425-rawmessage

Conversation

@thanethomson
Copy link
Copy Markdown
Contributor

@thanethomson thanethomson commented Sep 6, 2025

Builds on #668, since that PR seems stale and I'd like to make use of this feature.

@goccy I've attempted to add the test cases you requested in #668 (comment) - do these work for you?

Let me know if there's anything else I should add.

Closes #425.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Sep 6, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 46.66667% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.91%. Comparing base (25e5d90) to head (4aa949a).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #790      +/-   ##
==========================================
- Coverage   77.97%   77.91%   -0.06%     
==========================================
  Files          22       22              
  Lines        8108     8123      +15     
==========================================
+ Hits         6322     6329       +7     
- Misses       1370     1376       +6     
- Partials      416      418       +2     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

cpuguy83 and others added 2 commits September 6, 2025 09:09
This adds a type that users can use to refer to raw data in the yaml for
deferred decoding.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for yaml.RawMessage, a type similar to json.RawMessage from the standard library. It enables delayed YAML decoding or precomputed YAML encoding, with JSON interoperability through additional marshal/unmarshal methods.

Key Changes

  • Implemented RawMessage type with YAML and JSON marshaling/unmarshaling support
  • Added comprehensive test coverage including JSON compatibility scenarios
  • Minor style improvements (var to := syntax) in existing tests

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
yaml.go Adds RawMessage type with MarshalYAML, UnmarshalYAML, MarshalJSON, and UnmarshalJSON methods for bidirectional YAML/JSON compatibility
yaml_test.go Adds comprehensive test coverage including TestRawMessage for basic functionality and TestRawMessageJSONCompatibility for JSON interoperability scenarios

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread yaml.go
return m.UnmarshalYAML(b)
}

func (m RawMessage) MarshalJSON() ([]byte, error) {
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MarshalJSON method doesn't handle the nil case explicitly, unlike MarshalYAML which returns []byte("null") for nil values. When m is nil, YAMLToJSON(nil) will be called which may not produce the expected "null" JSON value. Consider adding a nil check:

func (m RawMessage) MarshalJSON() ([]byte, error) {
	if m == nil {
		return []byte("null"), nil
	}
	return YAMLToJSON(m)
}

This ensures consistency with MarshalYAML and matches the behavior of json.RawMessage in the standard library.

Suggested change
func (m RawMessage) MarshalJSON() ([]byte, error) {
func (m RawMessage) MarshalJSON() ([]byte, error) {
if m == nil {
return []byte("null"), nil
}

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok, In my understand, YAMLToJSON handles nil correctly.

@goccy
Copy link
Copy Markdown
Owner

goccy commented Nov 29, 2025

@thanethomson (cc: @cpuguy83 ) Thank you for your contribution !!! LGTM 👍

@goccy goccy merged commit a7b4bfb into goccy:master Nov 29, 2025
31 checks passed
Madoxen pushed a commit to barney-ci/go-yaml that referenced this pull request Jan 27, 2026
* Add support for RawMessage, similar to json.RawMessage

This adds a type that users can use to refer to raw data in the yaml for
deferred decoding.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>

* test: add suggested cases from goccy#668

---------

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
Co-authored-by: Brian Goff <cpuguy83@gmail.com>
wrouesnel pushed a commit to wrouesnel/go-yaml that referenced this pull request Feb 12, 2026
* Add support for RawMessage, similar to json.RawMessage

This adds a type that users can use to refer to raw data in the yaml for
deferred decoding.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>

* test: add suggested cases from goccy#668

---------

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
Co-authored-by: Brian Goff <cpuguy83@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle json.RawMessage or introduce a similar type

5 participants