Git Product home page Git Product logo

datasetconverter4delphi's Introduction

DataSet Converter For Delphi

The DataSetConverter4D it is an API to convert JSON objects for DataSet's and also doing reverse process, ie, converting DataSet's in JSON.

Works with the TDataSet, and TJSONObject TJSONArray classes.

To use this API you must add the "DataSetConverter4D\src" Path in your Delphi or on your project.

Convert DataSet to JSON

First you must have your DataSet and its Fields created.

uses 
  DataSetConverter4D, 
  DataSetConverter4D.Impl;    

var
  ja: TJSONArray;
  jo: TJSONObject;
begin
  fCdsCustomers.DataSetField := nil;
  fCdsCustomers.CreateDataSet;

  fCdsCustomers.Append;
  fCdsCustomers.FieldByName('Id').AsInteger := 1;
  fCdsCustomers.FieldByName('Name').AsString := 'Customers 1';
  fCdsCustomers.FieldByName('Birth').AsDateTime := StrToDateTime('22/01/2014 14:05:03');
  fCdsCustomers.Post;

  fCdsCustomers.Append;
  fCdsCustomers.FieldByName('Id').AsInteger := 2;
  fCdsCustomers.FieldByName('Name').AsString := 'Customers 2';
  fCdsCustomers.FieldByName('Birth').AsDateTime := StrToDateTime('22/01/2014 14:05:03');
  fCdsCustomers.Post;

  //Convert all records	
  ja := TConverter.New.DataSet(fCdsCustomers).AsJSONArray;
  
  //Convert current record
  jo := TConverter.New.DataSet.Source(fCdsCustomers).AsJSONObject;

  ja.Free;
  jo.Free;
end;

Convert JSON to DataSet

First you must have your DataSet and its Fields created.

uses 
  DataSetConverter4D, 
  DataSetConverter4D.Impl;  

JSON_ARRAY =
		[{
			"Id": 1,
			"Name": "Customers 1",
			"Birth": "2014-01-22 14:05:03"
		}, {
			"Id": 2,
			"Name": "Customers 2",
			"Birth": "2014-01-22 14:05:03"
		}]      
			  
JSON_OBJECT =
		{
			"Id": 2,
			"Name": "Customers 2",
			"Birth": "2014-01-22 14:05:03"
		}
var
  ja: TJSONArray;
  jo: TJSONObject;
begin
  fCdsCustomers.CreateDataSet;

  ja := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(JSON_ARRAY), 0) as TJSONArray;
  jo := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(JSON_OBJECT), 0) as TJSONObject;

  //Convert one record
  TConverter.New.JSON(jo).ToDataSet(fCdsCustomers);

  fCdsCustomers.EmptyDataSet;

  //Convert all records
  TConverter.New.JSON.Source(ja).ToDataSet(fCdsCustomers);

  ja.Free;
  jo.Free;
end;

Using DataSetConverter4D

Using this library will is very simple, you simply add the Search Path of your IDE or your project the following directories:

  • DataSetConverter4Delphi\src\

Analyze the unit tests they will assist you.

datasetconverter4delphi's People

Contributors

ezequieljuliano avatar hunsche avatar marcosafincotto avatar microsys-sistemas avatar oneideluizschneider avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.