Friday, April 18th, 2008

Conform your JSON to ECMAScript 4 with JCON

Category: JSON, Library

<p>Oliver Steele is doing great work, and he has just released a gem called JCON which stands for JavaScript Conformance. It tests JSON values to make sure that they are valid for the new world of ECMAScript 4 type definitions (e.g. new { x:int, y:string }( 3, “foo” ) ).

Usage

  1. type = JCON::parse "[string, int]"
  2. type.contains?([‘a’, 1])     # => true
  3. type.contains?([‘a’, ‘b’])   # => false
  4. type.contains?([‘a’, 1, 2])  # => true
  5.  
  6. // via RSpec
  7. [1, ‘xyzzy’].should conform_to_js(‘[int, string]’)
  8. [1, 2, ‘xyzzy’].should_not conform_to_js(‘[int, string]’)  # 2 isn’t a string
  9. {:x => 1}.should conform_to_js(‘{x: int}’)
  10.  
  11. // with JavaScript Fu
  12. # this will succeed if e.g. response contains a script tag that includes
  13. #   fn("id", {x:1, y:2}, true)
  14. response.should call_js(‘fn’) do |args|
  15.   args[0].should conform_to_js(‘string’)
  16.   args[1].should conform_to_js(‘{x:int, y:int}’)
  17.   args[2].should conform_to_js(‘boolean’)
  18.   # or:
  19.   args.should conform_to_js(‘[string, {x:int, y:int}, boolean]’)
  20. end

In other JSON news, it appears that new ECMAScript standard will no longer reserve the words:

abstract boolean byte char double final float implements int interface
long native package private protected public short static synchronized
throws transient volatile

And Douglas Crockford says that no browsers reserve them, and thus he is unreserving them from jsLint.

Related Content:

Posted by Dion Almaer at 5:50 am
1 Comment

+++--
3 rating from 9 votes

1 Comment »

Comments feed TrackBack URI

The smart quotes in the code are dumb.

Comment by AndyB — April 19, 2008

Leave a comment

You must be logged in to post a comment.