Skip to content

Commit 8cabf61

Browse files
committed
Improvement.
1 parent 34fed91 commit 8cabf61

5 files changed

Lines changed: 26 additions & 28 deletions

File tree

python-jsonic.c

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ PyJsonic_JsonicType_dealloc(
5151
) {
5252
jsonic_free_addr(self->node);
5353
jsonic_free_addr(self->from);
54-
free(self->json);
54+
Py_CLEAR(self->json);
5555
Py_TYPE(self)->tp_free((PyObject *) self);
5656
}
5757

@@ -61,18 +61,16 @@ PyJsonic_JsonicType_init(
6161
PyObject *args,
6262
PyObject *kwds
6363
) {
64-
char* json;
6564
int is_root = 1;
66-
if (!PyArg_ParseTuple(args, "s|i", &json, &is_root)) {
65+
if (!PyArg_ParseTuple(args, "U|i", &self->json, &is_root)) {
6766
return NULL;
6867
}
6968

70-
self->attr_json = PyUnicode_FromString(json);
71-
self->json = malloc(strlen(json)+1);
72-
strcpy(self->json, json);
69+
Py_INCREF(self->json);
70+
self->json_ascii = PyBytes_AS_STRING(PyUnicode_AsEncodedString(self->json, "utf-8", "asdasdads"));
7371

7472
if (is_root) {
75-
self->node = jsonic_get_root(self->json);
73+
self->node = jsonic_get_root(self->json_ascii);
7674
self->from = NULL;
7775
self->json_type = self->node->type;
7876
} else {
@@ -125,7 +123,7 @@ PyJsonic_JsonicType_key(
125123
}
126124

127125
jsonic_node_t* node = jsonic_object_get(
128-
self->json,
126+
self->json_ascii,
129127
self->node,
130128
key
131129
);
@@ -139,7 +137,7 @@ PyJsonic_JsonicType_key(
139137
if (node->type == JSONIC_NODE_TYPE_OBJECT) {
140138
pyNode = PyObject_CallObject(
141139
(PyObject *) &PyJsonic_JsonicType,
142-
Py_BuildValue("(si)", self->json, 0)
140+
Py_BuildValue("(si)", self->json_ascii, 0)
143141
);
144142

145143
((PyJsonic_JsonicObject *) pyNode)->node = node;
@@ -149,7 +147,7 @@ PyJsonic_JsonicType_key(
149147
} else if (node->type == JSONIC_NODE_TYPE_ARRAY) {
150148
pyNode = PyObject_CallObject(
151149
(PyObject *) &PyJsonic_JsonicType,
152-
Py_BuildValue("(si)", self->json, 0)
150+
Py_BuildValue("(si)", self->json_ascii, 0)
153151
);
154152

155153
((PyJsonic_JsonicObject *) pyNode)->node = node;
@@ -181,7 +179,7 @@ PyJsonic_JsonicType_item(
181179
}
182180

183181
jsonic_node_t* node = jsonic_array_get(
184-
self->json,
182+
self->json_ascii,
185183
self->node,
186184
index
187185
);
@@ -195,7 +193,7 @@ PyJsonic_JsonicType_item(
195193
if (node->type == JSONIC_NODE_TYPE_OBJECT) {
196194
pyNode = PyObject_CallObject(
197195
(PyObject *) &PyJsonic_JsonicType,
198-
Py_BuildValue("(s)", self->json)
196+
Py_BuildValue("(s)", self->json_ascii)
199197
);
200198

201199
((PyJsonic_JsonicObject *) pyNode)->node = node;
@@ -204,7 +202,7 @@ PyJsonic_JsonicType_item(
204202
} else if (node->type == JSONIC_NODE_TYPE_ARRAY) {
205203
pyNode = PyObject_CallObject(
206204
(PyObject *) &PyJsonic_JsonicType,
207-
Py_BuildValue("(s)", self->json)
205+
Py_BuildValue("(s)", self->json_ascii)
208206
);
209207

210208
((PyJsonic_JsonicObject *) pyNode)->node = node;
@@ -230,7 +228,7 @@ PyJsonic_JsonicType_len(
230228
PyObject *kwds
231229
) {
232230
unsigned int length = jsonic_array_length(
233-
self->json,
231+
self->json_ascii,
234232
self->node
235233
);
236234

@@ -249,7 +247,7 @@ PyJsonic_JsonicType_iterItem(
249247
}
250248

251249
jsonic_node_t* node = jsonic_array_iter(
252-
self->json,
250+
self->json_ascii,
253251
self->node,
254252
self->from,
255253
index
@@ -267,7 +265,7 @@ PyJsonic_JsonicType_iterItem(
267265
if (node->type == JSONIC_NODE_TYPE_OBJECT) {
268266
pyNode = PyObject_CallObject(
269267
(PyObject *) &PyJsonic_JsonicType,
270-
Py_BuildValue("(s)", self->json)
268+
Py_BuildValue("(s)", self->json_ascii)
271269
);
272270

273271
((PyJsonic_JsonicObject *) pyNode)->node = node;
@@ -276,7 +274,7 @@ PyJsonic_JsonicType_iterItem(
276274
} else if (node->type == JSONIC_NODE_TYPE_ARRAY) {
277275
pyNode = PyObject_CallObject(
278276
(PyObject *) &PyJsonic_JsonicType,
279-
Py_BuildValue("(s)", self->json)
277+
Py_BuildValue("(s)", self->json_ascii)
280278
);
281279

282280
((PyJsonic_JsonicObject *) pyNode)->node = node;
@@ -307,7 +305,7 @@ PyJsonic_JsonicType_iterKey(
307305
}
308306

309307
jsonic_node_t* node = jsonic_object_iter(
310-
self->json,
308+
self->json_ascii,
311309
self->node,
312310
self->from,
313311
key
@@ -325,7 +323,7 @@ PyJsonic_JsonicType_iterKey(
325323
if (node->type == JSONIC_NODE_TYPE_OBJECT) {
326324
pyNode = PyObject_CallObject(
327325
(PyObject *) &PyJsonic_JsonicType,
328-
Py_BuildValue("(s)", self->json)
326+
Py_BuildValue("(s)", self->json_ascii)
329327
);
330328

331329
((PyJsonic_JsonicObject *) pyNode)->node = node;
@@ -334,7 +332,7 @@ PyJsonic_JsonicType_iterKey(
334332
} else if (node->type == JSONIC_NODE_TYPE_ARRAY) {
335333
pyNode = PyObject_CallObject(
336334
(PyObject *) &PyJsonic_JsonicType,
337-
Py_BuildValue("(s)", self->json)
335+
Py_BuildValue("(s)", self->json_ascii)
338336
);
339337

340338
((PyJsonic_JsonicObject *) pyNode)->node = node;
@@ -360,7 +358,7 @@ PyJsonic_JsonicType_reset(
360358
PyObject *kwds
361359
) {
362360
self->from = malloc(sizeof(jsonic_node_t));
363-
memcpy(self->from, jsonic_get_root(self->json), sizeof(jsonic_node_t));
361+
memcpy(self->from, jsonic_get_root(self->json_ascii), sizeof(jsonic_node_t));
364362

365363
Py_RETURN_NONE;
366364
}

python-jsonic.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#include "lib/jsonic/jsonic.h"
1818

19-
#define PYJSONIC_VERSION "1.5"
19+
#define PYJSONIC_VERSION "1.6"
2020

2121
typedef struct {
2222
PyObject_HEAD
@@ -26,9 +26,9 @@ typedef struct {
2626
PyObject_HEAD
2727
jsonic_node_t* node;
2828
jsonic_node_t* from;
29-
PyObject* attr_json;
29+
PyObject* json;
30+
char* json_ascii;
3031
int json_type;
31-
char* json;
3232
} PyJsonic_JsonicObject;
3333

3434
int
@@ -117,7 +117,7 @@ PyJsonicModule = {
117117

118118
static PyMemberDef
119119
PyJsonic_JsonicType_members[] = {
120-
{"json", T_OBJECT, offsetof(PyJsonic_JsonicObject, attr_json), 0, "JSON String"},
120+
{"json", T_OBJECT, offsetof(PyJsonic_JsonicObject, json), 0, "JSON String"},
121121
{"type", T_INT, offsetof(PyJsonic_JsonicObject, json_type), 0, "JSON Type"},
122122
{NULL, 0, 0, 0, NULL}
123123
};

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = pyjsonic
3-
version = 1.5
3+
version = 1.6
44
author = Oğuzhan Eroğlu
55
author-email = [email protected]
66
home-page = https://github.com/rohanrhu/python-jsonic

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
setup(
2727
name = "pyjsonic",
28-
version = "1.5",
28+
version = "1.6",
2929
description = "Python bindings for Jsonic JSON reader library.",
3030
long_description = long_description,
3131
long_description_content_type='text/markdown',

0 commit comments

Comments
 (0)