summaryrefslogtreecommitdiff
path: root/bindings/python
diff options
context:
space:
mode:
Diffstat (limited to 'bindings/python')
-rw-r--r--bindings/python/example/test.json88
-rw-r--r--bindings/python/example/test.py11
-rw-r--r--bindings/python/jsonparser.pyx9
-rw-r--r--bindings/python/setup.py14
-rw-r--r--bindings/python/wrap_json.c95
5 files changed, 217 insertions, 0 deletions
diff --git a/bindings/python/example/test.json b/bindings/python/example/test.json
new file mode 100644
index 0000000..9eea6ad
--- /dev/null
+++ b/bindings/python/example/test.json
@@ -0,0 +1,88 @@
+{"web-app": {
+ "servlet": [
+ {
+ "servlet-name": "cofaxCDS",
+ "servlet-class": "org.cofax.cds.CDSServlet",
+ "init-param": {
+ "configGlossary:installationAt": "Philadelphia, PA",
+ "configGlossary:adminEmail": "ksm@pobox.com",
+ "configGlossary:poweredBy": "Cofax",
+ "configGlossary:poweredByIcon": "/images/cofax.gif",
+ "configGlossary:staticPath": "/content/static",
+ "templateProcessorClass": "org.cofax.WysiwygTemplate",
+ "templateLoaderClass": "org.cofax.FilesTemplateLoader",
+ "templatePath": "templates",
+ "templateOverridePath": "",
+ "defaultListTemplate": "listTemplate.htm",
+ "defaultFileTemplate": "articleTemplate.htm",
+ "useJSP": false,
+ "jspListTemplate": "listTemplate.jsp",
+ "jspFileTemplate": "articleTemplate.jsp",
+ "cachePackageTagsTrack": 200,
+ "cachePackageTagsStore": 200,
+ "cachePackageTagsRefresh": 60,
+ "cacheTemplatesTrack": 100,
+ "cacheTemplatesStore": 50,
+ "cacheTemplatesRefresh": 15,
+ "cachePagesTrack": 200,
+ "cachePagesStore": 100,
+ "cachePagesRefresh": 10,
+ "cachePagesDirtyRead": 10,
+ "searchEngineListTemplate": "forSearchEnginesList.htm",
+ "searchEngineFileTemplate": "forSearchEngines.htm",
+ "searchEngineRobotsDb": "WEB-INF/robots.db",
+ "useDataStore": true,
+ "dataStoreClass": "org.cofax.SqlDataStore",
+ "redirectionClass": "org.cofax.SqlRedirection",
+ "dataStoreName": "cofax",
+ "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver",
+ "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon",
+ "dataStoreUser": "sa",
+ "dataStorePassword": "dataStoreTestQuery",
+ "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';",
+ "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log",
+ "dataStoreInitConns": 10,
+ "dataStoreMaxConns": 100,
+ "dataStoreConnUsageLimit": 100,
+ "dataStoreLogLevel": "debug",
+ "maxUrlLength": 500}},
+ {
+ "servlet-name": "cofaxEmail",
+ "servlet-class": "org.cofax.cds.EmailServlet",
+ "init-param": {
+ "mailHost": "mail1",
+ "mailHostOverride": "mail2"}},
+ {
+ "servlet-name": "cofaxAdmin",
+ "servlet-class": "org.cofax.cds.AdminServlet"},
+
+ {
+ "servlet-name": "fileServlet",
+ "servlet-class": "org.cofax.cds.FileServlet"},
+ {
+ "servlet-name": "cofaxTools",
+ "servlet-class": "org.cofax.cms.CofaxToolsServlet",
+ "init-param": {
+ "templatePath": "toolstemplates/",
+ "log": 1,
+ "logLocation": "/usr/local/tomcat/logs/CofaxTools.log",
+ "logMaxSize": "",
+ "dataLog": 1,
+ "dataLogLocation": "/usr/local/tomcat/logs/dataLog.log",
+ "dataLogMaxSize": "",
+ "removePageCache": "/content/admin/remove?cache=pages&id=",
+ "removeTemplateCache": "/content/admin/remove?cache=templates&id=",
+ "fileTransferFolder": "/usr/local/tomcat/webapps/content/fileTransferFolder",
+ "lookInContext": 1,
+ "adminGroupID": 4,
+ "betaServer": true}}],
+ "servlet-mapping": {
+ "cofaxCDS": "/",
+ "cofaxEmail": "/cofaxutil/aemail/*",
+ "cofaxAdmin": "/admin/*",
+ "fileServlet": "/static/*",
+ "cofaxTools": "/tools/*"},
+
+ "taglib": {
+ "taglib-uri": "cofax.tld",
+ "taglib-location": "/WEB-INF/tlds/cofax.tld"}}}
diff --git a/bindings/python/example/test.py b/bindings/python/example/test.py
new file mode 100644
index 0000000..c3fde8b
--- /dev/null
+++ b/bindings/python/example/test.py
@@ -0,0 +1,11 @@
+import sys
+sys.path.append('..')
+
+import jsonparser
+
+data = open('test.json', 'rb').read()
+try:
+ output = jsonparser.decode(data)
+ print output
+except jsonparser.JSONException, e:
+ print 'Error -> %s' % e \ No newline at end of file
diff --git a/bindings/python/jsonparser.pyx b/bindings/python/jsonparser.pyx
new file mode 100644
index 0000000..49a7e08
--- /dev/null
+++ b/bindings/python/jsonparser.pyx
@@ -0,0 +1,9 @@
+cdef extern from "wrap_json.c":
+ object decode_json(char * value)
+ object get_exception_class()
+
+JSONException = get_exception_class()
+
+def decode(value):
+ value = value.encode('utf-8')
+ return decode_json(value) \ No newline at end of file
diff --git a/bindings/python/setup.py b/bindings/python/setup.py
new file mode 100644
index 0000000..c041155
--- /dev/null
+++ b/bindings/python/setup.py
@@ -0,0 +1,14 @@
+import sys
+from distutils.core import setup
+from Cython.Distutils.extension import Extension
+from Cython.Distutils import build_ext
+
+ext_modules = []
+ext_modules.append(Extension('jsonparser',
+ ['./jsonparser.pyx'], language = 'c++'))
+
+setup(
+ name = 'json-parser python wrapper',
+ cmdclass = {'build_ext' : build_ext},
+ ext_modules = ext_modules
+) \ No newline at end of file
diff --git a/bindings/python/wrap_json.c b/bindings/python/wrap_json.c
new file mode 100644
index 0000000..0f2fff2
--- /dev/null
+++ b/bindings/python/wrap_json.c
@@ -0,0 +1,95 @@
+/*
+ * Copyright (C) 2012 Mathias Kaerlev. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 THE AUTHOR OR CONTRIBUTORS 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.
+ */
+
+#include "../../json.c"
+
+PyObject * json_exception = PyErr_NewException("jsonparser.JSONException",
+ NULL, NULL);
+
+PyObject * get_exception_class()
+{
+ return json_exception;
+}
+
+PyObject * convert_value(json_value * data)
+{
+ PyObject * value;
+ switch (data->type) {
+ case json_object:
+ value = PyDict_New();
+ for (int i = 0; i < data->u.object.length; i++) {
+ PyObject * name = PyUnicode_FromString(
+ data->u.object.values[i].name);
+ PyObject * object_value = convert_value(
+ data->u.object.values[i].value);
+ PyDict_SetItem(value, name, object_value);
+ }
+ break;
+ case json_array:
+ value = PyList_New(0);
+ for (int i = 0; i < data->u.array.length; i++) {
+ PyObject * array_value = convert_value(
+ data->u.array.values[i]);
+ PyList_Append(value, array_value);
+ }
+ break;
+ case json_integer:
+ value = PyInt_FromLong(data->u.integer);
+ break;
+ case json_double:
+ value = PyFloat_FromDouble(data->u.dbl);
+ break;
+ case json_string:
+ value = PyUnicode_FromStringAndSize(data->u.string.ptr,
+ data->u.string.length);
+ break;
+ case json_boolean:
+ value = PyBool_FromLong((long)data->u.boolean);
+ break;
+ default:
+ // covers json_null, json_none
+ Py_INCREF(Py_None);
+ value = Py_None;
+ break;
+ }
+ return value;
+}
+
+PyObject * decode_json(char * data)
+{
+ json_settings settings;
+ memset(&settings, 0, sizeof (json_settings));
+ settings.settings = json_enable_comments;
+ char error[256];
+ json_value * value = json_parse_ex(&settings, data, strlen(data), error);
+ if (value == 0) {
+ return PyErr_Format(json_exception, error);
+ }
+ PyObject * converted = convert_value(value);
+ json_value_free(value);
+ return converted;
+}