52 Replies
$paste
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
BlazeBin - mlwyzvpuazmf
A tool for sharing your source code with the world!
BlazeBin - gjeosgyztenv
A tool for sharing your source code with the world!
it just returns nulll
using blazor
You're deserializing to an object
But the API returns a list of objects
it's crucial you determine which part returns null
(by debugging)
[JsonPropertyName("coordinates")] public List<List<List<object>>> Coordinates { get; set; }
kekAh, damn, didn't notice that lol
Wouldn't it just be
List<List<double>>
, looking at the json?Ah, right, missed that one
[
Although...
Depth depends on the type
First one is
Polygon
, the other is MultiPolygon
I'd imagine Point
would just be a List<double>
and a Path
would be List<List<double>>
polymorphic deserialization ftw
That's where union types would come in handy
BlazeBin - dnzhwlvzjeqi
A tool for sharing your source code with the world!
this doesnt still work
"[JsonPropertyName("coordinates")] public List<List<List<object>>> Coordinates { get; set; }"
changed this too
Do you still deserialize to a single object though?
thats because it has two different shapes
huh ? so how do i solve this
Make that property of type
object
and cast depending on what Type
it is I guessor
[JsonDerivedType]
hm, JsonDerivedType
might be annoying as the type descriminator is inside the geometry object
might still be a way to solve thistried it with "[JsonDerivedType]"
got a bunch of errors maybe did it wrong dont know
its not as easy as just slapping the attribute down
how would you have done it ?
ZZZZ's suggested way is by far the easiest
I think there is a way to get it working with polymorphic deserialization, but its not trivial
I tried making
Geometry
polymorphic, and it would work if the type descriminator was outside the geometry object
but its not
might need a custom converterNah, I'd legit just convert it at the point of usage
I agree with you, but now I just wanna figure this out 😄
it should be possible
Even if you were to use a custom converter, you can't exactly say that a property will be
T[] | T[][] | T[][][]
Unless we start introducing some Either
typeswe solve that by making
Geometry
a class hierarchy
System.Text.Json.JsonException: Read unrecognized type discriminator id 'LineString'. Path: $[2].warningAreas[0].area.features[0].geometry | LineNumber: 5866 | BytePositionInLine: 30.
hmmif you find the answer post it xd
Ah fuck
there is a third type
😄
Yeah, there's like 3-4 of them IIRC
getting close
not sure wtf those defaults are, gotta check that out
aaah
warningAreas[0].Area
is also polymorphicit can have
type: Feature
and type: FeatureCollection
God damnit Bilal, we shall write an angry letter to SMHI, förhelvete.
This JSON is annoying as fuckhahahahahahahahahahaha
ikrrrr
I got it working thou
just gotta tweak it for featurecollection
do you have a code for it ?
this fixes geometry
oooh, now I see
if its a featurecollection, it has only a list of features
instead of an inlined single feature
here is my total code
https://paste.mod.gg/otvmpzovbihu/0
BlazeBin - otvmpzovbihu
A tool for sharing your source code with the world!
I added a convenience method to
Area
that lets me get all geometries
this may or may not be useful to you
but the important bit was the polymorphism, and I added some nullability attributes to Area (since geometry and features can both be null, just not at the same time)
technically a lot here should also use polymorphism, but too much of that will make it very hard to work with the data.
examples include Area, Crs, Featurethis is mine now: https://paste.mod.gg/iegzuuvuoccg/0 "geometry.type" crashes my program because it is null and this is my warnings model: "https://paste.mod.gg/iyryywtqruyp/0"
BlazeBin - iegzuuvuoccg
A tool for sharing your source code with the world!
BlazeBin - iyryywtqruyp
A tool for sharing your source code with the world!
hm?
Geometry.Type
can never be null, as far as I can tell
and it isnt in that json"System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=WeatherApp
StackTrace:
at WeatherApp.Pages.Warnings.BuildRenderTree(RenderTreeBuilder __builder) in C:\Users\User\Documents\GitHub\WeatherApp\WeatherApp\Pages\Warnings.razor:line 33"
at that line
well yes
so, yours models had incorrect nullability
area.Area.Geometry
can be null
an area can also have multiple features (each feature has one geometry)
so Im not sure what you are trying to do here
(this was the problem my convenience method solved, btw)im trying to make it so the coordinates display on the page
well, step 1 would be to fix your models
so you get nullability warnings
writing
(area.Area.Geometry.Type
should tell you that Geometry can be null, but it doesnt, since your model has the wrong type
so go to Area
and make all properties except Type
nullable, by adding a ?
after their typeah solved it thanks ❤️
think everything is done now, det uppskattas ❤️
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.