question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GeoJSON Support in TypeScript

See original GitHub issue

Hi,

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:open
  • Created 5 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jochenjonccommented, Jun 18, 2019

@Silrem here’s an example of Point

import {
  Point as _point,
} from '@turf/turf';

export class Point implements _point {
  type: 'Point';
  coordinates: number[];
  bbox?;

  constructor(data?: _point) {
    if (data) {
      for (const property in data) {
        if (data.hasOwnProperty(property)) {
          (<any>this)[property] = (<any>data)[property];
        }
      }
    }
  }

  static fromJS(data: any): Point {
    data = typeof data === 'object' ? data : {};
    const result = new Point();
    result.init(data);
    return result;
  }

  init(data?: any) {
    if (data) {
      this.type = data['type'];
      this.coordinates = data['coordinates'];
      this.bbox = data['bbox'];
    }
  }

  toJSON(data?: any): any {
    data = typeof data === 'object' ? data : {};
    data['type'] = this.type;
    data['coordinates'] = this.coordinates;
    data['bbox'] = this.bbox;
    return data;
  }
}
1reaction
jochenjonccommented, Sep 10, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found