Beispiel #1
0
static JSBool
HandleData(JSContext *cx, JSONParser *jp, JSONDataType type, const jschar *buf, uint32 len)
{
  JSBool ok = JS_FALSE;
 
  switch (type) {
    case JSON_DATA_STRING:
      ok = HandleString(cx, jp, buf, len);
      break;
 
    case JSON_DATA_KEYSTRING:
      jp->objectKey = JS_NewUCStringCopyN(cx, buf, len);
      ok = JS_TRUE;
      break;
 
    case JSON_DATA_NUMBER:
      ok = HandleNumber(cx, jp, buf, len);
      break;
 
    case JSON_DATA_KEYWORD:
      ok = HandleKeyword(cx, jp, buf, len);
      break;
 
    default:
      JS_NOT_REACHED("Should have a JSON data type");
  }
 
  js_FinishStringBuffer(jp->buffer);
  js_InitStringBuffer(jp->buffer);
 
  return ok;
}
Beispiel #2
0
static JSBool
HandleData(JSContext *cx, JSONParser *jp, JSONDataType type)
{
  JSBool ok = JS_FALSE;

  if (!STRING_BUFFER_OK(jp->buffer))
      return JS_FALSE;

  switch (type) {
    case JSON_DATA_STRING:
      ok = HandleString(cx, jp, jp->buffer->base, STRING_BUFFER_OFFSET(jp->buffer));
      break;

    case JSON_DATA_KEYSTRING:
      js_AppendUCString(jp->objectKey, jp->buffer->base, STRING_BUFFER_OFFSET(jp->buffer));
      ok = STRING_BUFFER_OK(jp->objectKey);
      break;

    case JSON_DATA_NUMBER:
      ok = HandleNumber(cx, jp, jp->buffer->base, STRING_BUFFER_OFFSET(jp->buffer));
      break;

    case JSON_DATA_KEYWORD:
      ok = HandleKeyword(cx, jp, jp->buffer->base, STRING_BUFFER_OFFSET(jp->buffer));
      break;

    default:
      JS_NOT_REACHED("Should have a JSON data type");
  }

  js_FinishStringBuffer(jp->buffer);
  js_InitStringBuffer(jp->buffer);

  return ok;
}
Beispiel #3
0
JSONParser *
js_BeginJSONParse(JSContext *cx, jsval *rootVal)
{
    if (!cx)
        return NULL;

    JSObject *arr = js_NewArrayObject(cx, 0, NULL);
    if (!arr)
        return NULL;

    JSONParser *jp = (JSONParser*) JS_malloc(cx, sizeof(JSONParser));
    if (!jp)
        return NULL;
    jp->buffer = NULL;

    jp->objectStack = arr;
    if (!js_AddRoot(cx, &jp->objectStack, "JSON parse stack"))
        goto bad;

    jp->hexChar = 0;
    jp->numHex = 0;
    jp->statep = jp->stateStack;
    *jp->statep = JSON_PARSE_STATE_INIT;
    jp->rootVal = rootVal;

    jp->objectKey = (JSStringBuffer*) JS_malloc(cx, sizeof(JSStringBuffer));
    if (!jp->objectKey)
        goto bad;
    js_InitStringBuffer(jp->objectKey);
    
    jp->buffer = (JSStringBuffer*) JS_malloc(cx, sizeof(JSStringBuffer));
    if (!jp->buffer)
        goto bad;
    js_InitStringBuffer(jp->buffer);

    return jp;
bad:
    JS_free(cx, jp->buffer);
    JS_free(cx, jp);
    return NULL;
}
Beispiel #4
0
static JSBool
PushValue(JSContext *cx, JSONParser *jp, JSObject *parent, jsval value)
{
    JSAutoTempValueRooter tvr(cx, 1, &value);
 
    JSBool ok;
    if (OBJ_IS_ARRAY(cx, parent)) {
        jsuint len;
        ok = js_GetLengthProperty(cx, parent, &len);
        if (ok) {
            ok = OBJ_DEFINE_PROPERTY(cx, parent, INT_TO_JSID(len), value,
                                     NULL, NULL, JSPROP_ENUMERATE, NULL);
        }
    } else {
        ok = JS_DefineUCProperty(cx, parent, jp->objectKey->base,
                                 STRING_BUFFER_OFFSET(jp->objectKey), value,
                                 NULL, NULL, JSPROP_ENUMERATE);
        js_FinishStringBuffer(jp->objectKey);
        js_InitStringBuffer(jp->objectKey);
    }

    return ok;
}