#1 2015-12-10 11:17:29

hnb
Member
Registered: 2015-06-15
Posts: 290

[patch] AddOrUpdateObject for TObject

Another patch related to previous proposition "Don't store empty string patch" ( http://synopse.info/forum/viewtopic.php?id=3063 ). I think that "AddOrUpdateObject" attached patch, can be much improved (code below is used in my project, mainly for configuration classes).

procedure AddOrUpdateObject(ObjectInstance: TObject; const NewValues: Variant; OnlyAddMissing: boolean=false);
var
  LJson: RawJSON;
  LVariant: Variant;
  LVariantData: TDocVariantData absolute LVariant;
begin
  LJson := ObjectToJSON(ObjectInstance, [woDontStoreDefault, woDontStoreEmptyString]);
  LVariant := _Json(LJson);
  LVariantData.AddOrUpdateObject(NewValues, OnlyAddMissing);
  LJson := VariantSaveJSON(LVariant);
  ObjectLoadJSON(ObjectInstance, LJson, nil, [j2oIgnoreUnknownProperty, j2oIgnoreUnknownEnum]);
end;

procedure AddOrUpdateObject(ObjectInstance: TObject; const NewValues: TObject; OnlyAddMissing: boolean=false);
var
  LJson: RawJSON;
begin
  LJson := ObjectToJSON(NewValues, [woDontStoreDefault, woDontStoreEmptyString]);
  AddOrUpdateObject(ObjectInstance, _Json(LJson), OnlyAddMissing);
end; 

Last edited by hnb (2015-12-10 11:30:57)


best regards,
Maciej Izak

Offline

Board footer

Powered by FluxBB