background preloader

ECMAScript 5 Strict Mode, JSON, and More

ECMAScript 5 Strict Mode, JSON, and More
Previously I analyzed ECMAScript 5’s Object and Property system. This is a huge new aspect of the language and deserved its special consideration. There are a number of other new features and APIs that need attention, as well. The largest of which are Strict Mode and native JSON support. Strict Mode Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a “strict” operating context. Since ECMAScript 5 is backwards-compatible with ECMAScript 3, all of the “features” that were in ECMAScript 3 that were “deprecated” are just disabled (or throw errors) in strict mode, instead. Strict mode helps out in a couple ways: It catches some common coding bloopers, throwing exceptions.It prevents, or throws errors, when relatively “unsafe” actions are taken (such as gaining access to the global object).It disables features that are confusing or poorly thought out. Most of the information about strict mode can be found in the ES5 specification [PDF] on page #235.

Is It Too Late To Change JSON? In my last post, I wrote about the hijacking of JSON arrays. Near the end of the post, I mentioned a comment whereby someone suggests that what really should happen is that browsers should be more strict about honoring content types and not execute code with the content type of application/json. I totally agree! Another potential solution that came to mind was this: Can we simply change JSON? boat-left-harbor Let me run an idea by you. One way to do this, and I’m just throwing this out there, is to make it such that the JSON package must always begin and end with a curly brace. But when the response is a JSON array, we would go from here: to here: Client code would simply check to see if the JSON response starts with {[ to determine whether it’s an array, or an object. It’d be possible to do this without breaking every site out there by simply giving all the client libraries a head start. So what do you think?

JSON On Design Minimalism April 2009 ... or, How to implement "includes" on top of JSON Template ... or, Avoiding the Dinosaur Snaggletooth After introducing JSON Template, the most common feature request was for some kind of include or macro system. The language isn't frozen yet by any means, but it's meant to have a minimal design, while still retaining power. This article includes two detailed examples that show how JSON Template solves the problem of reuse, without any new features. Contents Two Template Reuse Patterns Sample program for reuse on the "outside" Sample program for reuse on the "inside" Functional Programming Now onto Design Minimalism Another example of multiple problems addressed by a single solution Conclusion Two Template Reuse Patterns Notice that an "include" or "macro" system is actually a solution. The problem is reuse, or avoiding duplication between template files. Reusing a template "inside" another template. I have less experience with the "inheritance" model. The idea is simple: Problems:

Weaving tweed with Scala and Json Extractor objects, a secret feature of the Scala language described only in Chapter 24 of Programming in Scala, certainly hit it big time this spring. Everybody used to be contented with case Some(thing) => kerpow(thing), but now you are not even allowed inside the #scala freenode unless you can produce your own extractor. If you try to cheat, the cutthroat Scala channel regulars will hack into your computer to make it play the astonishingly stupid Dollhouse on repeat (but skipping the coed shower scenes) until, like the show’s “actives”, you abandon consistent typing entirely. This extractor may pinch a little Fleeing this terrible fate, Coderspiel has endeavored to apply extractor objects to any and all circumstances. An extractor is created when you call ? val Echo.acting(a) = echo a: String = staring blankly ahead Ha ha. Extractors are not the only way to skin the Json cat, however. import Js._ ('acting ! 140 characters can’t be wrong (“I pitched it as Alias meets Quantum Leap.” Dope.

Introducing JSON Template March 2009 JSON Template is a minimal but powerful templating language, currently implemented in bothPython and JavaScript. To use it, simply copy the Python file or the JavaScript file into your project. Since there are over one bajillion template languages for Python alone, it deserves some explanation. Simple example This Python example should look familiar to many people.* The JavaScript version is a straightforward translation of this API. Full Example To read this example, keep in mind the following: These few constructs are surprisingly powerful. The following example uses most of the language, but not all. A template string ... {# This is a comment and will be removed from the output.} {.section songs} <h2>Songs in '{playlist-name}'</h2><table width="100%"> {.repeated section @} <tr><td><a href="{url-base|htmltag}{url|htmltag}">Play</a><td><i>{title}</i></td><td>{artist}</td></tr> {.end} </table> {.or} <p><em>(No page content matches)</em></p> {.end} ... gives output: Motivation Design

JSON Serialization Usage Google Reader API Google Reader is an online feed aggregator with heavy use of JavaScript and pretty quick loading of the latest feed data from around the web. Google’s AJAX front-end styles back-end data published in the Atom syndication format. The data technologies powering Google Reader can easily be used and extended by third-party feed aggregators for use in their own applications. I will walk you through the (previously) undocumented Google Reader API. Update 10:40 p.m.:Jason Shellen, PM of Google Reader, called me to let me know that Google built its feed API first and the Google Reader application second as a demonstration of what could be done with the underlying data. A reliable feed parser managed by a third party lowers the barrier to entry of new aggregator developers. Google Reader users are assigned a 20-digit user ID used throughout Google’s feed system. Feed retrieval /reader/atom/feed/ Google converts all feed data to Atom regardless of its original publication format. Subscription list

JSON Schema Proposal