Re: Why serializing to JSON doesn't work?

2009-03-06 Thread Jeff FW

Keep the way you're serializing the querysets, add each one to a
dictionary, and then:

return dict((k, simplejson.dumps(v)) for k, v in ret_val.iteritems())

-Jeff

On Mar 6, 10:49 am, Marek Wawrzyczek  wrote:
> Thanks for your responses, they helped :)
>
> In the code below:
>
>        ret['b'] = State.objects.all()
>             print 'ret: %' % ret
>             try:
>                 serialized = encoder.JSONEncoder().encode(ret)
>             except Exception, e:
>                 print 'exception: %s' % e
>
> I was doing the following mistake - I wanted to serialize query set in a
> dictionary directly, and the exception was thrown, I think because
> encoder.JSONEncoder() can't encode a QuerySet.
>
> While using code like this:
>
>          ret = State.objects.all()
>          json_serializer = serializers.get_serializer("json")()
>          serialized_model = json_serializer.serialize(ret, ensure_ascii = 
> False)
>          ret_val['b'] = serialized_model
>          serialized = encoder.JSONEncoder().encode(ret_val)
>
> then it doesn't throw exception, but code above just creates a
> serialized dicionary of serialized model, so when I try to decode it
> with JSON parser on a page, i get a dictionary of strings. I can create
> a JSON response manually by:
>
>     serialized = '{"b":%s}'% serialized_model
>
> but it's a little annoying while serializing a few models.
>
> Is there any way to serialize dictionary of QuerySets without creating
> json response manually?
>
> Regards,
> Marek
>
> > You know what's weird?  I've used simplejson.dumps() plenty of times
> > in my own code... not sure why that one just slipped out of my
> > memory.  I should just stop responding to things  :-)
>
> > Anyway, since you're serializing a model, you *should* be using your
> > originally posted method.  Use the way Marek said for anything other
> > than models (of course, the object has to be serializable.)
>
> > If that doesn't work, post the full (relevant) code.
>
> > -Jeff
>
> > On Mar 5, 7:01 am, Marek Wawrzyczek  wrote:
>
> >> > Thomas Guettler wrote:
>
> >>> > > Jeff FW schrieb:
>
>  > >> The serializers are for serializing querysets/models.  I'm surprised
>  > >> you're not getting an error message there--are you catching all
>  > >> exceptions?
>
>  > >> What you want is in django.utils.simplejson:
>
>  > >> from django.utils.simplejson import encoder
>  > >> encoder.JSONEncoder().encode(ret)
>
> >>> > > Or this:
>
> >> >  from django.utils import simplejson
> >> >  simplejson.dumps(...)
>
> >>> > > But unfortunately this does not encode datetime objects.
>
> >>> > >   Thomas
>
> >> > Thanks for your responses. Now when I try
>
> >> >      ret = { }
> >> >      ret['a'] = 'b'
> >> >      serialized = encoder.JSONEncoder().encode(ret)
> >> >      print serialized
>
> >> > then It works.
>
> >> > But now I have another problem. I have a class "State":
>
> >> > class State(models.Model):
> >> >     state = models.CharField(max_length = 30)
>
> >> >     def __unicode__(self):
> >> >         return self.state
>
> >> > Throutht the admin page I create a state called "Slaskie".      
> >> > Then the code :
>
> >> >             ret['b'] = State.objects.all()
> >> >             print 'ret: %' % ret
> >> >             try:
> >> >                 serialized = encoder.JSONEncoder().encode(ret)
> >> >             except Exception, e:
> >> >                 print 'exception: %s' % e
>
> >> > returns the output:
>
> >> >     {'b': []}
> >> >     [] is not JSON serializable
>
> >> > At the 
> >> > pagehttp://docs.djangoproject.com/en/dev/topics/serialization/#id2there's
> >> > written something about unicode and lazy translation. I tried to use
>
> >> >     le = LazyEncoder()       #lazy encoder is a given class from the
> >> > link above
> >> >     serialized = le.encode(ret)
>
> >> > and then the exception was:
> >> >     Circular reference detected
>
> >> > when I tried
>
> >> >     le = LazyEncoder (ensure_ascii = False)
>
> >> > the exception was the same:
> >> >     Circular reference detected
>
> >> > What's going on with this lazy translation and unicode ? How can I
> >> > serialize the data correctly ?
>
> >> > Regards,
> >> > Marek
>
>
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Why serializing to JSON doesn't work?

2009-03-06 Thread Marek Wawrzyczek

Thanks for your responses, they helped :)

In the code below:

   ret['b'] = State.objects.all()
print 'ret: %' % ret
try:
serialized = encoder.JSONEncoder().encode(ret)
except Exception, e:
print 'exception: %s' % e

