ezequieljuliano / DataSetConverter4Delphi

API to convert JSON objects for DataSet and DataSet to JSON Objects
Apache License 2.0
215 stars 99 forks source link

Create Fields from Json #30

Open ricardodarocha opened 1 year ago

ricardodarocha commented 1 year ago

I have a situation where would be interesting to fill an empty dataset with all the fields from the json, for example, before import all json objects from an Array of JsonObjects, I could give the dataset one scheme with the sample data that better represents the expected structure

var sample_data = '{"name": "Some String", "age": 99, "valid_date": "2023-01-01 11:00:00", "valid_number": 999.99 }';
dataset.addFieldFromJson(sample_data);
dataset.fromJsonArray; 

Intended to produce the following dataset

  dataset.FieldDefs.Add('name', ftString, 10, {required = } false);
  dataset.FieldDefs.Add('age', ftInteger, {required = } false);
  dataset.FieldDefs.Add('valid_date', ftDateTime, {required = } false);
  dataset.FieldDefs.Add('valid_number', ftExtended, {required = } false);

Or another most explicit structure whould result in the same dataset

var sample_data = '[
     {"field_name": "name, "field_type": "ftString", "field_size": 10, "field_required": true },
     {"field_name": "age, "field_type": "ftInteger", "field_required": false},
     {"field_name": "valid_date, "field_type": "ftDate", "field_required": true },
     {"field_name": "valid_number, "field_type": "ftExtended", "field_size": 18, "field_precision": 2 , "field_required": false},
 ]';
ezequieljuliano commented 1 year ago

It's a pretty useful feature.

Do you think you would be able to open a PR with this functionality?

jcaique commented 2 months ago

Its just to create the dataset?

ezequieljuliano commented 1 month ago

I believe there are two resources here:

1 - Based on the data, create the fields automatically. To do this, it will be necessary to handle specific types, for example dates, you would have to validate whether a string is a date and load it for the correct field type.

2 - Provide metadata with the schema before importing and creating the fields.

I think the two resources can exist together.

Feel free to contribute, all help is welcome.