<commit_before>// This is part of the Mommon Library
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This software is distributed without any warranty.
//
// @author <NAME> (<EMAIL> - www.mammola.net)  

unit mDataProviderJsonSerializer;

interface

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}

uses
  mDataProviderInterfaces, mNamingConventions;

function SerializeDataProviderToJson (const aDataProvider : IVDDataProvider; const aSourceNamingConvention, aDestinationNamingConvention : TmNamingConvention): String;

implementation

uses
  Classes, Contnrs, SysUtils, Variants,
  mDataProviderFieldDefs, mUtility, mDataProviderSerializerClasses;

//https://restfulapi.net/json-data-types/

const
  ISO8601FormatExtendedUTC='yyyy"-"mm"-"dd"T"hh":"mm":"ss"Z"';
  ISO8601FormatExtendedUTC_Date='yyyy"-"mm"-"dd"Z"';
  ISO8601FormatExtendedUTC_Time='"T"hh":"mm":"ss"Z"';


function SerializeDataProviderToJson(const aDataProvider: IVDDataProvider; const aSourceNamingConvention, aDestinationNamingConvention : TmNamingConvention): String;
var
  i, k : integer;
  curDatum : IVDDatum;
  fields : TSerializedFields;
  value : Variant;
begin
  Result := '[';

  if aDataProvider.Count = 0 then
    Result := Result + ' ]'
  else
  begin
    fields := TSerializedFields.Create;
    try
      GetSerializedFields(aDataProvider, fields, aSourceNamingConvention, aDestinationNamingConvention);

      for i := 0 to aDataProvider.Count - 1 do
      begin
        if i > 0 then
          Result := Result + ',{'
        else
          Result := Result + '{';
        curDatum := aDataProvider.GetDatum(i);
        for k := 0 to fields.Count - 1 do
        begin
          if k > 0 then
            Result := Result + ',';

          Result := Result + '"' + fields.Get(k).SerializedFieldName + '":';
          value := curDatum.GetPropertyByFieldName(fields.Get(k).OriginalFieldName);
          if VarIsNull(value) then
            Result := Result + 'null'
          else
          begin
            case fields.Get(k).DataType of
              vftInteger : Result := Result + IntToStr(value);
              vftBoolean : if value then Result := Result + 'true' else Result := Result + 'false';
              vftFloat, vftCurrency : Result := Result + FormatFloat('#.#', value);
              vftDate : Result := Result + '"' + FormatDateTime(ISO8601FormatExtendedUTC_Date, value) + '"';
              vftTime : Result := Result + '"' + FormatDateTime(ISO8601FormatExtendedUTC_Time, value) + '"';
              vftDateTime, vftTimeStamp : Result := Result + '"' + FormatDateTime(ISO8601FormatExtendedUTC, value) + '"';
            else<commit_msg>* migrated to procedures from mUtility unit<commit_after>// This is part of the Mommon Library
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This software is distributed without any warranty.
//
// @author <NAME> (<EMAIL> - www.mammola.net)  

unit mDataProviderJsonSerializer;

interface

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}

uses
  mDataProviderInterfaces, mNamingConventions;

function SerializeDataProviderToJson (const aDataProvider : IVDDataProvider; const aSourceNamingConvention, aDestinationNamingConvention : TmNamingConvention): String;

implementation

uses
  Classes, Contnrs, SysUtils, Variants,
  mDataProviderFieldDefs, mUtility, mDataProviderSerializerClasses;

//https://restfulapi.net/json-data-types/

//const
//  ISO8601FormatExtendedUTC='yyyy"-"mm"-"dd"T"hh":"mm":"ss"Z"';
//  ISO8601FormatExtendedUTC_Date='yyyy"-"mm"-"dd"Z"';
//  ISO8601FormatExtendedUTC_Time='"T"hh":"mm":"ss"Z"';


function SerializeDataProviderToJson(const aDataProvider: IVDDataProvider; const aSourceNamingConvention, aDestinationNamingConvention : TmNamingConvention): String;
var
  i, k : integer;
  curDatum : IVDDatum;
  fields : TSerializedFields;
  value : Variant;
begin
  Result := '[';

  if aDataProvider.Count = 0 then
    Result := Result + ' ]'
  else
  begin
    fields := TSerializedFields.Create;
    try
      GetSerializedFields(aDataProvider, fields, aSourceNamingConvention, aDestinationNamingConvention);

      for i := 0 to aDataProvider.Count - 1 do
      begin
        if i > 0 then
          Result := Result + ',{'
        else
          Result := Result + '{';
        curDatum := aDataProvider.GetDatum(i);
        for k := 0 to fields.Count - 1 do
        begin
          if k > 0 then
            Result := Result + ',';

          Result := Result + '"' + fields.Get(k).SerializedFieldName + '":';
          value := curDatum.GetPropertyByFieldName(fields.Get(k).OriginalFieldName);
          if VarIsNull(value) then
            Result := Result + 'null'
          else
          begin
            case fields.Get(k).DataType of
              vftInteger : Result := Result + IntToStr(value);
              vftBoolean : if value then Result := Result + 'true' else Result := Result + 'false';
              vftFloat, vftCurrency : Result := Result + FormatFloat('#.#', value);
              vftDate : Result := Result + '"' + DateToJsonString(VarToDateTime(value)) + '"';
              vftTime : Result := Result + '"' + TimeToJsonString(VarToDateTime(value)) + '"';
              vftDateTime, vftTimeStamp : Result := Result + '"' + DateTimeToJsonString(VarToDateTime(value)) + '"';
            else
{'fraction_non_alphanumeric': 0.11920529801324503, 'fraction_numerical': 0.010381242169321639, 'mean_word_length': 3.072886297376093, 'pattern_counts': {'":': 10, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1778547', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1671, 'n_words': 424}