Jump to content

Recommended Posts

Do we have a working lib/class which implements TNothingable<T> in Delphi?

Where "null" is a valid value.

For simple types I could achieve that relative easily, for structured types it's funnier to implement.

Share this post


Link to post

as an example:

T=type

 y: TNothingable<TArray<string>>;

 z: TNothingable<TSomeObject>;

end;

 

T.tojson => {}

 

T.y := [];

T.y.SetNull;

 

T.tojson => { "y" : null }

 

T.z := nil;

 

T.tojson => { "y": null, "z": null }

 

 

There are API's where I have to pass null values if I want to clear its content and I also want to keep everything mapped.

I'm not willing to map those fields with string literals and build up a json-object.

 

 

Edited by Attila Kovacs

Share this post


Link to post

@Stefan Glienke Please tell this the people who are writing the php API's. I did the same with objects and passed "{}" and the server created the object with default values.

 

@Fritzew A simple webshop php API, but any json API's out there where the values are interpreted individually.

Share this post


Link to post
1 minute ago, Attila Kovacs said:

@Stefan Glienke Please tell this the people who are writing the php API's. I did the same with objects and passed "{}" and the server created the object with default values.

Which is correct, because {} represents an empty object, which is not the same as no object (null).

Share this post


Link to post

If you are telling me that you can't check a json array in php if it's holding a value of null, just if it's empty, then I'm fine, the above example is wrong.

I'm not sure.

 

Bollock what I wrote. It's just a key/value pair. There is no such thing that an array can't be null.

This is the problem.

 

Edit: My example above is indeed wrong on arrays. It should have been t.y.SetNull or similar. [] should translate to [], you are right.

Edited by Attila Kovacs

Share this post


Link to post

Last time I checked this was a Delphi forum, so no clue if php can do this and the API can very well be not 100% adhering the spec - but an array with a null in json would be [null]

Anyway none of that relates to your initial question imo - all mentioned cases can be handled with default data types - explicit nullable type is to add that additional state of nothing/null/nada to a value type.

Share this post


Link to post

Yes, i did the same, returning TNothing = interface end; in the TValue. I just lost my temper on the structured types and I thought I'm asking if somebody has something similar (and maybe much simpler/better) done.

Share this post


Link to post

Ooookay, this conversation brought me to a moment of enlightenment.

We were both right, but they are two different things (null/[]) so I will threat them differently, with the same JSONName attribute.

This solves everything for now. I can keep my object mapping, which was the most important thing for me.

 

Thx guys!

Share this post


Link to post

@Rollo62 Sometimes I have no Idea what am I talking about.

 

Saint Augustine, on being asked what is time: "I know what it is, but when you ask me I don’t."

  • Haha 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×