[fpc-pascal] FCL-JSON: Getting the data into pascal classes

Michael Van Canneyt michael at freepascal.org
Sat Dec 8 12:15:30 CET 2012



On Fri, 7 Dec 2012, Reimar Grabowski wrote:

> Hi,
>
> I have a relatively simple JSON object consisting of some integer and
> string fields and a large (500-500000 entries) float array.  Following the
> parsedemo.pp I extract the values from JSON and put them in my pascal
> class.  All works as expected only that I do not seem to find a method to
> copy the complete float array from the TJSONArray object to an array of
> double in one go.  I can do it manually by traversing the array but
> wouldn't it be more convenient to have this functionality in TJSONArray
> (same for string, integer and other arrays).  Am I missing something?


No.

The reason I did not implement this is 2-fold:
1. Because there is no guarantee that the array contains only floats, a JSON array is an array of elements.
    These elements can be of any type.
2. The implementation of such a routine would do the same as you:
    manually traverse the array. So it would not bemore efficient.

Of course, it could be convenient.

>
> On another note. There is no automatic way to create an instance of a custom class from a TJSONObject or is there?
> JSON: {"fooStr":"foobar","fooInt":700,"fooFloatArray":[1.1, 1.2, 1.3]}
> Pascal:
> TMyCustomClass = class
>  fooStr: String;
>  fooInt: Integer;
>  fooFloatArray: array of double;
> end;
>
> Thanks for any info on this matter.

There currently is not. 
For the same reason as 1. above the last element fooFloatArray, is hard to generate.
But I think it is in general a good idea to have such a mechanism, it would help in 
many cases.

Michael.



More information about the fpc-pascal mailing list