Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix loading json data #490

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

Merged
merged 1 commit into from
Jul 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions 7 pymysql/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,12 @@ def convert_set(s):
return set(s.split(","))


def convert_json(b):
# JSON is returned as binary data.
# Decode with utf-8 regardless connection encoding.
return b.decode('utf-8')


def through(x):
return x

Expand Down Expand Up @@ -410,6 +416,7 @@ def convert_characters(connection, field, data):
FIELD_TYPE.VARCHAR: through,
FIELD_TYPE.DECIMAL: Decimal,
FIELD_TYPE.NEWDECIMAL: Decimal,
FIELD_TYPE.JSON: convert_json,
}


Expand Down
35 changes: 28 additions & 7 deletions 35 pymysql/tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import pymysql.cursors

from pymysql.tests import base
from pymysql import util
from pymysql.err import ProgrammingError

import time
# coding: utf-8
import datetime
import json
import time
import warnings

from unittest2 import SkipTest

from pymysql import util
import pymysql.cursors
from pymysql.tests import base
from pymysql.err import ProgrammingError


__all__ = ["TestConversion", "TestCursor", "TestBulkInserts"]

Expand Down Expand Up @@ -238,6 +239,26 @@ def test_single_tuple(self):
self.assertEqual([(1,)], list(c.fetchall()))
c.close()

def test_json(self):
args = self.databases[0].copy()
args["charset"] = "utf8mb4"
conn = pymysql.connect(**args)
if not self.mysql_server_is(conn, (5, 7, 0)):
raise SkipTest("JSON type is not supported on MySQL <= 5.6")

self.safe_create_table(conn, "test_json", """\
create table test_json (
id int not null,
json JSON not null,
primary key (id)
);""")
cur = conn.cursor()
json_str = u'{"hello": "こんにちは"}'
cur.execute("INSERT INTO test_json (id, `json`) values (42, %s)", (json_str,))
cur.execute("SELECT `json` from `test_json` WHERE `id`=42")
res = cur.fetchone()[0]
self.assertEqual(json.loads(res), json.loads(json_str))


class TestBulkInserts(base.PyMySQLTestCase):

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.