diff --git a/Source/MARS.Core.JSON.pas b/Source/MARS.Core.JSON.pas index 0925fb38..5f9c1412 100644 --- a/Source/MARS.Core.JSON.pas +++ b/Source/MARS.Core.JSON.pas @@ -65,8 +65,25 @@ JSONNameAttribute = class(TCustomAttribute) TJSONRawString = type string; + TMARSJSONDateFormat = (UNIX, ISO8601); TMARSJSONSerializationOptions = record SkipEmptyValues: Boolean; +// joIgnoreEmptyStrings +// joIgnoreEmptyArrays + DateIsUTC: Boolean; + DateFormat: TMARSJSONDateFormat; +// joDateFormatUnix +// joDateFormatISO8601 +// joDateFormatMongo +// joDateFormatParse +// +// joBytesFormatArray +// joBytesFormatBase64, +// +// joIndentCaseCamel +// joIndentCaseLower +// joIndentCaseUpper +// joIndentCasePreserve end; @@ -92,9 +109,12 @@ TJSONArrayHelper= class helper for TJSONArray public function ToArrayOfRecord(): TArray; procedure FromArrayOfRecord(const AArray: TArray; + const AOptions: TMARSJSONSerializationOptions; const AFilterProc: TToJSONFilterProc = nil); + procedure FromArrayOfObject(const AArray: TArray; - const AOptions: TJsonOptions = [joDateIsUTC, joDateFormatISO8601]); + const AOptions: TMARSJSONSerializationOptions); overload; + procedure FromArrayOfObject(const AArray: TArray); overload; function ForEach(const AFunc: TFunc): Integer; {$ifndef DelphiXE6_UP} @@ -104,8 +124,14 @@ TJSONArrayHelper= class helper for TJSONArray property Items[const Index: Integer]: TJSONValue read GetValue; {$endif} - class function ArrayOfRecordToJSON(const AArray: TArray; const AFilterProc: TToJSONFilterProc = nil): TJSONArray; - class function ArrayOfObjectToJSON(const AArray: TArray): TJSONArray; + class function ArrayOfRecordToJSON(const AArray: TArray; + const AFilterProc: TToJSONFilterProc = nil): TJSONArray; overload; + class function ArrayOfRecordToJSON(const AArray: TArray; + const AOptions: TMARSJSONSerializationOptions; const AFilterProc: TToJSONFilterProc = nil): TJSONArray; overload; + + class function ArrayOfObjectToJSON(const AArray: TArray): TJSONArray; overload; + class function ArrayOfObjectToJSON(const AArray: TArray; + const AOptions: TMARSJSONSerializationOptions): TJSONArray; overload; end; TJSONObjectHelper = class helper(TJSONValueHelper) for TJSONObject @@ -123,8 +149,9 @@ TJSONObjectHelper = class helper(TJSONValueHelper) for TJSONObject {$endif} function ReadDoubleValue(const AName: string; const ADefault: Double = 0.0): Double; function ReadBoolValue(const AName: string; const ADefault: Boolean = False): Boolean; - function ReadDateTimeValue(const AName: string; const ADefault: TDateTime = 0.0; - const AReturnUTC: Boolean = False): TDateTime; + function ReadDateTimeValue(const AName: string; const ADefault: TDateTime; + const AOptions: TMARSJSONSerializationOptions): TDateTime; overload; + function ReadDateTimeValue(const AName: string; const ADefault: TDateTime = 0.0): TDateTime; overload; function ReadUnixTimeValue(const AName: string; const ADefault: TDateTime = 0.0): TDateTime; function ReadValue(const AName: string; const ADefault: TValue; const ADesiredType: TRttiType; const ANameCaseSensitive: Boolean = True): TValue; overload; @@ -139,20 +166,27 @@ TJSONObjectHelper = class helper(TJSONValueHelper) for TJSONObject procedure WriteDoubleValue(const AName: string; const AValue: Double); procedure WriteBoolValue(const AName: string; const AValue: Boolean); procedure WriteDateTimeValue(const AName: string; const AValue: TDateTime; - const AInputIsUTC: Boolean = False); + const AOptions: TMARSJSONSerializationOptions); overload; + procedure WriteDateTimeValue(const AName: string; const AValue: TDateTime); overload; procedure WriteUnixTimeValue(const AName: string; const AValue: TDateTime); - procedure WriteTValue(const AName: string; const AValue: TValue; const AOptions: TMARSJSONSerializationOptions); overload; + procedure WriteTValue(const AName: string; const AValue: TValue; + const AOptions: TMARSJSONSerializationOptions); overload; procedure WriteTValue(const AName: string; const AValue: TValue); overload; procedure WriteArrayValue(const AName: string; const AArray: TJSONArray); overload; inline; + procedure WriteArrayValue(const AName: string; const AArray: TArray; + const AOptions: TMARSJSONSerializationOptions); overload; inline; procedure WriteArrayValue(const AName: string; const AArray: TArray); overload; inline; + procedure FromObject(const AObject: TObject; const AFilterProc: TToJSONFilterProc = nil); overload; procedure FromObject(const AObject: T; const AFilterProc: TToJSONFilterProc = nil); overload; procedure ToObject(const AInstance: TObject; const AFilterProc: TToObjectFilterProc = nil); overload; procedure ToObject(const AInstance: TObject; const AObjectType: TRttiType; const AFilterProc: TToObjectFilterProc = nil); overload; - procedure FromRecord(ARecord: T; const AFilterProc: TToJSONFilterProc = nil); overload; - procedure FromRecord(const ARecord: TValue; const AFilterProc: TToJSONFilterProc = nil); overload; + procedure FromRecord(ARecord: T; + const AOptions: TMARSJSONSerializationOptions; const AFilterProc: TToJSONFilterProc = nil); overload; + procedure FromRecord(const ARecord: TValue; + const AOptions: TMARSJSONSerializationOptions; const AFilterProc: TToJSONFilterProc = nil); overload; function ToRecord(const AFilterProc: TToRecordFilterProc = nil): T; overload; function ToRecord(const ARecordType: TRttiType; const AFilterProc: TToRecordFilterProc = nil): TValue; overload; @@ -162,24 +196,38 @@ TJSONObjectHelper = class helper(TJSONValueHelper) for TJSONObject {$endif} class function DictionaryToJSON(const ADictionary: TObject; - const AOptions: TJsonOptions = [joDateIsUTC, joDateFormatISO8601]): TJSONObject; overload; + const AOptions: TMARSJSONSerializationOptions): TJSONObject; overload; + class function DictionaryToJSON(const ADictionary: TObject): TJSONObject; overload; class function ObjectListToJSON(const AObjectList: TObject; - const AOptions: TJsonOptions = [joDateIsUTC, joDateFormatISO8601]): TJSONArray; overload; + const AOptions: TMARSJSONSerializationOptions): TJSONArray; overload; + class function ObjectListToJSON(const AObjectList: TObject): TJSONArray; overload; class function ObjectToJSON(const AObject: TObject; - const AOptions: TJsonOptions = [joDateIsUTC, joDateFormatISO8601]): TJSONObject; overload; + const AOptions: TMARSJSONSerializationOptions): TJSONObject; overload; + class function ObjectToJSON(const AObject: TObject): TJSONObject; overload; class function JSONToObject(const AJSON: TJSONObject; - const AOptions: TJsonOptions = [joDateIsUTC, joDateFormatISO8601]): T; overload; + const AOptions: TMARSJSONSerializationOptions): T; overload; + class function JSONToObject(const AJSON: TJSONObject): T; overload; + class function JSONToObject(const AClassType: TClass; const AJSON: TJSONObject; - const AOptions: TJsonOptions = [joDateIsUTC, joDateFormatISO8601]): TObject; overload; + const AOptions: TMARSJSONSerializationOptions): TObject; overload; + class function JSONToObject(const AClassType: TClass; const AJSON: TJSONObject): TObject; overload; + class function RecordToJSON(ARecord: T; + const AOptions: TMARSJSONSerializationOptions; + const AFilterProc: TToJSONFilterProc = nil): TJSONObject; overload; class function RecordToJSON(ARecord: T; const AFilterProc: TToJSONFilterProc = nil): TJSONObject; overload; + class function RecordToJSON(const ARecord: TValue; + const AOptions: TMARSJSONSerializationOptions; const AFilterProc: TToJSONFilterProc = nil): TJSONObject; overload; + class function RecordToJSON(const ARecord: TValue; + const AFilterProc: TToJSONFilterProc = nil): TJSONObject; overload; + class function JSONToRecord(const AJSON: TJSONObject; const AFilterProc: TToRecordFilterProc = nil): T; overload; @@ -197,7 +245,9 @@ TJSONObjectHelper = class helper(TJSONValueHelper) for TJSONObject function JsonArrayToIntegerArray(const AJSONArray: TJSONArray): TArray; var DefaultMARSJSONSerializationOptions: TMARSJSONSerializationOptions = ( - SkipEmptyValues: True + SkipEmptyValues: True; + DateIsUTC: True; + DateFormat: ISO8601; ); @@ -438,12 +488,17 @@ function TJSONArrayHelper.ToArrayOfRecord: TArray; Result := Result + [(LElement as TJSONObject).ToRecord()] end; -class function TJSONArrayHelper.ArrayOfObjectToJSON( - const AArray: TArray): TJSONArray; +class function TJSONArrayHelper.ArrayOfObjectToJSON(const AArray: TArray): TJSONArray; +begin + Result := ArrayOfObjectToJSON(AArray); +end; + +class function TJSONArrayHelper.ArrayOfObjectToJSON(const AArray: TArray; + const AOptions: TMARSJSONSerializationOptions): TJSONArray; begin Result := TJSONArray.Create; try - Result.FromArrayOfObject(AArray); + Result.FromArrayOfObject(AArray, AOptions); except Result.Free; raise; @@ -452,10 +507,16 @@ class function TJSONArrayHelper.ArrayOfObjectToJSON( class function TJSONArrayHelper.ArrayOfRecordToJSON(const AArray: TArray; const AFilterProc: TToJSONFilterProc): TJSONArray; +begin + Result := ArrayOfRecordToJSON(AArray, DefaultMARSJSONSerializationOptions, AFilterProc); +end; + +class function TJSONArrayHelper.ArrayOfRecordToJSON(const AArray: TArray; + const AOptions: TMARSJSONSerializationOptions; const AFilterProc: TToJSONFilterProc): TJSONArray; begin Result := TJSONArray.Create; try - Result.FromArrayOfRecord(AArray, AFilterProc); + Result.FromArrayOfRecord(AArray, AOptions, AFilterProc); except Result.Free; raise; @@ -482,8 +543,13 @@ function TJSONArrayHelper.ForEach(const AFunc: TFunc): Integer; end; end; +procedure TJSONArrayHelper.FromArrayOfObject(const AArray: TArray); +begin + FromArrayOfObject(AArray, DefaultMARSJSONSerializationOptions); +end; + procedure TJSONArrayHelper.FromArrayOfObject(const AArray: TArray; - const AOptions: TJsonOptions = [joDateIsUTC, joDateFormatISO8601]); + const AOptions: TMARSJSONSerializationOptions); var LObject: T; LObj: TJSONObject; @@ -497,7 +563,7 @@ procedure TJSONArrayHelper.FromArrayOfObject(const AArray: TArray; end; procedure TJSONArrayHelper.FromArrayOfRecord(const AArray: TArray; - const AFilterProc: TToJSONFilterProc); + const AOptions: TMARSJSONSerializationOptions; const AFilterProc: TToJSONFilterProc); var LRecord: T; LObj: TJSONObject; @@ -510,7 +576,7 @@ procedure TJSONArrayHelper.FromArrayOfRecord(const AArray: TArray; begin LObj := TJSONObject.Create; try - LObj.FromRecord(LRecord, AFilterProc); + LObj.FromRecord(LRecord, AOptions, AFilterProc); AddElement(LObj); except LObj.Free; @@ -572,7 +638,13 @@ function TJSONObjectHelper.GetExactPairName( class function TJSONObjectHelper.JSONToObject(const AClassType: TClass; - const AJSON: TJSONObject; const AOptions: TJsonOptions): TObject; + const AJSON: TJSONObject): TObject; +begin + Result := JSONToObject(AClassType, AJSON, DefaultMARSJSONSerializationOptions); +end; + +class function TJSONObjectHelper.JSONToObject(const AClassType: TClass; + const AJSON: TJSONObject; const AOptions: TMARSJSONSerializationOptions): TObject; var LConstructor: TRttiMethod; LType: TRttiType; @@ -596,10 +668,34 @@ class function TJSONObjectHelper.JSONToObject(const AClassType: TClass; end; end; +class function TJSONObjectHelper.JSONToObject(const AJSON: TJSONObject): T; +begin + Result := JSONToObject(AJSON, DefaultMARSJSONSerializationOptions); +end; + class function TJSONObjectHelper.JSONToObject(const AJSON: TJSONObject; - const AOptions: TJsonOptions = [joDateIsUTC, joDateFormatISO8601]): T; + const AOptions: TMARSJSONSerializationOptions): T; +var + LConstructor: TRttiMethod; + LType: TRttiType; begin + {$IFDEF MARS_JSON_LEGACY} Result := TJSON.JsonToObject(AJSON, AOptions); + {$ELSE} + Result := nil; + LType := TRttiContext.Create.GetType(TypeInfo(T)); + LConstructor := TRTTIHelper.FindParameterLessConstructor(LType); + if not Assigned(LConstructor) then + Exit; + + Result := LConstructor.Invoke((LType as TRttiInstanceType).MetaclassType, []).AsObject as T; + try + AJSON.ToObject(Result, LType); + except + Result.Free; + raise; + end; + {$ENDIF} end; class function TJSONObjectHelper.JSONToRecord(const ARecordType: TRttiType; @@ -616,8 +712,13 @@ class function TJSONObjectHelper.JSONToRecord(const AJSON: TJSONObject; Result := AJSON.ToRecord(AFilterProc); end; +class function TJSONObjectHelper.ObjectListToJSON(const AObjectList: TObject): TJSONArray; +begin + Result := ObjectListToJSON(AObjectList, DefaultMARSJSONSerializationOptions); +end; + class function TJSONObjectHelper.ObjectListToJSON(const AObjectList: TObject; - const AOptions: TJsonOptions): TJSONArray; + const AOptions: TMARSJSONSerializationOptions): TJSONArray; var LResult: TJSONArray; begin @@ -632,8 +733,14 @@ class function TJSONObjectHelper.ObjectListToJSON(const AObjectList: TObject; Result := LResult; end; + +class function TJSONObjectHelper.ObjectToJSON(const AObject: TObject): TJSONObject; +begin + Result := ObjectToJSON(AObject, DefaultMARSJSONSerializationOptions); +end; + class function TJSONObjectHelper.ObjectToJSON(const AObject: TObject; - const AOptions: TJsonOptions): TJSONObject; + const AOptions: TMARSJSONSerializationOptions): TJSONObject; begin {$IFDEF MARS_JSON_LEGACY} Result := TJSON.ObjectToJsonObject(AObject, AOptions); @@ -686,12 +793,17 @@ function TJSONObjectHelper.ReadBoolValue(const AName: string; const ADefault: Bo {$endif} +function TJSONObjectHelper.ReadDateTimeValue(const AName: string; const ADefault: TDateTime): TDateTime; +begin + Result := ReadDateTimeValue(AName, ADefault, DefaultMARSJSONSerializationOptions); +end; + function TJSONObjectHelper.ReadDateTimeValue(const AName: string; const ADefault: TDateTime; - const AReturnUTC: Boolean): TDateTime; + const AOptions: TMARSJSONSerializationOptions): TDateTime; begin Result := ADefault; if Assigned(Self) then - Result := JSONToDate(ReadStringValue(AName), AReturnUTC, ADefault); + Result := JSONToDate(ReadStringValue(AName), AOptions, ADefault); end; function TJSONObjectHelper.ReadDoubleValue(const AName: string; @@ -704,8 +816,13 @@ function TJSONObjectHelper.ReadDoubleValue(const AName: string; Result := LValue.AsDouble; end; +class function TJSONObjectHelper.DictionaryToJSON(const ADictionary: TObject): TJSONObject; +begin + Result := DictionaryToJSON(ADictionary, DefaultMARSJSONSerializationOptions); +end; + class function TJSONObjectHelper.DictionaryToJSON(const ADictionary: TObject; - const AOptions: TJsonOptions): TJSONObject; + const AOptions: TMARSJSONSerializationOptions): TJSONObject; var LResult: TJSONObject; begin @@ -794,7 +911,9 @@ procedure TJSONObjectHelper.FromObject(const AObject: T; FromObject(AObject as TObject, AFilterProc); end; -procedure TJSONObjectHelper.FromRecord(const ARecord: TValue; const AFilterProc: TToJSONFilterProc = nil); +procedure TJSONObjectHelper.FromRecord(const ARecord: TValue; + const AOptions: TMARSJSONSerializationOptions; const AFilterProc: TToJSONFilterProc = nil +); function GetRecordFilterProc(const ARecordType: TRttiType): TToJSONFilterProc; var @@ -860,9 +979,10 @@ procedure TJSONObjectHelper.FromRecord(const ARecord: TValue; const AFilterProc: end; end; -procedure TJSONObjectHelper.FromRecord(ARecord: T; const AFilterProc: TToJSONFilterProc = nil); +procedure TJSONObjectHelper.FromRecord(ARecord: T; + const AOptions: TMARSJSONSerializationOptions; const AFilterProc: TToJSONFilterProc = nil); begin - FromRecord(TValue.From(ARecord), AFilterProc); + FromRecord(TValue.From(ARecord), AOptions, AFilterProc); end; {$ifdef DelphiXE6_UP} @@ -946,10 +1066,17 @@ function TJSONObjectHelper.ReadValue(const AName: string; class function TJSONObjectHelper.RecordToJSON(const ARecord: TValue; const AFilterProc: TToJSONFilterProc): TJSONObject; +begin + Result := RecordToJSON(ARecord, DefaultMARSJSONSerializationOptions, AFilterProc); +end; + +class function TJSONObjectHelper.RecordToJSON(const ARecord: TValue; + const AOptions: TMARSJSONSerializationOptions; + const AFilterProc: TToJSONFilterProc): TJSONObject; begin Result := TJSONObject.Create; try - Result.FromRecord(ARecord, AFilterProc); + Result.FromRecord(ARecord, AOptions, AFilterProc); except Result.Free; raise; @@ -958,10 +1085,17 @@ class function TJSONObjectHelper.RecordToJSON(const ARecord: TValue; class function TJSONObjectHelper.RecordToJSON(ARecord: T; const AFilterProc: TToJSONFilterProc): TJSONObject; +begin + Result := RecordToJSON(ARecord, DefaultMARSJSONSerializationOptions, AFilterProc); +end; + +class function TJSONObjectHelper.RecordToJSON(ARecord: T; + const AOptions: TMARSJSONSerializationOptions; + const AFilterProc: TToJSONFilterProc): TJSONObject; begin Result := TJSONObject.Create; try - Result.FromRecord(ARecord, AFilterProc); + Result.FromRecord(ARecord, AOptions, AFilterProc); except Result.Free; raise; @@ -1237,7 +1371,13 @@ procedure TJSONObjectHelper.WriteArrayValue(const AName: string; procedure TJSONObjectHelper.WriteArrayValue(const AName: string; const AArray: TArray); begin - WriteArrayValue(AName, TJSONArray.ArrayOfRecordToJSON(AArray)); + WriteArrayValue(AName, AArray, DefaultMARSJSONSerializationOptions); +end; + +procedure TJSONObjectHelper.WriteArrayValue(const AName: string; + const AArray: TArray; const AOptions: TMARSJSONSerializationOptions); +begin + WriteArrayValue(AName, TJSONArray.ArrayOfRecordToJSON(AArray, AOptions)); end; procedure TJSONObjectHelper.WriteBoolValue(const AName: string; @@ -1252,9 +1392,15 @@ procedure TJSONObjectHelper.WriteBoolValue(const AName: string; end; procedure TJSONObjectHelper.WriteDateTimeValue(const AName: string; - const AValue: TDateTime; const AInputIsUTC: Boolean); + const AValue: TDateTime); +begin + WriteDateTimeValue(AName, AValue, DefaultMARSJSONSerializationOptions); +end; + +procedure TJSONObjectHelper.WriteDateTimeValue(const AName: string; + const AValue: TDateTime; const AOptions: TMARSJSONSerializationOptions); begin - WriteStringValue(AName, DateToJSON(AValue, AInputIsUTC)); + WriteStringValue(AName, DateToJSON(AValue, AOptions)); end; procedure TJSONObjectHelper.WriteDoubleValue(const AName: string; diff --git a/Source/MARS.Core.Utils.pas b/Source/MARS.Core.Utils.pas index 7a3ab71a..71081b51 100644 --- a/Source/MARS.Core.Utils.pas +++ b/Source/MARS.Core.Utils.pas @@ -73,13 +73,17 @@ TDump = class function ISO8601ToDate(const AISODate: string; AReturnUTC: Boolean = False): TDateTime; {$endif} - function DateToJSON(const ADate: TDateTime; AInputIsUTC: Boolean = False): string; - function JSONToDate(const ADate: string; AReturnUTC: Boolean = False; const ADefault: TDateTime = 0.0): TDateTime; + function DateToJSON(const ADate: TDateTime): string; overload; + function DateToJSON(const ADate: TDateTime; const AOptions: TMARSJSONSerializationOptions): string; overload; + + function JSONToDate(const ADate: string; const ADefault: TDateTime = 0.0): TDateTime; overload; + function JSONToDate(const ADate: string; const AOptions: TMARSJSONSerializationOptions; const ADefault: TDateTime = 0.0): TDateTime; overload; function IsMask(const AString: string): Boolean; function MatchesMask(const AString, AMask: string): Boolean; - function GuessTValueFromString(const AString: string): TValue; + function GuessTValueFromString(const AString: string): TValue; overload; + function GuessTValueFromString(const AString: string; const AOptions: TMARSJSONSerializationOptions): TValue; overload; function TValueToString(const AValue: TValue; const ARecursion: Integer = 0): string; procedure ZipStream(const ASource: TStream; const ADest: TStream; const WindowBits: Integer = 15); @@ -214,6 +218,11 @@ procedure Base64ToStream(const ABase64: string; const ADestStream: TStream); end; function GuessTValueFromString(const AString: string): TValue; +begin + Result := GuessTValueFromString(AString, DefaultMARSJSONSerializationOptions); +end; + +function GuessTValueFromString(const AString: string; const AOptions: TMARSJSONSerializationOptions): TValue; var LValueInteger, LDummy: Integer; LValueDouble: Double; @@ -235,7 +244,7 @@ function GuessTValueFromString(const AString: string): TValue; else if TryStrToBool(AString, LValueBool) then Result := LValueBool else if (AString.CountChar('-') >= 2) and Integer.TryParse(AString.SubString(0, 4), LDummy) - and TryISO8601ToDate(AString.DeQuotedString('"'), LValueDateTime, False) + and TryISO8601ToDate(AString.DeQuotedString('"'), LValueDateTime, AOptions.DateIsUTC) then Result := LValueDateTime else @@ -348,18 +357,28 @@ function MatchesMask(const AString, AMask: string): Boolean; end; -function DateToJSON(const ADate: TDateTime; AInputIsUTC: Boolean = False): string; +function DateToJSON(const ADate: TDateTime): string; +begin + Result := DateToJSON(ADate, DefaultMARSJSONSerializationOptions); +end; + +function DateToJSON(const ADate: TDateTime; const AOptions: TMARSJSONSerializationOptions): string; begin Result := ''; if ADate <> 0 then - Result := DateToISO8601(ADate, AInputIsUTC); + Result := DateToISO8601(ADate, AOptions.DateIsUTC); +end; + +function JSONToDate(const ADate: string; const ADefault: TDateTime = 0.0): TDateTime; +begin + Result := JSONToDate(ADate, DefaultMARSJSONSerializationOptions, ADefault); end; -function JSONToDate(const ADate: string; AReturnUTC: Boolean = False; const ADefault: TDateTime = 0.0): TDateTime; +function JSONToDate(const ADate: string; const AOptions: TMARSJSONSerializationOptions; const ADefault: TDateTime = 0.0): TDateTime; begin Result := ADefault; if ADate<>'' then - Result := ISO8601ToDate(ADate, AReturnUTC); + Result := ISO8601ToDate(ADate, AOptions.DateIsUTC); end; {$ifndef DelphiXE6_UP} diff --git a/Source/MARS.Data.Utils.pas b/Source/MARS.Data.Utils.pas index 95aa21b8..378ece96 100644 --- a/Source/MARS.Data.Utils.pas +++ b/Source/MARS.Data.Utils.pas @@ -61,7 +61,7 @@ function NumericFieldToJSON(const AField: TField): TJSONValue; end; -function DateFieldToJSON(const AField: TField; const AInputIsUTC: Boolean = False): string; +function DateFieldToJSON(const AField: TField; const AOptions: TMARSJSONSerializationOptions): string; overload; var LDisplayFormat: string; begin @@ -74,9 +74,15 @@ function DateFieldToJSON(const AField: TField; const AInputIsUTC: Boolean = Fals if LDisplayFormat <> '' then Result := AField.DisplayText else - Result := DateToJSON(AField.AsDateTime, AInputIsUTC); + Result := DateToJSON(AField.AsDateTime, AOptions); end; +function DateFieldToJSON(const AField: TField): string; overload; +begin + Result := DateFieldToJSON(AField, DefaultMARSJSONSerializationOptions); +end; + + function RecordToJSONObject(const ADataSet: TDataSet; const ARootPath: string = ''): TJSONObject; var LField: TField; diff --git a/tests/Tests.MessageBodyReaders.pas b/tests/Tests.MessageBodyReaders.pas index 9810bdde..28dbf41a 100644 --- a/tests/Tests.MessageBodyReaders.pas +++ b/tests/Tests.MessageBodyReaders.pas @@ -127,7 +127,7 @@ procedure TMARSRecordReaderTest.PrimitiveTypes; '{' + ' "AString": "Andrea", "ABoolean": true, "AInteger": 123,' + ' "AFloat": 1234.56789, "ACurrency": 7.75, ' - + ' "ADate": "1982-05-24T00:00:00.000+02:00", "AChar": "C"' + + ' "ADate": "1982-05-24T00:00:00.000+00:00", "AChar": "C"' + '}' ); LValue := FMBR.ReadFrom(LData, FPrimitiveTypesRttiObject, FJSONMediaType, nil).AsType; @@ -147,7 +147,7 @@ procedure TMARSRecordReaderTest.Simple; LData: TBytes; begin LData := TEncoding.UTF8.GetBytes( - '{ "Name": "Andrea", "Surname": "Magni", "DateOfBirth": "1982-05-24T00:00:00.000+02:00" }' + '{ "Name": "Andrea", "Surname": "Magni", "DateOfBirth": "1982-05-24T00:00:00.000+00:00" }' ); LValue := FMBR.ReadFrom(LData, FPersonRttiObject, FJSONMediaType, nil).AsType; @@ -287,7 +287,7 @@ procedure TMARSArrayOfRecordReaderTest.SingleItem; LData: TBytes; begin LData := TEncoding.UTF8.GetBytes( - '[{"Name":"Andrea", "Surname": "Magni", "DateOfBirth": "1982-05-24T00:00:00.000+02:00"}]' + '[{"Name":"Andrea", "Surname": "Magni", "DateOfBirth": "1982-05-24T00:00:00.000+00:00"}]' ); LValue := FMBR.ReadFrom(LData, FPersonArrayRttiObject, FJSONMediaType, nil).AsType>;