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 NoneType for (de)serializing None options for other schematypes #121

Closed
wants to merge 5 commits into from
Closed

Conversation

mrijken
Copy link
Contributor

@mrijken mrijken commented Jul 30, 2013

In my apps, I have Colander schemas with optional fields for which None is needed to store the absence of a value in ie a SQL database. For that, the colander.null value can not be used. So I made a supertype which can embrace every other schematype to add the posibility to add None as value.

Example:

typ = NoneType(Boolean())
serialize True --> 'true'
serialize None --> ''
deserialize '' --> None
deserlialize 'true' --> True

@claytron
Copy link
Contributor

Can you update the sphinx docs with more information about this new feature?

@mrijken
Copy link
Contributor Author

mrijken commented Aug 24, 2013

I have added some documentation.

@romanlevin
Copy link

@mrijken, you've just saved me a couple of hours of having to implement this myself.

@iksteen
Copy link

iksteen commented Oct 16, 2013

Any updates on this pull request? I'm looking into migrating one of our projects to use colander 1.0b1, but I need some integers to have an optional None value. I'd rather update the schemas than update all the views.

without converting `colander.null` to None outside the schema. In other words, you have defined in the
schema that the node can have None as value.

Note: NoneType can be used for strings, because the machinery does not known whether a serialization of `""` is
Copy link
Member

Choose a reason for hiding this comment

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

Should that read "cannot be used for strings"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right. I should read it twice before committing.

2013/10/16 Tres Seaver notifications@github.com

In docs/null.rst:

  • assert deserialized['age'] == None
  • deserialized = schema.deserialize({'name':'Fred', 'age': '19'})
  • assert deserialized['age'] == 19
  • serialized = schema.serialize({'name':'Fred', 'age': None})
  • assert serialized['age'] == ''
  • serialized = schema.serialize({'name':'Fred', 'age': 19})
  • assert serialized['age'] == '19'

+When you use colander.NoneType you can pass the results of the deserialization directly to a database,
+without converting colander.null to None outside the schema. In other words, you have defined in the
+schema that the node can have None as value.
+
+Note: NoneType can be used for strings, because the machinery does not known whether a serialization of "" is

Should that read "cannot be used for strings"?


Reply to this email directly or view it on GitHubhttps://github.com//pull/121/files#r7001230
.

@sq9mev
Copy link

sq9mev commented Nov 13, 2013

Any news about this pull request? This seems very useful for me…

The None Value
==============

Every colander SchemaType will only (de)serialize a value which belongs to its type.
Copy link
Member

Choose a reason for hiding this comment

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

For some reason SchemaType is not actually a public API, so it probably shouldn't be mentioned here.

@mmerickel
Copy link
Member

Basically every type is supposed to handle colander.null, so I think you'll want to take care of that. I'm not a fan of handling the empty string as None in this implementation. I guess you're trying to keep it symmetric in colander's silly I-only-serialize-to-strings sort of way right now. I'd expect to see something closer to if cstruct is colander.null or cstruct is None: return None and no other special cases.

@tseaver
Copy link
Member

tseaver commented Nov 26, 2014

Thank you for the patch. Can you please:

  • Merge with master (or rebase) to allow us to review / merge your changes cleanly.
  • Add an entry to CHANGES.rst.
  • Add your name to CONTRIBUTORS.txt.

@delijati
Copy link

delijati commented Apr 1, 2016

updated in #258

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.

8 participants