Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-12

FCL-JSON: Getting the data into pascal classes

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Reimar Grabowski

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?

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.
R.

Reply by email

Michael Van Canneyt
to Reimar Grabowski
Reimar Grabowski wrote 10 lines
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.

Show quoted text 11 lines
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.

Reply by email