C
C#9mo ago
Squirtle

❔ How do i get the api with geojson?

Need to get api with json
52 Replies
Squirtle
Squirtle9mo ago
$paste
MODiX
MODiX9mo ago
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!
Squirtle
Squirtle9mo ago
BlazeBin - mlwyzvpuazmf
A tool for sharing your source code with the world!
Squirtle
Squirtle9mo ago
BlazeBin - gjeosgyztenv
A tool for sharing your source code with the world!
Squirtle
Squirtle9mo ago
it just returns nulll using blazor
Angius
Angius9mo ago
You're deserializing to an object But the API returns a list of objects
Angius
Angius9mo ago
No description
Angius
Angius9mo ago
No description
SG97
SG979mo ago
it's crucial you determine which part returns null (by debugging)
Pobiega
Pobiega9mo ago
[JsonPropertyName("coordinates")] public List<List<List<object>>> Coordinates { get; set; } kek
Angius
Angius9mo ago
Ah, damn, didn't notice that lol Wouldn't it just be List<List<double>>, looking at the json?
Pobiega
Pobiega9mo ago
"coordinates": [
[
[
[
17.10089,
57.34399
],
"coordinates": [
[
[
[
17.10089,
57.34399
],
Angius
Angius9mo ago
Ah, right, missed that one [
Angius
Angius9mo ago
Although...
No description
No description
Angius
Angius9mo ago
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>>
Pobiega
Pobiega9mo ago
polymorphic deserialization ftw
Angius
Angius9mo ago
That's where union types would come in handy
Squirtle
Squirtle9mo ago
BlazeBin - dnzhwlvzjeqi
A tool for sharing your source code with the world!
Squirtle
Squirtle9mo ago
this doesnt still work "[JsonPropertyName("coordinates")] public List<List<List<object>>> Coordinates { get; set; }" changed this too
Angius
Angius9mo ago
Do you still deserialize to a single object though?
Pobiega
Pobiega9mo ago
thats because it has two different shapes
Squirtle
Squirtle9mo ago
huh ? so how do i solve this
Angius
Angius9mo ago
Make that property of type object and cast depending on what Type it is I guess
Pobiega
Pobiega9mo ago
or [JsonDerivedType] hm, JsonDerivedType might be annoying as the type descriminator is inside the geometry object might still be a way to solve this
Squirtle
Squirtle9mo ago
tried it with "[JsonDerivedType]" got a bunch of errors maybe did it wrong dont know
Pobiega
Pobiega9mo ago
its not as easy as just slapping the attribute down
Squirtle
Squirtle9mo ago
how would you have done it ?
Pobiega
Pobiega9mo ago
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 converter
Angius
Angius9mo ago
Nah, I'd legit just convert it at the point of usage
Pobiega
Pobiega9mo ago
I agree with you, but now I just wanna figure this out 😄 it should be possible
Angius
Angius9mo ago
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 types
Pobiega
Pobiega9mo ago
we 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. hmm
Squirtle
Squirtle9mo ago
if you find the answer post it xd
Pobiega
Pobiega9mo ago
Ah fuck there is a third type 😄
Angius
Angius9mo ago
Yeah, there's like 3-4 of them IIRC
Pobiega
Pobiega9mo ago
MultiPoly!
Poly!
Default?
Default?
Poly!
Poly!
...
Poly!
Poly!
Poly!
Linestr!
Poly!
MultiPoly!
Poly!
Default?
Default?
Poly!
Poly!
...
Poly!
Poly!
Poly!
Linestr!
Poly!
getting close not sure wtf those defaults are, gotta check that out aaah warningAreas[0].Area is also polymorphic
Angius
Angius9mo ago
KEKW
Pobiega
Pobiega9mo ago
it 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 fuck
Squirtle
Squirtle9mo ago
hahahahahahahahahahaha ikrrrr
Pobiega
Pobiega9mo ago
I got it working thou just gotta tweak it for featurecollection
Squirtle
Squirtle9mo ago
do you have a code for it ?
Pobiega
Pobiega9mo ago
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
[JsonDerivedType(typeof(PolygonGeometry), "Polygon")]
[JsonDerivedType(typeof(MultiPolygonGeometry), "MultiPolygon")]
[JsonDerivedType(typeof(LineStringGeometry), "LineString")]
public abstract class Geometry
{
[JsonPropertyName("type")] public string Type { get; set; }
}

public class PolygonGeometry : Geometry
{
[JsonPropertyName("coordinates")] public List<List<List<double>>> Coordinates { get; set; }
}

public class MultiPolygonGeometry : Geometry
{
[JsonPropertyName("coordinates")] public List<List<List<List<double>>>> Coordinates { get; set; }
}

public class LineStringGeometry : Geometry
{
[JsonPropertyName("coordinates")] public List<List<double>> Coordinates { get; set; }
}
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
[JsonDerivedType(typeof(PolygonGeometry), "Polygon")]
[JsonDerivedType(typeof(MultiPolygonGeometry), "MultiPolygon")]
[JsonDerivedType(typeof(LineStringGeometry), "LineString")]
public abstract class Geometry
{
[JsonPropertyName("type")] public string Type { get; set; }
}

public class PolygonGeometry : Geometry
{
[JsonPropertyName("coordinates")] public List<List<List<double>>> Coordinates { get; set; }
}

public class MultiPolygonGeometry : Geometry
{
[JsonPropertyName("coordinates")] public List<List<List<List<double>>>> Coordinates { get; set; }
}

public class LineStringGeometry : Geometry
{
[JsonPropertyName("coordinates")] public List<List<double>> Coordinates { get; set; }
}
this fixes geometry oooh, now I see if its a featurecollection, it has only a list of features instead of an inlined single feature
Pobiega
Pobiega9mo ago
BlazeBin - otvmpzovbihu
A tool for sharing your source code with the world!
Pobiega
Pobiega9mo ago
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, Feature
Squirtle
Squirtle9mo ago
this 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!
Pobiega
Pobiega9mo ago
hm? Geometry.Type can never be null, as far as I can tell and it isnt in that json
Squirtle
Squirtle9mo ago
"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
Pobiega
Pobiega9mo ago
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)
Squirtle
Squirtle9mo ago
im trying to make it so the coordinates display on the page
Pobiega
Pobiega9mo ago
well, step 1 would be to fix your models so you get nullability warnings writing (area.Area.Geometry.Typeshould 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 type
Squirtle
Squirtle9mo ago
ah solved it thanks ❤️ think everything is done now, det uppskattas ❤️
Accord
Accord9mo ago
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.