123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- unit jsonhelper;
- interface
- uses
- System.Generics.Collections, System.JSON, System.IOUtils, System.SysUtils;
- type
- TJSONKind = (jkNone, jkObject, jkArray, jkString, jkNumber, jkBoolean, jkNull);
- TJSONPairHelper = class helper for TJSONPair
- public
- procedure Rename(ANewName: string);
- end;
- TJSONValueHelper = class helper for TJSONValue
- private
- function GetJsonKind: TJSONKind;
- function GetItemCount: Integer;
- function GetItem(const AIndex: Integer): TJSONAncestor;
- public
- property JsonKind: TJSONKind read GetJsonKind;
- property ItemCount: Integer read GetItemCount;
- property Items[const AIndex: Integer]: TJSONAncestor read GetItem; Default;
- public
- function IsObjectOrArray: boolean;
- class function LoadFromFile(const AFileName: string; AUseBool: boolean = False; ARaiseExc: boolean = False): TJSONValue;
- end;
- TJSONObjectHelper = class helper for TJSONObject
- public
- function DeletePair(AIndex: Integer): TJSONPair; overload;
- function DeletePair(const AItem: TJSONPair): TJSONPair; overload;
- procedure InsertPair(AIndex: NativeInt; const AValue: TJSONPair);
- procedure Move(ACurIndex, ANewIndex: NativeInt);
- end;
- TJSONArrayHelper = class helper for TJSONArray
- public
- function Delete(const AItem: TJSONValue): TJSONValue;
- function IndexOf(const AItem: TJSONValue): Integer;
- procedure SetValue(const AIndex: Integer; const AValue: TJSONValue);
- procedure InsertElement(AIndex: NativeInt; const AValue: TJSONValue);
- procedure Move(ACurIndex, ANewIndex: NativeInt);
- end;
- function JSONFormat(AValue: TJSONValue; AIndentation: Integer; AEncodeBelow32: boolean; AEncodeAbove127: boolean): string;
- implementation
- function JSONToUniCode(const AStr: string; AEncodeBelow32: boolean = True; AEncodeAbove127: boolean = True): string;
- var
- ch: char;
- I: Integer;
- UnicodeValue: Integer;
- Buff: array [0 .. 5] of char;
- begin
- for I := 1 to AStr.Length do
- begin
- ch := AStr[I];
- case ch of
- #0 .. #7, #$b, #$e .. #31, #$0080 .. High(char):
- begin
- UnicodeValue := Ord(ch);
- if AEncodeBelow32 and (UnicodeValue < 32) or AEncodeAbove127 and (UnicodeValue > 127) then
- begin
- Buff[0] := '\';
- Buff[1] := 'u';
- Buff[2] := char(DecimalToHex((UnicodeValue and 61440) shr 12));
- Buff[3] := char(DecimalToHex((UnicodeValue and 3840) shr 8));
- Buff[4] := char(DecimalToHex((UnicodeValue and 240) shr 4));
- Buff[5] := char(DecimalToHex((UnicodeValue and 15)));
- Result := Result + Buff;
- end
- else
- begin
- Result := Result + ch;
- end;
- end
- else
- begin
- Result := Result + ch;
- end;
- end;
- end;
- end;
- function JsonToJsonStr(AValue: TJSONValue; AEncodeBelow32: boolean = True; AEncodeAbove127: boolean = True): string;
- var
- Options: TJSONAncestor.TJSONOutputOptions;
- begin
- Result := '';
- if AValue <> nil then
- begin
- Options := [];
- if AEncodeBelow32 then
- Include(Options, TJSONAncestor.TJSONOutputOption.EncodeBelow32);
- if AEncodeAbove127 then
- Include(Options, TJSONAncestor.TJSONOutputOption.EncodeAbove127);
- Result := AValue.ToJSON(Options);
- end;
- end;
- function JSONFormat(AValue: TJSONValue; AIndentation: Integer; AEncodeBelow32: boolean; AEncodeAbove127: boolean): string;
- begin
- if AIndentation >= 0 then
- begin
- Result := '';
- if AValue <> nil then
- begin
- Result := AValue.Format(AIndentation);
- if AEncodeBelow32 or AEncodeAbove127 then
- begin
- Result := JSONToUniCode(Result, AEncodeBelow32, AEncodeAbove127);
- end;
- end;
- end
- else
- begin
- Result := JsonToJsonStr(AValue, AEncodeBelow32, AEncodeAbove127);
- end;
- end;
- procedure TJSONPairHelper.Rename(ANewName: string);
- begin
- SetJsonString(TJSONString.Create(ANewName));
- end;
- ////////////////////////////////////////////////////////////////////////////////
- // TJSONValue
- function TJSONValueHelper.GetJsonKind: TJSONKind;
- begin
- if self = nil then
- Exit(jkNone);
- if ClassType = TJSONObject then
- begin
- Result := jkObject;
- end
- else if ClassType = TJSONArray then
- begin
- Result := jkArray;
- end
- else if ClassType = TJSONString then
- begin
- Result := jkString;
- end
- else if ClassType = TJSONNumber then
- begin
- Result := jkNumber;
- end
- else if ClassType = TJSONBool then
- begin
- Result := jkBoolean;
- end
- else if ClassType = TJSONNull then
- begin
- Result := jkNull;
- end
- else
- begin
- Result := jkNone;
- end;
- end;
- function TJSONValueHelper.IsObjectOrArray: boolean;
- begin
- Result := (self is TJSONObject) or (self is TJSONArray);
- end;
- class function TJSONValueHelper.LoadFromFile(const AFileName: string; AUseBool: boolean; ARaiseExc: boolean): TJSONValue;
- begin
- Result := nil;
- try
- Result := TJSONValue.ParseJSONValue(TFile.ReadAllText(AFileName, TEncoding.UTF8), AUseBool, ARaiseExc);
- except
- on E: Exception do
- begin
- if ARaiseExc then
- begin
- raise Exception.Create(E.Message);
- end;
- end;
- end;
- end;
- function TJSONValueHelper.GetItemCount: Integer;
- begin
- if self is TJSONObject then
- Result := TJSONObject(self).Count
- else if self is TJSONArray then
- Result := TJSONArray(self).Count
- else
- Result := 0;
- end;
- function TJSONValueHelper.GetItem(const AIndex: Integer): TJSONAncestor;
- begin
- if self is TJSONObject then
- Result := TJSONObject(self).Pairs[AIndex]
- else if self is TJSONArray then
- Result := TJSONArray(self)[AIndex]
- else
- Result := nil;
- end;
- ////////////////////////////////////////////////////////////////////////////////
- // TJSONObject
- function TJSONObjectHelper.DeletePair(AIndex: Integer): TJSONPair;
- begin
- with self do
- begin
- if (AIndex >= 0) and (AIndex < FMembers.Count) then
- begin
- Result := FMembers[AIndex];
- FMembers.Remove(Result);
- end
- else
- begin
- Result := nil;
- end;
- end;
- end;
- function TJSONObjectHelper.DeletePair(const AItem: TJSONPair): TJSONPair;
- begin
- with self do
- begin
- Exit(FMembers.Extract(AItem));
- end;
- end;
- procedure TJSONObjectHelper.InsertPair(AIndex: NativeInt; const AValue: TJSONPair);
- begin
- with self do
- begin
- FMembers.Insert(AIndex, AValue);
- end;
- end;
- procedure TJSONObjectHelper.Move(ACurIndex, ANewIndex: NativeInt);
- begin
- with self do
- begin
- FMembers.Move(ACurIndex, ANewIndex);
- end;
- end;
- function TJSONArrayHelper.Delete(const AItem: TJSONValue): TJSONValue;
- begin
- with self do
- begin
- Result := FElements.Extract(AItem);
- end;
- end;
- function TJSONArrayHelper.IndexOf(const AItem: TJSONValue): Integer;
- begin
- with self do
- begin
- Result := FElements.IndexOf(AItem);
- end;
- end;
- procedure TJSONArrayHelper.SetValue(const AIndex: Integer; const AValue: TJSONValue);
- begin
- with self do
- begin
- if FElements[AIndex].Owned then
- FElements[AIndex].Free;
- FElements[AIndex] := AValue;
- end;
- end;
- procedure TJSONArrayHelper.InsertElement(AIndex: NativeInt; const AValue: TJSONValue);
- begin
- with self do
- begin
- FElements.Insert(AIndex, AValue);
- end;
- end;
- procedure TJSONArrayHelper.Move(ACurIndex, ANewIndex: NativeInt);
- begin
- with self do
- begin
- FElements.Move(ACurIndex, ANewIndex);
- end;
- end;
- end.
|