python what is the different of size when replacing json.dump by json.load
Question: ### Background:

I am working on python sdk to insert some data to couchbase.

When I tried to insert the data using the `set` method like this:

    
    
    connection = set(key, document)
    

I got exception states that the data is unicoded and it is not ascii. I solved
that problem using this:

    
    
    couchbase.set_json_converters(json.dumps, json.loads)
    

### My question:

will that solution affects the size of the data that is being inserted to
couchbase?

Plus, could you give me a little explanation about the different of
joson.dumps and json.loads. I tried to search over internet and I found that
json.dump is for encoding while json.loads is for decoding, but i didn't
understand

### After the comment

I did this:

    
    
    print "First Approach is using JSON loads instead of JSON dumps"
    import json
    import sys
    d = "roma"
    sizeBefore = sys.getsizeof(d)
    print "size of d  = {0}".format(sizeBefore)
    
    dumped = json.dumps(d)
    sizeAfter = sys.getsizeof(dumped)
    print "size with json.dumps = {0}".format(sizeAfter)
    
    loads = json.loads(dumped)
    sizeLoad = sys.getsizeof(loads)
    print "size with json.loads = {0}".format(sizeLoad)
    
    print "-------------------------"
    print "Second approach is decoding the string as UTF-8"
    sizeBeforeDecode = sys.getsizeof(d)
    print "Size before applying decode = {0}".format(sizeBeforeDecode)
    
    d=d.decode("utf-8")
    sizeAfterDecode = sys.getsizeof(d)
    print "Size after applying decode = {0}".format(sizeAfterDecode)
    

The result is:

    
    
    First Approach is using JSON loads instead of JSON dumps
    size of d  = 37
    size with json.dumps = 39
    size with json.loads = 58
    -------------------------
    Second approach is decoding the string as UTF-8
    Size before applying decode = 37
    Size after applying decode = 58
    

What does that mean please?


Answer: To answer the title question, this is like comparing apples to oranges.
`json.dumps` will convert a Python object to a JSON representation, and
`json.loads` will convert a JSON representation to a Python object. The
`set_json_converters` simply accepts a _pair_ of an encoder and decoder
function (so you basically need to set them both at the same time, even if you
only want to replace one of them).

To address your _real_ issue:

As mentioned in the original post here ([couchbase python sdk ascii
exception](http://stackoverflow.com/questions/28593263/couchbase-python-sdk-
ascii-exception)), the _proper_ solution is to ensure your input (or in this
case, its constituent key/values) are either:

  1. 7-Bit ASCII Python `str` objects
  2. Python `unicode` objects

_Ideally_ , you should ensure that any data you intend to treat as "Text"
(i.e. non-'binary' data, or data which you intend to encode into non-'binary'
formats (such as JSON)) should be converted into a `unicode` object as _early
as possible_. Unfortunately in Python2 (see below), it is very easy to
accidentally mix up the two -- often resulting in discovering problems deep in
the processing chain, rather than early on.

The `set_json_converters` "solution" is merely just a hack so that your code
can work (sub-optimally) until it is actually fixed. To fix it, you need to
properly sanitize and normalize your input. Currently your input is a
dictionary which has "byte" values which are _not_ Unicode. Yes, the specific
values in your _may_ be interpreted as valid UTF-8 sequences, but Python does
not care about this, and therefore functions which expect valid "Unicode"
inputs will fail.

I _strongly_ suggest you read <http://nedbatchelder.com/text/unipain.html> for
a very thorough, practical, and correct explanation of Unicode handling in
Python.

