Advertisement
KiberInfinity

Jsprs

Nov 3rd, 2014
201
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // JSON Parser  - one level parse Array or Object to WStrList
  2. type
  3.   TJsPair = record
  4.     Name:string;
  5.     Value:string;
  6.   end;
  7.  
  8. function IsJsonArray(const S: String): Boolean;
  9. var
  10.   Len: Integer;
  11. begin
  12.   Len := Length(S);
  13.   Result := (Len >= 2) and (S[1] = '[') and (S[Len] = ']');
  14. end;
  15. function IsJsonObject(const S: String): Boolean;
  16. var
  17.   Len: Integer;
  18. begin
  19.   Len := Length(S);
  20.   Result := (Len >= 2) and (S[1] = '{') and (S[Len] = '}');
  21. end;
  22. function IsJsonString(const S: String): Boolean;
  23. var
  24.   Len: Integer;
  25. begin
  26.   Len := Length(S);
  27.   Result := (Len >= 2) and (S[1] = '"') and (S[Len] = '"');
  28. end;
  29.  
  30. function JsonParse(JsonString:string; OutList:PWStrList):Boolean;
  31.   procedure Split(const S: String; const Delimiter: Char;
  32.     Strings: PWStrList);
  33.  
  34.     function IsPairBegin(C: Char): Boolean;
  35.     begin
  36.       Result := (C = '{') or (C = '[') or (C = '"');
  37.     end;
  38.  
  39.     function GetPairEnd(C: Char): Char;
  40.     begin
  41.       case C of
  42.         '{': Result := '}';
  43.         '[': Result := ']';
  44.         '"': Result := '"';
  45.         else Result := #0;
  46.       end;
  47.     end;
  48.  
  49.     function MoveToPair(P: PChar): PChar;
  50.     var
  51.       PairBegin, PairEnd: Char;
  52.       C: Char;
  53.     begin
  54.       PairBegin := P^;
  55.       PairEnd := GetPairEnd(PairBegin);
  56.       Result := P;
  57.       while Result^ <> #0 do
  58.       begin
  59.         Inc(Result);
  60.         C := Result^;
  61.         if C = PairEnd then Break
  62.         else if (PairBegin = '"') and (C = '\') then Inc(Result)
  63.         else if (PairBegin <> '"') and IsPairBegin(C) then Result := MoveToPair(Result);
  64.       end;
  65.     end;
  66.   var
  67.     PtrBegin, PtrEnd: PChar;
  68.     C: Char;
  69.     StrItem: String;
  70.   begin
  71.     PtrBegin := PChar(S);
  72.     PtrEnd := PtrBegin;
  73.     while PtrEnd^ <> #0 do
  74.     begin
  75.       C := PtrEnd^;
  76.       if C = Delimiter then
  77.       begin
  78.         StrItem := Trim(Copy(PtrBegin, 1, PtrEnd - PtrBegin));
  79.         Strings.Add(StrItem);
  80.         PtrBegin := PtrEnd + 1;
  81.         PtrEnd := PtrBegin;
  82.         Continue;
  83.       end
  84.       else if IsPairBegin(C) then PtrEnd := MoveToPair(PtrEnd);
  85.       Inc(PtrEnd);
  86.     end;
  87.     StrItem := Trim(Copy(PtrBegin, 1, PtrEnd - PtrBegin));
  88.     if StrItem <> '' then Strings.Add(StrItem);
  89.   end;
  90.  
  91.   function JsPairParse(JsonString: String):TJsPair;
  92.   var
  93.     List: PWStrList;
  94.     StrName, StrVal: String;
  95.   begin
  96.     Result.Name  := '';
  97.     Result.Value := '';
  98.  
  99.     List := NewWStrList;
  100.     try
  101.       Split(JsonString, ':', List);
  102.       if List.Count >= 2 then
  103.       begin
  104.         StrName := List.Items[0];
  105.         StrVal := List.Items[1];
  106.         Result.Name  := Copy(StrName, 2, Length(StrName) - 2);
  107.         if IsJsonString(StrVal) then  // Remove " "
  108.           Result.Value := Copy(StrVal, 2, Length(StrVal) - 2)
  109.         else
  110.           Result.Value := StrVal;
  111.       end;
  112.     finally
  113.       List.Free;
  114.     end;
  115.   end;
  116.  
  117. var
  118.   I: Integer;
  119.   S: String;
  120.   List: PWStrList;
  121.   Item: TJsPair;
  122.   isJO, isJA: Boolean;
  123. begin
  124.   OutList.Clear;
  125.   JsonString := Trim(JsonString);
  126.   isJO := IsJsonObject(JsonString);
  127.   isJA := IsJsonArray(JsonString);
  128.  
  129.   if isJO or isJA then
  130.   begin
  131.     S := Trim(Copy(JsonString, 2, Length(JsonString) - 2));
  132.     List := NewWStrList;
  133.     List.Clear;
  134.     try
  135.       Split(S, ',', List);
  136.       for I := 0 to List.Count - 1 do
  137.       begin
  138.         if isJO then
  139.         begin
  140.           Item := JsPairParse(List.Items[I]);
  141.           OutList.Values[Item.Name] := Item.Value;
  142.         end else
  143.         if isJA then
  144.         begin
  145.           S := List.Items[I];
  146.           if IsJsonString(S) then  // Remove " "
  147.             OutList.Add(Copy(S, 2, Length(S) - 2))
  148.           else
  149.             OutList.Add(S);
  150.         end;
  151.       end;
  152.       Result:=True;
  153.     finally
  154.       List.Free;
  155.     end;
  156.   end else begin
  157.     Result:=False;
  158.     exit;
  159.   end;
  160. end;
  161. // JSON Parser end
Advertisement
RAW Paste Data Copied
Advertisement