Re: JSON Serialisation



"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


.



Relevant Pages

  • Re: Looking for BASIC compiler
    ... > VB-NET offers an environment that is similar to VB-6. ... > is that there are a large number of books available for it. ... > used by VB, REALbasic, or Delphi. ...
    (comp.lang.basic.misc)
  • TeamB, Borland, admit obvious
    ... Exhibit A: From Delphi 7 help. ... strings as using the Concat function: ... The plus operator is faster than Concat. ...
    (borland.public.delphi.non-technical)
  • Delphi 2009
    ... I upgraded to Delphi 2009 today, and I thought some potential CodeGear customers might like an opinion of someone with a lot of code. ... That failed miserably because MS's .Net package wouldn't install (despite it being a MS OS in an MS virtual machine... ... The only things that needed fixing were where the client and server programs communicated strings over a named pipe - they just had to be touched up to realize strings were in unicode - which is what I wanted anyways. ...
    (alt.comp.lang.borland-delphi)
  • Re: main argument curiosity
    ... > strings, and that those strings represent a command line. ... If argc is not zero, then the array members argvthrough to ... On computers where there is such a thing as a command line, ... the host environment prior to program startup. ...
    (comp.lang.c)
  • Re: Cross platform password string encryption
    ... I happen to have solved it now, I ended up using the Delphi LockBox ... Also to those "posters" suggesting I use other languages, ... The consuming code doesn't use the library code in the right way. ... The consuming code is trying to pass strings to the routines and ...
    (sci.crypt)