Re: JSON Serialisation
- From: "Maarten Wiltink" <maarten@xxxxxxxxxxxxxxxxxx>
- Date: Thu, 8 Mar 2007 10:03:52 +0100
"Christakis John" <noemail@xxxxxx> wrote in message
news:q3LHh.8550$8U4.8007@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I have been playing around with JavaScript Object Notation (JSON) of
late and it seems like a cool way to handle objects as strings. Sort
of like a cleaner/smaller version of XML I guess. It's used natively
by JS and I was wondering if there was a similar system used natively
by Delphi that I could use (instead)?
No, and there's a good reason for that. In Delphi, an object is of a
classtype, and the classtype determines the available methods. (For
virtual methods, the object may have to supply them, but that too is
done through classes, and method _availability_ is still defined by a
class.)
JavaScript on the other hand, is very relaxed about methods. Resolution
is done only at the moment of the actual call. No assumptions are made
about available methods, and you can add methods to an object on a per-
object basis.
What it means is that JavaScript can usefully treat objects as mere data
containers in the first place, and Delphi can't. Because Delphi is
strongly typed and needs to know a classtype before you can even start
making an object. You certainly can't change it later.
So much for a one-to-one object correspondence; it won't work. That
doesn't mean you can't construct or find something that _works_ about
the same. If you're not so much concerned with neat, class-based,
object-orientation and just want something that does what JSON gets
you, think for a moment what exactly it gets you. That turns out to
be serialisation. The 'object' bit is really a misnomer: all you get
is a data structure, not an object's type or methods.
The most important part of the data structure is probably a simple list
of key-value pairs. The keys are strings, the values can be complex
nested types but in practice mostly basic types like Booleans, integers,
reals, and strings. Any TStrings object can do that much.
The word I learned for a list of key-value pairs is environment, but
they're also known as dictionaries, maps, or hashes. With some extra
work, you can make a basic environment object accept values that are
itself environments, and then you have everything JSON describes. You
do not yet have everything JavaScript does; for that you'd need code
that serialises and parses environments, and in fact you could use a
different exchange format then JSON without noticing it in your code.
It will never get as easy as JavaScript makes it to get at the named
values in the object, because Delphi simply has different syntax for
looking in an environment object and extracting the value associated
with a given name - JavaScript's 'obj.field' is rather hard to beat for
that. For that, Delphi gives you strong typing and the possibility to
declare and use object that are more than just fancy records.
Groetjes,
Maarten Wiltink
.
- Follow-Ups:
- Re: JSON Serialisation
- From: Christakis John
- Re: JSON Serialisation
- References:
- JSON Serialisation
- From: Christakis John
- JSON Serialisation
- Prev by Date: Re: Delphi to VB6
- Next by Date: Re: JSON Serialisation
- Previous by thread: JSON Serialisation
- Next by thread: Re: JSON Serialisation
- Index(es):
Relevant Pages
|