JSONtoObj.c
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2011-2012, ESN Social Software AB and Jonas Tarnstrom
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007     * Redistributions of source code must retain the above copyright
00008       notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright
00010       notice, this list of conditions and the following disclaimer in the
00011       documentation and/or other materials provided with the distribution.
00012     * Neither the name of the ESN Social Software AB nor the
00013       names of its contributors may be used to endorse or promote products
00014       derived from this software without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00017 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL ESN SOCIAL SOFTWARE AB OR JONAS TARNSTROM BE LIABLE 
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Portions of code from:
00028 MODP_ASCII - Ascii transformations (upper/lower, etc)
00029 http://code.google.com/p/stringencoders/
00030 Copyright (c) 2007  Nick Galbreath -- nickg [at] modp [dot] com. All rights reserved.
00031 
00032 */
00033 
00034 #include "py_defines.h"
00035 #include <ultrajson.h>
00036 
00037 
00038 
00039 
00040 void Object_objectAddKey(JSOBJ obj, JSOBJ name, JSOBJ value)
00041 {
00042     PyDict_SetItem (obj, name, value);
00043     Py_DECREF( (PyObject *) name);
00044     Py_DECREF( (PyObject *) value);
00045     return;
00046 }
00047 
00048 void Object_arrayAddItem(JSOBJ obj, JSOBJ value)
00049 {
00050     PyList_Append(obj, value);
00051     Py_DECREF( (PyObject *) value);
00052     return;
00053 }
00054 
00055 JSOBJ Object_newString(wchar_t *start, wchar_t *end)
00056 {
00057     return PyUnicode_FromWideChar (start, (end - start));
00058 }
00059 
00060 JSOBJ Object_newTrue(void)
00061 { 
00062     Py_RETURN_TRUE;
00063 }
00064 
00065 JSOBJ Object_newFalse(void)
00066 {
00067     Py_RETURN_FALSE;
00068 }
00069 
00070 JSOBJ Object_newNull(void)
00071 {
00072     Py_RETURN_NONE;
00073 }
00074 
00075 JSOBJ Object_newObject(void)
00076 {
00077     return PyDict_New();
00078 }
00079 
00080 JSOBJ Object_newArray(void)
00081 {
00082     return PyList_New(0);
00083 }
00084 
00085 JSOBJ Object_newInteger(JSINT32 value)
00086 {
00087     return PyInt_FromLong( (long) value);
00088 }
00089 
00090 JSOBJ Object_newLong(JSINT64 value)
00091 {
00092     return PyLong_FromLongLong (value);
00093 }
00094 
00095 JSOBJ Object_newDouble(double value)
00096 { 
00097     return PyFloat_FromDouble(value);
00098 }
00099 
00100 static void Object_releaseObject(JSOBJ obj)
00101 {
00102     Py_DECREF( ((PyObject *)obj));
00103 }
00104 
00105 
00106 
00107 PyObject* JSONToObj(PyObject* self, PyObject *arg)
00108 {
00109     PyObject *ret;
00110     PyObject *sarg;
00111     JSONObjectDecoder decoder = 
00112     {
00113         Object_newString,
00114         Object_objectAddKey,
00115         Object_arrayAddItem,
00116         Object_newTrue,
00117         Object_newFalse,
00118         Object_newNull,
00119         Object_newObject,
00120         Object_newArray,
00121         Object_newInteger,
00122         Object_newLong,
00123         Object_newDouble,
00124         Object_releaseObject,
00125         PyObject_Malloc,
00126         PyObject_Free,
00127         PyObject_Realloc
00128     };
00129 
00130     if (PyString_Check(arg))
00131     {
00132         sarg = arg;
00133     }
00134     else
00135     if (PyUnicode_Check(arg))
00136     {
00137         sarg = PyUnicode_AsUTF8String(arg);
00138         if (sarg == NULL)
00139         {
00140             //Exception raised above us by codec according to docs
00141             return NULL;
00142         }
00143     }
00144     else
00145     {
00146         PyErr_Format(PyExc_TypeError, "Expected String or Unicode");
00147         return NULL;
00148     }
00149 
00150     decoder.errorStr = NULL;
00151     decoder.errorOffset = NULL;
00152     
00153     ret = JSON_DecodeObject(&decoder, PyString_AS_STRING(sarg), PyString_GET_SIZE(sarg)); 
00154 
00155     if (sarg != arg)
00156     {
00157         Py_DECREF(sarg);
00158     }
00159 
00160     if (decoder.errorStr)
00161     {
00162         /*
00163         FIXME: It's possible to give a much nicer error message here with actual failing element in input etc*/
00164         
00165         PyErr_Format (PyExc_ValueError, "%s", decoder.errorStr);
00166         
00167         if (ret)
00168         {
00169             Py_DECREF( (PyObject *) ret);
00170         }
00171         
00172         return NULL;
00173     }
00174     
00175     return ret;
00176 }
00177 
00178 PyObject* JSONFileToObj(PyObject* self, PyObject *file)
00179 {
00180     PyObject *read;
00181     PyObject *string;
00182     PyObject *result;
00183 
00184     if (!PyObject_HasAttrString (file, "read"))
00185     {
00186         PyErr_Format (PyExc_TypeError, "expected file");
00187         return NULL;
00188     }
00189 
00190     read = PyObject_GetAttrString (file, "read");
00191 
00192     if (!PyCallable_Check (read)) {
00193         Py_XDECREF(read);
00194         PyErr_Format (PyExc_TypeError, "expected file");
00195         return NULL;
00196     }
00197 
00198     string = PyObject_CallObject (read, NULL);
00199     Py_XDECREF(read);
00200 
00201     if (string == NULL)
00202     {
00203         return NULL;
00204     }
00205 
00206     result = JSONToObj (self, string);
00207     Py_XDECREF(string);
00208 
00209     if (result == NULL) {
00210         return NULL;
00211     }
00212 
00213     return result;
00214 }
00215 


rosbridge_library
Author(s): Jonathan Mace
autogenerated on Thu Jan 2 2014 11:53:35