@cratis/fundamentals 7.15.0 → 7.15.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/JsonSerializer.ts +88 -56
- package/dist/JsonSerializer.d.ts.map +1 -1
- package/dist/JsonSerializer.js +50 -54
- package/dist/JsonSerializer.js.map +1 -1
- package/dist/cjs/JsonSerializer.d.ts.map +1 -1
- package/dist/cjs/JsonSerializer.js +56 -54
- package/dist/cjs/JsonSerializer.js.map +1 -1
- package/dist/cjs/geospatial/LineString.d.ts +7 -0
- package/dist/cjs/geospatial/LineString.d.ts.map +1 -0
- package/dist/cjs/geospatial/LineString.js +20 -0
- package/dist/cjs/geospatial/LineString.js.map +1 -0
- package/dist/cjs/geospatial/LinearRing.d.ts +7 -0
- package/dist/cjs/geospatial/LinearRing.d.ts.map +1 -0
- package/dist/cjs/geospatial/LinearRing.js +17 -0
- package/dist/cjs/geospatial/LinearRing.js.map +1 -0
- package/dist/cjs/geospatial/Point.d.ts +7 -0
- package/dist/cjs/geospatial/Point.d.ts.map +1 -0
- package/dist/cjs/geospatial/Point.js +30 -0
- package/dist/cjs/geospatial/Point.js.map +1 -0
- package/dist/cjs/geospatial/Polygon.d.ts +8 -0
- package/dist/cjs/geospatial/Polygon.d.ts.map +1 -0
- package/dist/cjs/geospatial/Polygon.js +23 -0
- package/dist/cjs/geospatial/Polygon.js.map +1 -0
- package/dist/cjs/geospatial/index.d.ts +5 -0
- package/dist/cjs/geospatial/index.d.ts.map +1 -0
- package/dist/cjs/index.d.ts +2 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +24 -2
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/json/DateJsonConverter.d.ts +8 -0
- package/dist/cjs/json/DateJsonConverter.d.ts.map +1 -0
- package/dist/cjs/json/DateJsonConverter.js +18 -0
- package/dist/cjs/json/DateJsonConverter.js.map +1 -0
- package/dist/cjs/json/GuidJsonConverter.d.ts +9 -0
- package/dist/cjs/json/GuidJsonConverter.d.ts.map +1 -0
- package/dist/cjs/json/GuidJsonConverter.js +19 -0
- package/dist/cjs/json/GuidJsonConverter.js.map +1 -0
- package/dist/cjs/json/JsonConverter.d.ts +8 -0
- package/dist/cjs/json/JsonConverter.d.ts.map +1 -0
- package/dist/cjs/json/JsonConverter.js +10 -0
- package/dist/cjs/json/JsonConverter.js.map +1 -0
- package/dist/cjs/json/LineStringJsonConverter.d.ts +9 -0
- package/dist/cjs/json/LineStringJsonConverter.d.ts.map +1 -0
- package/dist/cjs/json/LineStringJsonConverter.js +27 -0
- package/dist/cjs/json/LineStringJsonConverter.js.map +1 -0
- package/dist/cjs/json/PointJsonConverter.d.ts +9 -0
- package/dist/cjs/json/PointJsonConverter.d.ts.map +1 -0
- package/dist/cjs/json/PointJsonConverter.js +28 -0
- package/dist/cjs/json/PointJsonConverter.js.map +1 -0
- package/dist/cjs/json/PolygonJsonConverter.d.ts +9 -0
- package/dist/cjs/json/PolygonJsonConverter.d.ts.map +1 -0
- package/dist/cjs/json/PolygonJsonConverter.js +29 -0
- package/dist/cjs/json/PolygonJsonConverter.js.map +1 -0
- package/dist/cjs/json/TimeSpanJsonConverter.d.ts +9 -0
- package/dist/cjs/json/TimeSpanJsonConverter.d.ts.map +1 -0
- package/dist/cjs/json/TimeSpanJsonConverter.js +19 -0
- package/dist/cjs/json/TimeSpanJsonConverter.js.map +1 -0
- package/dist/cjs/json/ValueMapJsonConverter.d.ts +12 -0
- package/dist/cjs/json/ValueMapJsonConverter.d.ts.map +1 -0
- package/dist/cjs/json/ValueMapJsonConverter.js +33 -0
- package/dist/cjs/json/ValueMapJsonConverter.js.map +1 -0
- package/dist/cjs/json/index.d.ts +9 -0
- package/dist/cjs/json/index.d.ts.map +1 -0
- package/dist/esm/JsonSerializer.d.ts.map +1 -1
- package/dist/esm/JsonSerializer.js +56 -54
- package/dist/esm/JsonSerializer.js.map +1 -1
- package/dist/esm/geospatial/LineString.d.ts +7 -0
- package/dist/esm/geospatial/LineString.d.ts.map +1 -0
- package/dist/esm/geospatial/LineString.js +18 -0
- package/dist/esm/geospatial/LineString.js.map +1 -0
- package/dist/esm/geospatial/LinearRing.d.ts +7 -0
- package/dist/esm/geospatial/LinearRing.d.ts.map +1 -0
- package/dist/esm/geospatial/LinearRing.js +15 -0
- package/dist/esm/geospatial/LinearRing.js.map +1 -0
- package/dist/esm/geospatial/Point.d.ts +7 -0
- package/dist/esm/geospatial/Point.d.ts.map +1 -0
- package/dist/esm/geospatial/Point.js +28 -0
- package/dist/esm/geospatial/Point.js.map +1 -0
- package/dist/esm/geospatial/Polygon.d.ts +8 -0
- package/dist/esm/geospatial/Polygon.d.ts.map +1 -0
- package/dist/esm/geospatial/Polygon.js +21 -0
- package/dist/esm/geospatial/Polygon.js.map +1 -0
- package/dist/esm/geospatial/index.d.ts +5 -0
- package/dist/esm/geospatial/index.d.ts.map +1 -0
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +12 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/json/DateJsonConverter.d.ts +8 -0
- package/dist/esm/json/DateJsonConverter.d.ts.map +1 -0
- package/dist/esm/json/DateJsonConverter.js +16 -0
- package/dist/esm/json/DateJsonConverter.js.map +1 -0
- package/dist/esm/json/GuidJsonConverter.d.ts +9 -0
- package/dist/esm/json/GuidJsonConverter.d.ts.map +1 -0
- package/dist/esm/json/GuidJsonConverter.js +17 -0
- package/dist/esm/json/GuidJsonConverter.js.map +1 -0
- package/dist/esm/json/JsonConverter.d.ts +8 -0
- package/dist/esm/json/JsonConverter.d.ts.map +1 -0
- package/dist/esm/json/JsonConverter.js +8 -0
- package/dist/esm/json/JsonConverter.js.map +1 -0
- package/dist/esm/json/LineStringJsonConverter.d.ts +9 -0
- package/dist/esm/json/LineStringJsonConverter.d.ts.map +1 -0
- package/dist/esm/json/LineStringJsonConverter.js +25 -0
- package/dist/esm/json/LineStringJsonConverter.js.map +1 -0
- package/dist/esm/json/PointJsonConverter.d.ts +9 -0
- package/dist/esm/json/PointJsonConverter.d.ts.map +1 -0
- package/dist/esm/json/PointJsonConverter.js +26 -0
- package/dist/esm/json/PointJsonConverter.js.map +1 -0
- package/dist/esm/json/PolygonJsonConverter.d.ts +9 -0
- package/dist/esm/json/PolygonJsonConverter.d.ts.map +1 -0
- package/dist/esm/json/PolygonJsonConverter.js +27 -0
- package/dist/esm/json/PolygonJsonConverter.js.map +1 -0
- package/dist/esm/json/TimeSpanJsonConverter.d.ts +9 -0
- package/dist/esm/json/TimeSpanJsonConverter.d.ts.map +1 -0
- package/dist/esm/json/TimeSpanJsonConverter.js +17 -0
- package/dist/esm/json/TimeSpanJsonConverter.js.map +1 -0
- package/dist/esm/json/ValueMapJsonConverter.d.ts +12 -0
- package/dist/esm/json/ValueMapJsonConverter.d.ts.map +1 -0
- package/dist/esm/json/ValueMapJsonConverter.js +31 -0
- package/dist/esm/json/ValueMapJsonConverter.js.map +1 -0
- package/dist/esm/json/index.d.ts +9 -0
- package/dist/esm/json/index.d.ts.map +1 -0
- package/dist/geospatial/LineString.d.ts +7 -0
- package/dist/geospatial/LineString.d.ts.map +1 -0
- package/dist/geospatial/LineString.js +16 -0
- package/dist/geospatial/LineString.js.map +1 -0
- package/dist/geospatial/LinearRing.d.ts +7 -0
- package/dist/geospatial/LinearRing.d.ts.map +1 -0
- package/dist/geospatial/LinearRing.js +13 -0
- package/dist/geospatial/LinearRing.js.map +1 -0
- package/dist/geospatial/Point.d.ts +7 -0
- package/dist/geospatial/Point.d.ts.map +1 -0
- package/dist/geospatial/Point.js +25 -0
- package/dist/geospatial/Point.js.map +1 -0
- package/dist/geospatial/Polygon.d.ts +8 -0
- package/dist/geospatial/Polygon.d.ts.map +1 -0
- package/dist/geospatial/Polygon.js +19 -0
- package/dist/geospatial/Polygon.js.map +1 -0
- package/dist/geospatial/index.d.ts +5 -0
- package/dist/geospatial/index.d.ts.map +1 -0
- package/dist/geospatial/index.js +5 -0
- package/dist/geospatial/index.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/json/DateJsonConverter.d.ts +8 -0
- package/dist/json/DateJsonConverter.d.ts.map +1 -0
- package/dist/json/DateJsonConverter.js +13 -0
- package/dist/json/DateJsonConverter.js.map +1 -0
- package/dist/json/GuidJsonConverter.d.ts +9 -0
- package/dist/json/GuidJsonConverter.d.ts.map +1 -0
- package/dist/json/GuidJsonConverter.js +14 -0
- package/dist/json/GuidJsonConverter.js.map +1 -0
- package/dist/json/JsonConverter.d.ts +8 -0
- package/dist/json/JsonConverter.d.ts.map +1 -0
- package/dist/json/JsonConverter.js +6 -0
- package/dist/json/JsonConverter.js.map +1 -0
- package/dist/json/LineStringJsonConverter.d.ts +9 -0
- package/dist/json/LineStringJsonConverter.d.ts.map +1 -0
- package/dist/json/LineStringJsonConverter.js +21 -0
- package/dist/json/LineStringJsonConverter.js.map +1 -0
- package/dist/json/PointJsonConverter.d.ts +9 -0
- package/dist/json/PointJsonConverter.d.ts.map +1 -0
- package/dist/json/PointJsonConverter.js +23 -0
- package/dist/json/PointJsonConverter.js.map +1 -0
- package/dist/json/PolygonJsonConverter.d.ts +9 -0
- package/dist/json/PolygonJsonConverter.d.ts.map +1 -0
- package/dist/json/PolygonJsonConverter.js +22 -0
- package/dist/json/PolygonJsonConverter.js.map +1 -0
- package/dist/json/TimeSpanJsonConverter.d.ts +9 -0
- package/dist/json/TimeSpanJsonConverter.d.ts.map +1 -0
- package/dist/json/TimeSpanJsonConverter.js +14 -0
- package/dist/json/TimeSpanJsonConverter.js.map +1 -0
- package/dist/json/ValueMapJsonConverter.d.ts +12 -0
- package/dist/json/ValueMapJsonConverter.d.ts.map +1 -0
- package/dist/json/ValueMapJsonConverter.js +28 -0
- package/dist/json/ValueMapJsonConverter.js.map +1 -0
- package/dist/json/index.d.ts +9 -0
- package/dist/json/index.d.ts.map +1 -0
- package/dist/json/index.js +9 -0
- package/dist/json/index.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/geospatial/LineString.ts +33 -0
- package/geospatial/LinearRing.ts +30 -0
- package/geospatial/Point.ts +36 -0
- package/geospatial/Polygon.ts +37 -0
- package/geospatial/index.ts +7 -0
- package/index.ts +2 -1
- package/json/DateJsonConverter.ts +27 -0
- package/json/GuidJsonConverter.ts +28 -0
- package/json/JsonConverter.ts +40 -0
- package/json/LineStringJsonConverter.ts +35 -0
- package/json/PointJsonConverter.ts +37 -0
- package/json/PolygonJsonConverter.ts +38 -0
- package/json/TimeSpanJsonConverter.ts +28 -0
- package/json/ValueMapJsonConverter.ts +57 -0
- package/json/for_DateJsonConverter/when_reading_date_with_date_converter.ts +12 -0
- package/json/for_DateJsonConverter/when_writing_date_with_date_converter.ts +12 -0
- package/json/for_GuidJsonConverter/when_reading_guid_with_guid_converter.ts +13 -0
- package/json/for_GuidJsonConverter/when_writing_guid_with_guid_converter.ts +13 -0
- package/json/for_LineStringJsonConverter/when_deserializing_linestring.ts +19 -0
- package/json/for_LineStringJsonConverter/when_reading_linestring_with_linestring_converter.ts +24 -0
- package/json/for_LineStringJsonConverter/when_serializing_linestring.ts +25 -0
- package/json/for_LineStringJsonConverter/when_writing_linestring_with_linestring_converter.ts +27 -0
- package/json/for_PointJsonConverter/when_deserializing_point.ts +14 -0
- package/json/for_PointJsonConverter/when_reading_point_with_point_converter.ts +14 -0
- package/json/for_PointJsonConverter/when_serializing_point.ts +17 -0
- package/json/for_PointJsonConverter/when_writing_point_with_point_converter.ts +16 -0
- package/json/for_PolygonJsonConverter/when_deserializing_polygon_with_holes.ts +28 -0
- package/json/for_PolygonJsonConverter/when_deserializing_polygon_with_no_holes.ts +22 -0
- package/json/for_PolygonJsonConverter/when_reading_polygon_with_polygon_converter.ts +27 -0
- package/json/for_PolygonJsonConverter/when_serializing_polygon_with_holes.ts +53 -0
- package/json/for_PolygonJsonConverter/when_serializing_polygon_with_no_holes.ts +33 -0
- package/json/for_PolygonJsonConverter/when_writing_polygon_with_polygon_converter.ts +35 -0
- package/json/for_TimeSpanJsonConverter/when_reading_timespan_with_timespan_converter.ts +16 -0
- package/json/for_TimeSpanJsonConverter/when_writing_timespan_with_timespan_converter.ts +13 -0
- package/json/for_ValueMapJsonConverter/when_writing_valuemap_with_valuemap_converter.ts +28 -0
- package/json/index.ts +11 -0
- package/package.json +11 -1
- package/Coordinate.ts +0 -38
- package/dist/Coordinate.d.ts +0 -7
- package/dist/Coordinate.d.ts.map +0 -1
- package/dist/Coordinate.js +0 -27
- package/dist/Coordinate.js.map +0 -1
- package/dist/cjs/Coordinate.d.ts +0 -7
- package/dist/cjs/Coordinate.d.ts.map +0 -1
- package/dist/cjs/Coordinate.js +0 -32
- package/dist/cjs/Coordinate.js.map +0 -1
- package/dist/esm/Coordinate.d.ts +0 -7
- package/dist/esm/Coordinate.d.ts.map +0 -1
- package/dist/esm/Coordinate.js +0 -30
- package/dist/esm/Coordinate.js.map +0 -1
- package/for_Coordinate/when_creating_coordinate.ts +0 -11
- package/for_Coordinate/when_deserializing_coordinate.ts +0 -14
- package/for_Coordinate/when_deserializing_coordinate_with_missing_properties.ts +0 -13
- package/for_Coordinate/when_serializing_coordinate.ts +0 -14
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Constructor } from '../Constructor';
|
|
5
|
+
import { Guid } from '../Guid';
|
|
6
|
+
import { JsonConverter } from './JsonConverter';
|
|
7
|
+
|
|
8
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* JSON converter for Guid type.
|
|
12
|
+
*/
|
|
13
|
+
export class GuidJsonConverter extends JsonConverter<Guid> {
|
|
14
|
+
/** @inheritdoc */
|
|
15
|
+
get type(): Constructor<Guid> {
|
|
16
|
+
return Guid;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** @inheritdoc */
|
|
20
|
+
read(value: any): Guid {
|
|
21
|
+
return Guid.parse(value.toString());
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** @inheritdoc */
|
|
25
|
+
write(value: any): any {
|
|
26
|
+
return value?.toString() ?? '';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Constructor } from '../Constructor';
|
|
5
|
+
|
|
6
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Base class for JSON converters that handle serialization and deserialization of specific types.
|
|
10
|
+
* Similar to System.Text.Json.Serialization.JsonConverter<T>.
|
|
11
|
+
*/
|
|
12
|
+
export abstract class JsonConverter<T = any> {
|
|
13
|
+
/**
|
|
14
|
+
* Gets the type that this converter handles.
|
|
15
|
+
*/
|
|
16
|
+
abstract get type(): Constructor<T>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Determines whether the converter can convert the specified type.
|
|
20
|
+
* @param {Constructor} typeToConvert - The type to check.
|
|
21
|
+
* @returns {boolean} True if the converter can convert the type; otherwise, false.
|
|
22
|
+
*/
|
|
23
|
+
canConvert(typeToConvert: Constructor): boolean {
|
|
24
|
+
return typeToConvert === this.type;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Reads and converts the JSON to the target type.
|
|
29
|
+
* @param {any} value - The value to deserialize.
|
|
30
|
+
* @returns {T} The deserialized value.
|
|
31
|
+
*/
|
|
32
|
+
abstract read(value: any): T;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Writes the value as JSON.
|
|
36
|
+
* @param {T} value - The value to serialize.
|
|
37
|
+
* @returns {any} The serialized value.
|
|
38
|
+
*/
|
|
39
|
+
abstract write(value: T): any;
|
|
40
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Constructor } from '../Constructor';
|
|
5
|
+
import { Point, LineString } from '../geospatial';
|
|
6
|
+
import { JsonConverter } from './JsonConverter';
|
|
7
|
+
|
|
8
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* JSON converter for LineString type (GeoJSON format).
|
|
12
|
+
*/
|
|
13
|
+
export class LineStringJsonConverter extends JsonConverter<LineString> {
|
|
14
|
+
/** @inheritdoc */
|
|
15
|
+
get type(): Constructor<LineString> {
|
|
16
|
+
return LineString;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** @inheritdoc */
|
|
20
|
+
read(value: any): LineString {
|
|
21
|
+
if (value === null || value === undefined) {
|
|
22
|
+
throw new Error('Cannot deserialize null or undefined to LineString');
|
|
23
|
+
}
|
|
24
|
+
if (value.type !== 'LineString' || !value.coordinates || value.coordinates.length < 2) {
|
|
25
|
+
throw new Error('Cannot deserialize LineString: invalid GeoJSON format');
|
|
26
|
+
}
|
|
27
|
+
const points = value.coordinates.map((coord: number[]) => new Point(coord[0], coord[1]));
|
|
28
|
+
return new LineString(points);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** @inheritdoc */
|
|
32
|
+
write(value: any): any {
|
|
33
|
+
return value?.toJSON() ?? null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Constructor } from '../Constructor';
|
|
5
|
+
import { Point } from '../geospatial';
|
|
6
|
+
import { JsonConverter } from './JsonConverter';
|
|
7
|
+
|
|
8
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* JSON converter for Point type (GeoJSON format).
|
|
12
|
+
*/
|
|
13
|
+
export class PointJsonConverter extends JsonConverter<Point> {
|
|
14
|
+
/** @inheritdoc */
|
|
15
|
+
get type(): Constructor<Point> {
|
|
16
|
+
return Point;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** @inheritdoc */
|
|
20
|
+
read(value: any): Point {
|
|
21
|
+
if (value === null || value === undefined) {
|
|
22
|
+
throw new Error('Cannot deserialize null or undefined to Point');
|
|
23
|
+
}
|
|
24
|
+
if (value.type === 'Point' && value.coordinates && value.coordinates.length === 2) {
|
|
25
|
+
return new Point(value.coordinates[0], value.coordinates[1]);
|
|
26
|
+
}
|
|
27
|
+
if (typeof value.longitude === 'number' && typeof value.latitude === 'number') {
|
|
28
|
+
return new Point(value.longitude, value.latitude);
|
|
29
|
+
}
|
|
30
|
+
throw new Error('Cannot deserialize Point: expected GeoJSON format { type, coordinates } or { longitude, latitude }');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** @inheritdoc */
|
|
34
|
+
write(value: any): any {
|
|
35
|
+
return value?.toJSON() ?? null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Constructor } from '../Constructor';
|
|
5
|
+
import { Point, LinearRing, Polygon } from '../geospatial';
|
|
6
|
+
import { JsonConverter } from './JsonConverter';
|
|
7
|
+
|
|
8
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* JSON converter for Polygon type (GeoJSON format).
|
|
12
|
+
*/
|
|
13
|
+
export class PolygonJsonConverter extends JsonConverter<Polygon> {
|
|
14
|
+
/** @inheritdoc */
|
|
15
|
+
get type(): Constructor<Polygon> {
|
|
16
|
+
return Polygon;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** @inheritdoc */
|
|
20
|
+
read(value: any): Polygon {
|
|
21
|
+
if (value === null || value === undefined) {
|
|
22
|
+
throw new Error('Cannot deserialize null or undefined to Polygon');
|
|
23
|
+
}
|
|
24
|
+
if (value.type !== 'Polygon' || !value.coordinates || value.coordinates.length === 0) {
|
|
25
|
+
throw new Error('Cannot deserialize Polygon: invalid GeoJSON format');
|
|
26
|
+
}
|
|
27
|
+
const shell = new LinearRing(value.coordinates[0].map((coord: number[]) => new Point(coord[0], coord[1])));
|
|
28
|
+
const holes = value.coordinates.slice(1).map((ring: number[][]) =>
|
|
29
|
+
new LinearRing(ring.map((coord: number[]) => new Point(coord[0], coord[1])))
|
|
30
|
+
);
|
|
31
|
+
return new Polygon(shell, holes);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** @inheritdoc */
|
|
35
|
+
write(value: any): any {
|
|
36
|
+
return value?.toJSON() ?? null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Constructor } from '../Constructor';
|
|
5
|
+
import { TimeSpan } from '../TimeSpan';
|
|
6
|
+
import { JsonConverter } from './JsonConverter';
|
|
7
|
+
|
|
8
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* JSON converter for TimeSpan type.
|
|
12
|
+
*/
|
|
13
|
+
export class TimeSpanJsonConverter extends JsonConverter<TimeSpan> {
|
|
14
|
+
/** @inheritdoc */
|
|
15
|
+
get type(): Constructor<TimeSpan> {
|
|
16
|
+
return TimeSpan;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** @inheritdoc */
|
|
20
|
+
read(value: any): TimeSpan {
|
|
21
|
+
return TimeSpan.parse(value.toString());
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** @inheritdoc */
|
|
25
|
+
write(value: any): any {
|
|
26
|
+
return value?.toString() ?? '';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Constructor } from '../Constructor';
|
|
5
|
+
import { ValueMap } from '../ValueMap';
|
|
6
|
+
import { JsonConverter } from './JsonConverter';
|
|
7
|
+
|
|
8
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
9
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* JSON converter for ValueMap type.
|
|
13
|
+
*
|
|
14
|
+
* Note: ValueMap deserialization is handled separately by JsonSerializer.deserializeValueMapFromField
|
|
15
|
+
* based on field metadata to properly deserialize keys and values according to their types.
|
|
16
|
+
* The read() method is not used directly during normal deserialization.
|
|
17
|
+
*/
|
|
18
|
+
export class ValueMapJsonConverter extends JsonConverter<ValueMap<any, any>> {
|
|
19
|
+
// Circular dependency workaround - these will be injected by JsonSerializer
|
|
20
|
+
private static serializeMapKey: ((key: any) => string) | null = null;
|
|
21
|
+
private static convertTypesOnInstance: ((instance: any) => any) | null = null;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Sets the required helper functions from JsonSerializer to avoid circular dependencies.
|
|
25
|
+
*/
|
|
26
|
+
static setHelpers(serializeMapKey: (key: any) => string, convertTypesOnInstance: (instance: any) => any): void {
|
|
27
|
+
ValueMapJsonConverter.serializeMapKey = serializeMapKey;
|
|
28
|
+
ValueMapJsonConverter.convertTypesOnInstance = convertTypesOnInstance;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** @inheritdoc */
|
|
32
|
+
get type(): Constructor<ValueMap<any, any>> {
|
|
33
|
+
return ValueMap;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** @inheritdoc */
|
|
37
|
+
read(_value: any): ValueMap<any, any> {
|
|
38
|
+
// ValueMap deserialization is handled separately by JsonSerializer.deserializeValueMapFromField
|
|
39
|
+
// based on field metadata to properly deserialize keys and values according to their types.
|
|
40
|
+
// This method should not be called directly during normal deserialization.
|
|
41
|
+
const valueMap = new ValueMap<any, any>();
|
|
42
|
+
return valueMap;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** @inheritdoc */
|
|
46
|
+
write(value: ValueMap<any, any>): any {
|
|
47
|
+
if (!ValueMapJsonConverter.serializeMapKey || !ValueMapJsonConverter.convertTypesOnInstance) {
|
|
48
|
+
throw new Error('ValueMapJsonConverter helpers not initialized. Call setHelpers() first.');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const converted: any = {};
|
|
52
|
+
for (const [key, mapValue] of value.entries()) {
|
|
53
|
+
converted[ValueMapJsonConverter.serializeMapKey(key)] = ValueMapJsonConverter.convertTypesOnInstance(mapValue);
|
|
54
|
+
}
|
|
55
|
+
return converted;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { DateJsonConverter } from '../DateJsonConverter';
|
|
5
|
+
|
|
6
|
+
describe('when reading date with date converter', () => {
|
|
7
|
+
const converter = new DateJsonConverter();
|
|
8
|
+
const result = converter.read('2024-01-15T10:30:00.000Z');
|
|
9
|
+
|
|
10
|
+
it('should return Date instance', () => result.should.be.instanceof(Date));
|
|
11
|
+
it('should have correct time', () => result.getTime().should.equal(new Date('2024-01-15T10:30:00.000Z').getTime()));
|
|
12
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { DateJsonConverter } from '../DateJsonConverter';
|
|
5
|
+
|
|
6
|
+
describe('when writing date with date converter', () => {
|
|
7
|
+
const converter = new DateJsonConverter();
|
|
8
|
+
const date = new Date('2024-01-15T10:30:00.000Z');
|
|
9
|
+
const result = converter.write(date);
|
|
10
|
+
|
|
11
|
+
it('should return ISO string', () => result.should.equal('2024-01-15T10:30:00.000Z'));
|
|
12
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Guid } from '../../Guid';
|
|
5
|
+
import { GuidJsonConverter } from '../GuidJsonConverter';
|
|
6
|
+
|
|
7
|
+
describe('when reading guid with guid converter', () => {
|
|
8
|
+
const converter = new GuidJsonConverter();
|
|
9
|
+
const result = converter.read('f47ac10b-58cc-4372-a567-0e02b2c3d479');
|
|
10
|
+
|
|
11
|
+
it('should return Guid instance', () => result.should.be.instanceof(Guid));
|
|
12
|
+
it('should have correct value', () => result.toString().should.equal('f47ac10b-58cc-4372-a567-0e02b2c3d479'));
|
|
13
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Guid } from '../../Guid';
|
|
5
|
+
import { GuidJsonConverter } from '../GuidJsonConverter';
|
|
6
|
+
|
|
7
|
+
describe('when writing guid with guid converter', () => {
|
|
8
|
+
const converter = new GuidJsonConverter();
|
|
9
|
+
const guid = Guid.parse('f47ac10b-58cc-4372-a567-0e02b2c3d479');
|
|
10
|
+
const result = converter.write(guid);
|
|
11
|
+
|
|
12
|
+
it('should return guid string', () => result.should.equal('f47ac10b-58cc-4372-a567-0e02b2c3d479'));
|
|
13
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { LineString } from '../../geospatial/LineString';
|
|
5
|
+
import { JsonSerializer } from '../../JsonSerializer';
|
|
6
|
+
|
|
7
|
+
describe('when deserializing linestring', () => {
|
|
8
|
+
const json = '{"type":"LineString","coordinates":[[10.5,20.3],[30.1,40.2],[50.7,60.8]]}';
|
|
9
|
+
const result = JsonSerializer.deserialize(LineString, json);
|
|
10
|
+
|
|
11
|
+
it('should be a LineString', () => result.should.be.instanceof(LineString));
|
|
12
|
+
it('should have three points', () => result.coordinates.length.should.equal(3));
|
|
13
|
+
it('should have correct first point longitude', () => result.coordinates[0].longitude.should.equal(10.5));
|
|
14
|
+
it('should have correct first point latitude', () => result.coordinates[0].latitude.should.equal(20.3));
|
|
15
|
+
it('should have correct second point longitude', () => result.coordinates[1].longitude.should.equal(30.1));
|
|
16
|
+
it('should have correct second point latitude', () => result.coordinates[1].latitude.should.equal(40.2));
|
|
17
|
+
it('should have correct third point longitude', () => result.coordinates[2].longitude.should.equal(50.7));
|
|
18
|
+
it('should have correct third point latitude', () => result.coordinates[2].latitude.should.equal(60.8));
|
|
19
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { LineString } from '../../geospatial/LineString';
|
|
5
|
+
import { LineStringJsonConverter } from '../LineStringJsonConverter';
|
|
6
|
+
|
|
7
|
+
describe('when reading linestring with linestring converter', () => {
|
|
8
|
+
const converter = new LineStringJsonConverter();
|
|
9
|
+
const result = converter.read({
|
|
10
|
+
type: 'LineString',
|
|
11
|
+
coordinates: [[10.5, 20.3], [11.2, 21.1]]
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('should return LineString instance', () => result.should.be.instanceof(LineString));
|
|
15
|
+
it('should have two points', () => result.coordinates.length.should.equal(2));
|
|
16
|
+
it('should have first point with correct coordinates', () => {
|
|
17
|
+
result.coordinates[0].longitude.should.equal(10.5);
|
|
18
|
+
result.coordinates[0].latitude.should.equal(20.3);
|
|
19
|
+
});
|
|
20
|
+
it('should have second point with correct coordinates', () => {
|
|
21
|
+
result.coordinates[1].longitude.should.equal(11.2);
|
|
22
|
+
result.coordinates[1].latitude.should.equal(21.1);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Point } from '../../geospatial/Point';
|
|
5
|
+
import { LineString } from '../../geospatial/LineString';
|
|
6
|
+
import { JsonSerializer } from '../../JsonSerializer';
|
|
7
|
+
|
|
8
|
+
describe('when serializing linestring', () => {
|
|
9
|
+
const point1 = new Point(10.5, 20.3);
|
|
10
|
+
const point2 = new Point(30.1, 40.2);
|
|
11
|
+
const point3 = new Point(50.7, 60.8);
|
|
12
|
+
const lineString = new LineString([point1, point2, point3]);
|
|
13
|
+
const result = JsonSerializer.serialize(lineString);
|
|
14
|
+
const deserialized = JSON.parse(result);
|
|
15
|
+
|
|
16
|
+
it('should have type of LineString', () => deserialized.type.should.equal('LineString'));
|
|
17
|
+
it('should have coordinates array', () => deserialized.coordinates.should.be.an('array'));
|
|
18
|
+
it('should have three coordinate pairs', () => deserialized.coordinates.length.should.equal(3));
|
|
19
|
+
it('should have correct first point longitude', () => deserialized.coordinates[0][0].should.equal(10.5));
|
|
20
|
+
it('should have correct first point latitude', () => deserialized.coordinates[0][1].should.equal(20.3));
|
|
21
|
+
it('should have correct second point longitude', () => deserialized.coordinates[1][0].should.equal(30.1));
|
|
22
|
+
it('should have correct second point latitude', () => deserialized.coordinates[1][1].should.equal(40.2));
|
|
23
|
+
it('should have correct third point longitude', () => deserialized.coordinates[2][0].should.equal(50.7));
|
|
24
|
+
it('should have correct third point latitude', () => deserialized.coordinates[2][1].should.equal(60.8));
|
|
25
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Point } from '../../geospatial/Point';
|
|
5
|
+
import { LineString } from '../../geospatial/LineString';
|
|
6
|
+
import { LineStringJsonConverter } from '../LineStringJsonConverter';
|
|
7
|
+
|
|
8
|
+
describe('when writing linestring with linestring converter', () => {
|
|
9
|
+
const converter = new LineStringJsonConverter();
|
|
10
|
+
const lineString = new LineString([
|
|
11
|
+
new Point(10.5, 20.3),
|
|
12
|
+
new Point(11.2, 21.1)
|
|
13
|
+
]);
|
|
14
|
+
const result = converter.write(lineString);
|
|
15
|
+
|
|
16
|
+
it('should have type property', () => result.type.should.equal('LineString'));
|
|
17
|
+
it('should have coordinates array', () => result.coordinates.should.be.instanceof(Array));
|
|
18
|
+
it('should have two points', () => result.coordinates.length.should.equal(2));
|
|
19
|
+
it('should have first point coordinates', () => {
|
|
20
|
+
result.coordinates[0][0].should.equal(10.5);
|
|
21
|
+
result.coordinates[0][1].should.equal(20.3);
|
|
22
|
+
});
|
|
23
|
+
it('should have second point coordinates', () => {
|
|
24
|
+
result.coordinates[1][0].should.equal(11.2);
|
|
25
|
+
result.coordinates[1][1].should.equal(21.1);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Point } from '../../geospatial/Point';
|
|
5
|
+
import { JsonSerializer } from '../../JsonSerializer';
|
|
6
|
+
|
|
7
|
+
describe('when deserializing point', () => {
|
|
8
|
+
const json = '{"type":"Point","coordinates":[10.5,20.3]}';
|
|
9
|
+
const result = JsonSerializer.deserialize(Point, json);
|
|
10
|
+
|
|
11
|
+
it('should be a Point', () => result.should.be.instanceof(Point));
|
|
12
|
+
it('should have correct longitude', () => result.longitude.should.equal(10.5));
|
|
13
|
+
it('should have correct latitude', () => result.latitude.should.equal(20.3));
|
|
14
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Point } from '../../geospatial/Point';
|
|
5
|
+
import { PointJsonConverter } from '../PointJsonConverter';
|
|
6
|
+
|
|
7
|
+
describe('when reading point with point converter', () => {
|
|
8
|
+
const converter = new PointJsonConverter();
|
|
9
|
+
const result = converter.read({ type: 'Point', coordinates: [10.5, 20.3] });
|
|
10
|
+
|
|
11
|
+
it('should return Point instance', () => result.should.be.instanceof(Point));
|
|
12
|
+
it('should have correct longitude', () => result.longitude.should.equal(10.5));
|
|
13
|
+
it('should have correct latitude', () => result.latitude.should.equal(20.3));
|
|
14
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Point } from '../../geospatial/Point';
|
|
5
|
+
import { JsonSerializer } from '../../JsonSerializer';
|
|
6
|
+
|
|
7
|
+
describe('when serializing point', () => {
|
|
8
|
+
const point = new Point(10.5, 20.3);
|
|
9
|
+
const result = JsonSerializer.serialize(point);
|
|
10
|
+
const deserialized = JSON.parse(result);
|
|
11
|
+
|
|
12
|
+
it('should have type of Point', () => deserialized.type.should.equal('Point'));
|
|
13
|
+
it('should have coordinates array', () => deserialized.coordinates.should.be.an('array'));
|
|
14
|
+
it('should have two coordinates', () => deserialized.coordinates.length.should.equal(2));
|
|
15
|
+
it('should have correct longitude', () => deserialized.coordinates[0].should.equal(10.5));
|
|
16
|
+
it('should have correct latitude', () => deserialized.coordinates[1].should.equal(20.3));
|
|
17
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Point } from '../../geospatial/Point';
|
|
5
|
+
import { PointJsonConverter } from '../PointJsonConverter';
|
|
6
|
+
|
|
7
|
+
describe('when writing point with point converter', () => {
|
|
8
|
+
const converter = new PointJsonConverter();
|
|
9
|
+
const point = new Point(10.5, 20.3);
|
|
10
|
+
const result = converter.write(point);
|
|
11
|
+
|
|
12
|
+
it('should have type property', () => result.type.should.equal('Point'));
|
|
13
|
+
it('should have coordinates array', () => result.coordinates.should.be.instanceof(Array));
|
|
14
|
+
it('should have longitude as first coordinate', () => result.coordinates[0].should.equal(10.5));
|
|
15
|
+
it('should have latitude as second coordinate', () => result.coordinates[1].should.equal(20.3));
|
|
16
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Polygon } from '../../geospatial/Polygon';
|
|
5
|
+
import { JsonSerializer } from '../../JsonSerializer';
|
|
6
|
+
|
|
7
|
+
describe('when deserializing polygon with holes', () => {
|
|
8
|
+
const json = '{"type":"Polygon","coordinates":[[[0,0],[20,0],[20,20],[0,20],[0,0]],[[2,2],[8,2],[8,8],[2,8],[2,2]],[[12,12],[18,12],[18,18],[12,18],[12,12]]]}';
|
|
9
|
+
const result = JsonSerializer.deserialize(Polygon, json);
|
|
10
|
+
|
|
11
|
+
it('should be a Polygon', () => result.should.be.instanceof(Polygon));
|
|
12
|
+
it('should have shell with five points', () => result.shell.coordinates.length.should.equal(5));
|
|
13
|
+
it('should have two holes', () => result.holes.length.should.equal(2));
|
|
14
|
+
it('should have five points in first hole', () => result.holes[0].coordinates.length.should.equal(5));
|
|
15
|
+
it('should have five points in second hole', () => result.holes[1].coordinates.length.should.equal(5));
|
|
16
|
+
it('should have correct first point in shell', () => {
|
|
17
|
+
result.shell.coordinates[0].longitude.should.equal(0);
|
|
18
|
+
result.shell.coordinates[0].latitude.should.equal(0);
|
|
19
|
+
});
|
|
20
|
+
it('should have correct first point in first hole', () => {
|
|
21
|
+
result.holes[0].coordinates[0].longitude.should.equal(2);
|
|
22
|
+
result.holes[0].coordinates[0].latitude.should.equal(2);
|
|
23
|
+
});
|
|
24
|
+
it('should have correct first point in second hole', () => {
|
|
25
|
+
result.holes[1].coordinates[0].longitude.should.equal(12);
|
|
26
|
+
result.holes[1].coordinates[0].latitude.should.equal(12);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Polygon } from '../../geospatial/Polygon';
|
|
5
|
+
import { JsonSerializer } from '../../JsonSerializer';
|
|
6
|
+
|
|
7
|
+
describe('when deserializing polygon with no holes', () => {
|
|
8
|
+
const json = '{"type":"Polygon","coordinates":[[[0,0],[10,0],[10,10],[0,10],[0,0]]]}';
|
|
9
|
+
const result = JsonSerializer.deserialize(Polygon, json);
|
|
10
|
+
|
|
11
|
+
it('should be a Polygon', () => result.should.be.instanceof(Polygon));
|
|
12
|
+
it('should have shell with five points', () => result.shell.coordinates.length.should.equal(5));
|
|
13
|
+
it('should have no holes', () => result.holes.length.should.equal(0));
|
|
14
|
+
it('should have correct first point in shell', () => {
|
|
15
|
+
result.shell.coordinates[0].longitude.should.equal(0);
|
|
16
|
+
result.shell.coordinates[0].latitude.should.equal(0);
|
|
17
|
+
});
|
|
18
|
+
it('should have correct last point in shell (closed ring)', () => {
|
|
19
|
+
result.shell.coordinates[4].longitude.should.equal(0);
|
|
20
|
+
result.shell.coordinates[4].latitude.should.equal(0);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Polygon } from '../../geospatial/Polygon';
|
|
5
|
+
import { PolygonJsonConverter } from '../PolygonJsonConverter';
|
|
6
|
+
|
|
7
|
+
describe('when reading polygon with polygon converter', () => {
|
|
8
|
+
const converter = new PolygonJsonConverter();
|
|
9
|
+
const result = converter.read({
|
|
10
|
+
type: 'Polygon',
|
|
11
|
+
coordinates: [
|
|
12
|
+
[[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]]
|
|
13
|
+
]
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('should return Polygon instance', () => result.should.be.instanceof(Polygon));
|
|
17
|
+
it('should have shell ring with five points', () => result.shell.coordinates.length.should.equal(5));
|
|
18
|
+
it('should have first point with correct coordinates', () => {
|
|
19
|
+
result.shell.coordinates[0].longitude.should.equal(0);
|
|
20
|
+
result.shell.coordinates[0].latitude.should.equal(0);
|
|
21
|
+
});
|
|
22
|
+
it('should have closed ring', () => {
|
|
23
|
+
result.shell.coordinates[0].longitude.should.equal(result.shell.coordinates[4].longitude);
|
|
24
|
+
result.shell.coordinates[0].latitude.should.equal(result.shell.coordinates[4].latitude);
|
|
25
|
+
});
|
|
26
|
+
it('should have no holes', () => result.holes.length.should.equal(0));
|
|
27
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Point } from '../../geospatial/Point';
|
|
5
|
+
import { LinearRing } from '../../geospatial/LinearRing';
|
|
6
|
+
import { Polygon } from '../../geospatial/Polygon';
|
|
7
|
+
import { JsonSerializer } from '../../JsonSerializer';
|
|
8
|
+
|
|
9
|
+
describe('when serializing polygon with holes', () => {
|
|
10
|
+
const shell = new LinearRing([
|
|
11
|
+
new Point(0, 0),
|
|
12
|
+
new Point(20, 0),
|
|
13
|
+
new Point(20, 20),
|
|
14
|
+
new Point(0, 20),
|
|
15
|
+
new Point(0, 0)
|
|
16
|
+
]);
|
|
17
|
+
const hole1 = new LinearRing([
|
|
18
|
+
new Point(2, 2),
|
|
19
|
+
new Point(8, 2),
|
|
20
|
+
new Point(8, 8),
|
|
21
|
+
new Point(2, 8),
|
|
22
|
+
new Point(2, 2)
|
|
23
|
+
]);
|
|
24
|
+
const hole2 = new LinearRing([
|
|
25
|
+
new Point(12, 12),
|
|
26
|
+
new Point(18, 12),
|
|
27
|
+
new Point(18, 18),
|
|
28
|
+
new Point(12, 18),
|
|
29
|
+
new Point(12, 12)
|
|
30
|
+
]);
|
|
31
|
+
const polygon = new Polygon(shell, [hole1, hole2]);
|
|
32
|
+
const result = JsonSerializer.serialize(polygon);
|
|
33
|
+
const deserialized = JSON.parse(result);
|
|
34
|
+
|
|
35
|
+
it('should have type of Polygon', () => deserialized.type.should.equal('Polygon'));
|
|
36
|
+
it('should have coordinates array', () => deserialized.coordinates.should.be.an('array'));
|
|
37
|
+
it('should have three rings (shell + 2 holes)', () => deserialized.coordinates.length.should.equal(3));
|
|
38
|
+
it('should have five points in shell', () => deserialized.coordinates[0].length.should.equal(5));
|
|
39
|
+
it('should have five points in first hole', () => deserialized.coordinates[1].length.should.equal(5));
|
|
40
|
+
it('should have five points in second hole', () => deserialized.coordinates[2].length.should.equal(5));
|
|
41
|
+
it('should have correct first point in shell', () => {
|
|
42
|
+
deserialized.coordinates[0][0][0].should.equal(0);
|
|
43
|
+
deserialized.coordinates[0][0][1].should.equal(0);
|
|
44
|
+
});
|
|
45
|
+
it('should have correct first point in first hole', () => {
|
|
46
|
+
deserialized.coordinates[1][0][0].should.equal(2);
|
|
47
|
+
deserialized.coordinates[1][0][1].should.equal(2);
|
|
48
|
+
});
|
|
49
|
+
it('should have correct first point in second hole', () => {
|
|
50
|
+
deserialized.coordinates[2][0][0].should.equal(12);
|
|
51
|
+
deserialized.coordinates[2][0][1].should.equal(12);
|
|
52
|
+
});
|
|
53
|
+
});
|