Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom JSON marshal and unmarshal #2011

Merged
merged 1 commit into from
May 18, 2024
Merged

Conversation

mitar
Copy link
Contributor

@mitar mitar commented May 13, 2024

Fixes #2005.

@mitar mitar marked this pull request as draft May 13, 2024 15:37
}

pgxtest.RunValueRoundTripTests(context.Background(), t, connTestRunner, pgxtest.KnownOIDQueryExecModes, "json", []pgxtest.ValueRoundTripTest{
{[]byte(`"hello"`), new(string), isExpectedEq(`{"custom":"value"}`)},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jackc I do not know how to write this test. This does not work. It seems like Marshal is simply not called? Any insight? Maybe I should not use RunValueRoundTripTests?

@jackc
Copy link
Owner

jackc commented May 17, 2024

[]byte is sent directly without being marshaled.

func (c JSONCodec) PlanEncode(m *Map, oid uint32, format int16, value any) EncodePlan {
	switch value.(type) {
	case string:
		return encodePlanJSONCodecEitherFormatString{}
	case []byte:
		return encodePlanJSONCodecEitherFormatByteSlice{}

You probably want to test something like a map[string]any.

@mitar mitar marked this pull request as ready for review May 18, 2024 06:10
@mitar
Copy link
Contributor Author

mitar commented May 18, 2024

@jackc Thanks! This is ready for review now I think.

The Codec interface is now implemented by *pgtype.JSONCodec
and *pgtype.JSONBCodec instead of pgtype.JSONCodec and
pgtype.JSONBCodec, respectively. This is technically a breaking
change, but it is extremely unlikely that anyone is depending on this,
and if there is downstream breakage it is trivial to fix.

Fixes jackc#2005.
@jackc jackc merged commit 7328897 into jackc:master May 18, 2024
14 checks passed
@jackc
Copy link
Owner

jackc commented May 18, 2024

LGTM

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.

Custom JSON marshal/unmarshal
2 participants