GeoJSON Support in TypeScript
See original GitHub issueHi,
Just wondering if anyone has already tried returning GeoJson type with NSwag? I’ve tried it in combination with GeoJSON.Net, but than NSwag renders the following typescript:
export abstract class IPosition implements IIPosition {
constructor(data?: IIPosition) {
if (data) {
for (var property in data) {
if (data.hasOwnProperty(property))
(<any>this)[property] = (<any>data)[property];
}
}
}
init(data?: any) {
if (data) {
}
}
static fromJS(data: any): IPosition {
data = typeof data === 'object' ? data : {};
throw new Error("The abstract class 'IPosition' cannot be instantiated.");
}
toJSON(data?: any) {
data = typeof data === 'object' ? data : {};
return data;
}
}
The Problem is it sees IPosition as an abstract class and thus it can be deserialized. But it is needed as input for the following LineString:
export class LineString extends GeoJSONObject implements ILineString {
type!: GeoJSONObjectType;
coordinates!: IPosition[];
constructor(data?: ILineString) {
super(data);
if (!data) {
this.coordinates = [];
}
}
init(data?: any) {
super.init(data);
if (data) {
this.type = data["type"];
if (data["coordinates"] && data["coordinates"].constructor === Array) {
this.coordinates = [];
for (let item of data["coordinates"])
this.coordinates.push(IPosition.fromJS(item));
}
}
}
static fromJS(data: any): LineString {
data = typeof data === 'object' ? data : {};
let result = new LineString();
result.init(data);
return result;
}
toJSON(data?: any) {
data = typeof data === 'object' ? data : {};
data["type"] = this.type;
if (this.coordinates && this.coordinates.constructor === Array) {
data["coordinates"] = [];
for (let item of this.coordinates)
data["coordinates"].push(item.toJSON());
}
super.toJSON(data);
return data;
}
}
Maybe I’m using the wrong .Net Library or is there a way to not generate the types and maybe use TypeScript equivalents from an external library?
Tips are welcome.
Regards,
Jochen
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:10 (5 by maintainers)
Top Results From Across the Web
types/geojson
TypeScript definitions for geojson. Latest version: 7946.0.10, ... Start using @types/geojson in your project by running `npm i @types/geojson`.
Read more >How to create a GeoJSON Feature element using ...
First, according to the @types/geojson type definitions, a point Feature needs to have a type field set to a string literal type of...
Read more >Typescript types for the GeoJSON RFC7946 specification
* A Feature object has a "type" member with the value "Feature". * with the name "id", and the value of this member...
Read more >GeoJSON and typescript - jaxtrax
This GeoJSON file is an object with two properties 'type', which identifies the type of the object as being a 'FeatureCollection' and 'features' ......
Read more >types/geojson@7946.0.10
Documentation for npm package @types/geojson@7946.0.10 - jsDocs.io.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@Silrem here’s an example of Point
From some types in .Net I use in my public API I do not want NSwag to generate the javascript/typescript counterpart because they are already part of my code or an external library.
In my case I have GeoJSON.Net on the API (.Net) side and @types/geojson on the client/ typescript side.
I was able to fix it partially with excludedTypeNames, NSwag then doesn’t generate the classes. But it still expects that the class has a .toSJON() and .fromJS() method. Where I expected them to be handled as a string or int.
// data["coordinates"] = this.coordinates ? this.coordinates.toJSON() : <any>undefined; data["coordinates"] = this.coordinates;
The first line is what has been generated and the second is what I expected.
I solved my issue by extending my GeoJson objects with a toJSON and fromJS method.
My question is to be able to tell NSwag that I’m providing the code myself for certain types and that they don’t need an fromJS an toJSON method.