Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > slightly more powerful JSON

Reply
Thread Tools

slightly more powerful JSON

 
 
Red Daly
Guest
Posts: n/a
 
      01-03-2007
Hello group,

I have been using JSON for a while and it has made many things a breeze.
However, JSON does not natively describe certain things like pointers
and custom types. I created a simple JSON extension that allows
cross-references, and I'm asking for your thoughts on a simple type system.

I've dubbed this variation RJSON (Red's JSON) for lack of a shorter
name. Here's how it works right now:

The JSON way to encode a few people might be :

{ "doug" : { "friends" : [ { "ryan" ] },
"ryan" : { "friends" : [ "doug"] } }

that is sort of odd since now doug and ryan's friends are not people,
but are instead strings. An RJSON encoder recognizes object equality
when serializing and encodes the friends with cross-references:

{ "doug" : { "friends" : [
xdecl(0, { "friends" : [ "doug"] } ) ]
"ryan" : xref(0) }

that allows more objects to be better described than in pure JSON. more
sophisticated relationships are allowed, including things like circular
references.

Now for a type system. YAML has a fairly effective type system, and
when I used Ruby I liked how easy it was to dump custom types. YAML is
not ideal for the browser because it requires more extensive parsing.
JSON, on the other hand, is great in the browser because it can be
evaled. The goals of the type system are a syntax that is
Javascript-compatible but simple enough to parse pretty easily. I would
like to combine the quick ability to dump objects that YAML has with the
evaluation speed of JSON.

so consider we have a object type called Person:

function Person(name, email) {
this.name = name;
this.email = email;
}

[ xtype("Person", { "name" : "Doug", "email" : "theeyesofmice@gmail" } ]

the type that is the first argument to xtype is a string descriptor of
the type. the parser can then, for example, look up the type string in
a table that maps type strings to handler functions. A handler that
will initialize person might look like

function(obj) { return new Person(obj.name, obj.email) }

It doesn't have to be a table lookup, though.. it can be any function.

Here's the unsafe parser I currently use:

function marshallRjson(type, arg) {

var lookupTable =

{ 'Person' : function (obj) {

new Person(obj.name, obj.email);

} };

if (lookupTable[type]) {

return lookupTable[type]();

};

};

function evalRjson(rjsonText) {

var xrefTable = { };

var xtypeWithObjects = { };

var xdecl =

function (name, value) {

return xrefTable[name] = value;

};

var xref =

function (name) {

return xrefTable[name];

};

var xtype = marshallRjson;

return eval('(' + rjsonText + ')');

};

All this code is, basically, is introducing the ability to call a few
functions inside would-be JSON code. The real idea behind these
features is sharing object representations between an application server
and client in a more sophisticated way than easily possible right now.
neat? not? suggestions?

thanks for your time,
Red Daly
International Object Database


[Aside] This is probably one of the less interesting webby things I'm
working on right now. Cooler is an alternative object system based on
generic functions and class-based inheritance. I am just starting to
look into the applications of lazy evaluation for web programming.

An example implementation of an encoder for RJSON is available here:
http://suavescript.googlecode.com/sv...src/rjson.lisp
 
Reply With Quote
 
 
 
 
Chad Burggraf
Guest
Posts: n/a
 
      01-03-2007
Red Daly wrote:

> The JSON way to encode a few people might be :
>
> { "doug" : { "friends" : [ { "ryan" ] },
> "ryan" : { "friends" : [ "doug"] } }


Does the above evaluate correctly in a browser? I would think it would
need to be:

{ doug : { friends : [ "ryan" ] },
ryan : { friends : [ "doug" ] } }

Cheers
Chad
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lib to generate XML/JSON[P] output from a DTD/XSD/JSON Schema/etc Acácio Centeno Python 1 02-15-2013 07:34 AM
I am facing an issue while decoding json string using json.loads sajuptpm Python 2 12-28-2012 07:16 AM
[ANN] Security Fix json-1.1.7 for json_pure and json gems Florian Frank Ruby 0 06-30-2009 05:18 PM
JSON Template: Minimal but powerful templating language implementedin Python and 3 other languages Andy Chu Python 1 04-17-2009 07:59 AM
"JSON for ASP" at json.org Tuðrul Topuz ASP General 1 06-27-2008 11:37 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57