forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONtoObj.c
171 lines (137 loc) · 6.13 KB
/
JSONtoObj.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
Copyright (c) 2011-2013, ESN Social Software AB and Jonas Tarnstrom
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the ESN Social Software AB nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ESN SOCIAL SOFTWARE AB OR JONAS TARNSTROM BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Portions of code from MODP_ASCII - Ascii transformations (upper/lower, etc)
https://github.com/client9/stringencoders
Copyright (c) 2007 Nick Galbreath -- nickg [at] modp [dot] com. All rights
reserved.
Numeric decoder derived from TCL library
https://www.opensource.apple.com/source/tcl/tcl-14/tcl/license.terms
* Copyright (c) 1988-1993 The Regents of the University of California.
* Copyright (c) 1994 Sun Microsystems, Inc.
*/
// Licence at LICENSES/ULTRAJSON_LICENSE
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "pandas/vendored/ujson/lib/ultrajson.h"
static int Object_objectAddKey(void *Py_UNUSED(prv), JSOBJ obj, JSOBJ name,
JSOBJ value) {
int ret = PyDict_SetItem(obj, name, value);
Py_DECREF((PyObject *)name);
Py_DECREF((PyObject *)value);
return ret == 0 ? 1 : 0;
}
static int Object_arrayAddItem(void *Py_UNUSED(prv), JSOBJ obj, JSOBJ value) {
int ret = PyList_Append(obj, value);
Py_DECREF((PyObject *)value);
return ret == 0 ? 1 : 0;
}
static JSOBJ Object_newString(void *Py_UNUSED(prv), wchar_t *start,
wchar_t *end) {
return PyUnicode_FromWideChar(start, (end - start));
}
static JSOBJ Object_newTrue(void *Py_UNUSED(prv)) { Py_RETURN_TRUE; }
static JSOBJ Object_newFalse(void *Py_UNUSED(prv)) { Py_RETURN_FALSE; }
static JSOBJ Object_newNull(void *Py_UNUSED(prv)) { Py_RETURN_NONE; }
static JSOBJ Object_newPosInf(void *Py_UNUSED(prv)) {
return PyFloat_FromDouble(Py_HUGE_VAL);
}
static JSOBJ Object_newNegInf(void *Py_UNUSED(prv)) {
return PyFloat_FromDouble(-Py_HUGE_VAL);
}
static JSOBJ Object_newObject(void *Py_UNUSED(prv), void *Py_UNUSED(decoder)) {
return PyDict_New();
}
static JSOBJ Object_endObject(void *Py_UNUSED(prv), JSOBJ obj) { return obj; }
static JSOBJ Object_newArray(void *Py_UNUSED(prv), void *Py_UNUSED(decoder)) {
return PyList_New(0);
}
static JSOBJ Object_endArray(void *Py_UNUSED(prv), JSOBJ obj) { return obj; }
static JSOBJ Object_newInteger(void *Py_UNUSED(prv), JSINT32 value) {
return PyLong_FromLong(value);
}
static JSOBJ Object_newLong(void *Py_UNUSED(prv), JSINT64 value) {
return PyLong_FromLongLong(value);
}
static JSOBJ Object_newUnsignedLong(void *Py_UNUSED(prv), JSUINT64 value) {
return PyLong_FromUnsignedLongLong(value);
}
static JSOBJ Object_newDouble(void *Py_UNUSED(prv), double value) {
return PyFloat_FromDouble(value);
}
static void Object_releaseObject(void *Py_UNUSED(prv), JSOBJ obj,
void *Py_UNUSED(decoder)) {
Py_XDECREF(((PyObject *)obj));
}
PyObject *JSONToObj(PyObject *Py_UNUSED(self), PyObject *args,
PyObject *kwargs) {
JSONObjectDecoder dec = {.newString = Object_newString,
.objectAddKey = Object_objectAddKey,
.arrayAddItem = Object_arrayAddItem,
.newTrue = Object_newTrue,
.newFalse = Object_newFalse,
.newNull = Object_newNull,
.newPosInf = Object_newPosInf,
.newNegInf = Object_newNegInf,
.newObject = Object_newObject,
.endObject = Object_endObject,
.newArray = Object_newArray,
.endArray = Object_endArray,
.newInt = Object_newInteger,
.newLong = Object_newLong,
.newUnsignedLong = Object_newUnsignedLong,
.newDouble = Object_newDouble,
.releaseObject = Object_releaseObject,
.malloc = PyObject_Malloc,
.free = PyObject_Free,
.realloc = PyObject_Realloc,
.errorStr = NULL,
.errorOffset = NULL,
.preciseFloat = 0,
.prv = NULL};
char *kwlist[] = {"obj", "precise_float", NULL};
char *buf;
Py_ssize_t len;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|b", kwlist, &buf, &len,
&dec.preciseFloat)) {
return NULL;
}
PyObject *ret = JSON_DecodeObject(&dec, buf, len);
if (PyErr_Occurred()) {
if (ret) {
Py_DECREF((PyObject *)ret);
}
return NULL;
}
if (dec.errorStr) {
/*
FIXME: It's possible to give a much nicer error message here with actual
failing element in input etc*/
PyErr_Format(PyExc_ValueError, "%s", dec.errorStr);
if (ret) {
Py_DECREF((PyObject *)ret);
}
return NULL;
}
return ret;
}