Esempio n. 1
0
TEST_F(UltraJson, Whitespace) {
    for (int i = 0; i < kTrialCount; i++) {
        decoder.errorStr = NULL;
        decoder.errorOffset = NULL;
        void *ret = JSON_DecodeObject(&decoder, whitespace_, whitespace_length_);
        ASSERT_TRUE(ret != 0);
    }
}
Esempio n. 2
0
UJObject UJDecode(const char *input, size_t cbInput, UJHeapFuncs *hf, void **outState)
{
	UJObject ret;
	struct DecoderState *ds;
	void *initialHeap;
	size_t cbInitialHeap;
	HeapSlab *slab;

	JSONObjectDecoder decoder = {
		newString,
		objectAddKey,
		arrayAddItem,
		newTrue,
		newFalse,
		newNull,
		newObject,
		newArray,
		newInt,
		newLong,
		newDouble,
		releaseObject,
		NULL,
		NULL,
		NULL,
		NULL,
		NULL,
		0, 
		NULL
	};

	if (hf == NULL)
	{
		decoder.malloc = malloc;
		decoder.free = free;
		decoder.realloc = realloc;
		cbInitialHeap = 16384;
		initialHeap = malloc(cbInitialHeap);
	}
	else
	{
		decoder.malloc = hf->malloc;
		decoder.free = hf->free;
		decoder.realloc = hf->realloc;
		initialHeap = hf->initalHeap;
		cbInitialHeap = hf->cbInitialHeap;
	
		if (cbInitialHeap < sizeof(HeapSlab) + sizeof(struct DecoderState))
		{
			return NULL;
		}
	}

	*outState = NULL;

	slab = (HeapSlab * ) initialHeap;
	slab->start = (unsigned char *) (slab + 1);
	slab->offset = slab->start;
	slab->end = (unsigned char *) initialHeap + cbInitialHeap;
	slab->size = cbInitialHeap;
	slab->owned = hf == NULL ? 1 : 0;
	slab->next = NULL;

	ds = (struct DecoderState *) slab->offset;
	slab->offset += sizeof(struct DecoderState);
	*outState = (void *) ds;

	ds->heap = slab;
	
	ds->malloc = decoder.malloc;
	ds->free = decoder.free;
	ds->error = NULL; 

	decoder.prv = (void *) ds;

	ret = (UJObject) JSON_DecodeObject(&decoder, input, cbInput);

	if (ret == NULL)
	{
		ds->error = decoder.errorStr;
	}

	return ret;
}
Esempio n. 3
0
PyObject* JSONToObj(PyObject* self, PyObject *args, PyObject *kwargs)
{
  PyObject *ret;
  PyObject *sarg;
  PyObject *arg;
  PyObject *opreciseFloat = NULL;
  PyObject *oobjectHook = NULL;
  PyObject *ostringHook = NULL;

  JSONObjectDecoder decoder =
  {
    Object_newString,
    Object_objectAddKey,
    Object_arrayAddItem,
    Object_newTrue,
    Object_newFalse,
    Object_newNull,
    Object_newObject,
    Object_newArray,
    Object_newInteger,
    Object_newLong,
    Object_newUnsignedLong,
    Object_newDouble,
    NULL, //callObjectHook (optional, initialized below if passed as the keyword parameter)
    Object_releaseObject,
    PyObject_Malloc,
    PyObject_Free,
    PyObject_Realloc
  };

  decoder.preciseFloat = 0;

  DecoderParams dp = {
      NULL, //objectHook
      NULL, //stringHook
  };
  decoder.prv = &dp;

  if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOO", g_kwlist, &arg, &opreciseFloat, &oobjectHook, &ostringHook))
  {
      return NULL;
  }

  if (opreciseFloat && PyObject_IsTrue(opreciseFloat))
  {
      decoder.preciseFloat = 1;
  }

  if (oobjectHook && PyCallable_Check(oobjectHook))
  {
    decoder.callObjectHook = Object_callObjectHook;
    dp.objectHook = oobjectHook;
  }

  if (ostringHook && PyCallable_Check(ostringHook))
  {
    dp.stringHook = ostringHook;
  }

  if (PyString_Check(arg))
  {
      sarg = arg;
  }
  else
  if (PyUnicode_Check(arg))
  {
    sarg = PyUnicode_AsUTF8String(arg);
    if (sarg == NULL)
    {
      //Exception raised above us by codec according to docs
      return NULL;
    }
  }
  else
  {
    PyErr_Format(PyExc_TypeError, "Expected String or Unicode");
    return NULL;
  }

  decoder.errorStr = NULL;
  decoder.errorOffset = NULL;

  ret = JSON_DecodeObject(&decoder, PyString_AS_STRING(sarg), PyString_GET_SIZE(sarg));

  if (sarg != arg)
  {
    Py_DECREF(sarg);
  }

  if (decoder.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", decoder.errorStr);

    if (ret)
    {
        Py_DECREF( (PyObject *) ret);
    }

    return NULL;
  }

  return ret;
}
Esempio n. 4
0
PyObject* JSONToObj(PyObject* self, PyObject *args, PyObject *kwargs)
{
  PyObject *ret;
  PyObject *sarg;
  PyObject *arg;
  PyObject *opreciseFloat = NULL;
  JSONObjectDecoder *decoder;
  PyObjectDecoder pyDecoder;
  PyArray_Descr *dtype = NULL;
  int numpy = 0, labelled = 0;

  JSONObjectDecoder dec =
  {
    Object_newString,
    Object_objectAddKey,
    Object_arrayAddItem,
    Object_newTrue,
    Object_newFalse,
    Object_newNull,
    Object_newObject,
    Object_endObject,
    Object_newArray,
    Object_endArray,
    Object_newInteger,
    Object_newLong,
    Object_newDouble,
    Object_releaseObject,
    PyObject_Malloc,
    PyObject_Free,
    PyObject_Realloc
  };

  dec.preciseFloat = 0;
  dec.prv = NULL;

  pyDecoder.dec = dec;
  pyDecoder.curdim = 0;
  pyDecoder.npyarr = NULL;
  pyDecoder.npyarr_addr = NULL;

  decoder = (JSONObjectDecoder*) &pyDecoder;

  if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OiiO&", g_kwlist, &arg, &opreciseFloat, &numpy, &labelled, PyArray_DescrConverter2, &dtype))
  {
      Npy_releaseContext(pyDecoder.npyarr);
      return NULL;
  }

  if (opreciseFloat && PyObject_IsTrue(opreciseFloat))
  {
      decoder->preciseFloat = 1;
  }

  if (PyString_Check(arg))
  {
      sarg = arg;
  }
  else
  if (PyUnicode_Check(arg))
  {
    sarg = PyUnicode_AsUTF8String(arg);
    if (sarg == NULL)
    {
      //Exception raised above us by codec according to docs
      return NULL;
    }
  }
  else
  {
    PyErr_Format(PyExc_TypeError, "Expected String or Unicode");
    return NULL;
  }

  decoder->errorStr = NULL;
  decoder->errorOffset = NULL;

  if (numpy)
  {
    pyDecoder.dtype = dtype;
    decoder->newArray = Object_npyNewArray;
    decoder->endArray = Object_npyEndArray;
    decoder->arrayAddItem = Object_npyArrayAddItem;

    if (labelled)
    {
      decoder->newObject = Object_npyNewObject;
      decoder->endObject = Object_npyEndObject;
      decoder->objectAddKey = Object_npyObjectAddKey;
    }
  }

  ret = JSON_DecodeObject(decoder, PyString_AS_STRING(sarg), PyString_GET_SIZE(sarg));

  if (sarg != arg)
  {
    Py_DECREF(sarg);
  }

  if (PyErr_Occurred())
  {    
    if (ret)
    {
        Py_DECREF( (PyObject *) ret);
    }
    Npy_releaseContext(pyDecoder.npyarr);   
    return NULL;
  }

  if (decoder->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", decoder->errorStr);

    if (ret)
    {
        Py_DECREF( (PyObject *) ret);
    }
    Npy_releaseContext(pyDecoder.npyarr);

    return NULL;
  }

  return ret;
}
Esempio n. 5
0
PyObject* JSONToObj(PyObject* self, PyObject *arg)
{
    PyObject *ret;
    PyObject *sarg;
    JSONObjectDecoder decoder = 
    {
        Object_newString,
        Object_newDateTime,
        Object_objectAddKey,
        Object_arrayAddItem,
        Object_newTrue,
        Object_newFalse,
        Object_newNull,
        Object_newObject,
        Object_newArray,
        Object_newInteger,
        Object_newLong,
        Object_newDouble,
        Object_releaseObject,
        PyObject_Malloc,
        PyObject_Free,
        PyObject_Realloc
    };

    if (PyString_Check(arg))
    {
        sarg = arg;
    }
    else
    if (PyUnicode_Check(arg))
    {
        sarg = PyUnicode_AsUTF8String(arg);
        if (sarg == NULL)
        {
            //Exception raised above us by codec according to docs
            return NULL;
        }
    }
    else
    {
        PyErr_Format(PyExc_TypeError, "Expected String or Unicode");
        return NULL;
    }

    decoder.errorStr = NULL;
    decoder.errorOffset = NULL;
    
    ret = JSON_DecodeObject(&decoder, PyString_AS_STRING(sarg), PyString_GET_SIZE(sarg)); 

    if (sarg != arg)
    {
        Py_DECREF(sarg);
    }

    if (decoder.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", decoder.errorStr);
        
        if (ret)
        {
            Py_DECREF( (PyObject *) ret);
        }
        
        return NULL;
    }
    
    return ret;
}