I was doing the following mistake - I wanted to serialize query set in a
dictionary directly, and the exception was thrown, I think because
encoder.JSONEncoder() can't encode a QuerySet.

While using code like this:

 ret = State.objects.all()
 json_serializer = serializers.get_serializer("json")()
 serialized_model = json_serializer.serialize(ret, ensure_ascii = False)
 ret_val['b'] = serialized_model
 serialized = encoder.JSONEncoder().encode(ret_val)

then it doesn't throw exception, but code above just creates a
serialized dicionary of serialized model, so when I try to decode it
with JSON parser on a page, i get a dictionary of strings. I can create
a JSON response manually by:

serialized = '{"b":%s}'% serialized_model

but it's a little annoying while serializing a few models.

Is there any way to serialize dictionary of QuerySets without creating
json response manually?

Regards,
Marek

> You know what's weird?  I've used simplejson.dumps() plenty of times
> in my own code... not sure why that one just slipped out of my
> memory.  I should just stop responding to things  :-) 
>
> Anyway, since you're serializing a model, you *should* be using your
> originally posted method.  Use the way Marek said for anything other
> than models (of course, the object has to be serializable.)
>
> If that doesn't work, post the full (relevant) code.
>
> -Jeff
>
> On Mar 5, 7:01 am, Marek Wawrzyczek  wrote:
>   
>> > Thomas Guettler wrote:
>> >
>> 
>>> > > Jeff FW schrieb:
>>>   
>> >
>> 
 > >> The serializers are for serializing querysets/models.  I'm surprised
 > >> you're not getting an error message there--are you catching all
 > >> exceptions?
 
>> >
>> 
 > >> What you want is in django.utils.simplejson:
 
>> >
>> 
 > >> from django.utils.simplejson import encoder
 > >> encoder.JSONEncoder().encode(ret)
 
>> >
>> 
>>> > > Or this:
>>>   
>> >
>> 
>> >  from django.utils import simplejson
>> >  simplejson.dumps(...)
>> 
>> >
>> 
>>> > > But unfortunately this does not encode datetime objects.
>>>   
>> >
>> 
>>> > >   Thomas
>>>   
>> >
>> > Thanks for your responses. Now when I try
>> >
>> >  ret = { }
>> >  ret['a'] = 'b'
>> >  serialized = encoder.JSONEncoder().encode(ret)
>> >  print serialized
>> >
>> > then It works.
>> >
>> > But now I have another problem. I have a class "State":
>> >
>> > class State(models.Model):
>> > state = models.CharField(max_length = 30)
>> >
>> > def __unicode__(self):
>> > return self.state
>> >
>> > Throutht the admin page I create a state called "Slaskie".  
>> > Then the code :
>> >
>> > ret['b'] = State.objects.all()
>> > print 'ret: %' % ret
>> > try:
>> > serialized = encoder.JSONEncoder().encode(ret)
>> > except Exception, e:
>> > print 'exception: %s' % e
>> >
>> > returns the output:
>> >
>> > {'b': []}
>> > [] is not JSON serializable
>> >
>> > At the 
>> > pagehttp://docs.djangoproject.com/en/dev/topics/serialization/#id2there's
>> > written something about unicode and lazy translation. I tried to use
>> >
>> > le = LazyEncoder()   #lazy encoder is a given class from the
>> > link above
>> > serialized = le.encode(ret)
>> >
>> > and then the exception was:
>> > Circular reference detected
>> >
>> > when I tried
>> >
>> > le = LazyEncoder (ensure_ascii = False)
>> >
>> > the exception was the same:
>> > Circular reference detected
>> >
>> > What's going on with this lazy translation and unicode ? How can I
>> > serialize the data correctly ?
>> >
>> > Regards,
>> > Marek
>> 
> >
>
>   


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Why serializing to JSON doesn't work?

2009-03-05 Thread Jeff FW

You know what's weird?  I've used simplejson.dumps() plenty of times
in my own code... not sure why that one just slipped out of my
memory.  I should just stop responding to things :-)

Anyway, since you're serializing a model, you *should* be using your
originally posted method.  Use the way Marek said for anything other
than models (of course, the object has to be serializable.)

If that doesn't work, post the full (relevant) code.

-Jeff

