[Dojo-interest] JSON Date objects
Rick Morrison
rickmorrison at gmail.com
Thu Jul 13 17:28:13 MDT 2006
Well, the regexp technique for parsing {"__jsonclass__"} JSON strings is not
working out.
It's straightforward enough for simple cases, but when nested objects and
worse, nested {"__jsonclass__"} objects are included, the regexp technique
becomes a mess of counting closing braces pretty fast.
I'm still trying to find a trick to avoid having to write/use a real (and
big, and slow) JS parser.
Now I'm thinking of a technique where the string is eval()'d as now, and
then a second pass will crawl over the resulting object and do post-eval
replacements. Here's a simplified version to show the idea:
function jsonUnhint(o) {
for (var p in o) {
if (o[p].jsonclass || o[p].__jsonclass__)
o[p] = jsonMkobj(o[p]);
else if (typeof o[p] == "object")
jsonUnhint(o[p]);
}
return o;
};
Thoughts?
Rick
On 7/11/06, Alan Moore <kahunamoore at hotmail.com> wrote:
>
> Rick,
>
> I think you are onto something here...
>
> I found myself doing a similar thing in my code, only for the top level
> JSON object that you get in the bind load() event/call back. I place the
> widget type into the JSON object (e.g. clazz:MyClass) on the server side
> and then create an object of that type, passing into the constructor the
> load() data object. Then, the MyClass constructor pulls data out of that
> object and makes assignments to it's own attributes as needed. For nested
> types, I just gave up and hardcoded types into some of the logic in the
> constructor.
>
> It all seems convoluted and one-off to me but it kinda works. I was
> wondering if a more general JSON -> JavaScript marshalling/mapping could
> be done w/o the extra constructor parameter + attribute copying. As you
> said, you'll need a clazz (or __jsonclass__) to make it work.
>
> Alan
>
> ------------------------------
> Date: Tue, 11 Jul 2006 12:11:59 -0400
> From: rickmorrison at gmail.com
> To: dojo-interest at dojotoolkit.org
> Subject: Re: [Dojo-interest] JSON Date objects
>
>
>
> Well for dates, the decision to use either an ISO format or a simple array
> of arguments for a date constructor function could be left up to the
> implementer. If dojo already has an ISO parser (there's also a simple,
> although dated one at http://www.merlyn.demon.co.uk/js-date3.htm#ISO),
> then tant mieux, that would make the implementation of the "MyDate" function
> that much easier.
>
> But what I'm talking about is the more general case of client-side types
> that need support from some constructor function when deserializing from
> JSON, and a dojo mechanism to provide that support. Dates fall into that
> group, but so can other types.
>
> To expand on the previous post, an implementation that took JSON-RPC
> strings as we've been talking about, and also accepted an object that
> provided a hash of conversion functions for class names such as:
>
> {
> 'MyISODate': myisodatefunc,
> 'MyParametizedDate', 'myparmdatefunc,
> 'MyDataSet': mydatasetfunc
> }
>
> could be used to implement the unserialize function for class-hinted
> objects in JSON:
>
> here's a sample JSON input string encoded with JSON-RPC class hints:
>
> {'fname':'John', 'lname':'Lennon', 'bdate':{'__jsonclass__':["MyISODate",
> "1940-10-09"]}}
>
> by using regexps and consulting the above mentioned hash, this could be
> converted to:
>
> {'fname':'John', 'lname':'Lennon', 'bdate':myisodatefunc("1940-10-09")}
>
> which could then be eval()'d directly.
>
> This technique would work not just for dates, but in a more general way
> for all sorts of types.
>
> Rick
>
>
>
> On 7/11/06, *Jim Washington* <jwashin at vt.edu> wrote:
>
> Rick Morrison wrote:
> > I had a thought on this that might get the job done on the JSON load
> > side without too much pain.
> >
> > Seems to me that with some clever use of regexps, that within a
> > serialized JSON string
> >
> > {"__jsonclass__": ["MyDate", [2005, 4, 4]]}
> >
> > could be mutated into:
> >
> > MyDate(2005,4,4)
> >
> > and could then be simply eval()'d as things are done now.
> >
> > So we could get complete behavior AND fast performance.
> >
> > Thoughts?
> I just saw a note come through on the JSON list with an interesting idea.
>
> Jayrock-JSON uses a specific ISO 8601format for date. Look at
> http://www.w3.org/TR/NOTE-datetime for a description. Perhaps, with
> appropriate regexps and agreement to use this format, strings that look
> like this could be automagically coerced into appropriate things by
> Dojo's JSON parser.
>
> For example, {"MyDate":"2005-04-04"} could reasonably have a Date
> recognized in it.
>
> -Jim Washington
>
>
> _______________________________________________
> Dojo FAQ: http://dojo.jot.com/FAQ
> Dojo Book: http://manual.dojotoolkit.org/DojoDotBook
> Dojo-interest at dojotoolkit.org
> http://dojotoolkit.org/mailman/listinfo/dojo-interest
>
>
>
> ------------------------------
> Express yourself instantly with Windows Live Messenger<http://imagine-msn.com/messenger/launch80/default.aspx?locale=en-us&source=joinmsncom/messenger>
>
> _______________________________________________
> Dojo FAQ: http://dojo.jot.com/FAQ
> Dojo Book: http://manual.dojotoolkit.org/DojoDotBook
> Dojo-interest at dojotoolkit.org
> http://dojotoolkit.org/mailman/listinfo/dojo-interest
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://dojotoolkit.org/pipermail/dojo-interest/attachments/20060713/8a41a6d2/attachment.html
More information about the Dojo-interest
mailing list