Changeset 56189 in webkit
- Timestamp:
- Mar 18, 2010 1:51:23 PM (14 years ago)
- Location:
- trunk/JavaScriptCore
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/API/JSValueRef.cpp
r55633 r56189 32 32 33 33 #include <runtime/JSGlobalObject.h> 34 #include <runtime/JSONObject.h> 34 35 #include <runtime/JSString.h> 36 #include <runtime/LiteralParser.h> 35 37 #include <runtime/Operations.h> 36 38 #include <runtime/Protect.h> … … 222 224 } 223 225 226 JSValueRef 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 234 JSStringRef 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 224 251 bool JSValueToBoolean(JSContextRef ctx, JSValueRef value) 225 252 { -
trunk/JavaScriptCore/API/JSValueRef.h
r34606 r56189 28 28 29 29 #include <JavaScriptCore/JSBase.h> 30 #include <JavaScriptCore/WebKitAvailability.h> 30 31 31 32 #ifndef __cplusplus … … 209 210 JS_EXPORT JSValueRef JSValueMakeString(JSContextRef ctx, JSStringRef string); 210 211 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 */ 221 JS_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 */ 232 JS_EXPORT JSStringRef JSValueCreateJSONString(JSContextRef ctx, JSValueRef value, unsigned indent, JSValueRef* exception) AVAILABLE_AFTER_WEBKIT_VERSION_4_0; 233 211 234 /* Converting to primitive values */ 212 235 -
trunk/JavaScriptCore/API/tests/testapi.c
r53657 r56189 870 870 JSObjectSetProperty(context, globalObject, EmptyObjectIString, EmptyObject, kJSPropertyAttributeNone, NULL); 871 871 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"); 873 890 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"); 875 929 // Conversions that throw exceptions 876 930 exception = NULL; -
trunk/JavaScriptCore/ChangeLog
r56150 r56189 1 2010-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 1 20 2010-03-16 Sam Weinig <sam@webkit.org> 2 21 -
trunk/JavaScriptCore/JavaScriptCore.exp
r55943 r56189 58 58 _JSStringRelease 59 59 _JSStringRetain 60 _JSValueCreateJSONString 60 61 _JSValueGetType 61 62 _JSValueIsBoolean … … 74 75 _JSValueMakeString 75 76 _JSValueMakeUndefined 77 _JSValueMakeFromJSONString 76 78 _JSValueProtect 77 79 _JSValueToBoolean -
trunk/JavaScriptCore/runtime/JSONObject.cpp
r56085 r56189 869 869 } 870 870 871 UString 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 871 879 } // namespace JSC -
trunk/JavaScriptCore/runtime/JSONObject.h
r54022 r56189 58 58 }; 59 59 60 UString JSONStringify(ExecState* exec, JSValue value, unsigned indent); 61 60 62 } // namespace JSC 61 63
Note: See TracChangeset
for help on using the changeset viewer.