Changeset 56189 in webkit


Ignore:
Timestamp:
Mar 18, 2010 1:51:23 PM (14 years ago)
Author:
oliver@apple.com
Message:

2010-03-18 Oliver Hunt <oliver@apple.com>

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:
Location:
trunk/JavaScriptCore
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/API/JSValueRef.cpp

    r55633 r56189  
    3232
    3333#include <runtime/JSGlobalObject.h>
     34#include <runtime/JSONObject.h>
    3435#include <runtime/JSString.h>
     36#include <runtime/LiteralParser.h>
    3537#include <runtime/Operations.h>
    3638#include <runtime/Protect.h>
     
    222224}
    223225
     226JSValueRef JSValueMakeFromJSONString(JSContextRef ctx, JSStringRef string)
     227{
     228    ExecState* exec = toJS(ctx);
     229    APIEntryShim entryShim(exec);
     230    LiteralParser parser(exec, string->ustring(), LiteralParser::StrictJSON);
     231    return toRef(exec, parser.tryLiteralParse());
     232}
     233
     234JSStringRef JSValueCreateJSONString(JSContextRef ctx, JSValueRef apiValue, unsigned indent, JSValueRef* exception)
     235{
     236    ExecState* exec = toJS(ctx);
     237    APIEntryShim entryShim(exec);
     238    JSValue value = toJS(exec, apiValue);
     239    UString result = JSONStringify(exec, value, indent);
     240    if (exception)
     241        *exception = 0;
     242    if (exec->hadException()) {
     243        if (exception)
     244            *exception = toRef(exec, exec->exception());
     245        exec->clearException();
     246        return 0;
     247    }
     248    return OpaqueJSString::create(result).releaseRef();
     249}
     250
    224251bool JSValueToBoolean(JSContextRef ctx, JSValueRef value)
    225252{
  • trunk/JavaScriptCore/API/JSValueRef.h

    r34606 r56189  
    2828
    2929#include <JavaScriptCore/JSBase.h>
     30#include <JavaScriptCore/WebKitAvailability.h>
    3031
    3132#ifndef __cplusplus
     
    209210JS_EXPORT JSValueRef JSValueMakeString(JSContextRef ctx, JSStringRef string);
    210211
     212/* Converting to and from JSON formatted strings */
     213
     214/*!
     215 @function
     216 @abstract       Creates a JavaScript value from a JSON formatted string.
     217 @param ctx      The execution context to use.
     218 @param string   The JSString containing the JSON string to be parsed.
     219 @result         A JSValue containing the parsed value, or NULL if the input is invalid.
     220 */
     221JS_EXPORT JSValueRef JSValueMakeFromJSONString(JSContextRef ctx, JSStringRef string) AVAILABLE_AFTER_WEBKIT_VERSION_4_0;
     222
     223/*!
     224 @function
     225 @abstract       Creates a JavaScript string containing the JSON serialized representation of a JS value.
     226 @param ctx      The execution context to use.
     227 @param value    The value to serialize.
     228 @param indent   The number of spaces to indent when nesting.  If 0, the resulting JSON will not contains newlines.  The size of the indent is clamped to 10 spaces.
     229 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
     230 @result         A JSString with the result of serialization, or NULL if an exception is thrown.
     231 */
     232JS_EXPORT JSStringRef JSValueCreateJSONString(JSContextRef ctx, JSValueRef value, unsigned indent, JSValueRef* exception) AVAILABLE_AFTER_WEBKIT_VERSION_4_0;
     233
    211234/* Converting to primitive values */
    212235
  • 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;
  • trunk/JavaScriptCore/ChangeLog

    r56150 r56189  
     12010-03-18  Oliver Hunt  <oliver@apple.com>
     2
     3        Reviewed by Sam Weinig.
     4
     5        Add API to directly expose JSON parsing
     6        https://bugs.webkit.org/show_bug.cgi?id=34887
     7
     8        Add API to expose JSON parsing directly, and add tests to testapi
     9
     10        * API/JSValueRef.cpp:
     11        (JSValueMakeFromJSONString):
     12        (JSValueCreateJSONString):
     13        * API/tests/testapi.c:
     14        (main):
     15        * JavaScriptCore.exp:
     16        * runtime/JSONObject.cpp:
     17        (JSC::JSONStringify):
     18        * runtime/JSONObject.h:
     19
    1202010-03-16  Sam Weinig  <sam@webkit.org>
    221
  • trunk/JavaScriptCore/JavaScriptCore.exp

    r55943 r56189  
    5858_JSStringRelease
    5959_JSStringRetain
     60_JSValueCreateJSONString
    6061_JSValueGetType
    6162_JSValueIsBoolean
     
    7475_JSValueMakeString
    7576_JSValueMakeUndefined
     77_JSValueMakeFromJSONString
    7678_JSValueProtect
    7779_JSValueToBoolean
  • trunk/JavaScriptCore/runtime/JSONObject.cpp

    r56085 r56189  
    869869}
    870870
     871UString JSONStringify(ExecState* exec, JSValue value, unsigned indent)
     872{
     873    JSValue result = Stringifier(exec, jsNull(), jsNumber(exec, indent)).stringify(value);
     874    if (result.isUndefinedOrNull())
     875        return UString();
     876    return result.getString(exec);
     877}
     878
    871879} // namespace JSC
  • trunk/JavaScriptCore/runtime/JSONObject.h

    r54022 r56189  
    5858    };
    5959
     60    UString JSONStringify(ExecState* exec, JSValue value, unsigned indent);
     61
    6062} // namespace JSC
    6163
Note: See TracChangeset for help on using the changeset viewer.