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

Serialize ''(empty) string to empty database value #66

Closed
wants to merge 4 commits into from

Conversation

Romamo
Copy link

@Romamo Romamo commented Jan 27, 2014

Uses ""(empty) string in database to store empty json ([]).

You can use empty value as default for your table fields.
You will have no overhead in database if you have no data.

@Romamo
Copy link
Author

Romamo commented Jan 28, 2014

Also you can use empty string as 'no value' state because of avoiding NULL in MySQL (http://dev.mysql.com/doc/refman/5.6/en/data-size.html)

@bradjasper
Copy link
Collaborator

Hey @Romamo thanks for the pull request (and sorry for the delay)—I'm trying to reproduce this on my end but having trouble. Could you send over a test case that might help me debug this?

Here's what I've got, but I'm not sure I'm capturing the issue:

class JsonModel(models.Model):
     empty_default_json = JSONField(default="")


def test_default_empty_json(self):
    """Make sure you can set JSON to empty string"""

    model = JsonModel()
    model.save()
    self.assertEqual(model.empty_default_json, "")

    model.empty_default_json = "hello"
    model.save()
    self.assertEqual(model.empty_default_json, "hello")

@bradjasper
Copy link
Collaborator

Hey @Romamo feel free to re-open this if you can help come up with a test case I can add. We've had a lot of edge-case regressions with empty strings (""), so I don't want to make this change without one. If you can help me with the test case that demonstrates this issue, I should be able to get this fixed.

@bradjasper bradjasper closed this May 26, 2014
@Romamo
Copy link
Author

Romamo commented May 27, 2014

My patch supports stored empty string in database (not "[]").
Without this empty string in database causes ValueError: No JSON object could be decoded.

Storing "empty"(sure []) JSON will be saved as empty zero-length string in database.

I think this is more friendly to use, to create new fields and saves some disk space and performance.

@bradjasper
Copy link
Collaborator

@Romamo I'm working my way through some of the high priority issues and do plan on exploring this more—however this needs to be tested really well (specifically because we've had a bunch of bugs around an empty JSON object being None/""/{}/[]/etc...)

I plan on addressing this but the tests + research are the limiting factor. Any help there is appreciated and will help this get fixed sooner. Thanks!

@bradjasper bradjasper reopened this May 27, 2014
@Romamo
Copy link
Author

Romamo commented May 28, 2014

First you must define what is empty JSON object. May be empty == default ?

json = JSONField(default='""')

So, '""' will be stored in DB as empty zero-sized string.

[] is empty but not default and will be stored as [].

@edwardotis
Copy link

Hey guys, I'm getting burned by this bug right now o n Postgres 9.3.
Looks like there's a merge conflict when trying to submit the pull request? I'd love to see this fix get merged in to the main pip project.
I'll have to pull directly from this fork until then.
Thanks.

@bradjasper
Copy link
Collaborator

A test would really help speed this along. I'm slammed right now so just haven't been able to get to it—and last time testing/repeating the bug was the blocker. If someone can put a failing case together I'll be able to get this knocked out sooner.

@tutuca
Copy link

tutuca commented Sep 3, 2014

@bradjasper this package is quite helpful so it's no wonder it's popularity has exploded.
If you are not able to keep track of the development the wisest thing to do is to grant commit write access to some collaborator in order to better maintain the project.

@bradjasper
Copy link
Collaborator

Happy to add more collaborators if anyone is interested. Are you volunteering @tutuca? :)

koriaf pushed a commit to koriaf/django-jsonfield that referenced this pull request Jun 26, 2016
@dmkoch
Copy link
Collaborator

dmkoch commented Mar 5, 2017

Please reopen this pull request with an updated diff against version 2.0.0 if there is still interest.

@dmkoch dmkoch closed this Mar 5, 2017
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.

5 participants