Jump to content

Search the Community

Showing results for tags 'json'.



More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • Delphi Questions and Answers
    • Algorithms, Data Structures and Class Design
    • VCL
    • FMX
    • RTL and Delphi Object Pascal
    • Databases
    • Network, Cloud and Web
    • Windows API
    • Cross-platform
    • Delphi IDE and APIs
    • General Help
    • Delphi Third-Party
  • C++Builder Questions and Answers
    • General Help
  • General Discussions
    • Embarcadero Lounge
    • Tips / Blogs / Tutorials / Videos
    • Job Opportunities / Coder for Hire
    • I made this
  • Software Development
    • Project Planning and -Management
    • Software Testing and Quality Assurance
  • Community
    • Community Management

Calendars

  • Community Calendar

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Delphi-Version

Found 11 results

  1. I'm working in Delphi 10.2 I will need to make Json object which would need to ignore empty integers, numbers or entire class. For Integers I've tried with attribute [SuppressZero] and it works OK but problem is that integer output is like string, not integer. Is there any solution for that? Example classes: TOtherClass = class private FStringField1: string; FIntegerField1: Integer; FDoubleField1: Double; published property StringField1: string read FStringField1 write FStringField1; property IntegerField1: Integer read FIntegerField1 write FIntegerField1; property DoubleField1: Integer read FDoubleField1 write FDoubleField1; end; TMainClass = class private FStringField: string; FIntegerField: Integer; FDoubleField: Double; FOtherClass: TOtherClass; published property StringField: string read FStringField write FStringField; property IntegerField: Integer read FIntegerField write FIntegerField; property DoubleField: Double read FDoubleField write FDoubleField; property OtherClass: TOtherClass read FOtherClass; public end; If I fill all fields I'll get result like this: { "StringField": "123", "IntegerField": 1, "DoubleField": 111.11, "OtherClass": { "StringField1": "999", "IntegerField1": 9, "DoubleField1": 999.99 } } What I need is that if integers and doubles are 0 then it shoud not exists in result, and if entire Class is empty than that class shoud not be in result Json. That means for: Integerfield=0, DoubleField=0.00, OtherClass->StringField1='', OtherClass->IntegerField1=0, OtherClass->doublefield1=0.00 the result shoud be: { "StringField": "123", } not { "StringField": "123", "IntegerField": 0, "DoubleField": 0.0, "OtherClass": { "StringField1": "", "IntegerField1": 0, "DoubleField1": 0.00 } } or { "StringField": "123", "IntegerField": 0, "DoubleField": 0.0, "OtherClass": { } } I've tried for "integer fields" something like Uwe Raabe did with Date fields and it works for integer values 0(zero) but for integers different from 0 problem is that the result in JSON is string not integer because StringConverter and StringReverter works with string. My idea for class with attribute [SupressZero] TOtherClass = class private FStringField1: string; [SuppressZero] FIntegerField1: Integer; [SuppressZero] FDoubleField1: Double; published property StringField1: string read FStringField1 write FStringField1; property IntegerField1: Integer read FIntegerField1 write FIntegerField1; property DoubleField1: Integer read FDoubleField1 write FDoubleField1; end; TMainClass = class private FStringField: string; [SuppressZero] FIntegerField: Integer; [SuppressZero] FDoubleField: Double; [SuppressZero] FOtherClass: TOtherClass; published property StringField: string read FStringField write FStringField; property IntegerField: Integer read FIntegerField write FIntegerField; property DoubleField: Double read FDoubleField write FDoubleField; property OtherClass: TOtherClass read FOtherClass; public end; My code for integer fields (did not try supressZero for Double and Class because this don't work for integer fields) type SuppressZeroAttribute = class(JsonReflectAttribute) public constructor Create; end; TSuppressZeroInterceptor = class(TJSONInterceptor) public function StringConverter(Data: TObject; Field: string): string; override; procedure StringReverter(Data: TObject; Field: string; Arg: string); override; end; implementation { SuppressZeroAttribute } constructor SuppressZeroAttribute.Create; begin inherited Create(ctString, rtString, TSuppressZeroInterceptor); end; { TSuppressZeroInterceptor } function TSuppressZeroInterceptor.StringConverter(Data: TObject; Field: string): string; var RttiContext: TRttiContext; iValue:integer; begin iValue := RttiContext.GetType(Data.ClassType).GetField(Field).GetValue(Data).AsType<Integer>; if (iValue = 0) then begin Result := EmptyStr; end else begin Result := iValue.ToString; end; end; procedure TSuppressZeroInterceptor.StringReverter(Data: TObject; Field, Arg: string); var RttiContext: TRttiContext; iValue:Integer; begin if (Arg.IsEmpty) or (Arg.StartsWith('0')) or (Arg.ToLower.Contains('null')) then begin iValue := 0; end else begin iValue := arg.ToInteger; end; RttiContext.GetType(Data.ClassType).GetField(Field).SetValue(Data, iValue); end; If looking only string and integer fields for first class result is: for StringField="123", IntegerField=0 { "StringField": "123" -> OK - there is no IntegerField } for StringField="123", IntegerField=123 { "StringField": "123", "IntegerField": "123" -> NOT OK - this shoud be without quotes "" } Is there some solution for "SupressEmptyValues" for integer, double and classes that can be done with JSonReflectAttribute and TJSONInterceptor or should I look for some other solution for my problem? Thanks for replays.
  2. Hi all. I'm new to JSON frameworks and I'm getting lost in a spoonful of water. In the Sydney help I've found this code: JSONValue := TJSONObject.ParseJSONValue('{"colors":[{"name":"red", "hex":"#f00"}]}'); Memo1.Lines.Add('READER:'); if JSONValue is TJSONArray then //... else if JSONVAlue is TJSONObject then Memo1.Lines.Add('colors'); Memo1.Lines.Add('name: '+ JSONValue.GetValue<string>('colors[0].name')); Memo1.Lines.Add('hex: '+ JSONValue.GetValue<string>('colors[0].hex')); Now this works fine but I need to read a client request so formatted: var Text: string; Command: string; JsonValue: TJSONValue; JsonValueArg: TJSONValue; begin // parse json JSONValue := TJSONObject.ParseJSONValue('{"cmd":"program.add.text", "txt":"for I := 0 to 100 do"}'); if not (JSONValue is TJSONObject) then Exit; // gets command type (in Command I've found the "cmd" value "program.add.text". ALL RIGHT!!!) if JSONValue.TryGetValue('cmd', Command) then Exit; // gets command argument txt (does not found the "txt" value and program EXITS!!!) if JSONValue.TryGetValue('txt', Text) then Exit; //... I can't change the JSON request string to contain an array as well as in the Embarcardero sample because reach from a customer program and I've checked with only JSON validator and seem to be fine. What was I've mistaken? Thank you in advance for your replies.
  3. Hi guys! I'm having a little problem, my rest api on horse (Backend, console application), it's working very nice, but, i'm about to receive about 1000 jsons, how can I control that? The response time 201 - Created is about 1 ~ 2 seconds for one json. Thanks.
  4. Erik Vestergaard

    JsonTypeInfo in Delphi?

    Hi I am trying to write a Delphi RESTCall using JSON. We allready have a javaclass that works. It is using this annotation to make Jackson able to parse correctly. @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "className") But what is the correspondent construction in Delphi? My code: s := TJson.ObjectToJsonString(o90RpcCall); The error I get is this: <div>JSON parse error: Missing type id when trying to resolve subtype of [simple type, class java.lang.Object]: missing type id property 'className' (for POJO property 'args'); nested exception is com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id when trying to resolve subtype of [simple type, class java.lang.Object]: missing type id property 'className' (for POJO property 'args') at [Source: (PushbackInputStream); line: 1, column: 181] (through reference chain: o90client.generel.JSONRPCCall["args"]->java.lang.Object[][0])</div></body></html> I guess it is because of the lack of annotation in my class
  5. Hello,I am learning to use the TJSONIterator(Readers and Writers JSON Framework) under delphi 10.3.3 and I found a russian link putting this into practice through the creation of an application assessing the negative impact on the environment: https://webdelphi.ru/. Firstly, i have a custom class definition named "NVOS.JsonParser.pas" and the "open-data-1.json" file, i don't know how to proceed However, I can't figure out already knowing that the result should look like: "App.png". open-data-1.json
  6. Consider this pseudo code uses Rest.Json; TDateClass = class private FHasDate: TDateTime; FNoDate: TDateTIme; public constructor Create; property HasDate: TDateTime read FHasDate write FHasDate; property NoDate: TDateTime read FNoDate write FNoDate; end; constructor TDateClass.Create; begin HasDate := Now; NoDate := 0; end; var Json: String; DateClass: TDateClass; begin DateClass := TDateClass.Create; Json := TJson.ObjectToJsonString(Self, [joIgnoreEmptyStrings, joIgnoreEmptyArrays, joDateIsUTC, joDateFormatISO8601]); which results in the Json string looking like { "HasDate":"2019-02-14T06:09:00.000Z", "NoDate":"1899-12-30T00:00:00.000Z", } while the ideal result would be { "HasDate":"2019-02-14T06:09:00.000Z", } Q:Is there a way to make TDateTime properties with a zero value be output as an empty string - and hence be stripped?
  7. Jacek Laskowski

    JSON string value

    Does Delphi have a function in RTL that will encode the text to a form consistent with the JSON value? For example, from string: "value\with {strange}chars/" to: "\"value\\with\r\n{strange}chars\/\"" It is possible?
  8. Hi, I cannot get this procedure TPerson.LoadSettings(const aJsonString: String). Is there any reason why self instance is not updated with new value ? Is their any workaround or should I create a LoadSettings method outside TPerson ? program Project1; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, REST.JSON, Unit1 in 'Unit1.pas'; var Person:TPerson; jsonstr:string; begin try Person:=TPerson.Create; try person.Name := 'Hello'; jsonstr:= Person.SaveSettings; writeln(jsonstr); //write {"name":"hello"} person.Name := 'hi'; Person.LoadSettings(jsonstr); writeln(person.Name); //write hi instead of Hello readln; finally Person.Free; end; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end. unit Unit1; interface type TPerson = class(TObject) private FName: string; public procedure LoadSettings(const aJsonString: String); function SaveSettings: string; property Name: string read FName write FName; end; implementation uses REST.JSON; procedure TPerson.LoadSettings(const aJsonString: String); begin self := TJSon.JsonToObject<TPerson>(aJsonString); end; function TPerson.SaveSettings: string; begin result := TJSon.ObjectToJsonString(self); end; end. Ps: I'm using Delphi RIO Updt 2
  9. Flavio Basile

    Firedac Json Reflection from XE7 to Rio

    Hi All; we have a big commercial project; It is coded with C++ Builder XE7. We have many Web Broker that after a call returns a dataset by Firedac Json Reflection. We would like to migrate our code from XE7 to Rio but we would like to know if there are difference between Json Reflection format of XE7 and Rio. There are also some important difference in Firedac between XE7 and Rio that we need to evaluate? Could we migrate our code without any important problems? Thanks, Flavio
  10. It is just a small addition to MARS Client library capabilities but TMARSClientResourceJSON now implements REST.Client.IRESTResponseJSON interface (from Delphi's REST Client library). This basically means you can use a TMARSClientResourceJSON and materialize a dataset through the TRESTResponseDataSetAdapter component (from Delphi's REST Client library). Enjoy 😉
  11. I have strange problem occurring when parsing a JSON string on iOS using the System.JSON library in Berlin. I call TJSONObject.ParseJSONValue() to get a value from the start of a JSON string which has worked fine if I run under Windows. As soon as I run under iOS, the same JSON string causes a null pointer exception. When debugging through, the null pointer is the TJSONObject class itself which is odd as ParseJSONValue() is a class function so don’t know why that would ever happen. I suspect this isn’t really the case and is just what the debugger is interpreting (the iOS debugger in Berlin isn’t great). I suspect the JSON string is too large for the parser as it’s a huge string. My question is whether anyone is aware of a size limit for parsing a JSON string or whether there’s a known issue in the Berlin JSON libraries which might cause this? Unfortunately I can’t provide a sample JSON string as it’s a client’s backup data but hopefully someone will be able to help.
×