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

json Marshaller interface for Inet #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

atz
Copy link

@atz atz commented Dec 2, 2020

Note that this does allow the serialization/deserialization between empty string and a Null struct. It does NOT permit invalid addresses or masks.

If this is workable, then I would pursue similar approach for Cidr and Macaddr types.

See #79

Note that this does allow the serialization/deserialization between
empty string and a Null struct. It does NOT permit invalid addresses or masks.

See jackc#79
@atz atz force-pushed the topic/atz/79-inet_json_marshaller branch from c7106ba to 3a5618b Compare December 2, 2020 00:25
@jackc
Copy link
Owner

jackc commented Dec 2, 2020

Nowhere else in pgtype is an empty string conflated with null. null is a valid JSON value -- we should use it. Also undefined should be handled.

See Bool MarshalJSON and UnmarshalJSON for examples of existing precedent.

@atz
Copy link
Author

atz commented Dec 2, 2020

null is expressible in JSON, but it is not valid under any json-schema that would specify a document with this type, therefore should not be produced as output. Either the JSON representation is a string or it isn't, but it cannot be sometimes a string and sometimes a null (and still work under json-schema).

Outside of the psql DB, I'm not sure what perspective would suggest that there is any difference between two different states where there is no address. Attempting to insert an empty string will fail with:

ERROR:  invalid input syntax for type inet: ""

Whereas attempting to insert a null will succeed (if the field allows null values, obviously).

AFAICT, the options for incoming json empty string are:

  • treat as null
  • return error

@jackc
Copy link
Owner

jackc commented Dec 2, 2020

I don't know about anything about "json-schema" but if I have a SQL NULL and I marshal it to JSON I certainly expect it to be null not "".

An incoming JSON empty string should be an error.

@atz
Copy link
Author

atz commented Dec 2, 2020

I'm OK w/ returning error. I think having two different serialization types in JSON (a string type and a null type) is fundamentally incorrect, but as long as omitempty still works, my applications could work around it (i.e. never see a null serialization).

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.

2 participants