On Mar 5, 7:01 am, Marek Wawrzyczek  wrote:
> Thomas Guettler wrote:
>
> > Jeff FW schrieb:
>
> >> The serializers are for serializing querysets/models.  I'm surprised
> >> you're not getting an error message there--are you catching all
> >> exceptions?
>
> >> What you want is in django.utils.simplejson:
>
> >> from django.utils.simplejson import encoder
> >> encoder.JSONEncoder().encode(ret)
>
> > Or this:
>
>  from django.utils import simplejson
>  simplejson.dumps(...)
>
> > But unfortunately this does not encode datetime objects.
>
> >   Thomas
>
> Thanks for your responses. Now when I try
>
>      ret = { }
>      ret['a'] = 'b'
>      serialized = encoder.JSONEncoder().encode(ret)
>      print serialized
>
> then It works.
>
> But now I have another problem. I have a class "State":
>
> class State(models.Model):
>     state = models.CharField(max_length = 30)
>
>     def __unicode__(self):
>         return self.state
>
> Throutht the admin page I create a state called "Slaskie".      
> Then the code :
>
>             ret['b'] = State.objects.all()
>             print 'ret: %' % ret
>             try:
>                 serialized = encoder.JSONEncoder().encode(ret)
>             except Exception, e:
>                 print 'exception: %s' % e
>
> returns the output:
>
>     {'b': []}
>     [] is not JSON serializable
>
> At the 
> pagehttp://docs.djangoproject.com/en/dev/topics/serialization/#id2there's
> written something about unicode and lazy translation. I tried to use
>
>     le = LazyEncoder()       #lazy encoder is a given class from the
> link above
>     serialized = le.encode(ret)
>
> and then the exception was:
>     Circular reference detected
>
> when I tried
>
>     le = LazyEncoder (ensure_ascii = False)
>
> the exception was the same:
>     Circular reference detected
>
> What's going on with this lazy translation and unicode ? How can I
> serialize the data correctly ?
>
> Regards,
> Marek
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Why serializing to JSON doesn't work?

2009-03-05 Thread Marek Wawrzyczek

Thomas Guettler wrote:
>
> Jeff FW schrieb:
>   
>> The serializers are for serializing querysets/models.  I'm surprised
>> you're not getting an error message there--are you catching all
>> exceptions?
>>
>> What you want is in django.utils.simplejson:
>>
>> from django.utils.simplejson import encoder
>> encoder.JSONEncoder().encode(ret)
>> 
>
> Or this:
>
>   
 from django.utils import simplejson
 simplejson.dumps(...)
 
>
> But unfortunately this does not encode datetime objects.
>
>   Thomas
>
>
>   

Thanks for your responses. Now when I try

 ret = { }
 ret['a'] = 'b'
 serialized = encoder.JSONEncoder().encode(ret)
 print serialized

then It works.

But now I have another problem. I have a class "State":

class State(models.Model):
state = models.CharField(max_length = 30)

def __unicode__(self):
return self.state

Throutht the admin page I create a state called "Slaskie".  
Then the code :

ret['b'] = State.objects.all()
print 'ret: %' % ret
try:
serialized = encoder.JSONEncoder().encode(ret)
except Exception, e:
print 'exception: %s' % e

returns the output:

{'b': []}
[] is not JSON serializable

At the page 
http://docs.djangoproject.com/en/dev/topics/serialization/#id2 there's 
written something about unicode and lazy translation. I tried to use

le = LazyEncoder()   #lazy encoder is a given class from the 
link above
serialized = le.encode(ret)

and then the exception was:
Circular reference detected

when I tried

le = LazyEncoder (ensure_ascii = False)

the exception was the same:
Circular reference detected

What's going on with this lazy translation and unicode ? How can I 
serialize the data correctly ?


Regards,
Marek

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Why serializing to JSON doesn't work?

2009-03-04 Thread Thomas Guettler



Jeff FW schrieb:
> The serializers are for serializing querysets/models.  I'm surprised
> you're not getting an error message there--are you catching all
> exceptions?
> 
> What you want is in django.utils.simplejson:
> 
> from django.utils.simplejson import encoder
> encoder.JSONEncoder().encode(ret)

Or this:

>>> from django.utils import simplejson
>>> simplejson.dumps(...)

But unfortunately this does not encode datetime objects.

  Thomas


-- 
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Why serializing to JSON doesn't work?

2009-03-04 Thread Jeff FW

The serializers are for serializing querysets/models.  I'm surprised
you're not getting an error message there--are you catching all
exceptions?

What you want is in django.utils.simplejson:

from django.utils.simplejson import encoder
encoder.JSONEncoder().encode(ret)

-Jeff

On Mar 4, 6:55 pm, Marek Wawrzyczek  wrote:
> Hello,
>
> I've got the code like this:
>
>     from django.core import serializers
>     ...
>
>     ret = { }
>     ret['a'] = 'b'
>     json_serializer = serializers.get_serializer("json")()
>     serialized = json_serializer.serialize(ret, ensure_ascii=False)
>     print serialized
>
> and this doesn't print serialized. What do I do wrong ?
> I'm using Python 2.5.2, Django 1.0.2.
>
> Thanks in advance,
> Marek
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---