Ignore:
Timestamp:
Mar 18, 2010, 1:51:23 PM (15 years ago)
Author:
[email protected]
Message:

2010-03-18 Oliver Hunt <[email protected]>

Reviewed by Sam Weinig.

Add API to directly expose JSON parsing
https://bugs.webkit.org/show_bug.cgi?id=34887

Add API to expose JSON parsing directly, and add tests to testapi

  • API/JSValueRef.cpp: (JSValueMakeFromJSONString): (JSValueCreateJSONString):
  • API/tests/testapi.c: (main):
  • JavaScriptCore.exp:
  • runtime/JSONObject.cpp: (JSC::JSONStringify):
  • runtime/JSONObject.h:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/API/tests/testapi.c

    r53657 r56189  
    870870    JSObjectSetProperty(context, globalObject, EmptyObjectIString, EmptyObject, kJSPropertyAttributeNone, NULL);
    871871    JSStringRelease(EmptyObjectIString);
    872    
     872
     873    JSStringRef validJSON = JSStringCreateWithUTF8CString("{\"aProperty\":true}");
     874    JSValueRef jsonObject = JSValueMakeFromJSONString(context, validJSON);
     875    JSStringRelease(validJSON);
     876    if (!JSValueIsObject(context, jsonObject)) {
     877        printf("FAIL: Did not parse valid JSON correctly\n");
     878        failed = 1;
     879    } else
     880        printf("PASS: Parsed valid JSON string.\n");
     881    JSStringRef propertyName = JSStringCreateWithUTF8CString("aProperty");
     882    assertEqualsAsBoolean(JSObjectGetProperty(context, JSValueToObject(context, jsonObject, 0), propertyName, 0), true);
     883    JSStringRelease(propertyName);
     884    JSStringRef invalidJSON = JSStringCreateWithUTF8CString("fail!");
     885    if (JSValueMakeFromJSONString(context, invalidJSON)) {
     886        printf("FAIL: Should return null for invalid JSON data\n");
     887        failed = 1;
     888    } else
     889        printf("PASS: Correctly returned null for invalid JSON data.\n");
    873890    JSValueRef exception;
    874 
     891    JSStringRef str = JSValueCreateJSONString(context, jsonObject, 0, 0);
     892    if (!JSStringIsEqualToUTF8CString(str, "{\"aProperty\":true}")) {
     893        printf("FAIL: Did not correctly serialise with indent of 0.\n");
     894        failed = 1;
     895    } else
     896        printf("PASS: Correctly serialised with indent of 0.\n");
     897    JSStringRelease(str);
     898
     899    str = JSValueCreateJSONString(context, jsonObject, 4, 0);
     900    if (!JSStringIsEqualToUTF8CString(str, "{\n    \"aProperty\": true\n}")) {
     901        printf("FAIL: Did not correctly serialise with indent of 4.\n");
     902        failed = 1;
     903    } else
     904        printf("PASS: Correctly serialised with indent of 4.\n");
     905    JSStringRelease(str);
     906    JSStringRef src = JSStringCreateWithUTF8CString("({get a(){ throw '';}})");
     907    JSValueRef unstringifiableObj = JSEvaluateScript(context, src, NULL, NULL, 1, NULL);
     908   
     909    str = JSValueCreateJSONString(context, unstringifiableObj, 4, 0);
     910    if (str) {
     911        printf("FAIL: Didn't return null when attempting to serialize unserializable value.\n");
     912        JSStringRelease(str);
     913        failed = 1;
     914    } else
     915        printf("PASS: returned null when attempting to serialize unserializable value.\n");
     916   
     917    str = JSValueCreateJSONString(context, unstringifiableObj, 4, &exception);
     918    if (str) {
     919        printf("FAIL: Didn't return null when attempting to serialize unserializable value.\n");
     920        JSStringRelease(str);
     921        failed = 1;
     922    } else
     923        printf("PASS: returned null when attempting to serialize unserializable value.\n");
     924    if (!exception) {
     925        printf("FAIL: Did not set exception on serialisation error\n");
     926        failed = 1;
     927    } else
     928        printf("PASS: set exception on serialisation error\n");
    875929    // Conversions that throw exceptions
    876930    exception = NULL;
Note: See TracChangeset for help on using the changeset viewer.