C
C#15mo ago
Squirtle

❔ How do i get the api with geojson?

Need to get api with json
52 Replies
Squirtle
SquirtleOP15mo ago
$paste
MODiX
MODiX15mo 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
SquirtleOP15mo ago
BlazeBin - mlwyzvpuazmf
A tool for sharing your source code with the world!
Squirtle
SquirtleOP15mo ago
BlazeBin - gjeosgyztenv
A tool for sharing your source code with the world!
Squirtle
SquirtleOP15mo ago
it just returns nulll using blazor
Angius
Angius15mo ago
You're deserializing to an object But the API returns a list of objects
Angius
Angius15mo ago
No description
Angius
Angius15mo ago
No description
SG97
SG9715mo ago
it's crucial you determine which part returns null (by debugging)
Pobiega
Pobiega15mo ago
[JsonPropertyName("coordinates")] public List<List<List<object>>> Coordinates { get; set; } kek
Angius
Angius15mo ago
Ah, damn, didn't notice that lol Wouldn't it just be List<List<double>>, looking at the json?
Pobiega
Pobiega15mo ago
"coordinates": [
[
[
[
17.10089,
57.34399
],
"coordinates": [
[
[
[
17.10089,
57.34399
],
Angius
Angius15mo ago
Ah, right, missed that one [
Angius
Angius15mo ago
Although...
No description
No description
Angius
Angius15mo 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
Pobiega15mo ago
polymorphic deserialization ftw
Angius
Angius15mo ago
That's where union types would come in handy
Squirtle
SquirtleOP15mo ago
BlazeBin - dnzhwlvzjeqi
A tool for sharing your source code with the world!
Squirtle
SquirtleOP15mo ago
this doesnt still work "[JsonPropertyName("coordinates")] public List<List<List<object>>> Coordinates { get; set; }" changed this too
Angius
Angius15mo ago
Do you still deserialize to a single object though?
Pobiega
Pobiega15mo ago
thats because it has two different shapes
Squirtle
SquirtleOP15mo ago
huh ? so how do i solve this
Angius
Angius15mo ago
Make that property of type object and cast depending on what Type it is I guess
Pobiega
Pobiega15mo 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
SquirtleOP15mo ago
tried it with "[JsonDerivedType]" got a bunch of errors maybe did it wrong dont know
Pobiega
Pobiega15mo ago
its not as easy as just slapping the attribute down
Squirtle
SquirtleOP15mo ago
how would you have done it ?
Pobiega
Pobiega15mo 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
Angius15mo ago
Nah, I'd legit just convert it at the point of usage
Pobiega
Pobiega15mo ago
I agree with you, but now I just wanna figure this out 😄 it should be possible
Angius
Angius15mo 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
Pobiega15mo 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
SquirtleOP15mo ago
if you find the answer post it xd
Pobiega
Pobiega15mo ago
Ah fuck there is a third type 😄
Angius
Angius15mo ago
Yeah, there's like 3-4 of them IIRC
Pobiega
Pobiega15mo 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
Angius15mo ago
KEKW
Pobiega
Pobiega15mo 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
SquirtleOP15mo ago
hahahahahahahahahahaha ikrrrr
Pobiega
Pobiega15mo ago
I got it working thou just gotta tweak it for featurecollection
Squirtle
SquirtleOP15mo ago
do you have a code for it ?
Pobiega
Pobiega15mo 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
Pobiega15mo ago
BlazeBin - otvmpzovbihu
A tool for sharing your source code with the world!
Pobiega
Pobiega15mo 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
SquirtleOP15mo 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
Pobiega15mo ago
hm? Geometry.Type can never be null, as far as I can tell and it isnt in that json
Squirtle
SquirtleOP15mo 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
Pobiega15mo 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
SquirtleOP15mo ago
im trying to make it so the coordinates display on the page
Pobiega
Pobiega15mo 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
SquirtleOP15mo ago
ah solved it thanks ❤️ think everything is done now, det uppskattas ❤️
Accord
Accord15mo 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.
Want results from more Discord servers?
Add your server