Skip to content
Snippets Groups Projects
Commit 2bf72d24a65c authored by Bob Ippolito's avatar Bob Ippolito
Browse files

Merge branch 'raw_json' of https://github.com/lamflam/simplejson into lamflam-raw_json

No related branches found
No related tags found
No related merge requests found
......@@ -110,7 +110,7 @@
from .scanner import JSONDecodeError
from .decoder import JSONDecoder
from .encoder import JSONEncoder, JSONEncoderForHTML
from .encoder import JSONEncoder, JSONEncoderForHTML, RawJSON
def _import_OrderedDict():
import collections
try:
......
......@@ -245,6 +245,8 @@
encoder_dealloc(PyObject *self);
static int
encoder_clear(PyObject *self);
static int
is_raw_json(PyObject *obj);
static PyObject *
encoder_stringify_key(PyEncoderObject *s, PyObject *key);
static int
......@@ -277,6 +279,20 @@
#define MIN_EXPANSION 6
static PyObject* RawJSONType;
static int
is_raw_json(PyObject *obj)
{
if (RawJSONType == NULL) {
PyObject *encoder_module = PyImport_ImportModule("simplejson.encoder");
RawJSONType = PyObject_GetAttrString(encoder_module, "RawJSON");
Py_DECREF(encoder_module);
if (RawJSONType == NULL)
return 0;
}
return PyObject_IsInstance(obj, RawJSONType) ? 1 : 0;
}
static int
JSON_Accu_Init(JSON_Accu *acc)
{
......@@ -2868,6 +2884,12 @@
if (encoded != NULL)
rv = _steal_accumulate(rval, encoded);
}
else if (is_raw_json(obj))
{
PyObject *encoded = PyObject_GetAttrString(obj, "encoded_json");
if (encoded != NULL)
rv = _steal_accumulate(rval, encoded);
}
else if (PyInt_Check(obj) || PyLong_Check(obj)) {
PyObject *encoded;
if (PyInt_CheckExact(obj) || PyLong_CheckExact(obj)) {
......
......@@ -39,6 +39,10 @@
FLOAT_REPR = repr
class RawJSON(object):
def __init__(self, encoded_json):
self.encoded_json = encoded_json
def encode_basestring(s, _PY3=PY3, _q=u('"')):
"""Return a JSON representation of a Python string
......@@ -472,6 +476,8 @@
if (isinstance(value, string_types) or
(_PY3 and isinstance(value, binary_type))):
yield buf + _encoder(value)
elif isinstance(value, RawJSON):
yield buf + value.encoded_json
elif value is None:
yield buf + 'null'
elif value is True:
......@@ -590,6 +596,8 @@
if (isinstance(value, string_types) or
(_PY3 and isinstance(value, binary_type))):
yield _encoder(value)
elif isinstance(value, RawJSON):
yield value.encoded_json
elif value is None:
yield 'null'
elif value is True:
......@@ -632,6 +640,8 @@
if (isinstance(o, string_types) or
(_PY3 and isinstance(o, binary_type))):
yield _encoder(o)
elif isinstance(o, RawJSON):
buf + o.encoded_json
elif o is None:
yield 'null'
elif o is True:
......
import unittest
import simplejson as json
dct1 = {
'key1': 'value1'
}
dct2 = {
'key2': 'value2',
'd1': dct1
}
dct3 = {
'key2': 'value2',
'd1': json.dumps(dct1)
}
dct4 = {
'key2': 'value2',
'd1': json.RawJSON(json.dumps(dct1))
}
class TestRawJson(unittest.TestCase):
def test_normal_str(self):
self.assertNotEqual(json.dumps(dct2), json.dumps(dct3))
def test_raw_json_str(self):
self.assertEqual(json.dumps(dct2), json.dumps(dct4))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment