-
Apr 24th, 2022, 03:54 AM
#1
Thread Starter
Lively Member
Serialisation of a Null value CLASS
I am trying to find a way to remove a class which has a single property which is null. The Newtonsoft.Json NullValueHandling.Ignore setting removes Null properties completely during serialisation, but if there are no other properties in the class, the class still remains in the construct and the server I am submitting to flags an error.
As the field is conditional to the server, I need it to be present sometimes.
As example:
Code:
.payeeProvider = New Payeeprovider With {
.providerNumber = "8888888H"}
returns "payeeProvider":{"providerNumber":"8888888H"}, to the JSON construct, which is correct,
while:
Code:
.payeeProvider = New Payeeprovider With {
.providerNumber = Nothing}
returns "payeeProvider":{} - and this produces a "property missing" error on submission.
Is there a way of removing the class when the property is Null?
-
Apr 24th, 2022, 04:16 AM
#2
Re: Serialisation of a Null value CLASS
Originally Posted by Bear89
I am trying to find a way to remove a class which has a single property which is null. The Newtonsoft.Json NullValueHandling.Ignore setting removes Null properties completely during serialisation, but if there are no other properties in the class, the class still remains in the construct and the server I am submitting to flags an error.
As the field is conditional to the server, I need it to be present sometimes.
As example:
Code:
.payeeProvider = New Payeeprovider With {
.providerNumber = "8888888H"}
returns "payeeProvider":{"providerNumber":"8888888H"}, to the JSON construct, which is correct,
while:
Code:
.payeeProvider = New Payeeprovider With {
.providerNumber = Nothing}
returns "payeeProvider":{} - and this produces a "property missing" error on submission.
Is there a way of removing the class when the property is Null?
Might be easier to not add an instance if there are no properties set.
-
Apr 24th, 2022, 04:24 AM
#3
Thread Starter
Lively Member
Re: Serialisation of a Null value CLASS
I thought that might be the case, as I found no solutions anywhere, its just clumsy in coding, but will have to do that...
-
Apr 24th, 2022, 06:12 AM
#4
Re: Serialisation of a Null value CLASS
I am no expert when it comes to JSON but I read something about creating custom converters. I wonder whether that would work here.
-
Apr 24th, 2022, 06:30 AM
#5
Thread Starter
Lively Member
Re: Serialisation of a Null value CLASS
Well done JMc, Microsoft describes it in their online documentation at
https://docs.microsoft.com/en-us/dot...ots=dotnet-6-0
But its pretty complex, I was hoping for a simple solution, but I think I’ll just pass on it for now and perhaps have a closer look later.
But thanks for alerting me to the option!
-
Apr 24th, 2022, 06:37 AM
#6
Re: Serialisation of a Null value CLASS
Originally Posted by Bear89
I am trying to find a way to remove a class which has a single property which is null. The Newtonsoft.Json NullValueHandling.Ignore setting removes Null properties completely during serialisation, but if there are no other properties in the class, the class still remains in the construct and the server I am submitting to flags an error.
As the field is conditional to the server, I need it to be present sometimes.
As example:
Code:
.payeeProvider = New Payeeprovider With {
.providerNumber = "8888888H"}
returns "payeeProvider":{"providerNumber":"8888888H"}, to the JSON construct, which is correct,
while:
Code:
.payeeProvider = New Payeeprovider With {
.providerNumber = Nothing}
returns "payeeProvider":{} - and this produces a "property missing" error on submission.
Is there a way of removing the class when the property is Null?
Just had a thought... Is this a public api or a private / internal api? I don't suppose you know if they provide a swagger definition for the api? If they do have a swagger api then you can normally get to it by adding "swagger" as an endpoint e.g. If the api is available at https://test.com/api try browsing to https://test.com/api/swagger - if that doesn't work maybe try https://test.com/swagger instead. Might even be worth contacting whoever provides the api and asking if they have a swagger definition available.
If one is available it can really simplify all of this process.
Last edited by PlausiblyDamp; Apr 24th, 2022 at 10:33 AM.
-
Apr 24th, 2022, 09:58 AM
#7
Thread Starter
Lively Member
Re: Serialisation of a Null value CLASS
Nice idea pd, but unfortunately its a private api. I’ll see if they have a swagger but I doubt it, nothing on the developer website (which is private).
I managed to complete a successful test run today with the code, all elements now working, and the remainder of the project is essentially the same, so hopefully I should be fine. I just need to work on the responses now which should be OK, and as I said once I have it in a more mature form I’ll post back.
Last edited by Bear89; Apr 24th, 2022 at 10:04 AM.
-
Apr 25th, 2022, 07:56 AM
#8
Re: Serialisation of a Null value CLASS
Ask about hte swagger page... doesn't matter if it's a private or public api. I work with a lot of private apis, but we have swagger pages on nearly all of them. That said, it's only as good as the documentation that's supplied with it (I'm currently working with one where the documentation says I need to pass in "x: {}"... which doesn't work... it actually wants { x: {}} .. still... it's worth asking about.
That said ... yes, in order to null out a json property, that property will need to be Nothing. Doesn't matter if all the child props are null/empty/nothing. You'll either need to add additional code to check the child properties, or flag, or somehting to indicate if child props were set or not. This can be done at serialization in the calling code, or in a custom converter.
-tg
-
Apr 25th, 2022, 08:02 AM
#9
Thread Starter
Lively Member
Re: Serialisation of a Null value CLASS
Thanks tg, I’ll ask them.
Ok, so if I’m reading you, in my example above, where the child is defined as nothing, I’d also need to define the parent (Payeeprovider) as nothing as well? If not how else would I code it?
-
Apr 25th, 2022, 08:10 AM
#10
Re: Serialisation of a Null value CLASS
Instead of:
Code:
.payeeProvider = New Payeeprovider With {
.providerNumber = Nothing}
use:
Code:
.payeeProvider = Nothing
-tg
-
Apr 25th, 2022, 08:12 AM
#11
Re: Serialisation of a Null value CLASS
What I do in these cases, where there's stuff that's conditional, I'll create a helper funciton... createPayeeprovider() for instance ... pass it the information needed to create the object, then in there, do the logic so that it either returns a Payeeprovider object with its props set... or Nothing if one wasn't created.
-tg
-
Apr 25th, 2022, 11:50 AM
#12
Thread Starter
Lively Member
Re: Serialisation of a Null value CLASS
Excellent, thank you tg.
Bear
-
Apr 26th, 2022, 12:18 AM
#13
Thread Starter
Lively Member
Re: Serialisation of a Null value CLASS
tg, what syntax do you use, I can't see a way to set .payeeProvider to nothing as conditional - so in my project, .payeeProvider as a class has only one property (.providerNumber = payeeprov), with payeeprov sometimes containing a value. So I still need the first construct in #10 above, and can't always have the second construct.
I have looked at adding the following to code prior to the serialisation line:
Code:
ShouldSerializepayeeProvider() As Boolean
from:
Code:
Public Function ShouldSerializepayeeProvider() As Boolean
Return payeeProv Is Nothing
End Function
but I cant see the correct syntax to make it work - serialise still returns the label payeeProvider without content which the server rejects. Have you any experience with the ShouldSerialize function?
-
Apr 26th, 2022, 06:14 AM
#14
Re: Serialisation of a Null value CLASS
payeeProv is Nothing but an expression that's going to return a boolean ...
I showed you how to do it in post #10 ... but here's a larger example:
Code:
Private Function createPayeeProvider() as PayeeProvider
Dim newPayeeProvider as PayeeProvider = Nothing
If (put your checks here to see if yoiu need to create an object)
newPayeeProvider = new PayeeProvider()
newPayeeProvicer.Property = what ever
End If
return newPayeeProvider
End Sub
So initially we asssume we don't need it and set the newPayeeProvider to Nothing... then we check what ever data you want to check see if one does need to be created.
If so, create it, and set the properties. If you don't, then newPayeeProvider remains Nothing.
Either wayl, the last thing done is to return it. IT will be either Nothing, or it'll be an object with properties set.
Now... call it like this:
Code:
.payeeProvider = createPayeeProvider()
-tg
-
Apr 26th, 2022, 07:39 AM
#15
Thread Starter
Lively Member
Re: Serialisation of a Null value CLASS
Ah, I've got it, runs perfectly, thank you!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|