unit osjson;

// This code is part of the opsi.org project

// Copyright (c) uib gmbh (www.uib.de)
// This sourcecode is owned by the uib gmbh, D55118 Mainz, Germany
// and published under the Terms of the GNU Affero General Public License.
// Text of the AGPL: http://www.gnu.org/licenses/agpl-3.0-standalone.html
// author: Rupert Roeder, detlef oertel
// credits: http://www.opsi.org/credits/


{$mode delphi}
{$RANGECHECKS ON}
{$OVERFLOWCHECKS ON}
{$IOCHECKS ON}
{$OBJECTCHECKS ON}
{$VARSTRINGCHECKS ON}
{$LONGSTRINGS ON}


interface

uses
  Classes, SysUtils, FpJson, JsonParser;

function jsonIsValid(str:string) : boolean;
function jsonGetCount(str:string) : integer;
function jsonGetPartByIndex(str:string; index:integer) : String;
function jsonGetValueByKey(str:string; key:string) : String;
function jsonGetNames(str:string) : TStringlist;
function jsonGetIndexByKey(str:string; key : string) : integer;

implementation

var
jo : TJSONObject;
ja : TJSONArray;
jp : TJSONParser;
jd : TJSONData;

function str2jsondata(str:string) : TJsondata;
var
myparser : TJSONParser;
myjo : TJSONObject;
myjd : TJSONData;
begin
  result := nil;
  try
    try
      myparser := TJSONParser.Create(str);
      result := myparser.Parse;
    except
      // not json
    end;
  finally
    myparser.Free;
  end;
end;


function jsonGetIndexByKey(str:string; key : string) : integer;
var
myjd : TJSONData;
myjo : TJSONObject;
i : integer;
begin
  try
    try
      myjd := str2jsondata(str);
      myjo := TJSONObject.Create;
      myjo := TJSONObject(myjd);
      result := myjo.IndexOfName(key);
    except
      // not json
    end;
  finally
    myjo.Free;
  end;
end;


function jsonGetNames(str:string) : TStringlist;
var
myjd : TJSONData;
myjo : TJSONObject;
i : integer;
begin
  try
    try
      result := TStringlist.Create;
      myjd := str2jsondata(str);
      myjo := TJSONObject.Create;
      myjo := TJSONObject(myjd);
      for i := 0 to myjo.Count-1 do
       result.Append(myjo.Names[i]);
    except
      // not json
    end;
  finally
    myjo.Free;
  end;
end;


function jsonGetValueByKey(str:string; key:string) : String;
var
myjd : TJSONData;
myjo : TJSONObject;
begin
  try
    try
      myjd := str2jsondata(str);
      myjo := TJSONObject.Create;
      myjo := TJSONObject(myjd);
      result := myjo.Elements[key].AsString;
    except
      // not json
    end;
  finally
    myjo.Free;
  end;
end;


function jsonGetPartByIndex(str:string; index:integer) : String;
var
  myjd : TJSONData;
begin
  myjd := str2jsondata(str);
  //myjo := TJSONObject.Create;
  //myja := TJSONArray.Create;
  //myja.Add(myjd);
  if (index >= 0) and (index < myjd.Count) then
    result := myjd.Items[index].AsJSON;
end;


function jsonGetCount(str:string) : integer;
var
  myjd : TJSONData;
begin
  myjd := str2jsondata(str);
  result := myjd.Count;
end;

function jsonIsValid(str:string) : boolean;
var
myparser : TJSONParser;
myjo : TJSONObject;
myjd : TJSONData;
begin
  result := false;
  try
    try
      myjo := TJSONObject.Create;
      myparser := TJSONParser.Create(str);
      myjd := myparser.Parse;
      result := true;
    except
      // not json
    end;
  finally
    myparser.Free;
    myjo.Free;
  end;
end;



function str2jsonobject(str:string) : TJSONData;
var
myparser : TJSONParser;
myjo : TJSONObject;
myjd : TJSONData;
begin
  result := nil;
  try
    try
      myparser := TJSONParser.Create(str);
      result := myparser.Parse;
    except
      // not json
    end;
  finally
    myparser.Free;
  end;
end;



end.

